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