aboutsummaryrefslogtreecommitdiffstats
path: root/test/rfc9114_SUITE.erl
blob: 4a36ee14fa6b330723e48999c86c62cc666fa366 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
%% Copyright (c) 2023-2024, Loïc Hoguin <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

-module(rfc9114_SUITE).
-compile(export_all).
-compile(nowarn_export_all).

-import(ct_helper, [config/2]).
-import(ct_helper, [doc/1]).

-ifdef(COWBOY_QUICER).

-include_lib("quicer/include/quicer.hrl").

all() ->
	[{group, h3}].

groups() ->
	%% @todo Enable parallel tests but for this issues in the
	%% QUIC accept loop need to be figured out (can't connect
	%% concurrently somehow, no backlog?).
	[{h3, [], ct_helper:all(?MODULE)}].

init_per_group(Name = h3, Config) ->
	cowboy_test:init_http3(Name, #{
		env => #{dispatch => cowboy_router:compile(init_routes(Config))}
	}, Config).

end_per_group(Name, _) ->
	cowboy_test:stop_group(Name).

init_routes(_) -> [
	{"localhost", [
		{"/", hello_h, []},
		{"/echo/:key", echo_h, []}
	]}
].

%% Starting HTTP/3 for "https" URIs.

alpn(Config) ->
	doc("Successful ALPN negotiation. (RFC9114 3.1)"),
	{ok, Conn} = quicer:connect("localhost", config(port, Config),
		#{alpn => ["h3"], verify => none}, 5000),
	{ok, <<"h3">>} = quicer:negotiated_protocol(Conn),
	%% To make sure the connection is fully established we wait
	%% to receive the SETTINGS frame on the control stream.
	{ok, _ControlRef, _Settings} = do_wait_settings(Conn),
	ok.

alpn_error(Config) ->
	doc("Failed ALPN negotiation using the 'h2' token. (RFC9114 3.1)"),
	{error, transport_down, #{status := alpn_neg_failure}}
		= quicer:connect("localhost", config(port, Config),
			#{alpn => ["h2"], verify => none}, 5000),
	ok.

%% @todo 3.2. Connection Establishment
%% After the QUIC connection is established, a SETTINGS frame MUST be sent by each endpoint as the initial frame of their respective HTTP control stream.

%% @todo 3.3. Connection Reuse
%% Servers are encouraged to maintain open HTTP/3 connections for as long as
%possible but are permitted to terminate idle connections if necessary. When
%either endpoint chooses to close the HTTP/3 connection, the terminating
%endpoint SHOULD first send a GOAWAY frame (Section 5.2) so that both endpoints
%can reliably determine whether previously sent frames have been processed and
%gracefully complete or terminate any necessary remaining tasks.

%% Frame format.

req_stream(Config) ->
	doc("Complete lifecycle of a request stream. (RFC9114 4.1)"),
	{ok, Conn} = quicer:connect("localhost", config(port, Config),
		#{alpn => ["h3"], verify => none}, 5000),
	%% To make sure the connection is fully established we wait
	%% to receive the SETTINGS frame on the control stream.
	{ok, ControlRef, _Settings} = do_wait_settings(Conn),
	%% Send a request on a request stream.
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedRequest)),
		EncodedRequest
	], ?QUIC_SEND_FLAG_FIN),
	%% Receive the response.
	{ok, Data} = do_receive_data(StreamRef),
	{HLenEnc, HLenBits} = do_guess_int_encoding(Data),
	<<
		1, %% HEADERS frame.
		HLenEnc:2, HLen:HLenBits,
		EncodedResponse:HLen/bytes,
		Rest/bits
	>> = Data,
	{ok, DecodedResponse, _DecData, _DecSt}
		= cow_qpack:decode_field_section(EncodedResponse, 0, cow_qpack:init(decoder)),
	#{
		<<":status">> := <<"200">>,
		<<"content-length">> := BodyLen
	} = maps:from_list(DecodedResponse),
	{DLenEnc, DLenBits} = do_guess_int_encoding(Rest),
	<<
		0, %% DATA frame.
		DLenEnc:2, DLen:DLenBits,
		Body:DLen/bytes
	>> = Rest,
	<<"Hello world!">> = Body,
	BodyLen = integer_to_binary(byte_size(Body)),
	ok = do_wait_peer_send_shutdown(StreamRef),
	ok = do_wait_stream_closed(StreamRef).

%% @todo Same test as above but with content-length unset?

req_stream_two_requests(Config) ->
	doc("Receipt of multiple requests on a single stream must "
		"be rejected with an H3_MESSAGE_ERROR stream error. "
		"(RFC9114 4.1, RFC9114 4.1.2)"),
	{ok, Conn} = quicer:connect("localhost", config(port, Config),
		#{alpn => ["h3"], verify => none}, 5000),
	%% To make sure the connection is fully established we wait
	%% to receive the SETTINGS frame on the control stream.
	{ok, ControlRef, _Settings} = do_wait_settings(Conn),
	%% Send two requests on a request stream.
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedRequest1, _EncData1, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedRequest2, _EncData2, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, EncSt0),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedRequest1)),
		EncodedRequest1,
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedRequest2)),
		EncodedRequest2
	]),
	%% The stream should have been aborted.
	#{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
	ok.

headers_then_trailers(Config) ->
	doc("Receipt of HEADERS followed by trailer HEADERS must be accepted. (RFC9114 4.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
		{<<"content-type">>, <<"text/plain">>}
	], 0, EncSt0),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers)),
		EncodedTrailers
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

headers_then_data_then_trailers(Config) ->
	doc("Receipt of HEADERS followed by DATA followed by trailer HEADERS "
		"must be accepted. (RFC9114 4.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"13">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
		{<<"content-type">>, <<"text/plain">>}
	], 0, EncSt0),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello server!">>,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers)),
		EncodedTrailers
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

data_then_headers(Config) ->
	doc("Receipt of DATA before HEADERS must be rejected "
		"with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"13">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello server!">>,
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders
	], ?QUIC_SEND_FLAG_FIN),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

headers_then_trailers_then_data(Config) ->
	doc("Receipt of DATA after trailer HEADERS must be rejected "
		"with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
		{<<"content-type">>, <<"text/plain">>}
	], 0, EncSt0),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers)),
		EncodedTrailers,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello server!">>
	], ?QUIC_SEND_FLAG_FIN),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

headers_then_data_then_trailers_then_data(Config) ->
	doc("Receipt of DATA after trailer HEADERS must be rejected "
		"with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"13">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
		{<<"content-type">>, <<"text/plain">>}
	], 0, EncSt0),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello server!">>,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers)),
		EncodedTrailers,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello server!">>
	], ?QUIC_SEND_FLAG_FIN),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

headers_then_data_then_trailers_then_trailers(Config) ->
	doc("Receipt of DATA after trailer HEADERS must be rejected "
		"with an H3_FRAME_UNEXPECTED connection error. (RFC9114 4.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"13">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedTrailers1, _EncData2, EncSt1} = cow_qpack:encode_field_section([
		{<<"content-type">>, <<"text/plain">>}
	], 0, EncSt0),
	{ok, EncodedTrailers2, _EncData3, _EncSt} = cow_qpack:encode_field_section([
		{<<"content-type">>, <<"text/plain">>}
	], 0, EncSt1),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello server!">>,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers1)),
		EncodedTrailers1,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers2)),
		EncodedTrailers2
	], ?QUIC_SEND_FLAG_FIN),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

unknown_then_headers(Config) ->
	doc("Receipt of unknown frame followed by HEADERS "
		"must be accepted. (RFC9114 4.1, RFC9114 9)"),
	unknown_then_headers(Config, do_unknown_frame_type(),
		rand:bytes(rand:uniform(4096))).

unknown_then_headers(Config, Type, Bytes) ->
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		cow_http3:encode_int(Type), %% Unknown frame.
		cow_http3:encode_int(iolist_size(Bytes)),
		Bytes,
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

headers_then_unknown(Config) ->
	doc("Receipt of HEADERS followed by unknown frame "
		"must be accepted. (RFC9114 4.1, RFC9114 9)"),
	headers_then_unknown(Config, do_unknown_frame_type(),
		rand:bytes(rand:uniform(4096))).

headers_then_unknown(Config, Type, Bytes) ->
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		cow_http3:encode_int(Type), %% Unknown frame.
		cow_http3:encode_int(iolist_size(Bytes)),
		Bytes
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

