ntex-httparse 2.2.0

A tiny, safe, speedy, zero-copy HTTP/1.x parser.
Documentation
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
#![deny(clippy::pedantic, clippy::missing_safety_doc)]
#![allow(
    clippy::cast_lossless,
    clippy::cast_possible_truncation,
    clippy::missing_panics_doc,
    clippy::missing_errors_doc
)]
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![cfg_attr(test, deny(warnings))]

//! # ntex-httparse
//!
//! A push library for parsing HTTP/1.x requests and responses, used by
//! [ntex](https://crates.io/crates/ntex). It is a fork of
//! [httparse](https://crates.io/crates/httparse) with a lower level API.
//!
//! Parsers do not borrow the input. Parsed parts are returned as
//! [`SlicePos`] ranges into the buffer passed to `parse`. The start line and
//! the headers are parsed separately:
//!
//! * [`Request::parse`] or [`Response::parse`] parses the start line and
//!   returns the position right after it.
//! * [`Header::parse`] parses one header at a time, until it returns
//!   [`HeaderParsed::Eof`] for the empty line ending the header section.
//! * [`parse_chunk_size`] parses the size line of a chunked body.
//!
//! Incomplete input is not an error, the parsers return
//! [`Status::Partial`]. The `parse_with_state` variants save their progress
//! in a [`State`], so the next call with more data continues where the
//! previous one stopped instead of starting over.
//!
//! The focus is on speed and safety. Parsing is bounds-checked, unsafe code
//! is limited to the SIMD matchers in a submodule.
//!
//! SIMD optimizations are enabled automatically when available.
//! If building an executable to be run on multiple platforms, and thus
//! not passing `target_feature` or `target_cpu` flags to the compiler,
//! runtime detection can still detect SSE4.2 or AVX2 support to provide
//! massive wins.
//!
//! If compiling for a specific target, remembering to include
//! `-C target_cpu=native` allows the detection to become compile time checks,
//! making it *even* faster.
//!
//! # Example
//!
//! ```
//! use ntex_httparse::{Header, HeaderParsed, Request, Status};
//!
//! let buf = b"GET /index.html HTTP/1.1\r\nHost: example.com\r\n\r\nbody";
//!
//! let mut req = Request::default();
//! let Status::Complete(mut pos) = req.parse(buf).unwrap() else {
//!     unreachable!("the request line is complete")
//! };
//! assert_eq!(&buf[req.path.start..req.path.end], b"/index.html");
//! assert_eq!(req.version, 1);
//!
//! let mut header = Header::default();
//! loop {
//!     let src = &buf[pos..];
//!     match header.parse(src).unwrap() {
//!         Status::Complete(HeaderParsed::Header(len)) => {
//!             assert_eq!(&src[header.name.start..header.name.end], b"Host");
//!             assert_eq!(&src[header.value.start..header.value.end], b"example.com");
//!             pos += len;
//!         }
//!         Status::Complete(HeaderParsed::Eof(len)) => {
//!             pos += len;
//!             break;
//!         }
//!         // wait for more data, then parse again
//!         Status::Partial => unreachable!(),
//!     }
//! }
//! assert_eq!(&buf[pos..], b"body");
//! ```

use core::{fmt, result, str};

mod iter;
#[macro_use]
mod macros;
mod headers;
mod simd;
mod utils;
mod version;

pub use crate::headers::{Header, HeaderParsed};
pub use crate::version::parse_version;

use crate::iter::Bytes;

/// An error in parsing.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Error {
    /// Invalid byte in header name.
    HeaderName,
    /// Invalid byte in header value.
    HeaderValue,
    /// Invalid byte in new line.
    NewLine,
    /// Invalid byte in Response status.
    Status,
    /// Invalid byte where token is required.
    Token,
    /// Unused, kept for compatibility. Headers are parsed one at a time.
    TooManyHeaders,
    /// Invalid byte in HTTP version.
    Version,
}

impl Error {
    #[inline]
    fn description_str(self) -> &'static str {
        match self {
            Error::HeaderName => "invalid header name",
            Error::HeaderValue => "invalid header value",
            Error::NewLine => "invalid new line",
            Error::Status => "invalid response status",
            Error::Token => "invalid token",
            Error::TooManyHeaders => "too many headers",
            Error::Version => "invalid HTTP version",
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.description_str())
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
    fn description(&self) -> &str {
        self.description_str()
    }
}

/// An error in parsing a chunk size.
#[derive(Debug, PartialEq, Eq)]
pub struct InvalidChunkSize;

impl fmt::Display for InvalidChunkSize {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid chunk size")
    }
}

/// A Result of any parsing action.
///
/// If the input is invalid, an `Error` will be returned. Note that incomplete
/// data is not considered invalid, and so will not return an error, but rather
/// a `Ok(Status::Partial)`.
pub type Result<T> = result::Result<Status<T>, Error>;

/// The result of a successful parse pass.
///
/// `Complete` is used when the buffer contained the complete value.
/// `Partial` is used when parsing did not reach the end of the expected value,
/// but no invalid data was found.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Status<T> {
    /// The completed result.
    Complete(T),
    /// A partial result.
    Partial,
}

impl<T> Status<T> {
    /// Convenience method to check if status is complete.
    #[inline]
    pub fn is_complete(&self) -> bool {
        match *self {
            Status::Complete(..) => true,
            Status::Partial => false,
        }
    }

    /// Convenience method to check if status is partial.
    #[inline]
    pub fn is_partial(&self) -> bool {
        match *self {
            Status::Complete(..) => false,
            Status::Partial => true,
        }
    }

    /// Convenience method to unwrap a Complete value. Panics if the status is
    /// `Partial`.
    #[inline]
    pub fn unwrap(self) -> T {
        match self {
            Status::Complete(t) => t,
            Status::Partial => panic!("Tried to unwrap Status::Partial"),
        }
    }
}

/// Progress of a resumable parse, used by the `parse_with_state` methods.
///
/// Start with `State::default()` and pass the same `State` again, with the
/// same (possibly grown) buffer, after a [`Status::Partial`] result. Reset it
/// to default before parsing the next item from a different buffer position.
/// The fields are internal bookkeeping and should not be changed by callers.
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
pub struct State {
    /// Parser specific step.
    pub state: u8,
    /// Start of the part being parsed.
    pub start: usize,
    /// Position of the next byte to parse.
    pub cursor: usize,
}

#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
/// A range of the buffer passed to `parse`, i.e. `&src[pos.start..pos.end]`.
///
/// An empty part, like an empty header value or reason phrase, is `0..0`.
pub struct SlicePos {
    /// Start of the range, inclusive.
    pub start: usize,
    /// End of the range, exclusive.
    pub end: usize,
}

impl SlicePos {
    pub(crate) fn reset(&mut self) {
        self.start = 0;
        self.end = 0;
    }
}

/// A parsed request line.
///
/// Only the request line is parsed, use [`Header`] for the headers that
/// follow it.
///
/// # Example
///
/// ```
/// use ntex_httparse::{Request, State, Status};
///
/// let mut req = Request::default();
/// let mut st = State::default();
///
/// // incomplete data, the parser remembers its progress in `st`
/// let buf = b"GET /index.html HT";
/// assert_eq!(req.parse_with_state(buf, &mut st), Ok(Status::Partial));
///
/// let buf = b"GET /index.html HTTP/1.1\r\nHost: example.com\r\n\r\n";
/// assert_eq!(req.parse_with_state(buf, &mut st), Ok(Status::Complete(26)));
/// assert_eq!(&buf[req.method.start..req.method.end], b"GET");
/// assert_eq!(&buf[req.path.start..req.path.end], b"/index.html");
/// ```
#[derive(Copy, Clone, Default, PartialEq, Eq, Debug)]
pub struct Request {
    /// Request method, an ASCII token.
    pub method: SlicePos,
    /// Request target. May contain bytes `0x80..=0xFF`, so it is not
    /// guaranteed to be UTF-8.
    pub path: SlicePos,
    /// HTTP version, `0` for HTTP/1.0 and `1` for HTTP/1.1.
    pub version: u8,
}

impl Request {
    #[inline]
    /// Parse a request line.
    ///
    /// Returns the position right after the line ending, where the headers
    /// start. Empty lines before the request line are skipped.
    pub fn parse(&mut self, src: &[u8]) -> Result<usize> {
        let mut st = State::default();
        self.parse_with_state(src, &mut st)
    }

