Skip to main content

weida_http_codec/
fields.rs

1//! Field names and values (RFC 9110 §5).
2//!
3//! The grammar every version shares: a name is a token, a value is visible
4//! octets, spaces and tabs with none at either end. HTTP/2 and HTTP/3 add
5//! that names are lowercase and that connection-specific fields are malformed
6//! ([`CONNECTION_SPECIFIC`]); those rules live in their modules.
7
8/// The fields RFC 9113 §8.2.2 and RFC 9114 §4.2 call connection-specific: an
9/// HTTP/2 or HTTP/3 message that carries one is malformed, and an HTTP/1.1
10/// library manages them itself rather than taking them from an application.
11pub const CONNECTION_SPECIFIC: [&str; 5] = [
12    "connection",
13    "keep-alive",
14    "proxy-connection",
15    "transfer-encoding",
16    "upgrade",
17];
18
19/// One field line as it arrived: the name as written (case preserved) and
20/// the value with surrounding whitespace removed.
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub struct Field<'a> {
23    /// The field name, a token.
24    pub name: &'a [u8],
25    /// The field value, trimmed.
26    pub value: &'a [u8],
27}
28
29/// `tchar` (RFC 9110 §5.6.2).
30#[must_use]
31pub const fn is_tchar(b: u8) -> bool {
32    matches!(b,
33        b'!' | b'#' | b'$' | b'%' | b'&' | b'\'' | b'*' | b'+' | b'-' | b'.'
34        | b'^' | b'_' | b'`' | b'|' | b'~'
35        | b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z')
36}
37
38/// A non-empty run of `tchar`: a field name or a method (RFC 9110 §5.6.2).
39#[must_use]
40pub fn is_token(b: &[u8]) -> bool {
41    !b.is_empty() && b.iter().all(|&c| is_tchar(c))
42}
43
44/// A field value (RFC 9110 §5.5): `VCHAR`, `obs-text`, SP and HTAB, no CR, LF
45/// or NUL, and no SP or HTAB at either end. The empty value is valid.
46#[must_use]
47pub fn is_field_value(b: &[u8]) -> bool {
48    let inner = b
49        .iter()
50        .all(|&c| c == b' ' || c == b'\t' || (0x21..=0x7e).contains(&c) || c >= 0x80);
51    let edges = !matches!(b.first(), Some(b' ' | b'\t')) && !matches!(b.last(), Some(b' ' | b'\t'));
52    inner && edges
53}
54
55/// Whether a field carries a credential an encoder must never put into a
56/// compression table, and a log must never print: `authorization`,
57/// `proxy-authorization`, `cookie` and `set-cookie`, compared ignoring ASCII
58/// case (RFC 7541 §7.1.3, RFC 9204 §7.1.3).
59#[must_use]
60pub fn is_sensitive(name: &[u8]) -> bool {
61    [
62        "authorization",
63        "proxy-authorization",
64        "cookie",
65        "set-cookie",
66    ]
67    .iter()
68    .any(|s| name.eq_ignore_ascii_case(s.as_bytes()))
69}
70
71/// Whether `name` is one of [`CONNECTION_SPECIFIC`], ignoring ASCII case.
72#[must_use]
73pub fn is_connection_specific(name: &[u8]) -> bool {
74    CONNECTION_SPECIFIC
75        .iter()
76        .any(|s| name.eq_ignore_ascii_case(s.as_bytes()))
77}
78
79/// Strips SP and HTAB from both ends (RFC 9110 §5.5 OWS).
80#[must_use]
81pub fn trim_ows(mut b: &[u8]) -> &[u8] {
82    while let [b' ' | b'\t', rest @ ..] = b {
83        b = rest;
84    }
85    while let [rest @ .., b' ' | b'\t'] = b {
86        b = rest;
87    }
88    b
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn tokens_refuse_separators_and_the_empty_string() {
97        assert!(is_token(b"Content-Length"));
98        assert!(is_token(b"x!#$%&'*+-.^_`|~9"));
99        assert!(!is_token(b""));
100        assert!(!is_token(b"a b"));
101        assert!(!is_token(b"a:b"));
102        assert!(!is_token(b"(a)"));
103    }
104
105    #[test]
106    fn values_refuse_line_breaks_nul_and_edge_whitespace() {
107        assert!(is_field_value(b""));
108        assert!(is_field_value(b"a b\tc"));
109        assert!(is_field_value(b"\x80\xff"));
110        assert!(!is_field_value(b"a\r\nb"));
111        assert!(!is_field_value(b"a\nb"));
112        assert!(!is_field_value(b"a\0b"));
113        assert!(!is_field_value(b" a"));
114        assert!(!is_field_value(b"a\t"));
115    }
116
117    #[test]
118    fn credentials_are_sensitive_in_any_case() {
119        assert!(is_sensitive(b"Authorization"));
120        assert!(is_sensitive(b"SET-COOKIE"));
121        assert!(!is_sensitive(b"content-type"));
122    }
123}