headers_then_data_then_unknown(Config) ->
	doc("Receipt of HEADERS followed by DATA followed by unknown frame "
		"must be accepted. (RFC9114 4.1, RFC9114 9)"),
	headers_then_data_then_unknown(Config, do_unknown_frame_type(),
		rand:bytes(rand:uniform(4096))).

headers_then_data_then_unknown(Config, Type, Bytes) ->
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"13">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello server!">>,
		cow_http3:encode_int(Type), %% Unknown frame.
		cow_http3:encode_int(iolist_size(Bytes)),
		Bytes
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

headers_then_trailers_then_unknown(Config) ->
	doc("Receipt of HEADERS followed by trailer HEADERS followed by unknown frame "
		"must be accepted. (RFC9114 4.1, RFC9114 9)"),
	headers_then_data_then_unknown(Config, do_unknown_frame_type(),
		rand:bytes(rand:uniform(4096))).

headers_then_trailers_then_unknown(Config, Type, Bytes) ->
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
		{<<"content-type">>, <<"text/plain">>}
	], 0, EncSt0),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers)),
		EncodedTrailers,
		cow_http3:encode_int(Type), %% Unknown frame.
		cow_http3:encode_int(iolist_size(Bytes)),
		Bytes
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

headers_then_data_then_unknown_then_trailers(Config) ->
	doc("Receipt of HEADERS followed by DATA followed by "
		"unknown frame followed by trailer HEADERS "
		"must be accepted. (RFC9114 4.1, RFC9114 9)"),
	headers_then_data_then_unknown_then_trailers(Config,
		do_unknown_frame_type(), rand:bytes(rand:uniform(4096))).

headers_then_data_then_unknown_then_trailers(Config, Type, Bytes) ->
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"13">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
		{<<"content-type">>, <<"text/plain">>}
	], 0, EncSt0),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello server!">>,
		cow_http3:encode_int(Type), %% Unknown frame.
		cow_http3:encode_int(iolist_size(Bytes)),
		Bytes,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers)),
		EncodedTrailers
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

headers_then_data_then_unknown_then_data(Config) ->
	doc("Receipt of HEADERS followed by DATA followed by "
		"unknown frame followed by DATA "
		"must be accepted. (RFC9114 4.1, RFC9114 9)"),
	headers_then_data_then_unknown_then_data(Config,
		do_unknown_frame_type(), rand:bytes(rand:uniform(4096))).

headers_then_data_then_unknown_then_data(Config, Type, Bytes) ->
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"13">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(6),
		<<"Hello ">>,
		cow_http3:encode_int(Type), %% Unknown frame.
		cow_http3:encode_int(iolist_size(Bytes)),
		Bytes,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(7),
		<<"server!">>
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

headers_then_data_then_trailers_then_unknown(Config) ->
	doc("Receipt of HEADERS followed by DATA followed by "
		"trailer HEADERS followed by unknown frame "
		"must be accepted. (RFC9114 4.1, RFC9114 9)"),
	headers_then_data_then_trailers_then_unknown(Config,
		do_unknown_frame_type(), rand:bytes(rand:uniform(4096))).

headers_then_data_then_trailers_then_unknown(Config, Type, Bytes) ->
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"13">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
		{<<"content-type">>, <<"text/plain">>}
	], 0, EncSt0),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello server!">>,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers)),
		EncodedTrailers,
		cow_http3:encode_int(Type), %% Unknown frame.
		cow_http3:encode_int(iolist_size(Bytes)),
		Bytes
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

do_unknown_frame_type() ->
	Type = rand:uniform(4611686018427387904) - 1,
	%% Retry if we get a value that's specified.
	case lists:member(Type, [
		16#0, 16#1, 16#3, 16#4, 16#5, 16#7, 16#d, %% HTTP/3 core frame types.
		16#2, 16#6, 16#8, 16#9 %% HTTP/3 reserved frame types that must be rejected.
	]) of
		true -> do_unknown_frame_type();
		false -> Type
	end.