    #[inline]
    /// Parse a request line, resuming from `st` saved by a previous `Partial`
    /// result.
    ///
    /// `st` must come from a previous call with the same buffer (which may have
    /// grown since), or be `State::default()`. An invalid state returns
    /// `Error::Token`.
    ///
    /// Bytes accepted by earlier calls are not scanned again, so feeding the
    /// line in pieces takes linear time.
    pub fn parse_with_state(&mut self, src: &[u8], st: &mut State) -> Result<usize> {
        const EMPTY_LINES: u8 = 0;
        const METHOD: u8 = 1;
        const SPACES_BEFORE_URI: u8 = 2;
        const URI: u8 = 3;
        const SPACES_BEFORE_VERSION: u8 = 4;
        const VERSION: u8 = 5;
        const NEWLINE: u8 = 6;

        if st.state > NEWLINE || st.start > st.cursor || st.cursor > src.len() {
            return Err(Error::Token);
        }
        let mut bytes = Bytes::new(src, st);

        if bytes.st.state == EMPTY_LINES {
            complete!(utils::skip_empty_lines(&mut bytes));
            bytes.st.state = METHOD;
        }
        if bytes.st.state == METHOD {
            self.method = complete!(parse_method_inner(&mut bytes));
            bytes.st.state = SPACES_BEFORE_URI;
        }
        if bytes.st.state == SPACES_BEFORE_URI {
            complete!(utils::skip_spaces(&mut bytes));
            bytes.st.state = URI;
        }
        if bytes.st.state == URI {
            self.path = complete!(parse_uri_inner(&mut bytes));
            bytes.st.state = SPACES_BEFORE_VERSION;
        }
        if bytes.st.state == SPACES_BEFORE_VERSION {
            complete!(utils::skip_spaces(&mut bytes));
            bytes.st.state = VERSION;
        }
        if bytes.st.state == VERSION {
            // at most 8 bytes, parsed again from the start if incomplete
            let mut tmp = *bytes.st;
            self.version = complete!(version::parse_version_inner(&mut Bytes::new(src, &mut tmp)));
            *bytes.st = tmp;
            bytes.st.state = NEWLINE;
        }

        newline!(bytes);
        Ok(Status::Complete(bytes.cursor()))
    }
}

/// A parsed status line.
///
/// Only the status line is parsed, use [`Header`] for the headers that
/// follow it.
///
/// # Example
///
/// ```
/// use ntex_httparse::{Response, Status};
///
/// let buf = b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
/// let mut res = Response::default();
/// assert_eq!(res.parse(buf), Ok(Status::Complete(24)));
/// assert_eq!(res.code, 404);
/// assert_eq!(&buf[res.reason.start..res.reason.end], b"Not Found");
/// ```
#[derive(Copy, Clone, Default, PartialEq, Eq, Debug)]
pub struct Response {
    /// HTTP version, `0` for HTTP/1.0 and `1` for HTTP/1.1.
    pub version: u8,
    /// Status code, three digits.
    pub code: u16,
    /// Reason phrase, empty if missing. A non-empty reason is ASCII, a reason
    /// containing obs-text (bytes `0x80..=0xFF`) is returned as empty.
    pub reason: SlicePos,
}

impl Response {
    #[inline]
    /// Parse a status line.
    ///
    /// Returns the position right after the line ending, where the headers
    /// start. Empty lines before the status line are skipped. The reason
    /// phrase is optional.
    pub fn parse(&mut self, src: &[u8]) -> Result<usize> {
        let mut st = State::default();
        self.parse_with_state(src, &mut st)
    }

    #[inline]
    /// Parse a status line, resuming from `st` saved by a previous `Partial`
    /// result.
    ///
    /// `st` must come from a previous call with the same buffer (which may have
    /// grown since), or be `State::default()`. An invalid state returns
    /// `Error::Status`.
    ///
    /// Bytes accepted by earlier calls are not scanned again, so feeding the
    /// line in pieces takes linear time.
    pub fn parse_with_state(&mut self, src: &[u8], st: &mut State) -> Result<usize> {
        const EMPTY_LINES: u8 = 0;
        const VERSION: u8 = 1;
        const SPACES_BEFORE_CODE: u8 = 2;
        const CODE: u8 = 3;
        const AFTER_CODE: u8 = 4;
        const SPACES_BEFORE_REASON: u8 = 5;
        const REASON: u8 = 6;
        const REASON_OBS_TEXT: u8 = 7;

        if st.state > REASON_OBS_TEXT || st.start > st.cursor || st.cursor > src.len() {
            return Err(Error::Status);
        }
        let mut bytes = Bytes::new(src, st);

        if bytes.st.state == EMPTY_LINES {
            complete!(utils::skip_empty_lines(&mut bytes));
            bytes.st.state = VERSION;
        }
        if bytes.st.state == VERSION {
            // at most 9 bytes, parsed again from the start if incomplete
            let mut tmp = *bytes.st;
            let mut b = Bytes::new(src, &mut tmp);
            self.version = complete!(version::parse_version_inner(&mut b));
            expect!(b.next() == b' ' => Err(Error::Version));
            b.commit();
            *bytes.st = tmp;
            bytes.st.state = SPACES_BEFORE_CODE;
        }
        if bytes.st.state == SPACES_BEFORE_CODE {
            complete!(utils::skip_spaces(&mut bytes));
            bytes.st.state = CODE;
        }
        if bytes.st.state == CODE {
            // 3 bytes, parsed again from the start if incomplete
            let mut tmp = *bytes.st;
            self.code = complete!(parse_code(&mut Bytes::new(src, &mut tmp)));
            *bytes.st = tmp;
            bytes.st.state = AFTER_CODE;
        }
        if bytes.st.state == AFTER_CODE {
            // RFC7230 says there must be 'SP' and then reason-phrase, but admits
            // its only for legacy reasons. With the reason-phrase completely
            // optional (and preferred to be omitted) in HTTP2, we'll just
            // handle any response that doesn't include a reason-phrase, because
            // it's more lenient, and we don't care anyways.
            //
            // So, a SP means parse a reason-phrase.
            // A newline means go to headers.
            // Anything else we'll say is a malformed status.
            match next!(bytes) {
                b' ' => bytes.st.state = SPACES_BEFORE_REASON,
                b'\r' => {
                    expect_lf!(bytes => Err(Error::Status));
                    bytes.commit();
                    self.reason = SlicePos::default();
                    return Ok(Status::Complete(bytes.cursor()));
                }
                b'\n' => {
                    bytes.commit();
                    self.reason = SlicePos::default();
                    return Ok(Status::Complete(bytes.cursor()));
                }
                _ => return Err(Error::Status),
            }
        }
        if bytes.st.state == SPACES_BEFORE_REASON {
            complete!(utils::skip_spaces(&mut bytes));
            bytes.st.state = REASON;
        }

        let mut seen_obs_text = bytes.st.state == REASON_OBS_TEXT;
        let res = parse_reason(&mut bytes, &mut seen_obs_text);
        if seen_obs_text {
            bytes.st.state = REASON_OBS_TEXT;
        }
        self.reason = complete!(res);
        Ok(Status::Complete(bytes.cursor()))
    }
}

#[inline]
#[doc(hidden)]
/// Parse a request method. Exported for internal benchmarks, not part of the
/// public API.
pub fn parse_method(src: &[u8]) -> Result<&str> {
    let mut st = State::default();
    let mut bytes = Bytes::new(src, &mut st);
    complete!(utils::skip_empty_lines(&mut bytes));
    let s = complete!(parse_method_inner(&mut bytes));
    // SAFETY: parse_method_inner verifies validity of method
    let m = unsafe { str::from_utf8_unchecked(&src[s.start..s.end]) };
    Ok(Status::Complete(m))
}

/// Parses a method followed by a space, starting at `bytes.start()`. Bytes
/// before the cursor were accepted by a previous call.
#[inline]
fn parse_method_inner(bytes: &mut Bytes<'_, '_>) -> Result<SlicePos> {
    const GET: [u8; 4] = *b"GET ";
    const POST: [u8; 4] = *b"POST";

    if bytes.cursor() == bytes.start() {
        match bytes.peek_n::<4>() {
            Some(GET) => {
                // we matched "GET " which has 4 bytes and is ASCII
                bytes.advance(4);
                return Ok(Status::Complete(bytes.slice_position(1)));
            }
            Some(POST) if bytes.peek_ahead(4) == Some(b' ') => {
                // we matched "POST " which has 5 bytes
                bytes.advance(5);
                return Ok(Status::Complete(bytes.slice_position(1)));
            }
            _ => {}
        }
        // First char must be a token char, it can't be a space which would
        // indicate an empty token.
        if !utils::is_method_token(next!(bytes)) {
            return Err(Error::Token);
        }
    }

    loop {
        let b = next!(bytes);
        if b == b' ' {
            // all bytes are `is_method_token`, so the method is ASCII
            return Ok(Status::Complete(bytes.slice_position(1)));
        } else if !utils::is_method_token(b) {
            return Err(Error::Token);
        }
    }
}

