Skip to main content

sark_http/h1/head/
mod.rs

1//! HTTP/1 head syntax, framing, and admission.
2
3use core::mem;
4
5use crate::{
6    h1::{
7        field::{self, syntax},
8        framing, switch, target,
9    },
10    payload::response::status,
11    request::span,
12};
13
14mod limit;
15pub mod line;
16pub mod request;
17pub mod response;
18mod state;
19mod tail;
20
21pub use limit::Limit;
22
23pub const MAX_BYTES: usize = 64 * 1024;
24
25pub trait Admission: Default {
26    type Input<'a>;
27    type Admitted: Copy;
28    type Error;
29
30    const MAX_BYTES: usize = crate::h1::head::MAX_BYTES;
31    const FIELDS: bool = true;
32
33    fn field(&mut self, _: &[u8], _: &[u8], _: span::Span) -> Result<(), Self::Error> {
34        Ok(())
35    }
36
37    fn finish<'a>(
38        self,
39        request: Self::Input<'a>,
40        fields: &request::Fields,
41        body: framing::RequestBody,
42    ) -> Result<Self::Admitted, Self::Error>;
43}
44
45impl Admission for () {
46    type Input<'a> = ();
47    type Admitted = ();
48    type Error = core::convert::Infallible;
49
50    const FIELDS: bool = false;
51
52    fn finish<'a>(
53        self,
54        _: Self::Input<'a>,
55        _: &request::Fields,
56        _: framing::RequestBody,
57    ) -> Result<(), Self::Error> {
58        Ok(())
59    }
60}
61
62#[derive(Debug)]
63pub enum Error<E, A> {
64    Storage(o3::buffer::storage::AllocError),
65    Syntax(syntax::Error),
66    Name { offset: usize, actual: u8 },
67    Value { offset: usize, actual: u8 },
68    Point { base: usize, relative: usize, available: usize },
69    Ending { start: usize, available: usize },
70    Limit { observed: usize, limit: usize },
71    Field {
72        start: usize,
73        available: usize,
74        cause: crate::field::head::Error,
75    },
76    Header(E),
77    Admission(A),
78    Span(crate::request::Error),
79    Offset { base: usize, relative: usize, suffix: usize, available: usize },
80    Fields { actual: usize, limit: usize },
81    Length { len: usize },
82    Target(Semantic),
83    Method(Semantic),
84    Layout(target::Error),
85    UnsupportedTransferEncoding,
86}
87
88
89/// Returns the new bytes through the first CRLFCRLF after an incomplete head.
90pub fn through_end<'a>(retained: &[u8], incoming: &'a [u8]) -> Option<&'a [u8]> {
91    const END: &[u8; 4] = b"\r\n\r\n";
92    for held in (1..END.len()).rev() {
93        if retained.ends_with(&END[..held]) && incoming.starts_with(&END[held..]) {
94            return Some(&incoming[..END.len() - held]);
95        }
96    }
97    let mut rest = incoming;
98    while let Some(found) = crate::field::scan::Match::find(b'\r', rest) {
99        rest = found.suffix();
100        if let Some(tail) = rest.strip_prefix(&END[1..]) {
101            return Some(&incoming[..incoming.len() - tail.len()]);
102        }
103    }
104    None
105}
106
107#[derive(Clone, Copy, PartialEq, Eq, Debug)]
108pub enum Boundary {
109    Invalid,
110    TooLarge { actual: usize, limit: usize },
111    Span(span::Error),
112}
113
114#[derive(Clone, Copy, PartialEq, Eq, Debug)]
115pub enum Semantic {
116    Status(status::Error),
117    Field(field::Error),
118    Host(target::HostError),
119    Routing(target::Invalid),
120    Authority(target::Error),
121    Connection(field::TokenError),
122    Upgrade(switch::UpgradeError),
123    Admission(framing::RequestError),
124    Transition(switch::Error),
125    Framing(framing::Error),
126}
127
128const _: () = {
129    assert!(mem::size_of::<Boundary>() == 4 * mem::size_of::<usize>());
130    assert!(mem::size_of::<Option<Boundary>>() == mem::size_of::<Boundary>());
131    assert!(!mem::needs_drop::<Boundary>());
132    assert!(mem::size_of::<Semantic>() == 6 * mem::size_of::<usize>());
133    assert!(mem::size_of::<Option<Semantic>>() == mem::size_of::<Semantic>());
134    assert!(!mem::needs_drop::<Semantic>());
135};
136
137#[derive(Clone, Copy, PartialEq, Eq, Debug)]
138pub enum ParseError<E> {
139    Boundary(Boundary),
140    Field(syntax::Error),
141    Semantic(Semantic),
142    Sink(E),
143}
144
145impl<E> ParseError<E> {
146    pub const fn invalid() -> Self {
147        Self::Boundary(Boundary::Invalid)
148    }
149
150    pub const fn too_large(actual: usize, limit: usize) -> Self {
151        Self::Boundary(Boundary::TooLarge { actual, limit })
152    }
153}
154
155impl<E, A> From<o3::buffer::storage::AllocError> for Error<E, A> {
156    fn from(error: o3::buffer::storage::AllocError) -> Self {
157        Self::Storage(error)
158    }
159}
160
161impl<E, A> From<core::convert::Infallible> for Error<E, A> {
162    fn from(never: core::convert::Infallible) -> Self {
163        match never {}
164    }
165}