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
use HttpTryFrom;
use byte_str::ByteStr;
use bytes::{Bytes, BytesMut};
use std::{fmt, mem};
use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
use std::str::FromStr;
use std::error::Error;
/// Represents an HTTP header field name
///
/// Header field names identify the header. Header sets may include multiple
/// headers with the same name. The HTTP specification defines a number of
/// standard headers, but HTTP messages may include non-standard header names as
/// well as long as they adhere to the specification.
///
/// `HeaderName` is used as the [`HeaderMap`] key. Constants are available for
/// all standard header names in the [`header`] module.
///
/// # Representation
///
/// `HeaderName` represents standard header names using an `enum`, as such they
/// will not require an allocation for storage. All custom header names are
/// lower cased upon conversion to a `HeaderName` value. This avoids the
/// overhead of dynamically doing lower case conversion during the hash code
/// computation and the comparison operation.
///
/// [`HeaderMap`]: struct.HeaderMap.html
/// [`header`]: index.html
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct HeaderName {
inner: Repr<Custom>,
}
// Almost a full `HeaderName`
#[derive(Debug, Hash)]
pub struct HdrName<'a> {
inner: Repr<MaybeLower<'a>>,
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
enum Repr<T> {
Standard(StandardHeader),
Custom(T),
}
// Used to hijack the Hash impl
#[derive(Debug, Clone, Eq, PartialEq)]
struct Custom(ByteStr);
#[derive(Debug, Clone)]
struct MaybeLower<'a> {
buf: &'a [u8],
lower: bool,
}
/// A possible error when converting a `HeaderName` from another type.
pub struct InvalidHeaderName {
_priv: (),
}
/// A possible error when converting a `HeaderName` from another type.
#[derive(Debug)]
pub struct InvalidHeaderNameBytes(InvalidHeaderName) ;
macro_rules! standard_headers {
(
$(
$(#[$docs:meta])*
($konst:ident, $upcase:ident, $name:expr);
)+
) => {
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
enum StandardHeader {
$(
$konst,
)+
}
$(
$(#[$docs])*
pub const $upcase: HeaderName = HeaderName {
inner: Repr::Standard(StandardHeader::$konst),
};
)+
impl StandardHeader {
#[inline]
fn as_str(&self) -> &'static str {
match *self {
$(
StandardHeader::$konst => $name,
)+
}
}
}
#[cfg(test)]
const TEST_HEADERS: &'static [(StandardHeader, &'static str)] = &[
$(
(StandardHeader::$konst, $name),
)+
];
#[test]
fn test_parse_standard_headers() {
for &(std, name) in TEST_HEADERS {
// Test lower case
assert_eq!(HeaderName::from_bytes(name.as_bytes()).unwrap(), HeaderName::from(std));
// Test upper case
let upper = name.to_uppercase().to_string();
assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(), HeaderName::from(std));
}
}
#[test]
fn test_standard_headers_into_bytes() {
for &(std, name) in TEST_HEADERS {
let std = HeaderName::from(std);
// Test lower case
let name_bytes = name.as_bytes();
let bytes: Bytes =
HeaderName::from_bytes(name_bytes).unwrap().into();
assert_eq!(bytes, name_bytes);
assert_eq!(HeaderName::from_bytes(name_bytes).unwrap(), std);
// Test upper case
let upper = name.to_uppercase().to_string();
let bytes: Bytes =
HeaderName::from_bytes(upper.as_bytes()).unwrap().into();
assert_eq!(bytes, name.as_bytes());
assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(),
std);
}
}
}
}
// Generate constants for all standard HTTP headers. This includes a static hash
// code for the "fast hash" path. The hash code for static headers *do not* have
// to match the text representation of those headers. This is because header
// strings are always converted to the static values (when they match) before
// being hashed. This means that it is impossible to compare the static hash
// code of CONTENT_LENGTH with "content-length".
standard_headers! {
/// Advertises which content types the client is able to understand.
///
/// The Accept request HTTP header advertises which content types, expressed
/// as MIME types, the client is able to understand. Using content
/// negotiation, the server then selects one of the proposals, uses it and
/// informs the client of its choice with the Content-Type response header.
/// Browsers set adequate values for this header depending of the context
/// where the request is done: when fetching a CSS stylesheet a different
/// value is set for the request than when fetching an image, video or a
/// script.
(Accept, ACCEPT, "accept");
/// Advertises which character set the client is able to understand.
///
/// The Accept-Charset request HTTP header advertises which character set
/// the client is able to understand. Using content negotiation, the server
/// then selects one of the proposals, uses it and informs the client of its
/// choice within the Content-Type response header. Browsers usually don't
/// set this header as the default value for each content type is usually
/// correct and transmitting it would allow easier fingerprinting.
///
/// If the server cannot serve any matching character set, it can
/// theoretically send back a 406 (Not Acceptable) error code. But, for a
/// better user experience, this is rarely done and the more common way is
/// to ignore the Accept-Charset header in this case.
(AcceptCharset, ACCEPT_CHARSET, "accept-charset");
/// Advertises which content encoding the client is able to understand.
///
/// The Accept-Encoding request HTTP header advertises which content
/// encoding, usually a compression algorithm, the client is able to
/// understand. Using content negotiation, the server selects one of the
/// proposals, uses it and informs the client of its choice with the
/// Content-Encoding response header.
///
/// Even if both the client and the server supports the same compression
/// algorithms, the server may choose not to compress the body of a
/// response, if the identity value is also acceptable. Two common cases
/// lead to this:
///
/// * The data to be sent is already compressed and a second compression
/// won't lead to smaller data to be transmitted. This may the case with
/// some image formats;
///
/// * The server is overloaded and cannot afford the computational overhead
/// induced by the compression requirement. Typically, Microsoft recommends
/// not to compress if a server use more than 80 % of its computational
/// power.
///
/// As long as the identity value, meaning no encryption, is not explicitly
/// forbidden, by an identity;q=0 or a *;q=0 without another explicitly set
/// value for identity, the server must never send back a 406 Not Acceptable
/// error.
(AcceptEncoding, ACCEPT_ENCODING, "accept-encoding");
/// Advertises which languages the client is able to understand.
///
/// The Accept-Language request HTTP header advertises which languages the
/// client is able to understand, and which locale variant is preferred.
/// Using content negotiation, the server then selects one of the proposals,
/// uses it and informs the client of its choice with the Content-Language
/// response header. Browsers set adequate values for this header according
/// their user interface language and even if a user can change it, this
/// happens rarely (and is frown upon as it leads to fingerprinting).
///
/// This header is a hint to be used when the server has no way of
/// determining the language via another way, like a specific URL, that is
/// controlled by an explicit user decision. It is recommended that the
/// server never overrides an explicit decision. The content of the
/// Accept-Language is often out of the control of the user (like when
/// traveling and using an Internet Cafe in a different country); the user
/// may also want to visit a page in another language than the locale of
/// their user interface.
///
/// If the server cannot serve any matching language, it can theoretically
/// send back a 406 (Not Acceptable) error code. But, for a better user
/// experience, this is rarely done and more common way is to ignore the
/// Accept-Language header in this case.
(AcceptLanguage, ACCEPT_LANGUAGE, "accept-language");
/// Marker used by the server to advertise partial request support.
///
/// The Accept-Ranges response HTTP header is a marker used by the server to
/// advertise its support of partial requests. The value of this field
/// indicates the unit that can be used to define a range.
///
/// In presence of an Accept-Ranges header, the browser may try to resume an
/// interrupted download, rather than to start it from the start again.
(AcceptRanges, ACCEPT_RANGES, "accept-ranges");
/// Preflight response indicating if the response to the request can be
/// exposed to the page.
///
/// The Access-Control-Allow-Credentials response header indicates whether
/// or not the response to the request can be exposed to the page. It can be
/// exposed when the true value is returned; it can't in other cases.
///
/// Credentials are cookies, authorization headers or TLS client
/// certificates.
///
/// When used as part of a response to a preflight request, this indicates
/// whether or not the actual request can be made using credentials. Note
/// that simple GET requests are not preflighted, and so if a request is
/// made for a resource with credentials, if this header is not returned
/// with the resource, the response is ignored by the browser and not
/// returned to web content.
///
/// The Access-Control-Allow-Credentials header works in conjunction with
/// the XMLHttpRequest.withCredentials property or with the credentials
/// option in the Request() constructor of the Fetch API. Credentials must
/// be set on both sides (the Access-Control-Allow-Credentials header and in
/// the XHR or Fetch request) in order for the CORS request with credentials
/// to succeed.
(AccessControlAllowCredentials, ACCESS_CONTROL_ALLOW_CREDENTIALS, "access-control-allow-credentials");
/// Preflight response indicating permitted HTTP headers.
///
/// The Access-Control-Allow-Headers response header is used in response to
/// a preflight request to indicate which HTTP headers will be available via
/// Access-Control-Expose-Headers when making the actual request.
///
/// The simple headers, Accept, Accept-Language, Content-Language,
/// Content-Type (but only with a MIME type of its parsed value (ignoring
/// parameters) of either application/x-www-form-urlencoded,
/// multipart/form-data, or text/plain), are always available and don't need
/// to be listed by this header.
///
/// This header is required if the request has an
/// Access-Control-Request-Headers header.
(AccessControlAllowHeaders, ACCESS_CONTROL_ALLOW_HEADERS, "access-control-allow-headers");
/// Preflight header response indicating permitted access methods.
///
/// The Access-Control-Allow-Methods response header specifies the method or
/// methods allowed when accessing the resource in response to a preflight
/// request.
(AccessControlAllowMethods, ACCESS_CONTROL_ALLOW_METHODS, "access-control-allow-methods");
/// Indicates whether the response can be shared with resources with the
/// given origin.
(AccessControlAllowOrigin, ACCESS_CONTROL_ALLOW_ORIGIN, "access-control-allow-origin");
/// Indicates which headers can be exposed as part of the response by
/// listing their names.
(AccessControlExposeHeaders, ACCESS_CONTROL_EXPOSE_HEADERS, "access-control-expose-headers");
/// Indicates how long the results of a preflight request can be cached.
(AccessControlMaxAge, ACCESS_CONTROL_MAX_AGE, "access-control-max-age");
/// Informs the server which HTTP headers will be used when an actual
/// request is made.
(AccessControlRequestHeaders, ACCESS_CONTROL_REQUEST_HEADERS, "access-control-request-headers");
/// Informs the server know which HTTP method will be used when the actual
/// request is made.
(AccessControlRequestMethod, ACCESS_CONTROL_REQUEST_METHOD, "access-control-request-method");
/// Indicates the time in seconds the object has been in a proxy cache.
///
/// The Age header is usually close to zero. If it is Age: 0, it was
/// probably just fetched from the origin server; otherwise It is usually
/// calculated as a difference between the proxy's current date and the Date
/// general header included in the HTTP response.
(Age, AGE, "age");
/// Lists the set of methods support by a resource.
///
/// This header must be sent if the server responds with a 405 Method Not
/// Allowed status code to indicate which request methods can be used. An
/// empty Allow header indicates that the resource allows no request
/// methods, which might occur temporarily for a given resource, for
/// example.
(Allow, ALLOW, "allow");
/// Advertises the availability of alternate services to clients.
(AltSvc, ALT_SVC, "alt-svc");
/// Contains the credentials to authenticate a user agent with a server.
///
/// Usually this header is included after the server has responded with a
/// 401 Unauthorized status and the WWW-Authenticate header.
(Authorization, AUTHORIZATION, "authorization");
/// Specifies directives for caching mechanisms in both requests and
/// responses.
///
/// Caching directives are unidirectional, meaning that a given directive in
/// a request is not implying that the same directive is to be given in the
/// response.
(CacheControl, CACHE_CONTROL, "cache-control");
/// Controls whether or not the network connection stays open after the
/// current transaction finishes.
///
/// If the value sent is keep-alive, the connection is persistent and not
/// closed, allowing for subsequent requests to the same server to be done.
///
/// Except for the standard hop-by-hop headers (Keep-Alive,
/// Transfer-Encoding, TE, Connection, Trailer, Upgrade, Proxy-Authorization
/// and Proxy-Authenticate), any hop-by-hop headers used by the message must
/// be listed in the Connection header, so that the first proxy knows he has
/// to consume them and not to forward them further. Standard hop-by-hop
/// headers can be listed too (it is often the case of Keep-Alive, but this
/// is not mandatory.
(Connection, CONNECTION, "connection");
/// Indicates if the content is expected to be displayed inline.
///
/// In a regular HTTP response, the Content-Disposition response header is a
/// header indicating if the content is expected to be displayed inline in
/// the browser, that is, as a Web page or as part of a Web page, or as an
/// attachment, that is downloaded and saved locally.
///
/// In a multipart/form-data body, the HTTP Content-Disposition general
/// header is a header that can be used on the subpart of a multipart body
/// to give information about the field it applies to. The subpart is
/// delimited by the boundary defined in the Content-Type header. Used on
/// the body itself, Content-Disposition has no effect.
///
/// The Content-Disposition header is defined in the larger context of MIME
/// messages for e-mail, but only a subset of the possible parameters apply
/// to HTTP forms and POST requests. Only the value form-data, as well as
/// the optional directive name and filename, can be used in the HTTP
/// context.
(ContentDisposition, CONTENT_DISPOSITION, "content-disposition");
/// Used to compress the media-type.
///
/// When present, its value indicates what additional content encoding has
/// been applied to the entity-body. It lets the client know, how to decode
/// in order to obtain the media-type referenced by the Content-Type header.
///
/// It is recommended to compress data as much as possible and therefore to
/// use this field, but some types of resources, like jpeg images, are
/// already compressed. Sometimes using additional compression doesn't
/// reduce payload size and can even make the payload longer.
(ContentEncoding, CONTENT_ENCODING, "content-encoding");
/// Used to describe the languages intended for the audience.
///
/// This header allows a user to differentiate according to the users' own
/// preferred language. For example, if "Content-Language: de-DE" is set, it
/// says that the document is intended for German language speakers
/// (however, it doesn't indicate the document is written in German. For
/// example, it might be written in English as part of a language course for
/// German speakers).
///
/// If no Content-Language is specified, the default is that the content is
/// intended for all language audiences. Multiple language tags are also
/// possible, as well as applying the Content-Language header to various
/// media types and not only to textual documents.
(ContentLanguage, CONTENT_LANGUAGE, "content-language");
/// Indicates the size fo the entity-body.
///
/// The header value must be a decimal indicating the number of octets sent
/// to the recipient.
(ContentLength, CONTENT_LENGTH, "content-length");
/// Indicates an alternate location for the returned data.
///
/// The principal use case is to indicate the URL of the resource
/// transmitted as the result of content negotiation.
///
/// Location and Content-Location are different: Location indicates the
/// target of a redirection (or the URL of a newly created document), while
/// Content-Location indicates the direct URL to use to access the resource,
/// without the need of further content negotiation. Location is a header
/// associated with the response, while Content-Location is associated with
/// the entity returned.
(ContentLocation, CONTENT_LOCATION, "content-location");
/// Indicates where in a full body message a partial message belongs.
(ContentRange, CONTENT_RANGE, "content-range");
/// Allows controlling resources the user agent is allowed to load for a
/// given page.
///
/// With a few exceptions, policies mostly involve specifying server origins
/// and script endpoints. This helps guard against cross-site scripting
/// attacks (XSS).
(ContentSecurityPolicy, CONTENT_SECURITY_POLICY, "content-security-policy");
/// Allows experimenting with policies by monitoring their effects.
///
/// The HTTP Content-Security-Policy-Report-Only response header allows web
/// developers to experiment with policies by monitoring (but not enforcing)
/// their effects. These violation reports consist of JSON documents sent
/// via an HTTP POST request to the specified URI.
(ContentSecurityPolicyReportOnly, CONTENT_SECURITY_POLICY_REPORT_ONLY, "content-security-policy-report-only");
/// Used to indicate the media type of the resource.
///
/// In responses, a Content-Type header tells the client what the content
/// type of the returned content actually is. Browsers will do MIME sniffing
/// in some cases and will not necessarily follow the value of this header;
/// to prevent this behavior, the header X-Content-Type-Options can be set
/// to nosniff.
///
/// In requests, (such as POST or PUT), the client tells the server what
/// type of data is actually sent.
(ContentType, CONTENT_TYPE, "content-type");
/// Contains stored HTTP cookies previously sent by the server with the
/// Set-Cookie header.
///
/// The Cookie header might be omitted entirely, if the privacy setting of
/// the browser are set to block them, for example.
(Cookie, COOKIE, "cookie");
/// Indicates the client's tracking preference.
///
/// This header lets users indicate whether they would prefer privacy rather
/// than personalized content.
(Dnt, DNT, "dnt");
/// Contains the date and time at which the message was originated.
(Date, DATE, "date");
/// Identifier for a specific version of a resource.
///
/// This header allows caches to be more efficient, and saves bandwidth, as
/// a web server does not need to send a full response if the content has
/// not changed. On the other side, if the content has changed, etags are
/// useful to help prevent simultaneous updates of a resource from
/// overwriting each other ("mid-air collisions").
///
/// If the resource at a given URL changes, a new Etag value must be
/// generated. Etags are therefore similar to fingerprints and might also be
/// used for tracking purposes by some servers. A comparison of them allows
/// to quickly determine whether two representations of a resource are the
/// same, but they might also be set to persist indefinitely by a tracking
/// server.
(Etag, ETAG, "etag");
/// Indicates expectations that need to be fulfilled by the server in order
/// to properly handle the request.
///
/// The only expectation defined in the specification is Expect:
/// 100-continue, to which the server shall respond with:
///
/// * 100 if the information contained in the header is sufficient to cause
/// an immediate success,
///
/// * 417 (Expectation Failed) if it cannot meet the expectation; or any
/// other 4xx status otherwise.
///
/// For example, the server may reject a request if its Content-Length is
/// too large.
///
/// No common browsers send the Expect header, but some other clients such
/// as cURL do so by default.
(Expect, EXPECT, "expect");
/// Contains the date/time after which the response is considered stale.
///
/// Invalid dates, like the value 0, represent a date in the past and mean
/// that the resource is already expired.
///
/// If there is a Cache-Control header with the "max-age" or "s-max-age"
/// directive in the response, the Expires header is ignored.
(Expires, EXPIRES, "expires");
/// Contains information from the client-facing side of proxy servers that
/// is altered or lost when a proxy is involved in the path of the request.
///
/// The alternative and de-facto standard versions of this header are the
/// X-Forwarded-For, X-Forwarded-Host and X-Forwarded-Proto headers.
///
/// This header is used for debugging, statistics, and generating
/// location-dependent content and by design it exposes privacy sensitive
/// information, such as the IP address of the client. Therefore the user's
/// privacy must be kept in mind when deploying this header.
(Forwarded, FORWARDED, "forwarded");
/// Contains an Internet email address for a human user who controls the
/// requesting user agent.
///
/// If you are running a robotic user agent (e.g. a crawler), the From
/// header should be sent, so you can be contacted if problems occur on
/// servers, such as if the robot is sending excessive, unwanted, or invalid
/// requests.
(From, FROM, "from");
/// Specifies the domain name of the server and (optionally) the TCP port
/// number on which the server is listening.
///
/// If no port is given, the default port for the service requested (e.g.,
/// "80" for an HTTP URL) is implied.
///
/// A Host header field must be sent in all HTTP/1.1 request messages. A 400
/// (Bad Request) status code will be sent to any HTTP/1.1 request message
/// that lacks a Host header field or contains more than one.
(Host, HOST, "host");
/// Makes a request conditional based on the E-Tag.
///
/// For GET and HEAD methods, the server will send back the requested
/// resource only if it matches one of the listed ETags. For PUT and other
/// non-safe methods, it will only upload the resource in this case.
///
/// The comparison with the stored ETag uses the strong comparison
/// algorithm, meaning two files are considered identical byte to byte only.
/// This is weakened when the W/ prefix is used in front of the ETag.
///
/// There are two common use cases:
///
/// * For GET and HEAD methods, used in combination with an Range header, it
/// can guarantee that the new ranges requested comes from the same resource
/// than the previous one. If it doesn't match, then a 416 (Range Not
/// Satisfiable) response is returned.
///
/// * For other methods, and in particular for PUT, If-Match can be used to
/// prevent the lost update problem. It can check if the modification of a
/// resource that the user wants to upload will not override another change
/// that has been done since the original resource was fetched. If the
/// request cannot be fulfilled, the 412 (Precondition Failed) response is
/// returned.
(IfMatch, IF_MATCH, "if-match");
/// Makes a request conditional based on the modification date.
///
/// The If-Modified-Since request HTTP header makes the request conditional:
/// the server will send back the requested resource, with a 200 status,
/// only if it has been last modified after the given date. If the request
/// has not been modified since, the response will be a 304 without any
/// body; the Last-Modified header will contain the date of last
/// modification. Unlike If-Unmodified-Since, If-Modified-Since can only be
/// used with a GET or HEAD.
///
/// When used in combination with If-None-Match, it is ignored, unless the
/// server doesn't support If-None-Match.
///
/// The most common use case is to update a cached entity that has no
/// associated ETag.
(IfModifiedSince, IF_MODIFIED_SINCE, "if-modified-since");
/// Makes a request conditional based on the E-Tag.
///
/// The If-None-Match HTTP request header makes the request conditional. For
/// GET and HEAD methods, the server will send back the requested resource,
/// with a 200 status, only if it doesn't have an ETag matching the given
/// ones. For other methods, the request will be processed only if the
/// eventually existing resource's ETag doesn't match any of the values
/// listed.
///
/// When the condition fails for GET and HEAD methods, then the server must
/// return HTTP status code 304 (Not Modified). For methods that apply
/// server-side changes, the status code 412 (Precondition Failed) is used.
/// Note that the server generating a 304 response MUST generate any of the
/// following header fields that would have been sent in a 200 (OK) response
/// to the same request: Cache-Control, Content-Location, Date, ETag,
/// Expires, and Vary.
///
/// The comparison with the stored ETag uses the weak comparison algorithm,
/// meaning two files are considered identical not only if they are
/// identical byte to byte, but if the content is equivalent. For example,
/// two pages that would differ only by the date of generation in the footer
/// would be considered as identical.
///
/// When used in combination with If-Modified-Since, it has precedence (if
/// the server supports it).
///
/// There are two common use cases:
///
/// * For `GET` and `HEAD` methods, to update a cached entity that has an associated ETag.
/// * For other methods, and in particular for `PUT`, `If-None-Match` used with
/// the `*` value can be used to save a file not known to exist,
/// guaranteeing that another upload didn't happen before, losing the data
/// of the previous put; this problems is the variation of the lost update
/// problem.
(IfNoneMatch, IF_NONE_MATCH, "if-none-match");
/// Makes a request conditional based on range.
///
/// The If-Range HTTP request header makes a range request conditional: if
/// the condition is fulfilled, the range request will be issued and the
/// server sends back a 206 Partial Content answer with the appropriate
/// body. If the condition is not fulfilled, the full resource is sent back,
/// with a 200 OK status.
///
/// This header can be used either with a Last-Modified validator, or with
/// an ETag, but not with both.
///
/// The most common use case is to resume a download, to guarantee that the
/// stored resource has not been modified since the last fragment has been
/// received.
(IfRange, IF_RANGE, "if-range");
/// Makes the request conditional based on the last modification date.
///
/// The If-Unmodified-Since request HTTP header makes the request
/// conditional: the server will send back the requested resource, or accept
/// it in the case of a POST or another non-safe method, only if it has not
/// been last modified after the given date. If the request has been
/// modified after the given date, the response will be a 412 (Precondition
/// Failed) error.
///
/// There are two common use cases:
///
/// * In conjunction non-safe methods, like POST, it can be used to
/// implement an optimistic concurrency control, like done by some wikis:
/// editions are rejected if the stored document has been modified since the
/// original has been retrieved.
///
/// * In conjunction with a range request with a If-Range header, it can be
/// used to ensure that the new fragment requested comes from an unmodified
/// document.
(IfUnmodifiedSince, IF_UNMODIFIED_SINCE, "if-unmodified-since");
/// Content-Types that are acceptable for the response.
(LastModified, LAST_MODIFIED, "last-modified");
/// Allows the server to point an interested client to another resource
/// containing metadata about the requested resource.
(Link, LINK, "link");
/// Indicates the URL to redirect a page to.
///
/// The Location response header indicates the URL to redirect a page to. It
/// only provides a meaning when served with a 3xx status response.
///
/// The HTTP method used to make the new request to fetch the page pointed
/// to by Location depends of the original method and of the kind of
/// redirection:
///
/// * If 303 (See Also) responses always lead to the use of a GET method,
/// 307 (Temporary Redirect) and 308 (Permanent Redirect) don't change the
/// method used in the original request;
///
/// * 301 (Permanent Redirect) and 302 (Found) doesn't change the method
/// most of the time, though older user-agents may (so you basically don't
/// know).
///
/// All responses with one of these status codes send a Location header.
///
/// Beside redirect response, messages with 201 (Created) status also
/// include the Location header. It indicates the URL to the newly created
/// resource.
///
/// Location and Content-Location are different: Location indicates the
/// target of a redirection (or the URL of a newly created resource), while
/// Content-Location indicates the direct URL to use to access the resource
/// when content negotiation happened, without the need of further content
/// negotiation. Location is a header associated with the response, while
/// Content-Location is associated with the entity returned.
(Location, LOCATION, "location");
/// Indicates the max number of intermediaries the request should be sent
/// through.
(MaxForwards, MAX_FORWARDS, "max-forwards");
/// Indicates where a fetch originates from.
///
/// It doesn't include any path information, but only the server name. It is
/// sent with CORS requests, as well as with POST requests. It is similar to
/// the Referer header, but, unlike this header, it doesn't disclose the
/// whole path.
(Origin, ORIGIN, "origin");
/// HTTP/1.0 header usually used for backwards compatibility.
///
/// The Pragma HTTP/1.0 general header is an implementation-specific header
/// that may have various effects along the request-response chain. It is
/// used for backwards compatibility with HTTP/1.0 caches where the
/// Cache-Control HTTP/1.1 header is not yet present.
(Pragma, PRAGMA, "pragma");
/// Defines the authentication method that should be used to gain access to
/// a proxy.
///
/// Unlike `www-authenticate`, the `proxy-authenticate` header field applies
/// only to the next outbound client on the response chain. This is because
/// only the client that chose a given proxy is likely to have the
/// credentials necessary for authentication. However, when multiple proxies
/// are used within the same administrative domain, such as office and
/// regional caching proxies within a large corporate network, it is common
/// for credentials to be generated by the user agent and passed through the
/// hierarchy until consumed. Hence, in such a configuration, it will appear
/// as if Proxy-Authenticate is being forwarded because each proxy will send
/// the same challenge set.
///
/// The `proxy-authenticate` header is sent along with a `407 Proxy
/// Authentication Required`.
(ProxyAuthenticate, PROXY_AUTHENTICATE, "proxy-authenticate");
/// Contains the credentials to authenticate a user agent to a proxy server.
///
/// This header is usually included after the server has responded with a
/// 407 Proxy Authentication Required status and the Proxy-Authenticate
/// header.
(ProxyAuthorization, PROXY_AUTHORIZATION, "proxy-authorization");
/// Associates a specific cryptographic public key with a certain server.
///
/// This decreases the risk of MITM attacks with forged certificates. If one
/// or several keys are pinned and none of them are used by the server, the
/// browser will not accept the response as legitimate, and will not display
/// it.
(PublicKeyPins, PUBLIC_KEY_PINS, "public-key-pins");
/// Sends reports of pinning violation to the report-uri specified in the
/// header.
///
/// Unlike `Public-Key-Pins`, this header still allows browsers to connect
/// to the server if the pinning is violated.
(PublicKeyPinsReportOnly, PUBLIC_KEY_PINS_REPORT_ONLY, "public-key-pins-report-only");
/// Indicates the part of a document that the server should return.
///
/// Several parts can be requested with one Range header at once, and the
/// server may send back these ranges in a multipart document. If the server
/// sends back ranges, it uses the 206 Partial Content for the response. If
/// the ranges are invalid, the server returns the 416 Range Not Satisfiable
/// error. The server can also ignore the Range header and return the whole
/// document with a 200 status code.
(Range, RANGE, "range");
/// Contains the address of the previous web page from which a link to the
/// currently requested page was followed.
///
/// The Referer header allows servers to identify where people are visiting
/// them from and may use that data for analytics, logging, or optimized
/// caching, for example.
(Referer, REFERER, "referer");
/// Governs which referrer information should be included with requests
/// made.
(ReferrerPolicy, REFERRER_POLICY, "referrer-policy");
/// Informs the web browser that the current page or frame should be
/// refreshed.
(Refresh, REFRESH, "refresh");
/// The Retry-After response HTTP header indicates how long the user agent
/// should wait before making a follow-up request. There are two main cases
/// this header is used:
///
/// * When sent with a 503 (Service Unavailable) response, it indicates how
/// long the service is expected to be unavailable.
///
/// * When sent with a redirect response, such as 301 (Moved Permanently),
/// it indicates the minimum time that the user agent is asked to wait
/// before issuing the redirected request.
(RetryAfter, RETRY_AFTER, "retry-after");
/// The |Sec-WebSocket-Accept| header field is used in the WebSocket
/// opening handshake. It is sent from the server to the client to
/// confirm that the server is willing to initiate the WebSocket
/// connection.
(SecWebSocketAccept, SEC_WEBSOCKET_ACCEPT, "sec-websocket-accept");
/// The |Sec-WebSocket-Extensions| header field is used in the WebSocket
/// opening handshake. It is initially sent from the client to the
/// server, and then subsequently sent from the server to the client, to
/// agree on a set of protocol-level extensions to use for the duration
/// of the connection.
(SecWebSocketExtensions, SEC_WEBSOCKET_EXTENSIONS, "sec-websocket-extensions");
/// The |Sec-WebSocket-Key| header field is used in the WebSocket opening
/// handshake. It is sent from the client to the server to provide part
/// of the information used by the server to prove that it received a
/// valid WebSocket opening handshake. This helps ensure that the server
/// does not accept connections from non-WebSocket clients (e.g., HTTP
/// clients) that are being abused to send data to unsuspecting WebSocket
/// servers.
(SecWebSocketKey, SEC_WEBSOCKET_KEY, "sec-websocket-key");
/// The |Sec-WebSocket-Protocol| header field is used in the WebSocket
/// opening handshake. It is sent from the client to the server and back
/// from the server to the client to confirm the subprotocol of the
/// connection. This enables scripts to both select a subprotocol and be
/// sure that the server agreed to serve that subprotocol.
(SecWebSocketProtocol, SEC_WEBSOCKET_PROTOCOL, "sec-websocket-protocol");
/// The |Sec-WebSocket-Version| header field is used in the WebSocket
/// opening handshake. It is sent from the client to the server to
/// indicate the protocol version of the connection. This enables
/// servers to correctly interpret the opening handshake and subsequent
/// data being sent from the data, and close the connection if the server
/// cannot interpret that data in a safe manner.
(SecWebSocketVersion, SEC_WEBSOCKET_VERSION, "sec-websocket-version");
/// Contains information about the software used by the origin server to
/// handle the request.
///
/// Overly long and detailed Server values should be avoided as they
/// potentially reveal internal implementation details that might make it
/// (slightly) easier for attackers to find and exploit known security
/// holes.
(Server, SERVER, "server");
/// Used to send cookies from the server to the user agent.
(SetCookie, SET_COOKIE, "set-cookie");
/// Tells the client to communicate with HTTPS instead of using HTTP.
(StrictTransportSecurity, STRICT_TRANSPORT_SECURITY, "strict-transport-security");
/// Informs the server of transfer encodings willing to be accepted as part
/// of the response.
///
/// See also the Transfer-Encoding response header for more details on
/// transfer encodings. Note that chunked is always acceptable for HTTP/1.1
/// recipients and you that don't have to specify "chunked" using the TE
/// header. However, it is useful for setting if the client is accepting
/// trailer fields in a chunked transfer coding using the "trailers" value.
(Te, TE, "te");
/// Allows the sender to include additional fields at the end of chunked
/// messages.
(Trailer, TRAILER, "trailer");
/// Specifies the form of encoding used to safely transfer the entity to the
/// client.
///
/// `transfer-encoding` is a hop-by-hop header, that is applying to a
/// message between two nodes, not to a resource itself. Each segment of a
/// multi-node connection can use different `transfer-encoding` values. If
/// you want to compress data over the whole connection, use the end-to-end
/// header `content-encoding` header instead.
///
/// When present on a response to a `HEAD` request that has no body, it
/// indicates the value that would have applied to the corresponding `GET`
/// message.
(TransferEncoding, TRANSFER_ENCODING, "transfer-encoding");
/// Contains a string that allows identifying the requesting client's
/// software.
(UserAgent, USER_AGENT, "user-agent");
/// Used as part of the exchange to upgrade the protocol.
(Upgrade, UPGRADE, "upgrade");
/// Sends a signal to the server expressing the client’s preference for an
/// encrypted and authenticated response.
(UpgradeInsecureRequests, UPGRADE_INSECURE_REQUESTS, "upgrade-insecure-requests");
/// Determines how to match future requests with cached responses.
///
/// The `vary` HTTP response header determines how to match future request
/// headers to decide whether a cached response can be used rather than
/// requesting a fresh one from the origin server. It is used by the server
/// to indicate which headers it used when selecting a representation of a
/// resource in a content negotiation algorithm.
///
/// The `vary` header should be set on a 304 Not Modified response exactly
/// like it would have been set on an equivalent 200 OK response.
(Vary, VARY, "vary");
/// Added by proxies to track routing.
///
/// The `via` general header is added by proxies, both forward and reverse
/// proxies, and can appear in the request headers and the response headers.
/// It is used for tracking message forwards, avoiding request loops, and
/// identifying the protocol capabilities of senders along the
/// request/response chain.
(Via, VIA, "via");
/// General HTTP header contains information about possible problems with
/// the status of the message.
///
/// More than one `warning` header may appear in a response. Warning header
/// fields can in general be applied to any message, however some warn-codes
/// are specific to caches and can only be applied to response messages.
(Warning, WARNING, "warning");
/// Defines the authentication method that should be used to gain access to
/// a resource.
(WwwAuthenticate, WWW_AUTHENTICATE, "www-authenticate");
/// Marker used by the server to indicate that the MIME types advertised in
/// the `content-type` headers should not be changed and be followed.
///
/// This allows to opt-out of MIME type sniffing, or, in other words, it is
/// a way to say that the webmasters knew what they were doing.
///
/// This header was introduced by Microsoft in IE 8 as a way for webmasters
/// to block content sniffing that was happening and could transform
/// non-executable MIME types into executable MIME types. Since then, other
/// browsers have introduced it, even if their MIME sniffing algorithms were
/// less aggressive.
///
/// Site security testers usually expect this header to be set.
(XContentTypeOptions, X_CONTENT_TYPE_OPTIONS, "x-content-type-options");
/// Controls DNS prefetching.
///
/// The `x-dns-prefetch-control` HTTP response header controls DNS
/// prefetching, a feature by which browsers proactively perform domain name
/// resolution on both links that the user may choose to follow as well as
/// URLs for items referenced by the document, including images, CSS,
/// JavaScript, and so forth.
///
/// This prefetching is performed in the background, so that the DNS is
/// likely to have been resolved by the time the referenced items are
/// needed. This reduces latency when the user clicks a link.
(XDnsPrefetchControl, X_DNS_PREFETCH_CONTROL, "x-dns-prefetch-control");
/// Indicates whether or not a browser should be allowed to render a page in
/// a frame.
///
/// Sites can use this to avoid clickjacking attacks, by ensuring that their
/// content is not embedded into other sites.
///
/// The added security is only provided if the user accessing the document
/// is using a browser supporting `x-frame-options`.
(XFrameOptions, X_FRAME_OPTIONS, "x-frame-options");
/// Stop pages from loading when an XSS attack is detected.
///
/// The HTTP X-XSS-Protection response header is a feature of Internet
/// Explorer, Chrome and Safari that stops pages from loading when they
/// detect reflected cross-site scripting (XSS) attacks. Although these
/// protections are largely unnecessary in modern browsers when sites
/// implement a strong Content-Security-Policy that disables the use of
/// inline JavaScript ('unsafe-inline'), they can still provide protections
/// for users of older web browsers that don't yet support CSP.
(XXssProtection, X_XSS_PROTECTION, "x-xss-protection");
}
/// Valid header name characters
///
/// ```not_rust
/// field-name = token
/// separators = "(" | ")" | "<" | ">" | "@"
/// | "," | ";" | ":" | "\" | <">
/// | "/" | "[" | "]" | "?" | "="
/// | "{" | "}" | SP | HT
/// token = 1*tchar
/// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
/// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
/// / DIGIT / ALPHA
/// ; any VCHAR, except delimiters
/// ```
const HEADER_CHARS: [u8; 256] = [
// 0 1 2 3 4 5 6 7 8 9
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x
0, 0, 0, b'!', b'"', b'#', b'$', b'%', b'&', b'\'', // 3x
0, 0, b'*', b'+', 0, b'-', b'.', 0, b'0', b'1', // 4x
b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', 0, 0, // 5x
0, 0, 0, 0, 0, b'a', b'b', b'c', b'd', b'e', // 6x
b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', // 7x
b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', // 8x
b'z', 0, 0, 0, b'^', b'_', b'`', b'a', b'b', b'c', // 9x
b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x
b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x
b'x', b'y', b'z', 0, b'|', 0, b'~', 0, 0, 0, // 12x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 13x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 18x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 19x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 21x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 22x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 23x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 24x
0, 0, 0, 0, 0, 0 // 25x
];
const HEADER_CHARS_H2: [u8; 256] = [
// 0 1 2 3 4 5 6 7 8 9
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x
0, 0, 0, b'!', b'"', b'#', b'$', b'%', b'&', b'\'', // 3x
0, 0, b'*', b'+', 0, b'-', b'.', 0, b'0', b'1', // 4x
b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', 0, 0, // 5x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x
0, 0, 0, 0, b'^', b'_', b'`', b'a', b'b', b'c', // 9x
b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x
b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x
b'x', b'y', b'z', 0, b'|', 0, b'~', 0, 0, 0, // 12x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 13x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 18x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 19x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 21x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 22x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 23x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 24x
0, 0, 0, 0, 0, 0 // 25x
];
#[cfg(any(not(debug_assertions), not(target_arch = "wasm32")))]
macro_rules! eq {
(($($cmp:expr,)*) $v:ident[$n:expr] ==) => {
$($cmp) && *
};
(($($cmp:expr,)*) $v:ident[$n:expr] == $a:tt $($rest:tt)*) => {
eq!(($($cmp,)* $v[$n] == $a,) $v[$n+1] == $($rest)*)
};
($v:ident == $($rest:tt)+) => {
eq!(() $v[0] == $($rest)+)
};
($v:ident[$n:expr] == $($rest:tt)+) => {
eq!(() $v[$n] == $($rest)+)
};
}
#[cfg(any(not(debug_assertions), not(target_arch = "wasm32")))]
/// This version is best under optimized mode, however in a wasm debug compile,
/// the `eq` macro expands to 1 + 1 + 1 + 1... and wasm explodes when this chain gets too long
/// See https://github.com/DenisKolodin/yew/issues/478
fn parse_hdr<'a>(data: &'a [u8], b: &'a mut [u8; 64], table: &[u8; 256])
-> Result<HdrName<'a>, InvalidHeaderName>
{
use self::StandardHeader::*;
let len = data.len();
let validate = |buf: &'a [u8], len: usize| {
let buf = &buf[..len];
if buf.iter().any(|&b| b == 0) {
Err(InvalidHeaderName::new())
} else {
Ok(HdrName::custom(buf, true))
}
};
macro_rules! to_lower {
($d:ident, $src:ident, 1) => { $d[0] = table[$src[0] as usize]; };
($d:ident, $src:ident, 2) => { to_lower!($d, $src, 1); $d[1] = table[$src[1] as usize]; };
($d:ident, $src:ident, 3) => { to_lower!($d, $src, 2); $d[2] = table[$src[2] as usize]; };
($d:ident, $src:ident, 4) => { to_lower!($d, $src, 3); $d[3] = table[$src[3] as usize]; };
($d:ident, $src:ident, 5) => { to_lower!($d, $src, 4); $d[4] = table[$src[4] as usize]; };
($d:ident, $src:ident, 6) => { to_lower!($d, $src, 5); $d[5] = table[$src[5] as usize]; };
($d:ident, $src:ident, 7) => { to_lower!($d, $src, 6); $d[6] = table[$src[6] as usize]; };
($d:ident, $src:ident, 8) => { to_lower!($d, $src, 7); $d[7] = table[$src[7] as usize]; };
($d:ident, $src:ident, 9) => { to_lower!($d, $src, 8); $d[8] = table[$src[8] as usize]; };
($d:ident, $src:ident, 10) => { to_lower!($d, $src, 9); $d[9] = table[$src[9] as usize]; };
($d:ident, $src:ident, 11) => { to_lower!($d, $src, 10); $d[10] = table[$src[10] as usize]; };
($d:ident, $src:ident, 12) => { to_lower!($d, $src, 11); $d[11] = table[$src[11] as usize]; };
($d:ident, $src:ident, 13) => { to_lower!($d, $src, 12); $d[12] = table[$src[12] as usize]; };
($d:ident, $src:ident, 14) => { to_lower!($d, $src, 13); $d[13] = table[$src[13] as usize]; };
($d:ident, $src:ident, 15) => { to_lower!($d, $src, 14); $d[14] = table[$src[14] as usize]; };
($d:ident, $src:ident, 16) => { to_lower!($d, $src, 15); $d[15] = table[$src[15] as usize]; };
($d:ident, $src:ident, 17) => { to_lower!($d, $src, 16); $d[16] = table[$src[16] as usize]; };
($d:ident, $src:ident, 18) => { to_lower!($d, $src, 17); $d[17] = table[$src[17] as usize]; };
($d:ident, $src:ident, 19) => { to_lower!($d, $src, 18); $d[18] = table[$src[18] as usize]; };
($d:ident, $src:ident, 20) => { to_lower!($d, $src, 19); $d[19] = table[$src[19] as usize]; };
($d:ident, $src:ident, 21) => { to_lower!($d, $src, 20); $d[20] = table[$src[20] as usize]; };
($d:ident, $src:ident, 22) => { to_lower!($d, $src, 21); $d[21] = table[$src[21] as usize]; };
($d:ident, $src:ident, 23) => { to_lower!($d, $src, 22); $d[22] = table[$src[22] as usize]; };
($d:ident, $src:ident, 24) => { to_lower!($d, $src, 23); $d[23] = table[$src[23] as usize]; };
($d:ident, $src:ident, 25) => { to_lower!($d, $src, 24); $d[24] = table[$src[24] as usize]; };
($d:ident, $src:ident, 26) => { to_lower!($d, $src, 25); $d[25] = table[$src[25] as usize]; };
($d:ident, $src:ident, 27) => { to_lower!($d, $src, 26); $d[26] = table[$src[26] as usize]; };
($d:ident, $src:ident, 28) => { to_lower!($d, $src, 27); $d[27] = table[$src[27] as usize]; };
($d:ident, $src:ident, 29) => { to_lower!($d, $src, 28); $d[28] = table[$src[28] as usize]; };
($d:ident, $src:ident, 30) => { to_lower!($d, $src, 29); $d[29] = table[$src[29] as usize]; };
($d:ident, $src:ident, 31) => { to_lower!($d, $src, 30); $d[30] = table[$src[30] as usize]; };
($d:ident, $src:ident, 32) => { to_lower!($d, $src, 31); $d[31] = table[$src[31] as usize]; };
($d:ident, $src:ident, 33) => { to_lower!($d, $src, 32); $d[32] = table[$src[32] as usize]; };
($d:ident, $src:ident, 34) => { to_lower!($d, $src, 33); $d[33] = table[$src[33] as usize]; };
($d:ident, $src:ident, 35) => { to_lower!($d, $src, 34); $d[34] = table[$src[34] as usize]; };
}
assert!(len < super::MAX_HEADER_NAME_LEN,
"header name too long -- max length is {}",
super::MAX_HEADER_NAME_LEN);
match len {
0 => {
Err(InvalidHeaderName::new())
}
2 => {
to_lower!(b, data, 2);
if eq!(b == b't' b'e') {
Ok(Te.into())
} else {
validate(b, len)
}
}
3 => {
to_lower!(b, data, 3);
if eq!(b == b'a' b'g' b'e') {
Ok(Age.into())
} else if eq!(b == b'v' b'i' b'a') {
Ok(Via.into())
} else if eq!(b == b'd' b'n' b't') {
Ok(Dnt.into())
} else {
validate(b, len)
}
}
4 => {
to_lower!(b, data, 4);
if eq!(b == b'd' b'a' b't' b'e') {
Ok(Date.into())
} else if eq!(b == b'e' b't' b'a' b'g') {
Ok(Etag.into())
} else if eq!(b == b'f' b'r' b'o' b'm') {
Ok(From.into())
} else if eq!(b == b'h' b'o' b's' b't') {
Ok(Host.into())
} else if eq!(b == b'l' b'i' b'n' b'k') {
Ok(Link.into())
} else if eq!(b == b'v' b'a' b'r' b'y') {
Ok(Vary.into())
} else {
validate(b, len)
}
}
5 => {
to_lower!(b, data, 5);
if eq!(b == b'a' b'l' b'l' b'o' b'w') {
Ok(Allow.into())
} else if eq!(b == b'r' b'a' b'n' b'g' b'e') {
Ok(Range.into())
} else {
validate(b, len)
}
}
6 => {
to_lower!(b, data, 6);
if eq!(b == b'a' b'c' b'c' b'e' b'p' b't') {
return Ok(Accept.into())
} else if eq!(b == b'c' b'o' b'o' b'k' b'i' b'e') {
return Ok(Cookie.into())
} else if eq!(b == b'e' b'x' b'p' b'e' b'c' b't') {
return Ok(Expect.into())
} else if eq!(b == b'o' b'r' b'i' b'g' b'i' b'n') {
return Ok(Origin.into())
} else if eq!(b == b'p' b'r' b'a' b'g' b'm' b'a') {
return Ok(Pragma.into())
} else if b[0] == b's' {
if eq!(b[1] == b'e' b'r' b'v' b'e' b'r') {
return Ok(Server.into())
}
}
validate(b, len)
}
7 => {
to_lower!(b, data, 7);
if eq!(b == b'a' b'l' b't' b'-' b's' b'v' b'c') {
Ok(AltSvc.into())
} else if eq!(b == b'e' b'x' b'p' b'i' b'r' b'e' b's') {
Ok(Expires.into())
} else if eq!(b == b'r' b'e' b'f' b'e' b'r' b'e' b'r') {
Ok(Referer.into())
} else if eq!(b == b'r' b'e' b'f' b'r' b'e' b's' b'h') {
Ok(Refresh.into())
} else if eq!(b == b't' b'r' b'a' b'i' b'l' b'e' b'r') {
Ok(Trailer.into())
} else if eq!(b == b'u' b'p' b'g' b'r' b'a' b'd' b'e') {
Ok(Upgrade.into())
} else if eq!(b == b'w' b'a' b'r' b'n' b'i' b'n' b'g') {
Ok(Warning.into())
} else {
validate(b, len)
}
}
8 => {
to_lower!(b, data, 8);
if eq!(b == b'i' b'f' b'-') {
if eq!(b[3] == b'm' b'a' b't' b'c' b'h') {
return Ok(IfMatch.into())
} else if eq!(b[3] == b'r' b'a' b'n' b'g' b'e') {
return Ok(IfRange.into())
}
} else if eq!(b == b'l' b'o' b'c' b'a' b't' b'i' b'o' b'n') {
return Ok(Location.into())
}
validate(b, len)
}
9 => {
to_lower!(b, data, 9);
if eq!(b == b'f' b'o' b'r' b'w' b'a' b'r' b'd' b'e' b'd') {
Ok(Forwarded.into())
} else {
validate(b, len)
}
}
10 => {
to_lower!(b, data, 10);
if eq!(b == b'c' b'o' b'n' b'n' b'e' b'c' b't' b'i' b'o' b'n') {
Ok(Connection.into())
} else if eq!(b == b's' b'e' b't' b'-' b'c' b'o' b'o' b'k' b'i' b'e') {
Ok(SetCookie.into())
} else if eq!(b == b'u' b's' b'e' b'r' b'-' b'a' b'g' b'e' b'n' b't') {
Ok(UserAgent.into())
} else {
validate(b, len)
}
}
11 => {
to_lower!(b, data, 11);
if eq!(b == b'r' b'e' b't' b'r' b'y' b'-' b'a' b'f' b't' b'e' b'r') {
Ok(RetryAfter.into())
} else {
validate(b, len)
}
}
12 => {
to_lower!(b, data, 12);
if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b't' b'y' b'p' b'e') {
Ok(ContentType.into())
} else if eq!(b == b'm' b'a' b'x' b'-' b'f' b'o' b'r' b'w' b'a' b'r' b'd' b's') {
Ok(MaxForwards.into())
} else {
validate(b, len)
}
}
13 => {
to_lower!(b, data, 13);
if b[0] == b'a' {
if eq!(b[1] == b'c' b'c' b'e' b'p' b't' b'-' b'r' b'a' b'n' b'g' b'e' b's') {
return Ok(AcceptRanges.into())
} else if eq!(b[1] == b'u' b't' b'h' b'o' b'r' b'i' b'z' b'a' b't' b'i' b'o' b'n') {
return Ok(Authorization.into())
}
} else if b[0] == b'c' {
if eq!(b[1] == b'a' b'c' b'h' b'e' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l') {
return Ok(CacheControl.into())
} else if eq!(b[1] == b'o' b'n' b't' b'e' b'n' b't' b'-' b'r' b'a' b'n' b'g' b'e' ) {
return Ok(ContentRange.into())
}
} else if eq!(b == b'i' b'f' b'-' b'n' b'o' b'n' b'e' b'-' b'm' b'a' b't' b'c' b'h') {
return Ok(IfNoneMatch.into())
} else if eq!(b == b'l' b'a' b's' b't' b'-' b'm' b'o' b'd' b'i' b'f' b'i' b'e' b'd') {
return Ok(LastModified.into())
}
validate(b, len)
}
14 => {
to_lower!(b, data, 14);
if eq!(b == b'a' b'c' b'c' b'e' b'p' b't' b'-' b'c' b'h' b'a' b'r' b's' b'e' b't') {
Ok(AcceptCharset.into())
} else if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b'l' b'e' b'n' b'g' b't' b'h') {
Ok(ContentLength.into())
} else {
validate(b, len)
}
}
15 => {
to_lower!(b, data, 15);
if eq!(b == b'a' b'c' b'c' b'e' b'p' b't' b'-') { // accept-
if eq!(b[7] == b'e' b'n' b'c' b'o' b'd' b'i' b'n' b'g') {
return Ok(AcceptEncoding.into())
} else if eq!(b[7] == b'l' b'a' b'n' b'g' b'u' b'a' b'g' b'e') {
return Ok(AcceptLanguage.into())
}
} else if eq!(b == b'p' b'u' b'b' b'l' b'i' b'c' b'-' b'k' b'e' b'y' b'-' b'p' b'i' b'n' b's') {
return Ok(PublicKeyPins.into())
} else if eq!(b == b'x' b'-' b'f' b'r' b'a' b'm' b'e' b'-' b'o' b'p' b't' b'i' b'o' b'n' b's') {
return Ok(XFrameOptions.into())
}
else if eq!(b == b'r' b'e' b'f' b'e' b'r' b'r' b'e' b'r' b'-' b'p' b'o' b'l' b'i' b'c' b'y') {
return Ok(ReferrerPolicy.into())
}
validate(b, len)
}
16 => {
to_lower!(b, data, 16);
if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-') {
if eq!(b[8] == b'l' b'a' b'n' b'g' b'u' b'a' b'g' b'e') {
return Ok(ContentLanguage.into())
} else if eq!(b[8] == b'l' b'o' b'c' b'a' b't' b'i' b'o' b'n') {
return Ok(ContentLocation.into())
} else if eq!(b[8] == b'e' b'n' b'c' b'o' b'd' b'i' b'n' b'g') {
return Ok(ContentEncoding.into())
}
} else if eq!(b == b'w' b'w' b'w' b'-' b'a' b'u' b't' b'h' b'e' b'n' b't' b'i' b'c' b'a' b't' b'e') {
return Ok(WwwAuthenticate.into())
} else if eq!(b == b'x' b'-' b'x' b's' b's' b'-' b'p' b'r' b'o' b't' b'e' b'c' b't' b'i' b'o' b'n') {
return Ok(XXssProtection.into())
}
validate(b, len)
}
17 => {
to_lower!(b, data, 17);
if eq!(b == b't' b'r' b'a' b'n' b's' b'f' b'e' b'r' b'-' b'e' b'n' b'c' b'o' b'd' b'i' b'n' b'g') {
Ok(TransferEncoding.into())
} else if eq!(b == b'i' b'f' b'-' b'm' b'o' b'd' b'i' b'f' b'i' b'e' b'd' b'-' b's' b'i' b'n' b'c' b'e') {
Ok(IfModifiedSince.into())
} else if eq!(b == b's' b'e' b'c' b'-' b'w' b'e' b'b' b's' b'o' b'c' b'k' b'e' b't' b'-' b'k' b'e' b'y') {
Ok(SecWebSocketKey.into())
} else {
validate(b, len)
}
}
18 => {
to_lower!(b, data, 18);
if eq!(b == b'p' b'r' b'o' b'x' b'y' b'-' b'a' b'u' b't' b'h' b'e' b'n' b't' b'i' b'c' b'a' b't' b'e') {
Ok(ProxyAuthenticate.into())
} else {
validate(b, len)
}
}
19 => {
to_lower!(b, data, 19);
if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b'd' b'i' b's' b'p' b'o' b's' b'i' b't' b'i' b'o' b'n') {
Ok(ContentDisposition.into())
} else if eq!(b == b'i' b'f' b'-' b'u' b'n' b'm' b'o' b'd' b'i' b'f' b'i' b'e' b'd' b'-' b's' b'i' b'n' b'c' b'e') {
Ok(IfUnmodifiedSince.into())
} else if eq!(b == b'p' b'r' b'o' b'x' b'y' b'-' b'a' b'u' b't' b'h' b'o' b'r' b'i' b'z' b'a' b't' b'i' b'o' b'n') {
Ok(ProxyAuthorization.into())
} else {
validate(b, len)
}
}
20 => {
to_lower!(b, data, 20);
if eq!(b == b's' b'e' b'c' b'-' b'w' b'e' b'b' b's' b'o' b'c' b'k' b'e' b't' b'-' b'a' b'c' b'c' b'e' b'p' b't') {
Ok(SecWebSocketAccept.into())
} else {
validate(b, len)
}
}
21 => {
to_lower!(b, data, 21);
if eq!(b == b's' b'e' b'c' b'-' b'w' b'e' b'b' b's' b'o' b'c' b'k' b'e' b't' b'-' b'v' b'e' b'r' b's' b'i' b'o' b'n') {
Ok(SecWebSocketVersion.into())
} else {
validate(b, len)
}
}
22 => {
to_lower!(b, data, 22);
if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-' b'm' b'a' b'x' b'-' b'a' b'g' b'e') {
Ok(AccessControlMaxAge.into())
} else if eq!(b == b'x' b'-' b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b't' b'y' b'p' b'e' b'-' b'o' b'p' b't' b'i' b'o' b'n' b's') {
Ok(XContentTypeOptions.into())
} else if eq!(b == b'x' b'-' b'd' b'n' b's' b'-' b'p' b'r' b'e' b'f' b'e' b't' b'c' b'h' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l') {
Ok(XDnsPrefetchControl.into())
} else if eq!(b == b's' b'e' b'c' b'-' b'w' b'e' b'b' b's' b'o' b'c' b'k' b'e' b't' b'-' b'p' b'r' b'o' b't' b'o' b'c' b'o' b'l') {
Ok(SecWebSocketProtocol.into())
} else {
validate(b, len)
}
}
23 => {
to_lower!(b, data, 23);
if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b's' b'e' b'c' b'u' b'r' b'i' b't' b'y' b'-' b'p' b'o' b'l' b'i' b'c' b'y') {
Ok(ContentSecurityPolicy.into())
} else {
validate(b, len)
}
}
24 => {
to_lower!(b, data, 24);
if eq!(b == b's' b'e' b'c' b'-' b'w' b'e' b'b' b's' b'o' b'c' b'k' b'e' b't' b'-' b'e' b'x' b't' b'e' b'n' b's' b'i' b'o' b'n' b's') {
Ok(SecWebSocketExtensions.into())
} else {
validate(b, len)
}
}
25 => {
to_lower!(b, data, 25);
if eq!(b == b's' b't' b'r' b'i' b'c' b't' b'-' b't' b'r' b'a' b'n' b's' b'p' b'o' b'r' b't' b'-' b's' b'e' b'c' b'u' b'r' b'i' b't' b'y') {
Ok(StrictTransportSecurity.into())
} else if eq!(b == b'u' b'p' b'g' b'r' b'a' b'd' b'e' b'-' b'i' b'n' b's' b'e' b'c' b'u' b'r' b'e' b'-' b'r' b'e' b'q' b'u' b'e' b's' b't' b's') {
Ok(UpgradeInsecureRequests.into())
} else {
validate(b, len)
}
}
27 => {
to_lower!(b, data, 27);
if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-' b'a' b'l' b'l' b'o' b'w' b'-' b'o' b'r' b'i' b'g' b'i' b'n') {
Ok(AccessControlAllowOrigin.into())
} else if eq!(b == b'p' b'u' b'b' b'l' b'i' b'c' b'-' b'k' b'e' b'y' b'-' b'p' b'i' b'n' b's' b'-' b'r' b'e' b'p' b'o' b'r' b't' b'-' b'o' b'n' b'l' b'y') {
Ok(PublicKeyPinsReportOnly.into())
} else {
validate(b, len)
}
}
28 => {
to_lower!(b, data, 28);
if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-' b'a' b'l' b'l' b'o' b'w' b'-') {
if eq!(b[21] == b'h' b'e' b'a' b'd' b'e' b'r' b's') {
return Ok(AccessControlAllowHeaders.into())
} else if eq!(b[21] == b'm' b'e' b't' b'h' b'o' b'd' b's') {
return Ok(AccessControlAllowMethods.into())
}
}
validate(b, len)
}
29 => {
to_lower!(b, data, 29);
if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-') {
if eq!(b[15] == b'e' b'x' b'p' b'o' b's' b'e' b'-' b'h' b'e' b'a' b'd' b'e' b'r' b's') {
return Ok(AccessControlExposeHeaders.into())
} else if eq!(b[15] == b'r' b'e' b'q' b'u' b'e' b's' b't' b'-' b'm' b'e' b't' b'h' b'o' b'd') {
return Ok(AccessControlRequestMethod.into())
}
}
validate(b, len)
}
30 => {
to_lower!(b, data, 30);
if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-' b'r' b'e' b'q' b'u' b'e' b's' b't' b'-' b'h' b'e' b'a' b'd' b'e' b'r' b's') {
Ok(AccessControlRequestHeaders.into())
} else {
validate(b, len)
}
}
32 => {
to_lower!(b, data, 32);
if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-' b'a' b'l' b'l' b'o' b'w' b'-' b'c' b'r' b'e' b'd' b'e' b'n' b't' b'i' b'a' b'l' b's') {
Ok(AccessControlAllowCredentials.into())
} else {
validate(b, len)
}
}
35 => {
to_lower!(b, data, 35);
if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b's' b'e' b'c' b'u' b'r' b'i' b't' b'y' b'-' b'p' b'o' b'l' b'i' b'c' b'y' b'-' b'r' b'e' b'p' b'o' b'r' b't' b'-' b'o' b'n' b'l' b'y') {
Ok(ContentSecurityPolicyReportOnly.into())
} else {
validate(b, len)
}
}
_ => {
if len < 64 {
for i in 0..len {
b[i] = table[data[i] as usize];
}
validate(b, len)
} else {
Ok(HdrName::custom(data, false))
}
}
}
}
#[cfg(all(debug_assertions, target_arch = "wasm32"))]
/// This version works best in debug mode in wasm
fn parse_hdr<'a>(
data: &'a [u8],
b: &'a mut [u8; 64],
table: &[u8; 256],
) -> Result<HdrName<'a>, InvalidHeaderName> {
use self::StandardHeader::*;
let len = data.len();
let validate = |buf: &'a [u8], len: usize| {
let buf = &buf[..len];
if buf.iter().any(|&b| b == 0) {
Err(InvalidHeaderName::new())
} else {
Ok(HdrName::custom(buf, true))
}
};
assert!(
len < super::MAX_HEADER_NAME_LEN,
"header name too long -- max length is {}",
super::MAX_HEADER_NAME_LEN
);
match len {
0 => Err(InvalidHeaderName::new()),
len if len > 64 => Ok(HdrName::custom(data, false)),
len => {
// Read from data into the buffer - transforming using `table` as we go
data.iter().zip(b.iter_mut()).for_each(|(index, out)| *out = table[*index as usize]);
match &b[0..len] {
b"te" => Ok(Te.into()),
b"age" => Ok(Age.into()),
b"via" => Ok(Via.into()),
b"dnt" => Ok(Dnt.into()),
b"date" => Ok(Date.into()),
b"etag" => Ok(Etag.into()),
b"from" => Ok(From.into()),
b"host" => Ok(Host.into()),
b"link" => Ok(Link.into()),
b"vary" => Ok(Vary.into()),
b"allow" => Ok(Allow.into()),
b"range" => Ok(Range.into()),
b"accept" => Ok(Accept.into()),
b"cookie" => Ok(Cookie.into()),
b"expect" => Ok(Expect.into()),
b"origin" => Ok(Origin.into()),
b"pragma" => Ok(Pragma.into()),
b"server" => Ok(Server.into()),
b"alt-svc" => Ok(AltSvc.into()),
b"expires" => Ok(Expires.into()),
b"referer" => Ok(Referer.into()),
b"refresh" => Ok(Refresh.into()),
b"trailer" => Ok(Trailer.into()),
b"upgrade" => Ok(Upgrade.into()),
b"warning" => Ok(Warning.into()),
b"if-match" => Ok(IfMatch.into()),
b"if-range" => Ok(IfRange.into()),
b"location" => Ok(Location.into()),
b"forwarded" => Ok(Forwarded.into()),
b"connection" => Ok(Connection.into()),
b"set-cookie" => Ok(SetCookie.into()),
b"user-agent" => Ok(UserAgent.into()),
b"retry-after" => Ok(RetryAfter.into()),
b"content-type" => Ok(ContentType.into()),
b"max-forwards" => Ok(MaxForwards.into()),
b"accept-ranges" => Ok(AcceptRanges.into()),
b"authorization" => Ok(Authorization.into()),
b"cache-control" => Ok(CacheControl.into()),
b"content-range" => Ok(ContentRange.into()),
b"if-none-match" => Ok(IfNoneMatch.into()),
b"last-modified" => Ok(LastModified.into()),
b"accept-charset" => Ok(AcceptCharset.into()),
b"content-length" => Ok(ContentLength.into()),
b"accept-encoding" => Ok(AcceptEncoding.into()),
b"accept-language" => Ok(AcceptLanguage.into()),
b"public-key-pins" => Ok(PublicKeyPins.into()),
b"x-frame-options" => Ok(XFrameOptions.into()),
b"referrer-policy" => Ok(ReferrerPolicy.into()),
b"content-language" => Ok(ContentLanguage.into()),
b"content-location" => Ok(ContentLocation.into()),
b"content-encoding" => Ok(ContentEncoding.into()),
b"www-authenticate" => Ok(WwwAuthenticate.into()),
b"x-xss-protection" => Ok(XXssProtection.into()),
b"transfer-encoding" => Ok(TransferEncoding.into()),
b"if-modified-since" => Ok(IfModifiedSince.into()),
b"sec-websocket-key" => Ok(SecWebSocketKey.into()),
b"proxy-authenticate" => Ok(ProxyAuthenticate.into()),
b"content-disposition" => Ok(ContentDisposition.into()),
b"if-unmodified-since" => Ok(IfUnmodifiedSince.into()),
b"proxy-authorization" => Ok(ProxyAuthorization.into()),
b"sec-websocket-accept" => Ok(SecWebSocketAccept.into()),
b"sec-websocket-version" => Ok(SecWebSocketVersion.into()),
b"access-control-max-age" => Ok(AccessControlMaxAge.into()),
b"x-content-type-options" => Ok(XContentTypeOptions.into()),
b"x-dns-prefetch-control" => Ok(XDnsPrefetchControl.into()),
b"sec-websocket-protocol" => Ok(SecWebSocketProtocol.into()),
b"content-security-policy" => Ok(ContentSecurityPolicy.into()),
b"sec-websocket-extensions" => Ok(SecWebSocketExtensions.into()),
b"strict-transport-security" => Ok(StrictTransportSecurity.into()),
b"upgrade-insecure-requests" => Ok(UpgradeInsecureRequests.into()),
b"access-control-allow-origin" => Ok(AccessControlAllowOrigin.into()),
b"public-key-pins-report-only" => Ok(PublicKeyPinsReportOnly.into()),
b"access-control-allow-headers" => Ok(AccessControlAllowHeaders.into()),
b"access-control-allow-methods" => Ok(AccessControlAllowMethods.into()),
b"access-control-expose-headers" => Ok(AccessControlExposeHeaders.into()),
b"access-control-request-method" => Ok(AccessControlRequestMethod.into()),
b"access-control-request-headers" => Ok(AccessControlRequestHeaders.into()),
b"access-control-allow-credentials" => Ok(AccessControlAllowCredentials.into()),
b"content-security-policy-report-only" => {
Ok(ContentSecurityPolicyReportOnly.into())
}
other => validate(other, len),
}
}
}
}
impl<'a> From<StandardHeader> for HdrName<'a> {
fn from(hdr: StandardHeader) -> HdrName<'a> {
HdrName { inner: Repr::Standard(hdr) }
}
}
impl HeaderName {
/// Converts a slice of bytes to an HTTP header name.
///
/// This function normalizes the input.
#[allow(deprecated)]
pub fn from_bytes(src: &[u8]) -> Result<HeaderName, InvalidHeaderName> {
let mut buf = unsafe { mem::uninitialized() };
match parse_hdr(src, &mut buf, &HEADER_CHARS)?.inner {
Repr::Standard(std) => Ok(std.into()),
Repr::Custom(MaybeLower { buf, lower: true }) => {
let buf = Bytes::from(buf);
let val = unsafe { ByteStr::from_utf8_unchecked(buf) };
Ok(Custom(val).into())
}
Repr::Custom(MaybeLower { buf, lower: false }) => {
use bytes::{BufMut};
let mut dst = BytesMut::with_capacity(buf.len());
for b in buf.iter() {
let b = HEADER_CHARS[*b as usize];
if b == 0 {
return Err(InvalidHeaderName::new());
}
dst.put(b);
}
let val = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) };
Ok(Custom(val).into())
}
}
}
/// Converts a slice of bytes to an HTTP header name.
///
/// This function expects the input to only contain lowercase characters.
/// This is useful when decoding HTTP/2.0 headers. The HTTP/2.0
/// specification requires that all headers be represented in lower case.
///
/// # Examples
///
/// ```
/// # use http::header::*;
///
/// // Parsing a lower case header
/// let hdr = HeaderName::from_lowercase(b"content-length").unwrap();
/// assert_eq!(CONTENT_LENGTH, hdr);
///
/// // Parsing a header that contains uppercase characters
/// assert!(HeaderName::from_lowercase(b"Content-Length").is_err());
/// ```
#[allow(deprecated)]
pub fn from_lowercase(src: &[u8]) -> Result<HeaderName, InvalidHeaderName> {
let mut buf = unsafe { mem::uninitialized() };
match parse_hdr(src, &mut buf, &HEADER_CHARS_H2)?.inner {
Repr::Standard(std) => Ok(std.into()),
Repr::Custom(MaybeLower { buf, lower: true }) => {
let buf = Bytes::from(buf);
let val = unsafe { ByteStr::from_utf8_unchecked(buf) };
Ok(Custom(val).into())
}
Repr::Custom(MaybeLower { buf, lower: false }) => {
for &b in buf.iter() {
if b != HEADER_CHARS[b as usize] {
return Err(InvalidHeaderName::new());
}
}
let buf = Bytes::from(buf);
let val = unsafe { ByteStr::from_utf8_unchecked(buf) };
Ok(Custom(val).into())
}
}
}
/// Converts a static string to a HTTP header name.
///
/// This function panics when the static string is a invalid header.
///
/// This function requires the static string to only contain lowercase
/// characters, numerals and symbols, as per the HTTP/2.0 specification
/// and header names internal representation within this library.
///
///
/// # Examples
///
/// ```
/// # use http::header::*;
/// // Parsing a standard header
/// let hdr = HeaderName::from_static("content-length");
/// assert_eq!(CONTENT_LENGTH, hdr);
///
/// // Parsing a custom header
/// let CUSTOM_HEADER: &'static str = "custom-header";
///
/// let a = HeaderName::from_lowercase(b"custom-header").unwrap();
/// let b = HeaderName::from_static(CUSTOM_HEADER);
/// assert_eq!(a, b);
/// ```
///
/// ```should_panic
/// # use http::header::*;
/// #
/// // Parsing a header that contains invalid symbols(s):
/// HeaderName::from_static("content{}{}length"); // This line panics!
///
/// // Parsing a header that contains invalid uppercase characters.
/// let a = HeaderName::from_static("foobar");
/// let b = HeaderName::from_static("FOOBAR"); // This line panics!
/// ```
#[allow(deprecated)]
pub fn from_static(src: &'static str) -> HeaderName {
let bytes = src.as_bytes();
let mut buf = unsafe { mem::uninitialized() };
match parse_hdr(bytes, &mut buf, &HEADER_CHARS_H2) {
Ok(hdr_name) => match hdr_name.inner {
Repr::Standard(std) => std.into(),
Repr::Custom(MaybeLower { buf: _, lower: true }) => {
let val = ByteStr::from_static(src);
Custom(val).into()
},
Repr::Custom(MaybeLower { buf: _, lower: false }) => {
// With lower false, the string is left unchecked by
// parse_hdr and must be validated manually.
for &b in bytes.iter() {
if HEADER_CHARS_H2[b as usize] == 0 {
panic!("invalid header name")
}
}
let val = ByteStr::from_static(src);
Custom(val).into()
}
},
Err(_) => panic!("invalid header name")
}
}
/// Returns a `str` representation of the header.
///
/// The returned string will always be lower case.
#[inline]
pub fn as_str(&self) -> &str {
match self.inner {
Repr::Standard(v) => v.as_str(),
Repr::Custom(ref v) => &*v.0,
}
}
}
impl FromStr for HeaderName {
type Err = InvalidHeaderName;
fn from_str(s: &str) -> Result<HeaderName, InvalidHeaderName> {
HeaderName::from_bytes(s.as_bytes())
.map_err(|_| InvalidHeaderName {
_priv: (),
})
}
}
impl AsRef<str> for HeaderName {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsRef<[u8]> for HeaderName {
fn as_ref(&self) -> &[u8] {
self.as_str().as_bytes()
}
}
impl Borrow<str> for HeaderName {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl fmt::Debug for HeaderName {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), fmt)
}
}
impl fmt::Display for HeaderName {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self.as_str(), fmt)
}
}
impl InvalidHeaderName {
fn new() -> InvalidHeaderName {
InvalidHeaderName { _priv: () }
}
}
impl<'a> From<&'a HeaderName> for HeaderName {
fn from(src: &'a HeaderName) -> HeaderName {
src.clone()
}
}
#[doc(hidden)]
impl<T> From<Repr<T>> for Bytes
where T: Into<Bytes> {
fn from(repr: Repr<T>) -> Bytes {
match repr {
Repr::Standard(header) =>
Bytes::from_static(header.as_str().as_bytes()),
Repr::Custom(header) => header.into()
}
}
}
impl From<Custom> for Bytes {
#[inline]
fn from(Custom(inner): Custom) -> Bytes {
Bytes::from(inner)
}
}
impl From<HeaderName> for Bytes {
#[inline]
fn from(name: HeaderName) -> Bytes {
name.inner.into()
}
}
impl<'a> HttpTryFrom<&'a HeaderName> for HeaderName {
type Error = ::error::Never;
#[inline]
fn try_from(t: &'a HeaderName) -> Result<Self, Self::Error> {
Ok(t.clone())
}
}
impl<'a> HttpTryFrom<&'a str> for HeaderName {
type Error = InvalidHeaderName;
#[inline]
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
Self::from_bytes(s.as_bytes())
}
}
impl<'a> HttpTryFrom<&'a String> for HeaderName {
type Error = InvalidHeaderName;
#[inline]
fn try_from(s: &'a String) -> Result<Self, Self::Error> {
Self::from_bytes(s.as_bytes())
}
}
impl<'a> HttpTryFrom<&'a [u8]> for HeaderName {
type Error = InvalidHeaderName;
#[inline]
fn try_from(s: &'a [u8]) -> Result<Self, Self::Error> {
Self::from_bytes(s)
}
}
impl HttpTryFrom<Bytes> for HeaderName {
type Error = InvalidHeaderNameBytes;
#[inline]
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
Self::from_bytes(bytes.as_ref()).map_err(InvalidHeaderNameBytes)
}
}
#[doc(hidden)]
impl From<StandardHeader> for HeaderName {
fn from(src: StandardHeader) -> HeaderName {
HeaderName {
inner: Repr::Standard(src),
}
}
}
#[doc(hidden)]
impl From<Custom> for HeaderName {
fn from(src: Custom) -> HeaderName {
HeaderName { inner: Repr::Custom(src) }
}
}
impl<'a> PartialEq<&'a HeaderName> for HeaderName {
#[inline]
fn eq(&self, other: &&'a HeaderName) -> bool {
*self == **other
}
}
impl<'a> PartialEq<HeaderName> for &'a HeaderName {
#[inline]
fn eq(&self, other: &HeaderName) -> bool {
*other == *self
}
}
impl PartialEq<str> for HeaderName {
/// Performs a case-insensitive comparison of the string against the header
/// name
///
/// # Examples
///
/// ```
/// use http::header::CONTENT_LENGTH;
///
/// assert_eq!(CONTENT_LENGTH, "content-length");
/// assert_eq!(CONTENT_LENGTH, "Content-Length");
/// assert_ne!(CONTENT_LENGTH, "content length");
/// ```
#[inline]
fn eq(&self, other: &str) -> bool {
eq_ignore_ascii_case(self.as_ref(), other.as_bytes())
}
}
impl PartialEq<HeaderName> for str {
/// Performs a case-insensitive comparison of the string against the header
/// name
///
/// # Examples
///
/// ```
/// use http::header::CONTENT_LENGTH;
///
/// assert_eq!(CONTENT_LENGTH, "content-length");
/// assert_eq!(CONTENT_LENGTH, "Content-Length");
/// assert_ne!(CONTENT_LENGTH, "content length");
/// ```
#[inline]
fn eq(&self, other: &HeaderName) -> bool {
*other == *self
}
}
impl<'a> PartialEq<&'a str> for HeaderName {
/// Performs a case-insensitive comparison of the string against the header
/// name
#[inline]
fn eq(&self, other: &&'a str) -> bool {
*self == **other
}
}
impl<'a> PartialEq<HeaderName> for &'a str {
/// Performs a case-insensitive comparison of the string against the header
/// name
#[inline]
fn eq(&self, other: &HeaderName) -> bool {
*other == *self
}
}
impl fmt::Debug for InvalidHeaderName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("InvalidHeaderName")
// skip _priv noise
.finish()
}
}
impl fmt::Display for InvalidHeaderName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.description().fmt(f)
}
}
impl Error for InvalidHeaderName {
fn description(&self) -> &str {
"invalid HTTP header name"
}
}
impl fmt::Display for InvalidHeaderNameBytes {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl Error for InvalidHeaderNameBytes {
fn description(&self) -> &str {
self.0.description()
}
}
// ===== HdrName =====
impl<'a> HdrName<'a> {
fn custom(buf: &'a [u8], lower: bool) -> HdrName<'a> {
HdrName {
inner: Repr::Custom(MaybeLower {
buf: buf,
lower: lower,
}),
}
}
#[allow(deprecated)]
pub fn from_bytes<F, U>(hdr: &[u8], f: F) -> Result<U, InvalidHeaderName>
where F: FnOnce(HdrName) -> U,
{
let mut buf = unsafe { mem::uninitialized() };
let hdr = parse_hdr(hdr, &mut buf, &HEADER_CHARS)?;
Ok(f(hdr))
}
#[allow(deprecated)]
pub fn from_static<F, U>(hdr: &'static str, f: F) -> U
where F: FnOnce(HdrName) -> U,
{
let mut buf = unsafe { mem::uninitialized() };
let hdr = parse_hdr(hdr.as_bytes(), &mut buf, &HEADER_CHARS)
.expect("static str is invalid name");
f(hdr)
}
}
#[doc(hidden)]
impl<'a> From<HdrName<'a>> for HeaderName {
fn from(src: HdrName<'a>) -> HeaderName {
match src.inner {
Repr::Standard(s) => {
HeaderName {
inner: Repr::Standard(s),
}
}
Repr::Custom(maybe_lower) => {
if maybe_lower.lower {
let buf = Bytes::from(&maybe_lower.buf[..]);
let byte_str = unsafe { ByteStr::from_utf8_unchecked(buf) };
HeaderName {
inner: Repr::Custom(Custom(byte_str)),
}
} else {
use bytes::{BufMut};
let mut dst = BytesMut::with_capacity(maybe_lower.buf.len());
for b in maybe_lower.buf.iter() {
dst.put(HEADER_CHARS[*b as usize]);
}
let buf = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) };
HeaderName {
inner: Repr::Custom(Custom(buf)),
}
}
}
}
}
}
#[doc(hidden)]
impl<'a> PartialEq<HdrName<'a>> for HeaderName {
#[inline]
fn eq(&self, other: &HdrName<'a>) -> bool {
match self.inner {
Repr::Standard(a) => {
match other.inner {
Repr::Standard(b) => a == b,
_ => false,
}
}
Repr::Custom(Custom(ref a)) => {
match other.inner {
Repr::Custom(ref b) => {
if b.lower {
a.as_bytes() == b.buf
} else {
eq_ignore_ascii_case(a.as_bytes(), b.buf)
}
}
_ => false,
}
}
}
}
}
// ===== Custom =====
impl Hash for Custom {
#[inline]
fn hash<H: Hasher>(&self, hasher: &mut H) {
hasher.write(self.0.as_bytes())
}
}
// ===== MaybeLower =====
impl<'a> Hash for MaybeLower<'a> {
#[inline]
fn hash<H: Hasher>(&self, hasher: &mut H) {
if self.lower {
hasher.write(self.buf);
} else {
for &b in self.buf {
hasher.write(&[HEADER_CHARS[b as usize]]);
}
}
}
}
// Assumes that the left hand side is already lower case
#[inline]
fn eq_ignore_ascii_case(lower: &[u8], s: &[u8]) -> bool {
if lower.len() != s.len() {
return false;
}
lower.iter().zip(s).all(|(a, b)| {
*a == HEADER_CHARS[*b as usize]
})
}
#[cfg(test)]
mod tests {
use super::*;
use self::StandardHeader::Vary;
#[test]
fn test_bounds() {
fn check_bounds<T: Sync + Send>() {}
check_bounds::<HeaderName>();
}
#[test]
fn test_parse_invalid_headers() {
for i in 0..128 {
let hdr = vec![1u8; i];
assert!(HeaderName::from_bytes(&hdr).is_err(), "{} invalid header chars did not fail", i);
}
}
#[test]
fn test_from_hdr_name() {
use self::StandardHeader::Vary;
let name = HeaderName::from(HdrName {
inner: Repr::Standard(Vary),
});
assert_eq!(name.inner, Repr::Standard(Vary));
let name = HeaderName::from(HdrName {
inner: Repr::Custom(MaybeLower {
buf: b"hello-world",
lower: true,
}),
});
assert_eq!(name.inner, Repr::Custom(Custom(ByteStr::from_static("hello-world"))));
let name = HeaderName::from(HdrName {
inner: Repr::Custom(MaybeLower {
buf: b"Hello-World",
lower: false,
}),
});
assert_eq!(name.inner, Repr::Custom(Custom(ByteStr::from_static("hello-world"))));
}
#[test]
fn test_eq_hdr_name() {
use self::StandardHeader::Vary;
let a = HeaderName { inner: Repr::Standard(Vary) };
let b = HdrName { inner: Repr::Standard(Vary) };
assert_eq!(a, b);
let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static("vaary"))) };
assert_ne!(a, b);
let b = HdrName { inner: Repr::Custom(MaybeLower {
buf: b"vaary",
lower: true,
})};
assert_eq!(a, b);
let b = HdrName { inner: Repr::Custom(MaybeLower {
buf: b"vaary",
lower: false,
})};
assert_eq!(a, b);
let b = HdrName { inner: Repr::Custom(MaybeLower {
buf: b"VAARY",
lower: false,
})};
assert_eq!(a, b);
let a = HeaderName { inner: Repr::Standard(Vary) };
assert_ne!(a, b);
}
#[test]
fn test_from_static_std() {
let a = HeaderName { inner: Repr::Standard(Vary) };
let b = HeaderName::from_static("vary");
assert_eq!(a, b);
let b = HeaderName::from_static("vaary");
assert_ne!(a, b);
}
#[test]
#[should_panic]
fn test_from_static_std_uppercase() {
HeaderName::from_static("Vary");
}
#[test]
#[should_panic]
fn test_from_static_std_symbol() {
HeaderName::from_static("vary{}");
}
// MaybeLower { lower: true }
#[test]
fn test_from_static_custom_short() {
let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static("customheader"))) };
let b = HeaderName::from_static("customheader");
assert_eq!(a, b);
}
#[test]
#[should_panic]
fn test_from_static_custom_short_uppercase() {
HeaderName::from_static("custom header");
}
#[test]
#[should_panic]
fn test_from_static_custom_short_symbol() {
HeaderName::from_static("CustomHeader");
}
// MaybeLower { lower: false }
#[test]
fn test_from_static_custom_long() {
let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static(
"longer-than-63--thisheaderislongerthansixtythreecharactersandthushandleddifferent"
))) };
let b = HeaderName::from_static(
"longer-than-63--thisheaderislongerthansixtythreecharactersandthushandleddifferent"
);
assert_eq!(a, b);
}
#[test]
#[should_panic]
fn test_from_static_custom_long_uppercase() {
HeaderName::from_static(
"Longer-Than-63--ThisHeaderIsLongerThanSixtyThreeCharactersAndThusHandledDifferent"
);
}
#[test]
#[should_panic]
fn test_from_static_custom_long_symbol() {
HeaderName::from_static(
"longer-than-63--thisheader{}{}{}{}islongerthansixtythreecharactersandthushandleddifferent"
);
}
#[test]
fn test_from_static_custom_single_char() {
let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static("a"))) };
let b = HeaderName::from_static("a");
assert_eq!(a, b);
}
#[test]
#[should_panic]
fn test_from_static_empty() {
HeaderName::from_static("");
}
#[test]
fn test_all_tokens() {
HeaderName::from_static("!#$%&'*+-.^_`|~0123456789abcdefghijklmnopqrstuvwxyz");
}
}