/// From [RFC 7230](https://tools.ietf.org/html/rfc7230):
///
/// > ```notrust
/// > reason-phrase  = *( HTAB / SP / VCHAR / obs-text )
/// > HTAB           = %x09        ; horizontal tab
/// > VCHAR          = %x21-7E     ; visible (printing) characters
/// > obs-text       = %x80-FF
/// > ```
///
/// > A.2.  Changes from RFC 2616
/// >
/// > Non-US-ASCII content in header fields and the reason phrase
/// > has been obsoleted and made opaque (the TEXT rule was removed).
///
/// Parses from `bytes.start()`, bytes before the cursor were accepted by a
/// previous call, `seen_obs_text` carries over whether they had obs-text.
#[inline]
fn parse_reason(bytes: &mut Bytes<'_, '_>, seen_obs_text: &mut bool) -> Result<SlicePos> {
    // Short reasons are scanned byte by byte, the SIMD call only pays off
    // for longer ones. Reason bytes are the same set as header value bytes.
    const SCALAR_PREFIX: usize = 8;
    let mut n = 0;
    while let Some(b) = bytes.peek() {
        if n == SCALAR_PREFIX {
            match_reason_vectored(bytes, seen_obs_text);
            break;
        }
        if b < 0x80 && utils::is_header_value_token(b) {
            bytes.advance(1);
            n += 1;
        } else {
            break;
        }
    }

    loop {
        let b = next!(bytes);
        let skip = if b == b'\r' {
            expect_lf!(bytes => Err(Error::Status));
            2
        } else if b == b'\n' {
            1
        } else if !(b == 0x09 || b == b' ' || (0x21..=0x7E).contains(&b) || b >= 0x80) {
            return Err(Error::Status);
        } else {
            if b >= 0x80 {
                *seen_obs_text = true;
            }
            continue;
        };

        // A non-empty reason contains only HTAB / SP / VCHAR, so it is
        // ASCII. With obs-text an empty reason is returned instead.
        return Ok(Status::Complete(if *seen_obs_text {
            bytes.commit();
            SlicePos::default()
        } else {
            bytes.slice_position(skip)
        }));
    }
}

#[inline(never)]
fn match_reason_vectored(bytes: &mut Bytes<'_, '_>, seen_obs_text: &mut bool) {
    let from = bytes.cursor();
    simd::match_header_value_vectored(bytes);
    if !*seen_obs_text && !bytes.since(from).is_ascii() {
        *seen_obs_text = true;
    }
}

#[inline]
#[doc(hidden)]
/// Parse a request target followed by a space. Exported for internal
/// benchmarks, not part of the public API.
///
/// Unlike [`Request::parse`], returns `Error::Token` if the target is not
/// valid UTF-8.
pub fn parse_uri(src: &[u8]) -> Result<&str> {
    let mut st = State::default();
    let mut bytes = Bytes::new(src, &mut st);
    if let Status::Complete(pos) = parse_uri_inner(&mut bytes)? {
        complete!(utils::skip_spaces(&mut bytes));
        if let Ok(path) = simdutf8::basic::from_utf8(&src[pos.start..pos.end]) {
            Ok(Status::Complete(path))
        } else {
            Err(Error::Token)
        }
    } else {
        Ok(Status::Partial)
    }
}

#[inline]
fn parse_uri_inner(bytes: &mut Bytes<'_, '_>) -> Result<SlicePos> {
    let start = bytes.start();
    simd::match_uri_vectored(bytes);
    let b_end = bytes.cursor();

    if next!(bytes) == b' ' {
        // URI must have at least one char
        if start == b_end {
            return Err(Error::Token);
        }

        // URI bytes may include 0x80..=0xFF, so they are not guaranteed to be UTF-8
        let end = bytes.cursor() - 1;
        bytes.commit();
        Ok(Status::Complete(SlicePos { start, end }))
    } else {
        Err(Error::Token)
    }
}

#[inline]
fn parse_code(bytes: &mut Bytes<'_, '_>) -> Result<u16> {
    let hundreds = expect!(bytes.next() == b'0'..=b'9' => Err(Error::Status));
    let tens = expect!(bytes.next() == b'0'..=b'9' => Err(Error::Status));
    let ones = expect!(bytes.next() == b'0'..=b'9' => Err(Error::Status));

    Ok(Status::Complete(
        (hundreds - b'0') as u16 * 100 + (tens - b'0') as u16 * 10 + (ones - b'0') as u16,
    ))
}

/// Parse a buffer of bytes as a chunk size line.
///
/// The return value, if complete and successful, includes the position right
/// after the line, where the chunk data starts, and the size of the chunk.
/// A size of `0` is the last chunk.
///
/// The size is 1 to 16 hex digits and the line must end with CRLF. Chunk
/// extensions are skipped, control characters other than HTAB are rejected
/// in them.
///
/// # Example
///
/// ```
/// let buf = b"4\r\nRust\r\n0\r\n\r\n";
/// assert_eq!(ntex_httparse::parse_chunk_size(buf),
///            Ok(ntex_httparse::Status::Complete((3, 4))));
/// ```
pub fn parse_chunk_size(buf: &[u8]) -> result::Result<Status<(usize, u64)>, InvalidChunkSize> {
    const RADIX: u64 = 16;
    let mut st = State::default();
    let mut bytes = Bytes::new(buf, &mut st);
    let mut size = 0;
    let mut in_chunk_size = true;
    let mut in_ext = false;
    let mut count = 0;
    loop {
        let b = next!(bytes);
        match b {
            b'0'..=b'9' if in_chunk_size => {
                if count > 15 {
                    return Err(InvalidChunkSize);
                }
                count += 1;
                if cfg!(debug_assertions) && size > (u64::MAX / RADIX) {
                    // actually unreachable!(), because count stops the loop at 15 digits before
                    // we can reach u64::MAX / RADIX == 0xfffffffffffffff, which requires 15 hex
                    // digits. This stops mirai reporting a false alarm regarding the `size *=
                    // RADIX` multiplication below.
                    return Err(InvalidChunkSize);
                }
                size *= RADIX;
                size += (b - b'0') as u64;
            }
            b'a'..=b'f' | b'A'..=b'F' if in_chunk_size => {
                if count > 15 {
                    return Err(InvalidChunkSize);
                }
                count += 1;
                if cfg!(debug_assertions) && size > (u64::MAX / RADIX) {
                    return Err(InvalidChunkSize);
                }
                size *= RADIX;
                size += ((b | 0x20) + 10 - b'a') as u64;
            }
            // the chunk size must have at least one digit
            b'\r' if count == 0 => return Err(InvalidChunkSize),
            b'\r' => match next!(bytes) {
                b'\n' => break,
                _ => return Err(InvalidChunkSize),
            },
            // If we weren't in the extension yet, the ";" signals its start
            b';' if !in_ext => {
                in_ext = true;
                in_chunk_size = false;
            }
            // "Linear white space" is ignored between the chunk size and the
            // extension separator token (";") due to the "implied *LWS rule".
            b'\t' | b' ' if !in_ext && !in_chunk_size => {}
            // LWS can follow the chunk size, but no more digits can come
            b'\t' | b' ' if in_chunk_size => in_chunk_size = false,
            // Control characters other than HTAB are not allowed in extensions,
            // a bare LF could be treated as the line end by other parsers.
            0x00..=0x08 | 0x0a..=0x1f | 0x7f if in_ext => return Err(InvalidChunkSize),
            // We allow any other octet once we are in the extension, since
            // they all get ignored anyway. According to the HTTP spec, valid
            // extensions would have a more strict syntax:
            //     (token ["=" (token | quoted-string)])
            // but we gain nothing by rejecting an otherwise valid chunk size.
            _ if in_ext => {}
            // Finally, if we aren't in the extension and we're reading any
            // other octet, the chunk size line is invalid!
            _ => return Err(InvalidChunkSize),
        }
    }
    Ok(Status::Complete((bytes.cursor(), size)))
}

#[cfg(test)]
mod tests {
    #![allow(clippy::items_after_statements)]
    use super::*;

