Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/src/http/grammar.rs

1.1 KiB, 1 run

created by r1435043967:77615, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! The byte grammar of an HTTP/1.1 head (RFC 9112 §2.2, §5.1; RFC 9110 §5.6.2), shared by every
2//! reader so that no two hops read one head two ways.
3
4use oxedyne_fe2o3_core::prelude::*;
5
6
7/// Is the byte a control other than HTAB?
8pub fn is_ctl(b: u8) -> bool {
9 (b < 0x20 && b != b'\t') || b == 0x7f
10}
11
12/// Is the byte a `tchar`, a field name's character?
13pub fn is_tchar(b: u8) -> bool {
14 b.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&b)
15}
16
17/// Checks a head without its blank line: lines end only in CRLF, no control byte stands anywhere,
18/// and every line after the first is a field with a non-empty `tchar` name before its colon.
19pub fn check_head(head: &str) -> Outcome<()> {
20 let lines = head.split("\r\n");
21 if lines.clone().any(|l| l.bytes().any(is_ctl))
22 || lines.skip(1).any(|l| !matches!(l.split_once(':'),
23 Some((n, _)) if !n.is_empty() && n.bytes().all(is_tchar)))
24 {
25 return Err(err!("An HTTP head has a line that is not a field, or a control byte.";
26 IO, Network, Wire, Invalid));
27 }
28 Ok(())
29}