reserved_then_headers(Config) ->
	doc("Receipt of reserved frame followed by HEADERS "
		"must be accepted when the reserved frame type is "
		"of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
	unknown_then_headers(Config, do_reserved_type(),
		rand:bytes(rand:uniform(4096))).

headers_then_reserved(Config) ->
	doc("Receipt of HEADERS followed by reserved frame "
		"must be accepted when the reserved frame type is "
		"of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
	headers_then_unknown(Config, do_reserved_type(),
		rand:bytes(rand:uniform(4096))).

headers_then_data_then_reserved(Config) ->
	doc("Receipt of HEADERS followed by DATA followed by reserved frame "
		"must be accepted when the reserved frame type is "
		"of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
	headers_then_data_then_unknown(Config, do_reserved_type(),
		rand:bytes(rand:uniform(4096))).

headers_then_trailers_then_reserved(Config) ->
	doc("Receipt of HEADERS followed by trailer HEADERS followed by reserved frame "
		"must be accepted when the reserved frame type is "
		"of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
	headers_then_trailers_then_unknown(Config, do_reserved_type(),
		rand:bytes(rand:uniform(4096))).

headers_then_data_then_reserved_then_trailers(Config) ->
	doc("Receipt of HEADERS followed by DATA followed by "
		"reserved frame followed by trailer HEADERS "
		"must be accepted when the reserved frame type is "
		"of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
	headers_then_data_then_unknown_then_trailers(Config,
		do_reserved_type(), rand:bytes(rand:uniform(4096))).

headers_then_data_then_reserved_then_data(Config) ->
	doc("Receipt of HEADERS followed by DATA followed by "
		"reserved frame followed by DATA "
		"must be accepted when the reserved frame type is "
		"of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
	headers_then_data_then_unknown_then_data(Config,
		do_reserved_type(), rand:bytes(rand:uniform(4096))).

headers_then_data_then_trailers_then_reserved(Config) ->
	doc("Receipt of HEADERS followed by DATA followed by "
		"trailer HEADERS followed by reserved frame "
		"must be accepted when the reserved frame type is "
		"of the format 0x1f * N + 0x21. (RFC9114 4.1, RFC9114 7.2.8)"),
	headers_then_data_then_trailers_then_unknown(Config,
		do_reserved_type(), rand:bytes(rand:uniform(4096))).

reject_transfer_encoding_header_with_body(Config) ->
	doc("Requests containing a transfer-encoding header must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.1, RFC9114 4.1.2, RFC9114 4.2)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, _EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"transfer-encoding">>, <<"chunked">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(24),
		<<"13\r\nHello server!\r\n0\r\n\r\n">>
	]),
	%% The stream should have been aborted.
	#{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
	ok.

%% 4. Expressing HTTP Semantics in HTTP/3
%% 4.1. HTTP Message Framing

%% An HTTP request/response exchange fully consumes a client-initiated
%bidirectional QUIC stream. After sending a request, a client MUST close the
%stream for sending. Unless using the CONNECT method (see Section 4.4), clients
%MUST NOT make stream closure dependent on receiving a response to their
%request. After sending a final response, the server MUST close the stream for
%sending. At this point, the QUIC stream is fully closed.
%% @todo What to do with clients that DON'T close the stream
%%       for sending after the request is sent?

%% If a client-initiated stream terminates without enough of the HTTP message
%to provide a complete response, the server SHOULD abort its response stream
%with the error code H3_REQUEST_INCOMPLETE.
%% @todo difficult!!

%% When the server does not need to receive the remainder of the request, it
%MAY abort reading the request stream, send a complete response, and cleanly
%close the sending part of the stream. The error code H3_NO_ERROR SHOULD be
%used when requesting that the client stop sending on the request stream.
%% @todo read_body related; h2 has this behavior but there is no corresponding test

%% 4.1.1. Request Cancellation and Rejection

%% When possible, it is RECOMMENDED that servers send an HTTP response with an
%appropriate status code rather than cancelling a request it has already begun
%processing.

%% Implementations SHOULD cancel requests by abruptly terminating any
%directions of a stream that are still open. To do so, an implementation resets
%the sending parts of streams and aborts reading on the receiving parts of
%streams; see Section 2.4 of [QUIC-TRANSPORT].

%% When the server cancels a request without performing any application
%processing, the request is considered "rejected". The server SHOULD abort its
%response stream with the error code H3_REQUEST_REJECTED. In this context,
%"processed" means that some data from the stream was passed to some higher
%layer of software that might have taken some action as a result. The client
%can treat requests rejected by the server as though they had never been sent
%at all, thereby allowing them to be retried later.

%% Servers MUST NOT use the H3_REQUEST_REJECTED error code for requests that
%were partially or fully processed. When a server abandons a response after
%partial processing, it SHOULD abort its response stream with the error code
%H3_REQUEST_CANCELLED.
%% @todo

%% Client SHOULD use the error code H3_REQUEST_CANCELLED to cancel requests.
%Upon receipt of this error code, a server MAY abruptly terminate the response
%using the error code H3_REQUEST_REJECTED if no processing was performed.
%Clients MUST NOT use the H3_REQUEST_REJECTED error code, except when a server
%has requested closure of the request stream with this error code.
%% @todo

%4.1.2. Malformed Requests and Responses
%A malformed request or response is one that is an otherwise valid sequence of
%frames but is invalid due to:
%
%the presence of prohibited fields or pseudo-header fields,
%% @todo reject_response_pseudo_headers
%% @todo reject_unknown_pseudo_headers
%% @todo reject_pseudo_headers_in_trailers

%the absence of mandatory pseudo-header fields,
%invalid values for pseudo-header fields,
%pseudo-header fields after fields,
%% @todo reject_pseudo_headers_after_regular_headers

%an invalid sequence of HTTP messages,
%the inclusion of invalid characters in field names or values.
%
%A request or response that is defined as having content when it contains a
%Content-Length header field (Section 8.6 of [HTTP]) is malformed if the value
%of the Content-Length header field does not equal the sum of the DATA frame
%lengths received. A response that is defined as never having content, even
%when a Content-Length is present, can have a non-zero Content-Length header
%field even though no content is included in DATA frames.
%
%For malformed requests, a server MAY send an HTTP response indicating the
%error prior to closing or resetting the stream.
%% @todo All the malformed tests

headers_reject_uppercase_header_name(Config) ->
	doc("Requests containing uppercase header names must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
	do_reject_malformed_header(Config,
		{<<"I-AM-GIGANTIC">>, <<"How's the weather up there?">>}
	).

%% 4.2. HTTP Fields
%% An endpoint MUST NOT generate an HTTP/3 field section containing
%connection-specific fields; any message containing connection-specific fields
%MUST be treated as malformed.

reject_connection_header(Config) ->
	doc("Requests containing a connection header must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
	do_reject_malformed_header(Config,
		{<<"connection">>, <<"close">>}
	).

reject_keep_alive_header(Config) ->
	doc("Requests containing a keep-alive header must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
	do_reject_malformed_header(Config,
		{<<"keep-alive">>, <<"timeout=5, max=1000">>}
	).

reject_proxy_authenticate_header(Config) ->
	doc("Requests containing a proxy-authenticate header must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
	do_reject_malformed_header(Config,
		{<<"proxy-authenticate">>, <<"Basic">>}
	).

reject_proxy_authorization_header(Config) ->
	doc("Requests containing a proxy-authorization header must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
	do_reject_malformed_header(Config,
		{<<"proxy-authorization">>, <<"Basic YWxhZGRpbjpvcGVuc2VzYW1l">>}
	).

reject_transfer_encoding_header(Config) ->
	doc("Requests containing a transfer-encoding header must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
	do_reject_malformed_header(Config,
		{<<"transfer-encoding">>, <<"chunked">>}
	).

reject_upgrade_header(Config) ->
	doc("Requests containing an upgrade header must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.5, RFC9114 4.1.2)"),
	do_reject_malformed_header(Config,
		{<<"upgrade">>, <<"websocket">>}
	).

accept_te_header_value_trailers(Config) ->
	doc("Requests containing a TE header with a value of \"trailers\" "
		"must be accepted. (RFC9114 4.2)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>},
		{<<"te">>, <<"trailers">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
		{<<"content-type">>, <<"text/plain">>}
	], 0, EncSt0),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers)),
		EncodedTrailers
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

reject_te_header_other_values(Config) ->
	doc("Requests containing a TE header with a value other than \"trailers\" must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.2, RFC9114 4.1.2)"),
	do_reject_malformed_header(Config,
		{<<"te">>, <<"trailers, deflate;q=0.5">>}
	).

%% @todo response_dont_send_header_in_connection
%% @todo response_dont_send_connection_header
%% @todo response_dont_send_keep_alive_header
%% @todo response_dont_send_proxy_connection_header
%% @todo response_dont_send_transfer_encoding_header
%% @todo response_dont_send_upgrade_header

%% 4.2.1. Field Compression
%% To allow for better compression efficiency, the Cookie header field
%([COOKIES]) MAY be split into separate field lines, each with one or more
%cookie-pairs, before compression. If a decompressed field section contains
%multiple cookie field lines, these MUST be concatenated into a single byte
%string using the two-byte delimiter of "; " (ASCII 0x3b, 0x20) before being
%passed into a context other than HTTP/2 or HTTP/3, such as an HTTP/1.1
%connection, or a generic HTTP server application.

%% 4.2.2. Header Size Constraints
%% An HTTP/3 implementation MAY impose a limit on the maximum size of the
%message header it will accept on an individual HTTP message. A server that
%receives a larger header section than it is willing to handle can send an HTTP
%431 (Request Header Fields Too Large) status code ([RFC6585]). The size of a
%field list is calculated based on the uncompressed size of fields, including
%the length of the name and value in bytes plus an overhead of 32 bytes for
%each field.
%% If an implementation wishes to advise its peer of this limit, it can be
%conveyed as a number of bytes in the SETTINGS_MAX_FIELD_SECTION_SIZE
%parameter. 

reject_unknown_pseudo_headers(Config) ->
	doc("Requests containing unknown pseudo-headers must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
	do_reject_malformed_header(Config,
		{<<":upgrade">>, <<"websocket">>}
	).

reject_response_pseudo_headers(Config) ->
	doc("Requests containing response pseudo-headers must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
	do_reject_malformed_header(Config,
		{<<":status">>, <<"200">>}
	).

reject_pseudo_headers_in_trailers(Config) ->
	doc("Requests containing pseudo-headers in trailers must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"trailer">>, <<"x-checksum">>}
	], 0, cow_qpack:init(encoder)),
	{ok, EncodedTrailers, _EncData2, _EncSt} = cow_qpack:encode_field_section([
		{<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>},
		{<<":path">>, <<"/">>}
	], 0, EncSt0),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(10000),
		<<0:10000/unit:8>>,
		<<1>>, %% HEADERS frame for trailers.
		cow_http3:encode_int(iolist_size(EncodedTrailers)),
		EncodedTrailers
	]),
	%% The stream should have been aborted.
	#{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
	ok.

reject_pseudo_headers_after_regular_headers(Config) ->
	doc("Requests containing pseudo-headers after regular headers must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<"content-length">>, <<"0">>},
		{<<":path">>, <<"/">>}
	]).

reject_userinfo(Config) ->
	doc("An authority containing a userinfo component must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"http">>},
		{<<":authority">>, <<"user@localhost">>},
		{<<":path">>, <<"/">>}
	]).

%% To ensure that the HTTP/1.1 request line can be reproduced accurately, this
%% pseudo-header field (:authority) MUST be omitted when translating from an
%% HTTP/1.1 request that has a request target in a method-specific form;
%% see Section 7.1 of [HTTP]. 

reject_empty_path(Config) ->
	doc("A request containing an empty path component must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"http">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<>>}
	]).

reject_missing_pseudo_header_method(Config) ->
	doc("A request without a method component must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":scheme">>, <<"http">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>}
	]).

reject_many_pseudo_header_method(Config) ->
	doc("A request containing more than one method component must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"http">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>}
	]).

reject_missing_pseudo_header_scheme(Config) ->
	doc("A request without a scheme component must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>}
	]).

reject_many_pseudo_header_scheme(Config) ->
	doc("A request containing more than one scheme component must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"http">>},
		{<<":scheme">>, <<"http">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>}
	]).

reject_missing_pseudo_header_authority(Config) ->
	doc("A request without an authority or host component must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"http">>},
		{<<":path">>, <<"/">>}
	]).