    macro_rules! req {
        ($name:ident, $buf:expr, |$len:ident, $method:ident, $path:ident, $version:ident, $headers:ident, $headers_eof:ident| $body:expr) => {
            #[test]
            fn $name() {
                let mut req = Request::default();
                let mut b = $buf.as_ref();
                if let Ok(Status::Complete(l)) = req.parse(b) {
                    let mut consumed = l;
                    let mut headers = Vec::new();
                    let mut header = Header::default();
                    let mut headers_eof = false;
                    b = &b[consumed..];

                    while let Status::Complete(hdr) = header.parse(b).unwrap() {
                        match hdr {
                            HeaderParsed::Header(l) => {
                                consumed += l;
                                let name = String::from_utf8(Vec::from(
                                    &b[header.name.start..header.name.end],
                                ))
                                .unwrap();
                                let value = Vec::from(&b[header.value.start..header.value.end]);
                                headers.push((name, value));
                                b = &b[l..];
                            }
                            HeaderParsed::Eof(l) => {
                                consumed += l;
                                headers_eof = true;
                                break;
                            }
                        }
                    }

                    // SAFETY: Request::parse() validates path
                    let (path, method) = unsafe {
                        (
                            str::from_utf8_unchecked(&$buf.as_ref()[req.path.start..req.path.end]),
                            str::from_utf8_unchecked(
                                &$buf.as_ref()[req.method.start..req.method.end],
                            ),
                        )
                    };

                    closure(consumed, method, path, req.version, headers, headers_eof);
                } else {
                    panic!()
                }

                fn closure(
                    $len: usize,
                    $method: &str,
                    $path: &str,
                    $version: u8,
                    $headers: Vec<(String, Vec<u8>)>,
                    $headers_eof: bool,
                ) {
                    $body
                }
            }
        };
    }

    macro_rules! headers {
        ($name:ident, $buf:expr, |$len:ident, $headers:ident, $headers_eof:ident| $body:expr) => {
            #[test]
            fn $name() {
                let mut b = $buf.as_ref();
                let mut consumed = 0;
                let mut headers = Vec::new();
                let mut header = Header::default();
                let mut headers_eof = false;

                while let Status::Complete(hdr) = header.parse(b).unwrap() {
                    match hdr {
                        HeaderParsed::Header(l) => {
                            consumed += l;
                            let name = String::from_utf8(Vec::from(
                                &b[header.name.start..header.name.end],
                            ))
                            .unwrap();
                            let value = Vec::from(&b[header.value.start..header.value.end]);
                            headers.push((name, value));
                            b = &b[l..];
                        }
                        HeaderParsed::Eof(l) => {
                            consumed += l;
                            headers_eof = true;
                            break;
                        }
                    }
                }
                closure(consumed, headers, headers_eof);

                fn closure($len: usize, $headers: Vec<(String, Vec<u8>)>, $headers_eof: bool) {
                    $body
                }
            }
        };
    }

    macro_rules! req_err {
        ($name:ident, $buf:expr, $err:expr) => {
            #[test]
            fn $name() {
                assert_eq!(Request::default().parse($buf.as_ref()), $err);
            }
        };
    }

    macro_rules! req_par {
        ($name:ident, $buf:expr) => {
            #[test]
            fn $name() {
                assert_eq!(Request::default().parse($buf.as_ref()), Ok(Status::Partial));
            }
        };
    }

    macro_rules! headers_err {
        ($name:ident, $buf:expr, $err:expr) => {
            #[test]
            fn $name() {
                let mut consumed = 0;
                let mut header = Header::default();

                let result = loop {
                    match header.parse(&$buf.as_ref()[consumed..]) {
                        Ok(Status::Complete(HeaderParsed::Header(l))) => {
                            consumed += l;
                        }
                        Ok(_) => break Ok(()),
                        Err(e) => break Err(e),
                    }
                };
                assert_eq!(result, $err);
            }
        };
    }

