weida_http_codec/
fields.rs1pub const CONNECTION_SPECIFIC: [&str; 5] = [
12 "connection",
13 "keep-alive",
14 "proxy-connection",
15 "transfer-encoding",
16 "upgrade",
17];
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub struct Field<'a> {
23 pub name: &'a [u8],
25 pub value: &'a [u8],
27}
28
29#[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#[must_use]
40pub fn is_token(b: &[u8]) -> bool {
41 !b.is_empty() && b.iter().all(|&c| is_tchar(c))
42}
43
44#[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#[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#[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#[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}