accept_host_header_on_missing_pseudo_header_authority(Config) ->
	doc("A request without an authority but with a host header must be accepted. "
		"(RFC9114 4.3.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, _EncSt0} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":path">>, <<"/">>},
		{<<"host">>, <<"localhost">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

%% @todo
%% If the :scheme pseudo-header field identifies a scheme that has a mandatory
%% authority component (including "http" and "https"), the request MUST contain
%% either an :authority pseudo-header field or a Host header field.
%%  - If both fields are present, they MUST NOT be empty.
%%  - If both fields are present, they MUST contain the same value. 

reject_many_pseudo_header_authority(Config) ->
	doc("A request containing more than one authority component must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"http">>},
		{<<":authority">>, <<"localhost">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>}
	]).

reject_missing_pseudo_header_path(Config) ->
	doc("A request without a path component must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"http">>},
		{<<":authority">>, <<"localhost">>}
	]).

reject_many_pseudo_header_path(Config) ->
	doc("A request containing more than one path component must be rejected "
		"with an H3_MESSAGE_ERROR stream error. (RFC9114 4.3.1, RFC9114 4.1.2)"),
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"http">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<":path">>, <<"/">>}
	]).

do_reject_malformed_header(Config, Header) ->
	do_reject_malformed_headers(Config, [
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		Header
	]).

do_reject_malformed_headers(Config, Headers) ->
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData1, _EncSt0}
		= cow_qpack:encode_field_section(Headers, 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders
	]),
	%% The stream should have been aborted.
	#{reason := h3_message_error} = do_wait_stream_aborted(StreamRef),
	ok.

%% For responses, a single ":status" pseudo-header field is defined that
%% carries the HTTP status code; see Section 15 of [HTTP]. This pseudo-header
%% field MUST be included in all responses; otherwise, the response is malformed
%% (see Section 4.1.2).

%% @todo Implement CONNECT. (RFC9114 4.4. The CONNECT Method)

%% @todo Maybe block the sending of 101 responses? (RFC9114 4.5. HTTP Upgrade) - also HTTP/2.

%% @todo Implement server push (RFC9114 4.6. Server Push)

%% @todo - need a way to list connections
%% 5.2. Connection Shutdown
%% Endpoints initiate the graceful shutdown of an HTTP/3 connection by sending
%% a GOAWAY frame. The GOAWAY frame contains an identifier that indicates to the
%% receiver the range of requests or pushes that were or might be processed in
%% this connection. The server sends a client-initiated bidirectional stream ID;
%% the client sends a push ID. Requests or pushes with the indicated identifier
%% or greater are rejected (Section 4.1.1) by the sender of the GOAWAY. This
%% identifier MAY be zero if no requests or pushes were processed.

%% @todo
%% Upon sending a GOAWAY frame, the endpoint SHOULD explicitly cancel (see
%% Sections 4.1.1 and 7.2.3) any requests or pushes that have identifiers greater
%% than or equal to the one indicated, in order to clean up transport state for
%% the affected streams. The endpoint SHOULD continue to do so as more requests
%% or pushes arrive.

%% @todo
%% Endpoints MUST NOT initiate new requests or promise new pushes on the
%% connection after receipt of a GOAWAY frame from the peer.

%% @todo
%% Requests on stream IDs less than the stream ID in a GOAWAY frame from the
%% server might have been processed; their status cannot be known until a
%% response is received, the stream is reset individually, another GOAWAY is
%% received with a lower stream ID than that of the request in question, or the
%% connection terminates.

%% @todo
%% Servers MAY reject individual requests on streams below the indicated ID if
%% these requests were not processed.

%% @todo
%% If a server receives a GOAWAY frame after having promised pushes with a push
%% ID greater than or equal to the identifier contained in the GOAWAY frame,
%% those pushes will not be accepted.

%% @todo
%% Servers SHOULD send a GOAWAY frame when the closing of a connection is known
%% in advance, even if the advance notice is small, so that the remote peer can
%% know whether or not a request has been partially processed.

%% @todo
%% An endpoint MAY send multiple GOAWAY frames indicating different
%% identifiers, but the identifier in each frame MUST NOT be greater than the
%% identifier in any previous frame, since clients might already have retried
%% unprocessed requests on another HTTP connection. Receiving a GOAWAY containing
%% a larger identifier than previously received MUST be treated as a connection
%% error of type H3_ID_ERROR.

%% @todo
%% An endpoint that is attempting to gracefully shut down a connection can send
%% a GOAWAY frame with a value set to the maximum possible value (2^62-4 for
%% servers, 2^62-1 for clients).

%% @todo
%% Even when a GOAWAY indicates that a given request or push will not be
%% processed or accepted upon receipt, the underlying transport resources still
%% exist. The endpoint that initiated these requests can cancel them to clean up
%% transport state.

%% @todo
%% Once all accepted requests and pushes have been processed, the endpoint can
%% permit the connection to become idle, or it MAY initiate an immediate closure
%% of the connection. An endpoint that completes a graceful shutdown SHOULD use
%% the H3_NO_ERROR error code when closing the connection.

%% @todo
%% If a client has consumed all available bidirectional stream IDs with
%% requests, the server need not send a GOAWAY frame, since the client is unable
%% to make further requests. @todo OK that one's some weird stuff lol

%% @todo
%% 5.3. Immediate Application Closure
%% Before closing the connection, a GOAWAY frame MAY be sent to allow the
%% client to retry some requests. Including the GOAWAY frame in the same packet
%% as the QUIC CONNECTION_CLOSE frame improves the chances of the frame being
%% received by clients.