    req! {
        test_request_simple,
        b"GET / HTTP/1.1\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 18);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    req! {
        test_request_simple_with_query_params,
        b"GET /thing?data=a HTTP/1.1\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 30);
            assert_eq!(method, "GET");
            assert_eq!(path, "/thing?data=a");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    req! {
        test_request_simple_with_whatwg_query_params,
        b"GET /thing?data=a^ HTTP/1.1\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 31);
            assert_eq!(method, "GET");
            assert_eq!(path, "/thing?data=a^");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    req! {
        test_request_headers,
        b"GET / HTTP/1.1\r\nHost: foo.com\r\nCookie: \r\n\r\n     ",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 43);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 2);
            assert_eq!(headers[0].0, "Host");
            assert_eq!(headers[0].1, b"foo.com");
            assert_eq!(headers[1].0, "Cookie");
            assert_eq!(headers[1].1, b"");
            assert!(eof);
        }
    }

    req! {
        test_request_headers_optional_whitespace,
        b"GET / HTTP/1.1\r\nHost: \tfoo.com\t \r\nCookie: \t \r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 48);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 2);
            assert_eq!(headers[0].0, "Host");
            assert_eq!(headers[0].1, b"foo.com");
            assert_eq!(headers[1].0, "Cookie");
            assert_eq!(headers[1].1, b"");
            assert!(eof);
        }
    }

    req! {
        // test the scalar parsing
        test_request_header_value_htab_short,
        b"GET / HTTP/1.1\r\nUser-Agent: some\tagent\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 42);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "User-Agent");
            assert_eq!(headers[0].1, b"some\tagent");
            assert!(eof);
        }
    }

    req! {
        // test the sse42 parsing
        test_request_header_value_htab_med,
        b"GET / HTTP/1.1\r\nUser-Agent: 1234567890some\tagent\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 52);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "User-Agent");
            assert_eq!(headers[0].1, b"1234567890some\tagent");
            assert!(eof);
        }
    }

    req! {
        // test the avx2 parsing
        test_request_header_value_htab_long,
        b"GET / HTTP/1.1\r\nUser-Agent: 1234567890some\t1234567890agent1234567890\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 72);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "User-Agent");
            assert_eq!(headers[0].1, &b"1234567890some\t1234567890agent1234567890"[..]);
            assert!(eof);
        }
    }

    req! {
        // test the avx2 parsing
        test_request_header_no_space_after_colon,
        b"GET / HTTP/1.1\r\nUser-Agent:omg-no-space1234567890some1234567890agent1234567890\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 82);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "User-Agent");
            assert_eq!(headers[0].1, &b"omg-no-space1234567890some1234567890agent1234567890"[..]);
            assert!(eof);
        }
    }

    req! {
        test_request_headers_max,
        b"GET / HTTP/1.1\r\nA: A\r\nB: B\r\nC: C\r\nD: D\r\n\r\n",
        |_len, _method, _path, _verion, headers, eof| {
            assert_eq!(headers.len(), 4);
            assert!(eof);
        }
    }

    req! {
        test_request_multibyte,
        b"GET / HTTP/1.1\r\nHost: foo.com\r\nUser-Agent: \xe3\x81\xb2\xe3/1.0\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 55);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 2);
            assert_eq!(headers[0].0, "Host");
            assert_eq!(headers[0].1, b"foo.com");
            assert_eq!(headers[1].0, "User-Agent");
            assert_eq!(headers[1].1, b"\xe3\x81\xb2\xe3/1.0");
            assert!(eof);
        }
    }

    // A single byte which is part of a method is not invalid
    req_par! {
        test_request_one_byte_method,
        b"G"
    }

    // A subset of a method is a partial method, not invalid
    req_par! {
        test_request_partial_method,
        b"GE"
    }

    // A method, without the delimiting space, is a partial request
    req_par! {
        test_request_method_no_delimiter,
        b"GET"
    }

    // Regression test: assert that a partial read with just the method and
    // space results in a partial, rather than a token error from uri parsing.
    req_par! {
        test_request_method_only,
        b"GET "
    }

    req! {
        test_request_partial,
        b"GET / HTTP/1.1\r\n\r",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, b"GET / HTTP/1.1\r\n\r".len() - 1);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 0);
            assert!(!eof);
        }
    }

    req_par! {
        test_request_partial_version,
        b"GET / HTTP/1."
    }

    req_par! {
        test_request_method_path_no_delimiter,
        b"GET /"
    }

    req_par! {
        test_request_method_path_only,
        b"GET / "
    }

    req! {
        test_request_partial_parses_headers_as_much_as_it_can,
        b"GET / HTTP/1.1\r\nHost: yolo\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 28);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "Host");
            assert_eq!(headers[0].1, b"yolo");
            assert!(!eof);
        }
    }

    req! {
        test_request_newlines,
        b"GET / HTTP/1.1\nHost: foo.bar\n\n",
        |_len, _method, _path, _verion, _headers, eof| {
            assert!(eof);
        }
    }

    req! {
        test_request_empty_lines_prefix,
        b"\r\n\r\nGET / HTTP/1.1\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 22);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    req! {
        test_request_empty_lines_prefix_lf_only,
        b"\n\nGET / HTTP/1.1\n\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 18);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    req! {
        test_request_path_backslash,
        b"\n\nGET /\\?wayne\\=5 HTTP/1.1\n\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 28);
            assert_eq!(method, "GET");
            assert_eq!(path, "/\\?wayne\\=5");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    req_err! {
        test_request_with_invalid_token_delimiter,
        b"GET\n/ HTTP/1.1\r\nHost: foo.bar\r\n\r\n",
        Err(Error::Token)
    }

    req_err! {
        test_request_with_invalid_but_short_version,
        b"GET / HTTP/1!",
        Err(Error::Version)
    }

    req_err! {
        test_request_with_empty_method,
        b" / HTTP/1.1\r\n\r\n",
        Err(Error::Token)
    }

    req_err! {
        test_request_with_empty_path,
        b"GET  HTTP/1.1\r\n\r\n",
        Err(Error::Token)
    }

    req_err! {
        test_request_with_empty_method_and_path,
        b"  HTTP/1.1\r\n\r\n",
        Err(Error::Token)
    }

    headers! {
        test_headers_optional_whitespace,
        b"Host: \tfoo.com\t \r\nCookie: \t \r\n",
        |len, headers, eof| {
            assert_eq!(len, 30);
            assert_eq!(headers.len(), 2);
            assert_eq!(headers[0].0, "Host");
            assert_eq!(headers[0].1, b"foo.com");
            assert_eq!(headers[1].0, "Cookie");
            assert_eq!(headers[1].1, b"");
            assert!(!eof);
        }
    }

    #[test]
    fn test_headers_with_state() {
        const B1: &[u8] = b"Host";
        const B2: &[u8] = b"Host: \t";
        const B3: &[u8] = b"Host: \tfoo.com\t ";
        const B4: &[u8] = b"Host: \tfoo.com\t \r\nCoo";
        const B5: &[u8] = b"Host: \tfoo.com\t \r\nCookie: \t \r\n\r\n";

        let mut st = State::default();
        let mut header = Header::default();
        assert!(header.parse_with_state(B1, &mut st).unwrap().is_partial());
        assert_eq!(st.state, 1);
        assert_eq!(st.start, 0);
        assert_eq!(st.cursor, 4);
        assert_eq!(header.name, SlicePos { start: 0, end: 0 });
        assert!(header.parse_with_state(B2, &mut st).unwrap().is_partial());
        assert_eq!(st.state, 2);
        assert_eq!(st.start, 0);
        assert_eq!(st.cursor, 7);
        assert_eq!(header.name, SlicePos { start: 0, end: 4 });
        assert_eq!(&B5[header.name.start..header.name.end], b"Host");

        assert!(header.parse_with_state(B3, &mut st).unwrap().is_partial());
        assert_eq!(st.state, 3);
        assert_eq!(st.start, 0);
        assert_eq!(st.cursor, 16);
        assert_eq!(header.name, SlicePos { start: 0, end: 4 });
        assert_eq!(header.value, SlicePos { start: 7, end: 0 });

        assert!(header.parse_with_state(B4, &mut st).unwrap().is_complete());
        assert_eq!(st.state, 0);
        assert_eq!(st.start, 18);
        assert_eq!(st.cursor, 18);
        assert_eq!(header.value, SlicePos { start: 7, end: 14 });
        assert_eq!(&B5[header.value.start..header.value.end], b"foo.com");

        assert!(header.parse_with_state(B5, &mut st).unwrap().is_complete());
        assert_eq!(st.state, 0);
        assert_eq!(st.start, 30);
        assert_eq!(st.cursor, 30);
        assert_eq!(header.name, SlicePos { start: 18, end: 24 });
        assert_eq!(header.value, SlicePos { start: 0, end: 0 });
        assert_eq!(&B5[header.name.start..header.name.end], b"Cookie");
        assert_eq!(&B5[header.value.start..header.value.end], b"");

        assert_eq!(
            header.parse_with_state(B5, &mut st),
            Ok(Status::Complete(HeaderParsed::Eof(32)))
        );
        assert_eq!(st.state, 0);
        assert_eq!(st.start, 32);
        assert_eq!(st.cursor, 32);
    }

    headers_err! {
        test_headers_with_obsolete_line_folding_at_start,
        b"Line-Folded-Header: \r\n   \r\n hello there\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_header_with_invalid_name,
        b"Host : foo.bar\r\n\r\n",
        Err(Error::HeaderName)
    }

    macro_rules! res {
        ($name:ident, $buf:expr, |$len:ident, $version:ident, $code:ident, $reason:ident, $headers:ident, $headers_eof:ident| $body:expr) => {
            #[test]
            fn $name() {
                let mut b = $buf.as_ref();
                let mut res = Response::default();
                let mut consumed = res.parse($buf.as_ref()).unwrap().unwrap();
                let mut headers = Vec::new();
                let mut header = Header::default();
                let mut headers_eof = false;
                b = &b[consumed..];

                while let Status::Complete(hdr) = header.parse(b).unwrap() {
                    match hdr {
                        HeaderParsed::Header(l) => {
                            consumed += l;
                            let name = String::from_utf8(Vec::from(
                                &b[header.name.start..header.name.end],
                            ))
                            .unwrap();
                            let value = Vec::from(&b[header.value.start..header.value.end]);
                            headers.push((name, value));
                            b = &b[l..];
                        }
                        HeaderParsed::Eof(l) => {
                            consumed += l;
                            headers_eof = true;
                            break;
                        }
                    }
                }

                // SAFETY: Request::parse() validates reason
                let reason = unsafe {
                    str::from_utf8_unchecked(&$buf.as_ref()[res.reason.start..res.reason.end])
                };

                closure(
                    consumed,
                    res.version,
                    res.code,
                    reason,
                    headers,
                    headers_eof,
                );

                fn closure(
                    $len: usize,
                    $version: u8,
                    $code: u16,
                    $reason: &str,
                    $headers: Vec<(String, Vec<u8>)>,
                    $headers_eof: bool,
                ) {
                    $body
                }
            }
        };
    }

    macro_rules! res_err {
        ($name:ident, $buf:expr, $err:expr) => {
            #[test]
            fn $name() {
                assert_eq!(Response::default().parse($buf.as_ref()), $err);
            }
        };
    }

    macro_rules! res_par {
        ($name:ident, $buf:expr) => {
            #[test]
            fn $name() {
                assert_eq!(
                    Response::default().parse($buf.as_ref()),
                    Ok(Status::Partial)
                );
            }
        };
    }

    res_err! {
        test_response_newline_after_version,
        b"HTTP/1.1\r\n\r\n 200 OK\r\n\r\n",
        Err(Error::Version)
    }

    res_err! {
        test_response_bare_newline_after_version,
        b"HTTP/1.1\n 200 OK\r\n\r\n",
        Err(Error::Version)
    }

    res! {
        test_response_simple,
        b"HTTP/1.1 200 OK\r\n\r\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 19);
            assert_eq!(version, 1);
            assert_eq!(code, 200);
            assert_eq!(reason, "OK");
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    res! {
        test_response_newlines,
        b"HTTP/1.0 403 Forbidden\nServer: foo.bar\n\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 40);
            assert_eq!(version, 0);
            assert_eq!(code, 403);
            assert_eq!(reason, "Forbidden");
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "Server");
            assert_eq!(headers[0].1, b"foo.bar");
            assert!(eof);
        }
    }

    res! {
        test_response_reason_missing,
        b"HTTP/1.1 200 \r\n\r\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 17);
            assert_eq!(version, 1);
            assert_eq!(code, 200);
            assert_eq!(reason, "");
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    res! {
        test_response_reason_missing_no_space,
        b"HTTP/1.1 200\r\n\r\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 16);
            assert_eq!(version, 1);
            assert_eq!(code, 200);
            assert_eq!(reason, "");
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    res! {
        test_response_reason_missing_no_space_with_headers,
        b"HTTP/1.1 200\r\nFoo: bar\r\n\r\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 26);
            assert_eq!(version, 1);
            assert_eq!(code, 200);
            assert_eq!(reason, "");
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "Foo");
            assert_eq!(headers[0].1, b"bar");
            assert!(eof);
        }
    }

    res! {
        test_response_reason_with_space_and_tab,
        b"HTTP/1.1 101 Switching Protocols\t\r\n\r\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 37);
            assert_eq!(version, 1);
            assert_eq!(code, 101);
            assert_eq!(reason, "Switching Protocols\t");
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    res! {
        test_response_reason_with_obsolete_text_byte,
        b"HTTP/1.1 200 X\xFFZ\r\n\r\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 20);
            assert_eq!(version, 1);
            assert_eq!(code, 200);
            // Empty string fallback in case of obs-text
            assert_eq!(reason, "");
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    res_err! {
        test_response_reason_with_nul_byte,
        b"HTTP/1.1 200 \x00\r\n\r\n",
        Err(crate::Error::Status)
    }

    #[test]
    fn test_response_reason_bytes_at_every_position() {
        const PREFIX: &[u8] = b"HTTP/1.1 200 ";
        for len in 1..80 {
            for pos in 0..len {
                for b in [b'\t', b' ', b'~', 0x00, 0x0b, 0x7f, 0x80, 0xff] {
                    let mut reason = vec![b'a'; len];
                    reason[pos] = b;
                    let buf = [PREFIX, &reason, b"\r\n"].concat();
                    let valid = b == b'\t' || b == b' ' || b == b'~' || b >= 0x80;

                    // parse at once and resumed at every split point
                    for split in PREFIX.len()..=buf.len() {
                        let mut resp = Response::default();
                        let mut st = State::default();
                        let first = resp.parse_with_state(&buf[..split], &mut st);
                        let res = if split == buf.len() {
                            first
                        } else if first.is_err() {
                            assert!(!valid, "len {len} pos {pos} byte {b:#x} split {split}");
                            continue;
                        } else {
                            assert_eq!(first, Ok(Status::Partial));
                            resp.parse_with_state(&buf, &mut st)
                        };
                        let ctx = format!("len {len} pos {pos} byte {b:#x} split {split}");
                        if !valid {
                            assert_eq!(res, Err(crate::Error::Status), "{ctx}");
                        } else if b >= 0x80 {
                            assert_eq!(res, Ok(Status::Complete(buf.len())), "{ctx}");
                            assert_eq!(resp.reason, SlicePos::default(), "{ctx}");
                        } else {
                            assert_eq!(res, Ok(Status::Complete(buf.len())), "{ctx}");
                            // leading spaces are skipped, trailing whitespace is kept
                            let start = usize::from(pos == 0 && b == b' ');
                            assert_eq!(
                                &buf[resp.reason.start..resp.reason.end],
                                &reason[start..],
                                "{ctx}"
                            );
                        }
                    }
                }
            }
        }
    }

    res_par! {
        test_response_version_missing_space,
        b"HTTP/1.1"
    }

    res_par! {
         test_response_code_missing_space,
         b"HTTP/1.1 200"
    }

    res! {
        test_response_partial_parses_headers_as_much_as_it_can,
        b"HTTP/1.1 200 OK\r\nServer: yolo\r\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 31);
            assert_eq!(version, 1);
            assert_eq!(code, 200);
            assert_eq!(reason, "OK");
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "Server");
            assert_eq!(headers[0].1, b"yolo");
            assert!(!eof);
        }
    }

    res! {
        test_response_empty_lines_prefix_lf_only,
        b"\n\nHTTP/1.1 200 OK\n\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 19);
            assert_eq!(version, 1);
            assert_eq!(code, 200);
            assert_eq!(reason, "OK");
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    res! {
        test_response_no_cr,
        b"HTTP/1.0 200\nContent-type: text/html\n\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 38);
            assert_eq!(version, 0);
            assert_eq!(code, 200);
            assert_eq!(reason, "");
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "Content-type");
            assert_eq!(headers[0].1, b"text/html");
            assert!(eof);
        }
    }

    /// Check all subset permutations of a partial request line with no headers
    #[test]
    fn partial_permutations() {
        let req_str = "GET / HTTP/1.1\r\n";
        let mut req = Request::default();
        for i in 0..req_str.len() {
            let status = req.parse(&req_str.as_bytes()[..i]);
            assert_eq!(
                status,
                Ok(Status::Partial),
                "partial request line should return partial. \
                  Portion which failed: '{seg}' (below {i})",
                seg = &req_str[..i]
            );
        }
    }

    headers_err! {
        test_forbid_headers_with_whitespace_between_header_name_and_colon,
        b"Access-Control-Allow-Credentials : true\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_forbid_headers_with_obsolete_line_folding_at_end,
        b"Line-Folded-Header: hello there\r\n   \r\n \r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_forbid_headers_with_obsolete_line_folding_in_middle,
        b"Line-Folded-Header: hello  \r\n \r\n there\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_forbid_headers_with_obsolete_line_folding_in_empty_header,
        b"Line-Folded-Header:   \r\n \r\n \r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_forbid_headers_with_empty_header_name,
        b": hello\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_forbid_headers_with_empty_header_name_second,
        b"Bread: baguette\r\n: hello\r\n\r\n",
        Err(Error::HeaderName)
    }

    #[test]
    fn test_chunk_size() {
        assert_eq!(parse_chunk_size(b"0\r\n"), Ok(Status::Complete((3, 0))));
        assert_eq!(
            parse_chunk_size(b"12\r\nchunk"),
            Ok(Status::Complete((4, 18)))
        );
        assert_eq!(
            parse_chunk_size(b"3086d\r\n"),
            Ok(Status::Complete((7, 198_765)))
        );
        assert_eq!(
            parse_chunk_size(b"3735AB1;foo bar*\r\n"),
            Ok(Status::Complete((18, 57_891_505)))
        );
        assert_eq!(
            parse_chunk_size(b"3735ab1 ; baz \r\n"),
            Ok(Status::Complete((16, 57_891_505)))
        );
        assert_eq!(parse_chunk_size(b"77a65\r"), Ok(Status::Partial));
        assert_eq!(parse_chunk_size(b"ab"), Ok(Status::Partial));
        assert_eq!(
            parse_chunk_size(b"567f8a\rfoo"),
            Err(crate::InvalidChunkSize)
        );
        assert_eq!(
            parse_chunk_size(b"567f8a\rfoo"),
            Err(crate::InvalidChunkSize)
        );
        assert_eq!(
            parse_chunk_size(b"567xf8a\r\n"),
            Err(crate::InvalidChunkSize)
        );
        assert_eq!(
            parse_chunk_size(b"ffffffffffffffff\r\n"),
            Ok(Status::Complete((18, u64::MAX)))
        );
        assert_eq!(
            parse_chunk_size(b"1ffffffffffffffff\r\n"),
            Err(crate::InvalidChunkSize)
        );
        assert_eq!(
            parse_chunk_size(b"Affffffffffffffff\r\n"),
            Err(crate::InvalidChunkSize)
        );
        assert_eq!(
            parse_chunk_size(b"fffffffffffffffff\r\n"),
            Err(crate::InvalidChunkSize)
        );
    }

    #[test]
    fn test_chunk_size_empty() {
        for buf in [&b"\r\n"[..], b";a\r\n", b" \r\n", b"\t;a\r\n", b"\r"] {
            assert_eq!(
                parse_chunk_size(buf),
                Err(crate::InvalidChunkSize),
                "{buf:?}"
            );
        }
        assert_eq!(parse_chunk_size(b"0\r\n"), Ok(Status::Complete((3, 0))));
    }

    // Feeds every prefix of `buf` to the resumable parser and checks that the
    // result matches parsing the whole buffer at once.
    fn header_split(buf: &[u8]) -> (Result<HeaderParsed>, Header) {
        let mut one = Header::default();
        let expected = one.parse(buf);
        for split in 0..buf.len() {
            let mut h = Header::default();
            let mut st = State::default();
            let first = h.parse_with_state(&buf[..split], &mut st);
            if first.is_err() {
                assert_eq!(first, expected, "split {split} of {buf:?}");
                continue;
            }
            assert_eq!(first, Ok(Status::Partial), "split {split} of {buf:?}");
            let res = h.parse_with_state(buf, &mut st);
            assert_eq!(res, expected, "split {split} of {buf:?}");
            if expected.is_ok() {
                assert_eq!(h, one, "split {split} of {buf:?}");
            }
        }
        (expected, one)
    }

    #[test]
    fn test_header_split_bare_cr() {
        assert_eq!(header_split(b"X: a\rb\r\n").0, Err(Error::HeaderValue));
        assert_eq!(header_split(b"X:\rb\r\n").0, Err(Error::HeaderValue));
        assert_eq!(header_split(b"\rX: b\r\n").0, Err(Error::NewLine));

        let (res, h) = header_split(b"X: a b\r\n");
        assert_eq!(res, Ok(Status::Complete(HeaderParsed::Header(8))));
        assert_eq!(h.value, SlicePos { start: 3, end: 6 });
        assert_eq!(
            header_split(b"X:\r\n").0,
            Ok(Status::Complete(HeaderParsed::Header(4)))
        );
        assert_eq!(
            header_split(b"\r\n").0,
            Ok(Status::Complete(HeaderParsed::Eof(2)))
        );
    }

    #[test]
    fn test_request_split() {
        for buf in [
            &b"GET /path HTTP/1.1\r\n"[..],
            b"GET  /path   HTTP/1.1\r\n",
            b"PUT /path HTTP/1.0\n",
            b"CUSTOM  /path HTTP/1.1\r\n",
            b"\r\n\nPOST /path HTTP/1.1\r\n",
            b"GET /path HTTP/1.1\r\r\n",
            b"GET /path HTTP/1.1\rX",
            b"\rGET /path HTTP/1.1\r\n",
            b"GE\x00T /path HTTP/1.1\r\n",
            b"GET /pa\x7fth HTTP/1.1\r\n",
            b"GET /path HTTP/2.0\r\n",
        ] {
            check_split(buf, Request::parse_with_state, Request::parse);
        }
    }

    #[test]
    fn test_response_split() {
        for buf in [
            &b"HTTP/1.1 200 OK\r\n"[..],
            b"HTTP/1.0 404  Not Found \n",
            b"\r\nHTTP/1.1 200\r\n",
            b"HTTP/1.1 200\n",
            b"HTTP/1.1   200 \r\n",
            b"HTTP/1.1 200 caf\xc3\xa9\r\n",
            b"HTTP/1.1 200 OK\r\r\n",
            b"HTTP/1.1 200\rX",
            b"HTTP/1.1 2x0 OK\r\n",
            b"HTTP/1.1\r\n200 OK\r\n",
            b"HTTP/1.1 200 O\x00K\r\n",
        ] {
            check_split(buf, Response::parse_with_state, Response::parse);
        }
    }

    // Feeds `buf` split at every position, and byte by byte, to the resumable
    // parser and checks that the result matches parsing it at once.
    fn check_split<T: Default + PartialEq + fmt::Debug>(
        buf: &[u8],
        parse: fn(&mut T, &[u8], &mut State) -> Result<usize>,
        one_shot: fn(&mut T, &[u8]) -> Result<usize>,
    ) {
        let mut one = T::default();
        let expected = one_shot(&mut one, buf);
        let check = |res: Result<usize>, v: &T, what: &str| {
            assert_eq!(res, expected, "{what} of {buf:?}");
            if expected.is_ok() {
                assert_eq!(v, &one, "{what} of {buf:?}");
            }
        };

        for split in 0..buf.len() {
            let mut v = T::default();
            let mut st = State::default();
            let first = parse(&mut v, &buf[..split], &mut st);
            if first.is_err() {
                check(first, &v, &format!("split {split}"));
                continue;
            }
            assert_eq!(first, Ok(Status::Partial), "split {split} of {buf:?}");
            let res = parse(&mut v, buf, &mut st);
            check(res, &v, &format!("split {split}"));
        }

        let mut v = T::default();
        let mut st = State::default();
        for len in 0..=buf.len() {
            let res = parse(&mut v, &buf[..len], &mut st);
            if res != Ok(Status::Partial) || len == buf.len() {
                check(res, &v, &format!("byte by byte, len {len}"));
                break;
            }
            // accepted input is not scanned again, except the version and the
            // status code, at most 9 bytes
            assert!(st.cursor + 9 >= len, "len {len} of {buf:?}");
        }
    }

    #[test]
    fn test_request_resumes_long_method() {
        let buf = [b'A'; 4096];
        let mut req = Request::default();
        let mut st = State::default();
        assert_eq!(req.parse_with_state(&buf, &mut st), Ok(Status::Partial));
        assert_eq!((st.state, st.start, st.cursor), (1, 0, 4096));
    }

    #[test]
    fn test_header_name_chars_at_every_position() {
        // covers the scalar prefix, the SIMD blocks and the tail
        for len in [1, 15, 16, 17, 31, 32, 33, 47, 48, 70] {
            for pos in 1..len {
                for b in 0..=255_u8 {
                    let mut buf = vec![b'x'; len];
                    buf[pos] = b;
                    buf.extend_from_slice(b": v\r\n");
                    let res = Header::default().parse(&buf);
                    if utils::TOKEN_MAP[b as usize] {
                        assert_eq!(
                            res,
                            Ok(Status::Complete(HeaderParsed::Header(len + 5))),
                            "len {len} pos {pos} byte {b}"
                        );
                    } else if b == b':' {
                        assert!(res.is_ok(), "len {len} pos {pos}");
                    } else {
                        assert_eq!(res, Err(Error::HeaderName), "len {len} pos {pos} byte {b}");
                    }
                }
            }
        }
    }

    #[test]
    fn test_header_state_reuse() {
        let buf = b"A: 1\r\nB: 2\r\n\r\nbody";
        let mut h = Header::default();
        let mut st = State::default();
        assert_eq!(
            h.parse_with_state(buf, &mut st),
            Ok(Status::Complete(HeaderParsed::Header(6)))
        );
        assert_eq!(
            h.parse_with_state(buf, &mut st),
            Ok(Status::Complete(HeaderParsed::Header(12)))
        );
        assert_eq!((h.name.start, h.value.end), (6, 10));
        assert_eq!(
            h.parse_with_state(buf, &mut st),
            Ok(Status::Complete(HeaderParsed::Eof(14)))
        );
    }

    #[test]
    fn test_invalid_state() {
        for st in [
            State {
                state: 3,
                start: 0,
                cursor: 100,
            },
            State {
                state: 1,
                start: 2,
                cursor: 1,
            },
            State {
                state: 8,
                start: 0,
                cursor: 0,
            },
        ] {
            let mut s = st;
            assert_eq!(
                Header::default().parse_with_state(b"abc", &mut s),
                Err(Error::HeaderName)
            );
            let mut s = st;
            assert_eq!(
                Request::default().parse_with_state(b"abc", &mut s),
                Err(Error::Token)
            );
            let mut s = st;
            assert_eq!(
                Response::default().parse_with_state(b"abc", &mut s),
                Err(Error::Status)
            );
        }
    }

    #[test]
    fn test_chunk_size_extension_control_chars() {
        for buf in [
            &b"4;a\nX\r\n"[..],
            b"4;a=\"\nX\"\r\n",
            b"4;a\x00\r\n",
            b"4;a\x7f\r\n",
            b"4;\n",
        ] {
            assert_eq!(
                parse_chunk_size(buf),
                Err(crate::InvalidChunkSize),
                "{buf:?}"
            );
        }
        assert_eq!(
            parse_chunk_size(b"4 ;a=\"b\tc \x80\";d\t\r\n"),
            Ok(Status::Complete((17, 4)))
        );
    }

    res! {
        test_allow_response_with_multiple_space_delimiters,
        b"HTTP/1.1   200  OK\r\n\r\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 22);
            assert_eq!(version, 1);
            assert_eq!(code, 200);
            assert_eq!(reason, "OK");
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    // /// This is technically allowed by the spec, but we only support multiple spaces as an option,
    // /// not stray `\r`s.
    res_err! {
        test_forbid_response_with_weird_whitespace_delimiters,
        b"HTTP/1.1 200\rOK\r\n\r\n",
        Err(Error::Status)
    }

    req! {
        test_allow_request_with_multiple_space_delimiters,
        b"GET  /    HTTP/1.1\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 22);
            assert_eq!(method, "GET");
            assert_eq!(path, "/");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 0);
            assert!(eof);
        }
    }

    // /// This is technically allowed by the spec, but we only support multiple spaces as an option,
    // /// not stray `\r`s.
    req_err! {
        test_forbid_request_with_weird_whitespace_delimiters,
        b"GET\r/\rHTTP/1.1\r\n\r\n",
        Err(Error::Token)
    }

    req_err! {
        test_request_with_multiple_spaces_and_bad_path,
        b"GET   /foo ohno HTTP/1.1\r\n\r\n",
        Err(Error::Version)
    }

    // // This test ensure there is an error when there is a DEL character in the path
    // // since we allow all char from 0x21 code except DEL, this test ensure that DEL
    // // is not allowed in the path
    req_err! {
        test_request_with_del_in_path,
        b"GET   /foo\x7Fohno HTTP/1.1\r\n\r\n",
        Err(Error::Token)
    }

    // #[test]
    // #[cfg_attr(miri, ignore)] // Miri is too slow for this test
    // fn test_all_utf8_char_in_paths() {
    //     // two code points
    //     for i in 128..256 {
    //         for j in 128..256 {
    //             let mut headers = [EMPTY_HEADER; NUM_OF_HEADERS];
    //             let mut request = Request::new(&mut headers[..]);
    //             let bytes = [i as u8, j as u8];

    //             match core::str::from_utf8(&bytes) {
    //                 Ok(s) => {
    //                     let first_line = format!("GET /{} HTTP/1.1\r\n\r\n", s);
    //                     let result = crate::ParserConfig::default()
    //                         .allow_multiple_spaces_in_request_line_delimiters(true)
    //                         .parse_request(&mut request, first_line.as_bytes());

    //                     assert_eq!(
    //                         result,
    //                         Ok(Status::Complete(20)),
    //                         "failed for utf8 char i: {}, j: {}",
    //                         i,
    //                         j
    //                     );
    //                 }
    //                 Err(_) => {
    //                     let mut first_line = b"GET /".to_vec();
    //                     first_line.extend(&bytes);
    //                     first_line.extend(b" HTTP/1.1\r\n\r\n");

    //                     let result = crate::ParserConfig::default()
    //                         .allow_multiple_spaces_in_request_line_delimiters(true)
    //                         .parse_request(&mut request, first_line.as_slice());

    //                     assert_eq!(
    //                         result,
    //                         Err(crate::Error::Token),
    //                         "failed for utf8 char i: {}, j: {}",
    //                         i,
    //                         j
    //                     );
    //                 }
    //             };

    //             // three code points starting from 0xe0
    //             if i < 0xe0 {
    //                 continue;
    //             }

    //             for k in 128..256 {
    //                 let mut headers = [EMPTY_HEADER; NUM_OF_HEADERS];
    //                 let mut request = Request::new(&mut headers[..]);
    //                 let bytes = [i as u8, j as u8, k as u8];

    //                 match core::str::from_utf8(&bytes) {
    //                     Ok(s) => {
    //                         let first_line = format!("GET /{} HTTP/1.1\r\n\r\n", s);
    //                         let result = crate::ParserConfig::default()
    //                             .allow_multiple_spaces_in_request_line_delimiters(true)
    //                             .parse_request(&mut request, first_line.as_bytes());

    //                         assert_eq!(
    //                             result,
    //                             Ok(Status::Complete(21)),
    //                             "failed for utf8 char i: {}, j: {}, k: {}",
    //                             i,
    //                             j,
    //                             k
    //                         );
    //                     }
    //                     Err(_) => {
    //                         let mut first_line = b"GET /".to_vec();
    //                         first_line.extend(&bytes);
    //                         first_line.extend(b" HTTP/1.1\r\n\r\n");

    //                         let result = crate::ParserConfig::default()
    //                             .allow_multiple_spaces_in_request_line_delimiters(true)
    //                             .parse_request(&mut request, first_line.as_slice());

    //                         assert_eq!(
    //                             result,
    //                             Err(crate::Error::Token),
    //                             "failed for utf8 char i: {}, j: {}, k: {}",
    //                             i,
    //                             j,
    //                             k
    //                         );
    //                     }
    //                 };

    //                 // four code points starting from 0xf0
    //                 if i < 0xf0 {
    //                     continue;
    //                 }

    //                 for l in 128..256 {
    //                     let mut headers = [EMPTY_HEADER; NUM_OF_HEADERS];
    //                     let mut request = Request::new(&mut headers[..]);
    //                     let bytes = [i as u8, j as u8, k as u8, l as u8];

    //                     match core::str::from_utf8(&bytes) {
    //                         Ok(s) => {
    //                             let first_line = format!("GET /{} HTTP/1.1\r\n\r\n", s);
    //                             let result = crate::ParserConfig::default()
    //                                 .allow_multiple_spaces_in_request_line_delimiters(true)
    //                                 .parse_request(&mut request, first_line.as_bytes());

    //                             assert_eq!(
    //                                 result,
    //                                 Ok(Status::Complete(22)),
    //                                 "failed for utf8 char i: {}, j: {}, k: {}, l: {}",
    //                                 i,
    //                                 j,
    //                                 k,
    //                                 l
    //                             );
    //                         }
    //                         Err(_) => {
    //                             let mut first_line = b"GET /".to_vec();
    //                             first_line.extend(&bytes);
    //                             first_line.extend(b" HTTP/1.1\r\n\r\n");

    //                             let result = crate::ParserConfig::default()
    //                                 .allow_multiple_spaces_in_request_line_delimiters(true)
    //                                 .parse_request(&mut request, first_line.as_slice());

    //                             assert_eq!(
    //                                 result,
    //                                 Err(crate::Error::Token),
    //                                 "failed for utf8 char i: {}, j: {}, k: {}, l: {}",
    //                                 i,
    //                                 j,
    //                                 k,
    //                                 l
    //                             );
    //                         }
    //                     };
    //                 }
    //             }
    //         }
    //     }
    // }

    res_err! {
        test_response_with_spaces_in_code,
        b"HTTP/1.1 99 200 OK\r\n\r\n",
        Err(Error::Status)
    }

    headers_err! {
        test_headers_with_whitespace_between_header_name_and_colon,
        b"Access-Control-Allow-Credentials  : true\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_headers_with_invalid_char_between_header_name_and_colon,
        b"Access-Control-Allow-Credentials\xFF: true\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_ignore_header_line_with_missing_colon_in_response,
        b"Access-Control-Allow-Credentials\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_headers_header_with_missing_colon_with_folding,
        b"Access-Control-Allow-Credentials   \r\n hello\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_headers_header_with_nul_in_header_name,
        b"Access-Control-Allow-Cred\0entials: hello\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_header_with_cr_in_header_name,
        b"Access-Control-Allow-Cred\rentials: hello\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_header_with_nul_in_whitespace_before_colon,
        b"Access-Control-Allow-Credentials   \0: hello\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderName)
    }

    headers_err! {
        test_header_with_nul_in_value,
        b"Access-Control-Allow-Credentials: hell\0o\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderValue)
    }

    headers_err! {
        test_header_with_invalid_char_in_value,
        b"Access-Control-Allow-Credentials: hell\x01o\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderValue)
    }

    headers_err! {
        test_header_with_invalid_char_in_value_with_folding,
        b"Access-Control-Allow-Credentials: hell\x01o  \n world!\r\nBread: baguette\r\n\r\n",
        Err(Error::HeaderValue)
    }

    headers_err! {
        test_header_with_space_before_first_header,
        b" Space-Before-Header: hello there\r\n\r\n",
        Err(Error::HeaderName)
    }

    res! {
        test_response_no_space_after_colon,
        b"HTTP/1.1 200 OK\r\nfoo:bar\r\n\r\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 28);
            assert_eq!(version, 1);
            assert_eq!(code, 200);
            assert_eq!(reason, "OK");
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "foo");
            assert_eq!(headers[0].1, b"bar");
            assert!(eof);
        }
    }

    req_err! {
        test_request_with_leading_space,
        b" GET / HTTP/1.1\r\nfoo:bar\r\n\r\n",
        Err(Error::Token)
    }

    req_err! {
        test_request_with_invalid_method,
        b"P()ST / HTTP/1.1\r\nfoo:bar\r\n\r\n",
        Err(Error::Token)
    }

    req! {
        test_utf8_in_path_ok,
        b"GET /test?post=I\xE2\x80\x99msorryIforkedyou HTTP/1.1\r\nHost: example.org\r\n\r\n",
        |len, method, path, version, headers, eof| {
            assert_eq!(len, 67);
            assert_eq!(method, "GET");
            assert_eq!(path, "/test?post=I’msorryIforkedyou");
            assert_eq!(version, 1);
            assert_eq!(headers.len(), 1);
            assert_eq!(headers[0].0, "Host");
            assert_eq!(headers[0].1, b"example.org");
            assert!(eof);
        }
    }

    #[test]
    fn test_bad_utf8_in_path() {
        const BUF: &[u8] =
            b"GET /test?post=I\xE2msorryIforkedyou HTTP/1.1\r\nHost: example.org\r\n\r\n";

        let mut req = Request::default();
        assert!(req.parse(BUF).unwrap().is_complete());
        assert!(str::from_utf8(&BUF[req.path.start..req.path.end]).is_err());
    }

    #[rustfmt::skip]
    res! {
        test_response_bench,
        b"\
HTTP/1.0 200 OK\r\n\
Date: Wed, 21 Oct 2015 07:28:00 GMT\r\n\
Set-Cookie: session=60; user_id=1\r\n\r\n",
        |len, version, code, reason, headers, eof| {
            assert_eq!(len, 91);
            assert_eq!(version, 0);
            assert_eq!(code, 200);
            assert_eq!(reason, "OK");
            assert_eq!(headers.len(), 2);
            assert_eq!(headers[0].0, "Date");
            assert_eq!(headers[0].1, b"Wed, 21 Oct 2015 07:28:00 GMT");
            assert_eq!(headers[1].0, "Set-Cookie");
            assert_eq!(headers[1].1, b"session=60; user_id=1");
            assert!(eof);
        }
    }
}