bidi_allow_at_least_a_hundred(Config) ->
	doc("Endpoints must allow the peer to create at least "
		"one hundred bidirectional streams. (RFC9114 6.1"),
	#{conn := Conn} = do_connect(Config),
	receive
		{quic, streams_available, Conn, #{bidi_streams := NumStreams}} ->
			true = NumStreams >= 100,
			ok
	after 5000 ->
		error(timeout)
	end.

unidi_allow_at_least_three(Config) ->
	doc("Endpoints must allow the peer to create at least "
		"three unidirectional streams. (RFC9114 6.2"),
	#{conn := Conn} = do_connect(Config),
	%% Confirm that the server advertised support for at least 3 unidi streams.
	receive
		{quic, streams_available, Conn, #{unidi_streams := NumStreams}} ->
			true = NumStreams >= 3,
			ok
	after 5000 ->
		error(timeout)
	end,
	%% Confirm that we can create the unidi streams.
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(ControlRef, [<<0>>, SettingsBin]),
	{ok, EncoderRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(EncoderRef, <<2>>),
	{ok, DecoderRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(DecoderRef, <<3>>),
	%% Streams shouldn't get closed.
	fun Loop() ->
		receive
			%% We don't care about these messages.
			{quic, dgram_state_changed, Conn, _} ->
				Loop();
			{quic, peer_needs_streams, Conn, _} ->
				Loop();
			%% Any other we do care.
			Msg ->
				error(Msg)
		after 1000 ->
			ok
		end
	end().

unidi_create_critical_first(Config) ->
	doc("Endpoints should create the HTTP control stream as well as "
		"the QPACK encoder and decoder streams first. (RFC9114 6.2"),
	%% The control stream is accepted in the do_connect/1 function.
	#{conn := Conn} = do_connect(Config, #{peer_unidi_stream_count => 3}),
	Unidi1 = do_accept_qpack_stream(Conn),
	Unidi2 = do_accept_qpack_stream(Conn),
	case {Unidi1, Unidi2} of
		{{encoder, _}, {decoder, _}} ->
			ok;
		{{decoder, _}, {encoder, _}} ->
			ok
	end.

do_accept_qpack_stream(Conn) ->
	receive
		{quic, new_stream, StreamRef, #{flags := Flags}} ->
			ok = quicer:setopt(StreamRef, active, true),
			true = quicer:is_unidirectional(Flags),
			receive {quic, <<Type>>, StreamRef, _} ->
				{case Type of
					2 -> encoder;
					3 -> decoder
				end, StreamRef}
			after 5000 ->
				error(timeout)
			end
	after 5000 ->
		error(timeout)
	end.

%% @todo We should also confirm that there's at least 1,024 bytes of
%%       flow-control credit for each unidi stream the server creates. (How?)
%%       It can be set via stream_recv_window_default in quicer.

unidi_abort_unknown_type(Config) ->
	doc("Receipt of an unknown stream type must be aborted "
		"with an H3_STREAM_CREATION_ERROR stream error. (RFC9114 6.2, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	%% Create an unknown unidirectional stream.
	{ok, StreamRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(StreamRef, [
		cow_http3:encode_int(1 + do_reserved_type()),
		rand:bytes(rand:uniform(4096))
	]),
	%% The stream should have been aborted.
	#{reason := h3_stream_creation_error} = do_wait_stream_aborted(StreamRef),
	ok.

unidi_abort_reserved_type(Config) ->
	doc("Receipt of a reserved stream type must be aborted "
		"with an H3_STREAM_CREATION_ERROR stream error. "
		"(RFC9114 6.2, RFC9114 6.2.3, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	%% Create a reserved unidirectional stream.
	{ok, StreamRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(StreamRef, [
		cow_http3:encode_int(do_reserved_type()),
		rand:bytes(rand:uniform(4096))
	]),
	%% The stream should have been aborted.
	#{reason := h3_stream_creation_error} = do_wait_stream_aborted(StreamRef),
	ok.

%% As certain stream types can affect connection state, a recipient SHOULD NOT
%% discard data from incoming unidirectional streams prior to reading the stream type.

%% Implementations MAY send stream types before knowing whether the peer
%supports them. However, stream types that could modify the state or semantics
%of existing protocol components, including QPACK or other extensions, MUST NOT
%be sent until the peer is known to support them.
%% @todo It may make sense for Cowboy to delay the creation of unidi streams
%%       a little in order to save resources. We could create them when the
%%       client does as well, or something similar.

%% A receiver MUST tolerate unidirectional streams being closed or reset prior
%% to the reception of the unidirectional stream header.

%% Each side MUST initiate a single control stream at the beginning of the
%% connection and send its SETTINGS frame as the first frame on this stream.
%% @todo What to do when the client never opens a control stream?
%% @todo Similarly, a stream could be opened but with no data being sent.
%% @todo Similarly, a control stream could be opened with no SETTINGS frame sent.

control_reject_first_frame_data(Config) ->
	doc("The first frame on a control stream must be a SETTINGS frame "
		"or the connection must be closed with an H3_MISSING_SETTINGS "
		"connection error. (RFC9114 6.2.1, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		<<0>>, %% DATA frame.
		cow_http3:encode_int(12),
		<<"Hello world!">>
	]),
	%% The connection should have been closed.
	#{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
	ok.

control_reject_first_frame_headers(Config) ->
	doc("The first frame on a control stream must be a SETTINGS frame "
		"or the connection must be closed with an H3_MISSING_SETTINGS "
		"connection error. (RFC9114 6.2.1, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders
	]),
	%% The connection should have been closed.
	#{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
	ok.

control_reject_first_frame_cancel_push(Config) ->
	doc("The first frame on a control stream must be a SETTINGS frame "
		"or the connection must be closed with an H3_MISSING_SETTINGS "
		"connection error. (RFC9114 6.2.1, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		<<3>>, %% CANCEL_PUSH frame.
		cow_http3:encode_int(1),
		cow_http3:encode_int(0)
	]),
	%% The connection should have been closed.
	#{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
	ok.

control_accept_first_frame_settings(Config) ->
	doc("The first frame on a control stream "
		"must be a SETTINGS frame. (RFC9114 6.2.1, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin
	]),
	%% The connection should remain up.
	receive
		{quic, shutdown, Conn, {unknown_quic_status, Code}} ->
			Reason = cow_http3:code_to_error(Code),
			error(Reason)
	after 1000 ->
		ok
	end.

control_reject_first_frame_push_promise(Config) ->
	doc("The first frame on a control stream must be a SETTINGS frame "
		"or the connection must be closed with an H3_MISSING_SETTINGS "
		"connection error. (RFC9114 6.2.1, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),

	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		<<5>>, %% PUSH_PROMISE frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders) + 1),
		cow_http3:encode_int(0),
		EncodedHeaders
	]),
	%% The connection should have been closed.
	#{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
	ok.

control_reject_first_frame_goaway(Config) ->
	doc("The first frame on a control stream must be a SETTINGS frame "
		"or the connection must be closed with an H3_MISSING_SETTINGS "
		"connection error. (RFC9114 6.2.1, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		<<7>>, %% GOAWAY frame.
		cow_http3:encode_int(1),
		cow_http3:encode_int(0)
	]),
	%% The connection should have been closed.
	#{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
	ok.

control_reject_first_frame_max_push_id(Config) ->
	doc("The first frame on a control stream must be a SETTINGS frame "
		"or the connection must be closed with an H3_MISSING_SETTINGS "
		"connection error. (RFC9114 6.2.1, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		<<13>>, %% MAX_PUSH_ID frame.
		cow_http3:encode_int(1),
		cow_http3:encode_int(0)
	]),
	%% The connection should have been closed.
	#{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
	ok.

control_reject_first_frame_reserved(Config) ->
	doc("The first frame on a control stream must be a SETTINGS frame "
		"or the connection must be closed with an H3_MISSING_SETTINGS "
		"connection error. (RFC9114 6.2.1, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	Len = rand:uniform(512),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		cow_http3:encode_int(do_reserved_type()),
		cow_http3:encode_int(Len),
		rand:bytes(Len)
	]),
	%% The connection should have been closed.
	#{reason := h3_missing_settings} = do_wait_connection_closed(Conn),
	ok.

control_reject_multiple(Config) ->
	doc("Endpoints must not create multiple control streams. (RFC9114 6.2.1)"),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	do_critical_reject_multiple(Config, [<<0>>, SettingsBin]).

do_critical_reject_multiple(Config, HeaderData) ->
	#{conn := Conn} = do_connect(Config),
	%% Create two critical streams.
	{ok, StreamRef1} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(StreamRef1, HeaderData),
	{ok, StreamRef2} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(StreamRef2, HeaderData),
	%% The connection should have been closed.
	#{reason := h3_stream_creation_error} = do_wait_connection_closed(Conn),
	ok.

control_local_closed_abort(Config) ->
	doc("Endpoints must not close the control stream. (RFC9114 6.2.1)"),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	do_critical_local_closed_abort(Config, [<<0>>, SettingsBin]).

do_critical_local_closed_abort(Config, HeaderData) ->
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(StreamRef, HeaderData),
	%% Wait a little to make sure the stream data was received before we abort.
	timer:sleep(100),
	%% Close the critical stream.
	quicer:async_shutdown_stream(StreamRef, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT, 0),
	%% The connection should have been closed.
	timer:sleep(1000),
	#{reason := h3_closed_critical_stream} = do_wait_connection_closed(Conn),
	ok.

control_local_closed_graceful(Config) ->
	doc("Endpoints must not close the control stream. (RFC9114 6.2.1)"),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	do_critical_local_closed_graceful(Config, [<<0>>, SettingsBin]).

do_critical_local_closed_graceful(Config, HeaderData) ->
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, _} = quicer:send(StreamRef, HeaderData),
	%% Close the critical stream.
	quicer:async_shutdown_stream(StreamRef, ?QUIC_STREAM_SHUTDOWN_FLAG_GRACEFUL, 0),
	%% The connection should have been closed.
	#{reason := h3_closed_critical_stream} = do_wait_connection_closed(Conn),
	ok.

control_remote_closed_abort(Config) ->
	doc("Endpoints must not close the control stream. (RFC9114 6.2.1)"),
	#{conn := Conn, control := ControlRef} = do_connect(Config),
	%% Close the control stream.
	quicer:async_shutdown_stream(ControlRef, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT, 0),
	%% The connection should have been closed.
	#{reason := h3_closed_critical_stream} = do_wait_connection_closed(Conn),
	ok.

%% We cannot gracefully shutdown a remote unidi stream; only abort reading.

%% Because the contents of the control stream are used to manage the behavior
%% of other streams, endpoints SHOULD provide enough flow-control credit to keep
%% the peer's control stream from becoming blocked.

%% @todo Implement server push (RFC9114 6.2.2 Push Streams)

data_frame_can_span_multiple_packets(Config) ->
	doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/echo/read_body">>},
		{<<"content-length">>, <<"13">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello ">>
	]),
	timer:sleep(100),
	{ok, _} = quicer:send(StreamRef, [
		<<"server!">>
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello server!">>
	} = do_receive_response(StreamRef),
	ok.

headers_frame_can_span_multiple_packets(Config) ->
	doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	Half = iolist_size(EncodedHeaders) div 2,
	<<EncodedHeadersPart1:Half/binary, EncodedHeadersPart2/bits>>
		= iolist_to_binary(EncodedHeaders),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeadersPart1
	]),
	timer:sleep(100),
	{ok, _} = quicer:send(StreamRef, [
		EncodedHeadersPart2
	]),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

%% @todo Implement server push. cancel_push_frame_can_span_multiple_packets(Config) ->

settings_frame_can_span_multiple_packets(Config) ->
	doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	<<SettingsPart1:1/binary, SettingsPart2/bits>> = SettingsBin,
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsPart1
	]),
	timer:sleep(100),
	{ok, _} = quicer:send(ControlRef, [
		SettingsPart2
	]),
	%% The connection should remain up.
	receive
		{quic, shutdown, Conn, {unknown_quic_status, Code}} ->
			Reason = cow_http3:code_to_error(Code),
			error(Reason)
	after 1000 ->
		ok
	end.

goaway_frame_can_span_multiple_packets(Config) ->
	doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		<<7>>, cow_http3:encode_int(1) %% GOAWAY part 1.
	]),
	timer:sleep(100),
	{ok, _} = quicer:send(ControlRef, [
		cow_http3:encode_int(0) %% GOAWAY part 2.
	]),
	%% The connection should be closed gracefully.
	receive
		{quic, shutdown, Conn, {unknown_quic_status, Code}} ->
			h3_no_error = cow_http3:code_to_error(Code),
			ok;
		%% @todo Temporarily also accept this message. I am
		%%       not sure why it happens but it isn't wrong per se.
		{quic, shutdown, Conn, success} ->
			ok
	after 1000 ->
		error(timeout)
	end.

max_push_id_frame_can_span_multiple_packets(Config) ->
	doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		<<13>>, cow_http3:encode_int(1) %% MAX_PUSH_ID part 1.
	]),
	timer:sleep(100),
	{ok, _} = quicer:send(ControlRef, [
		cow_http3:encode_int(0) %% MAX_PUSH_ID part 2.
	]),
	%% The connection should remain up.
	receive
		{quic, shutdown, Conn, {unknown_quic_status, Code}} ->
			Reason = cow_http3:code_to_error(Code),
			error(Reason)
	after 1000 ->
		ok
	end.

unknown_frame_can_span_multiple_packets(Config) ->
	doc("HTTP/3 frames can span multiple packets. (RFC9114 7)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, _} = quicer:send(StreamRef, [
		cow_http3:encode_int(do_unknown_frame_type()),
		cow_http3:encode_int(16383)
	]),
	timer:sleep(100),
	{ok, _} = quicer:send(StreamRef, rand:bytes(4096)),
	timer:sleep(100),
	{ok, _} = quicer:send(StreamRef, rand:bytes(4096)),
	timer:sleep(100),
	{ok, _} = quicer:send(StreamRef, rand:bytes(4096)),
	timer:sleep(100),
	{ok, _} = quicer:send(StreamRef, rand:bytes(4095)),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders
	], ?QUIC_SEND_FLAG_FIN),
	#{
		headers := #{<<":status">> := <<"200">>},
		body := <<"Hello world!">>
	} = do_receive_response(StreamRef),
	ok.

%% The DATA and SETTINGS frames can be zero-length therefore
%% they cannot be too short.

headers_frame_too_short(Config) ->
	doc("Frames that terminate before the end of identified fields "
		"must be rejected with an H3_FRAME_ERROR connection error. "
		"(RFC9114 7.1, RFC9114 10.8)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(0)
	]),
	%% The connection should have been closed.
	#{reason := h3_frame_error} = do_wait_connection_closed(Conn),
	ok.

%% @todo Implement server push. cancel_push_frame_too_short(Config) ->

goaway_frame_too_short(Config) ->
	doc("Frames that terminate before the end of identified fields "
		"must be rejected with an H3_FRAME_ERROR connection error. "
		"(RFC9114 7.1, RFC9114 10.8)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		<<7>>, cow_http3:encode_int(0) %% GOAWAY.
	]),
	%% The connection should have been closed.
	#{reason := h3_frame_error} = do_wait_connection_closed(Conn),
	ok.

max_push_id_frame_too_short(Config) ->
	doc("Frames that terminate before the end of identified fields "
		"must be rejected with an H3_FRAME_ERROR connection error. "
		"(RFC9114 7.1, RFC9114 10.8)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		<<13>>, cow_http3:encode_int(0) %% MAX_PUSH_ID.
	]),
	%% The connection should have been closed.
	#{reason := h3_frame_error} = do_wait_connection_closed(Conn),
	ok.

data_frame_truncated(Config) ->
	doc("Truncated frames must be rejected with an "
		"H3_FRAME_ERROR connection error. (RFC9114 7.1, RFC9114 10.8)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/echo/read_body">>},
		{<<"content-length">>, <<"13">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(13),
		<<"Hello ">>
	], ?QUIC_SEND_FLAG_FIN),
	%% The connection should have been closed.
	#{reason := h3_frame_error} = do_wait_connection_closed(Conn),
	ok.

headers_frame_truncated(Config) ->
	doc("Truncated frames must be rejected with an "
		"H3_FRAME_ERROR connection error. (RFC9114 7.1, RFC9114 10.8)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders))
	], ?QUIC_SEND_FLAG_FIN),
	%% The connection should have been closed.
	#{reason := h3_frame_error} = do_wait_connection_closed(Conn),
	ok.

%% I am not sure how to test truncated CANCEL_PUSH, SETTINGS, GOAWAY
%% or MAX_PUSH_ID frames, as those are sent on the control stream,
%% which we cannot terminate.

%% The DATA, HEADERS and SETTINGS frames can be of any length
%% therefore they cannot be too long per se, even if unwanted
%% data can be included at the end of the frame's payload.

%% @todo Implement server push. cancel_push_frame_too_long(Config) ->

goaway_frame_too_long(Config) ->
	doc("Frames that contain additional bytes after the end of identified fields "
		"must be rejected with an H3_FRAME_ERROR connection error. "
		"(RFC9114 7.1, RFC9114 10.8)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		<<7>>, cow_http3:encode_int(3), %% GOAWAY.
		<<0, 1, 2>>
	]),
	%% The connection should have been closed.
	#{reason := h3_frame_error} = do_wait_connection_closed(Conn),
	ok.

max_push_id_frame_too_long(Config) ->
	doc("Frames that contain additional bytes after the end of identified fields "
		"must be rejected with an H3_FRAME_ERROR connection error. "
		"(RFC9114 7.1, RFC9114 10.8)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		<<13>>, cow_http3:encode_int(9), %% MAX_PUSH_ID.
		<<0, 1, 2, 3, 4, 5, 6, 7, 8>>
	]),
	%% The connection should have been closed.
	#{reason := h3_frame_error} = do_wait_connection_closed(Conn),
	ok.

%% Streams may terminate abruptly in the middle of frames.

data_frame_rejected_on_control_stream(Config) ->
	doc("DATA frames received on the control stream must be rejected "
		"with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		<<0>>, %% DATA frame.
		cow_http3:encode_int(12),
		<<"Hello world!">>
	]),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

headers_frame_rejected_on_control_stream(Config) ->
	doc("HEADERS frames received on the control stream must be rejected "
		"with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.2)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders
	]),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

%% @todo Implement server push. (RFC9114 7.2.3. CANCEL_PUSH)

settings_twice(Config) ->
	doc("Receipt of a second SETTINGS frame on the control stream "
		"must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.4)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		SettingsBin
	]),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

settings_on_bidi_stream(Config) ->
	doc("Receipt of a SETTINGS frame on a bidirectional stream "
		"must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.4)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, EncodedRequest, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	{ok, _} = quicer:send(StreamRef, [
		SettingsBin,
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedRequest)),
		EncodedRequest
	], ?QUIC_SEND_FLAG_FIN),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

settings_identifier_twice(Config) ->
	doc("Receipt of a duplicate SETTINGS identifier must be rejected "
		"with an H3_SETTINGS_ERROR connection error. (RFC9114 7.2.4)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	SettingsPayload = [
		cow_http3:encode_int(6), cow_http3:encode_int(4096),
		cow_http3:encode_int(6), cow_http3:encode_int(8192)
	],
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		<<4>>, %% SETTINGS frame.
		cow_http3:encode_int(iolist_size(SettingsPayload)),
		SettingsPayload
	]),
	%% The connection should have been closed.
	#{reason := h3_settings_error} = do_wait_connection_closed(Conn),
	ok.

settings_ignore_unknown_identifier(Config) ->
	doc("Unknown SETTINGS identifiers must be ignored (RFC9114 7.2.4, RFC9114 9)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	SettingsPayload = [
		cow_http3:encode_int(999), cow_http3:encode_int(4096)
	],
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		<<4>>, %% SETTINGS frame.
		cow_http3:encode_int(iolist_size(SettingsPayload)),
		SettingsPayload
	]),
	%% The connection should remain up.
	receive
		{quic, shutdown, Conn, {unknown_quic_status, Code}} ->
			Reason = cow_http3:code_to_error(Code),
			error(Reason)
	after 1000 ->
		ok
	end.

settings_ignore_reserved_identifier(Config) ->
	doc("Reserved SETTINGS identifiers must be ignored (RFC9114 7.2.4.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	SettingsPayload = [
		cow_http3:encode_int(do_reserved_type()), cow_http3:encode_int(4096)
	],
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		<<4>>, %% SETTINGS frame.
		cow_http3:encode_int(iolist_size(SettingsPayload)),
		SettingsPayload
	]),
	%% The connection should remain up.
	receive
		{quic, shutdown, Conn, {unknown_quic_status, Code}} ->
			Reason = cow_http3:code_to_error(Code),
			error(Reason)
	after 1000 ->
		ok
	end.

%% @todo Check that we send a reserved SETTINGS identifier when sending a
%%       non-empty SETTINGS frame. (7.2.4.1. Defined SETTINGS Parameters)

%% @todo Check that setting SETTINGS_MAX_FIELD_SECTION_SIZE works.

%% It is unclear whether the SETTINGS identifier 0x00 must be rejected or ignored.

settings_reject_http2_0x02(Config) ->
	do_settings_reject_http2(Config, 2, 1).

settings_reject_http2_0x03(Config) ->
	do_settings_reject_http2(Config, 3, 100).

settings_reject_http2_0x04(Config) ->
	do_settings_reject_http2(Config, 4, 128000).

settings_reject_http2_0x05(Config) ->
	do_settings_reject_http2(Config, 5, 1000000).

do_settings_reject_http2(Config, Identifier, Value) ->
	doc("Receipt of an unused HTTP/2 SETTINGS identifier must be rejected "
		"with an H3_SETTINGS_ERROR connection error. (RFC9114 7.2.4, RFC9114 11.2.2)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	SettingsPayload = [
		cow_http3:encode_int(Identifier), cow_http3:encode_int(Value)
	],
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		<<4>>, %% SETTINGS frame.
		cow_http3:encode_int(iolist_size(SettingsPayload)),
		SettingsPayload
	]),
	%% The connection should have been closed.
	#{reason := h3_settings_error} = do_wait_connection_closed(Conn),
	ok.

%% 7.2.4.2. Initialization
%% An HTTP implementation MUST NOT send frames or requests that would be
%% invalid based on its current understanding of the peer's settings.
%% @todo In the case of SETTINGS_MAX_FIELD_SECTION_SIZE I don't think we have a choice.

%% All settings begin at an initial value. Each endpoint SHOULD use these
%% initial values to send messages before the peer's SETTINGS frame has arrived,
%% as packets carrying the settings can be lost or delayed. When the SETTINGS
%% frame arrives, any settings are changed to their new values.

%% Endpoints MUST NOT require any data to be received from the peer prior to
%% sending the SETTINGS frame; settings MUST be sent as soon as the transport is
%% ready to send data.

%% @todo Implement 0-RTT. (7.2.4.2. Initialization)

%% @todo Implement server push. (7.2.5. PUSH_PROMISE)

goaway_on_bidi_stream(Config) ->
	doc("Receipt of a GOAWAY frame on a bidirectional stream "
		"must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.6)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, _} = quicer:send(StreamRef, [
		<<7>>, cow_http3:encode_int(1), cow_http3:encode_int(0) %% GOAWAY.
	], ?QUIC_SEND_FLAG_FIN),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

%% @todo Implement server push. (7.2.6 GOAWAY - will have to reject too large push IDs)

max_push_id_on_bidi_stream(Config) ->
	doc("Receipt of a MAX_PUSH_ID frame on a bidirectional stream "
		"must be rejected with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.7)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, _} = quicer:send(StreamRef, [
		<<13>>, cow_http3:encode_int(1), cow_http3:encode_int(0) %% MAX_PUSH_ID.
	], ?QUIC_SEND_FLAG_FIN),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

%% @todo Implement server push. (7.2.7 MAX_PUSH_ID)

max_push_id_reject_lower(Config) ->
	doc("Receipt of a MAX_PUSH_ID value lower than previously received "
		"must be rejected with an H3_ID_ERROR connection error. (RFC9114 7.2.7)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		<<13>>, cow_http3:encode_int(1), cow_http3:encode_int(20), %% MAX_PUSH_ID.
		<<13>>, cow_http3:encode_int(1), cow_http3:encode_int(10) %% MAX_PUSH_ID.
	]),
	%% The connection should have been closed.
	#{reason := h3_id_error} = do_wait_connection_closed(Conn),
	ok.

reserved_on_control_stream(Config) ->
	doc("Receipt of a reserved frame type on a control stream "
		"must be ignored. (RFC9114 7.2.8)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	Len = rand:uniform(512),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		cow_http3:encode_int(do_reserved_type()),
		cow_http3:encode_int(Len),
		rand:bytes(Len)
	]),
	%% The connection should remain up.
	receive
		{quic, shutdown, Conn, {unknown_quic_status, Code}} ->
			Reason = cow_http3:code_to_error(Code),
			error(Reason)
	after 1000 ->
		ok
	end.

reserved_reject_http2_0x02_control(Config) ->
	do_reserved_reject_http2_control(Config, 2).

reserved_reject_http2_0x06_control(Config) ->
	do_reserved_reject_http2_control(Config, 6).

reserved_reject_http2_0x08_control(Config) ->
	do_reserved_reject_http2_control(Config, 8).

reserved_reject_http2_0x09_control(Config) ->
	do_reserved_reject_http2_control(Config, 9).

do_reserved_reject_http2_control(Config, Type) ->
	doc("Receipt of an unused HTTP/2 frame type must be rejected "
		"with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.8, RFC9114 11.2.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, ControlRef} = quicer:start_stream(Conn,
		#{open_flag => ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}),
	{ok, SettingsBin, _HTTP3Machine0} = cow_http3_machine:init(client, #{}),
	Len = rand:uniform(512),
	{ok, _} = quicer:send(ControlRef, [
		<<0>>, %% CONTROL stream.
		SettingsBin,
		cow_http3:encode_int(Type),
		cow_http3:encode_int(Len),
		rand:bytes(Len)
	]),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

reserved_reject_http2_0x02_bidi(Config) ->
	do_reserved_reject_http2_bidi(Config, 2).

reserved_reject_http2_0x06_bidi(Config) ->
	do_reserved_reject_http2_bidi(Config, 6).

reserved_reject_http2_0x08_bidi(Config) ->
	do_reserved_reject_http2_bidi(Config, 8).

reserved_reject_http2_0x09_bidi(Config) ->
	do_reserved_reject_http2_bidi(Config, 9).

do_reserved_reject_http2_bidi(Config, Type) ->
	doc("Receipt of an unused HTTP/2 frame type must be rejected "
		"with an H3_FRAME_UNEXPECTED connection error. (RFC9114 7.2.8, RFC9114 11.2.1)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, EncodedHeaders, _EncData, _EncSt} = cow_qpack:encode_field_section([
		{<<":method">>, <<"GET">>},
		{<<":scheme">>, <<"https">>},
		{<<":authority">>, <<"localhost">>},
		{<<":path">>, <<"/">>},
		{<<"content-length">>, <<"0">>}
	], 0, cow_qpack:init(encoder)),
	Len = rand:uniform(512),
	{ok, _} = quicer:send(StreamRef, [
		cow_http3:encode_int(Type),
		cow_http3:encode_int(Len),
		rand:bytes(Len),
		<<1>>, %% HEADERS frame.
		cow_http3:encode_int(iolist_size(EncodedHeaders)),
		EncodedHeaders
	], ?QUIC_SEND_FLAG_FIN),
	%% The connection should have been closed.
	#{reason := h3_frame_unexpected} = do_wait_connection_closed(Conn),
	ok.

%% An endpoint MAY choose to treat a stream error as a connection error under
%% certain circumstances, closing the entire connection in response to a
%% condition on a single stream.

%% Because new error codes can be defined without negotiation (see Section 9),
%% use of an error code in an unexpected context or receipt of an unknown error
%% code MUST be treated as equivalent to H3_NO_ERROR.

%% 8.1. HTTP/3 Error Codes
%% H3_INTERNAL_ERROR (0x0102): An internal error has occurred in the HTTP stack.
%% H3_EXCESSIVE_LOAD (0x0107): The endpoint detected that its peer is
%% exhibiting a behavior that might be generating excessive load.
%% H3_MISSING_SETTINGS (0x010a): No SETTINGS frame was received
%% at the beginning of the control stream.
%% H3_REQUEST_REJECTED (0x010b): A server rejected a request without
%% performing any application processing.
%% H3_REQUEST_CANCELLED (0x010c): The request or its response
%% (including pushed response) is cancelled.
%% H3_REQUEST_INCOMPLETE (0x010d): The client's stream terminated
%% without containing a fully formed request.
%% H3_CONNECT_ERROR (0x010f): The TCP connection established in
%% response to a CONNECT request was reset or abnormally closed.
%% H3_VERSION_FALLBACK (0x0110): The requested operation cannot
%% be served over HTTP/3. The peer should retry over HTTP/1.1.

%% 9. Extensions to HTTP/3
%% If a setting is used for extension negotiation, the default value MUST be
%% defined in such a fashion that the extension is disabled if the setting is
%% omitted.

%% 10. Security Considerations
%% 10.3. Intermediary-Encapsulation Attacks
%% Requests or responses containing invalid field names MUST be treated as malformed.
%% Any request or response that contains a character not permitted in a field
%% value MUST be treated as malformed.

%% 10.5. Denial-of-Service Considerations
%% Implementations SHOULD track the use of these features and set limits on
%% their use. An endpoint MAY treat activity that is suspicious as a connection
%% error of type H3_EXCESSIVE_LOAD, but false positives will result in disrupting
%% valid connections and requests.

reject_large_unknown_frame(Config) ->
	doc("Large unknown frames may risk denial-of-service "
		"and should be rejected. (RFC9114 10.5)"),
	#{conn := Conn} = do_connect(Config),
	{ok, StreamRef} = quicer:start_stream(Conn, #{}),
	{ok, _} = quicer:send(StreamRef, [
		cow_http3:encode_int(do_unknown_frame_type()),
		cow_http3:encode_int(16385)
	]),
	#{reason := h3_excessive_load} = do_wait_connection_closed(Conn),
	ok.

%% 10.5.1. Limits on Field Section Size
%% An endpoint can use the SETTINGS_MAX_FIELD_SECTION_SIZE (Section 4.2.2)
%% setting to advise peers of limits that might apply on the size of field
%% sections.
%%
%% A server that receives a larger field section than it is willing to handle
%% can send an HTTP 431 (Request Header Fields Too Large) status code
%% ([RFC6585]).

%% 10.6. Use of Compression
%% Implementations communicating on a secure channel MUST NOT compress content
%% that includes both confidential and attacker-controlled data unless separate
%% compression contexts are used for each source of data. Compression MUST NOT be
%% used if the source of data cannot be reliably determined.

%% 10.9. Early Data
%% The anti-replay mitigations in [HTTP-REPLAY] MUST be applied when using HTTP/3 with 0-RTT.

%% 10.10. Migration
%% Certain HTTP implementations use the client address for logging or
%% access-control purposes. Since a QUIC client's address might change during a
%% connection (and future versions might support simultaneous use of multiple
%% addresses), such implementations will need to either actively retrieve the
%% client's current address or addresses when they are relevant or explicitly
%% accept that the original address might change. @todo Document this behavior.

%% Appendix A. Considerations for Transitioning from HTTP/2
%% A.1. Streams
%% QUIC considers a stream closed when all data has been received and sent data
%% has been acknowledged by the peer. HTTP/2 considers a stream closed when the
%% frame containing the END_STREAM bit has been committed to the transport. As a
%% result, the stream for an equivalent exchange could remain "active" for a
%% longer period of time. HTTP/3 servers might choose to permit a larger number
%% of concurrent client-initiated bidirectional streams to achieve equivalent
%% concurrency to HTTP/2, depending on the expected usage patterns. @todo Document this.

%% Helper functions.

%% @todo Maybe have a function in cow_http3.
do_reserved_type() ->
	16#1f * (rand:uniform(148764065110560900) - 1) + 16#21.

do_connect(Config) ->
	do_connect(Config, #{}).

do_connect(Config, Opts) ->
	{ok, Conn} = quicer:connect("localhost", config(port, Config),
		Opts#{alpn => ["h3"], verify => none}, 5000),
	%% To make sure the connection is fully established we wait
	%% to receive the SETTINGS frame on the control stream.
	{ok, ControlRef, Settings} = do_wait_settings(Conn),
	#{
		conn => Conn,
		control => ControlRef, %% This is the peer control stream.
		settings => Settings
	}.

do_wait_settings(Conn) ->
	receive
		{quic, new_stream, StreamRef, #{flags := Flags}} ->
			ok = quicer:setopt(StreamRef, active, true),
			true = quicer:is_unidirectional(Flags),
			receive {quic, <<
				0, %% Control stream.
				SettingsFrame/bits
			>>, StreamRef, _} ->
				{ok, {settings, Settings}, <<>>} = cow_http3:parse(SettingsFrame),
				{ok, StreamRef, Settings}
			after 5000 ->
				{error, timeout}
			end
	after 5000 ->
		{error, timeout}
	end.

do_receive_data(StreamRef) ->
	receive
		{quic, Data, StreamRef, _Flags} when is_binary(Data) ->
			{ok, Data}
	after 5000 ->
		{error, timeout}
	end.

do_guess_int_encoding(Data) ->
	SizeWithLen = byte_size(Data) - 1,
	if
		SizeWithLen < 64 + 1 ->
			{0, 6};
		SizeWithLen < 16384 + 2 ->
			{1, 14};
		SizeWithLen < 1073741824 + 4 ->
			{2, 30};
		SizeWithLen < 4611686018427387904 + 8 ->
			{3, 62}
	end.

do_wait_peer_send_shutdown(StreamRef) ->
	receive
		{quic, peer_send_shutdown, StreamRef, undefined} ->
			ok
	after 5000 ->
		{error, timeout}
	end.

do_wait_stream_aborted(StreamRef) ->
	receive
		{quic, peer_send_aborted, StreamRef, Code} ->
			Reason = cow_http3:code_to_error(Code),
			#{reason => Reason};
		{quic, peer_receive_aborted, StreamRef, Code} ->
			Reason = cow_http3:code_to_error(Code),
			#{reason => Reason}
	after 5000 ->
		{error, timeout}
	end.

do_wait_stream_closed(StreamRef) ->
	receive
		{quic, stream_closed, StreamRef, #{error := Error, is_conn_shutdown := false}} ->
			0 = Error,
			ok
	after 5000 ->
		{error, timeout}
	end.

do_receive_response(StreamRef) ->
	{ok, Data} = do_receive_data(StreamRef),
	{HLenEnc, HLenBits} = do_guess_int_encoding(Data),
	<<
		1, %% HEADERS frame.
		HLenEnc:2, HLen:HLenBits,
		EncodedResponse:HLen/bytes,
		Rest/bits
	>> = Data,
	{ok, DecodedResponse, _DecData, _DecSt}
		= cow_qpack:decode_field_section(EncodedResponse, 0, cow_qpack:init(decoder)),
	Headers = maps:from_list(DecodedResponse),
	#{<<"content-length">> := BodyLen} = Headers,
	{DLenEnc, DLenBits} = do_guess_int_encoding(Rest),
	Body = case Rest of
		<<>> ->
			<<>>;
		<<
			0, %% DATA frame.
			DLenEnc:2, DLen:DLenBits,
			Body0:DLen/bytes
		>> ->
			BodyLen = integer_to_binary(byte_size(Body0)),
			Body0
	end,
	ok = do_wait_peer_send_shutdown(StreamRef),
	#{
		headers => Headers,
		body => Body
	}.

do_wait_connection_closed(Conn) ->
	receive
		{quic, shutdown, Conn, {unknown_quic_status, Code}} ->
			Reason = cow_http3:code_to_error(Code),
			#{reason => Reason}
	after 5000 ->
		{error, timeout}
	end.

-endif.