sark_http/field/metadata/block/
mod.rs1use core::num::NonZero;
2
3use crate::{field::metadata::map::record::Span, payload::response};
4
5pub mod buffer;
6mod gate;
7mod name;
8mod plan;
9mod scan;
10pub mod table;
11mod value;
12
13pub use gate::{Gate, initial::Initial};
14pub use name::Name;
15pub use plan::Plan;
16pub use scan::Scan;
17pub use value::Value;
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub struct Framing {
22 length: u64,
23 status: Option<NonZero<u16>>,
24 method: Option<response::Method>,
25 present: bool,
26}
27
28impl Framing {
29 pub const fn content_length(self) -> Option<u64> {
30 if self.present {
31 Some(self.length)
32 } else {
33 None
34 }
35 }
36
37 pub const fn status(self) -> Option<NonZero<u16>> {
38 self.status
39 }
40
41 pub const fn method(self) -> Option<response::Method> {
42 self.method
43 }
44
45 pub fn content(self, method: response::Method) -> Result<(Content, u64), ContentError> {
47 let length = self.content_length();
48 if let Some(status) = self.status {
49 match response::Content::new(method, status.get()) {
50 response::Content::Tunnel => return Ok((Content::Tunnel, 0)),
51 response::Content::Forbidden => {
52 if let Some(length) = length {
53 return Err(ContentError::ForbiddenLength { length });
54 }
55 return Ok((Content::Empty, 0));
56 }
57 response::Content::Suppressed => return Ok((Content::Empty, 0)),
58 response::Content::Empty => {
59 if let Some(length) = length.filter(|&length| length != 0) {
60 return Err(ContentError::NonemptyLength { length });
61 }
62 return Ok((Content::Exact, 0));
63 }
64 response::Content::Allowed => {}
65 }
66 } else if self.method == Some(response::Method::Connect) {
67 return Ok((Content::Tunnel, 0));
68 }
69 Ok(match length {
70 Some(length) => (Content::Exact, length),
71 None => (Content::Unbounded, 0),
72 })
73 }
74}
75
76#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
78pub enum Content {
79 #[default]
80 Unbounded,
81 Exact,
82 Empty,
83 Tunnel,
84}
85
86#[derive(Clone, Copy, Debug, PartialEq, Eq)]
87pub enum ContentError {
88 ForbiddenLength { length: u64 },
89 NonemptyLength { length: u64 },
90 Exceeded { remaining: u64, actual: usize },
91 Incomplete { remaining: u64 },
92 Trailers,
93}
94
95impl Content {
96 pub fn receive(
97 self,
98 remaining: u64,
99 bytes: usize,
100 finished: bool,
101 ) -> Result<u64, ContentError> {
102 let next = match self {
103 Self::Unbounded | Self::Tunnel => return Ok(remaining),
104 Self::Empty if bytes == 0 => return Ok(0),
105 Self::Empty => {
106 return Err(ContentError::Exceeded {
107 remaining: 0,
108 actual: bytes,
109 });
110 }
111 Self::Exact => remaining
112 .checked_sub(bytes as u64)
113 .ok_or(ContentError::Exceeded {
114 remaining,
115 actual: bytes,
116 })?,
117 };
118 if finished && next != 0 {
119 return Err(ContentError::Incomplete { remaining: next });
120 }
121 Ok(next)
122 }
123
124 pub fn trailers(self, remaining: u64) -> Result<(), ContentError> {
125 match self {
126 Self::Unbounded => Ok(()),
127 Self::Exact if remaining == 0 => Ok(()),
128 Self::Exact => Err(ContentError::Incomplete { remaining }),
129 Self::Empty | Self::Tunnel => Err(ContentError::Trailers),
130 }
131 }
132}
133
134const _: () = {
135 assert!(core::mem::size_of::<Framing>() == 16);
136 assert!(core::mem::size_of::<Option<Framing>>() == 16);
137};
138
139#[derive(Clone, Copy, Debug, PartialEq, Eq)]
140pub enum Outcome<C> {
141 Initial(C),
142 Other,
143 Rejected(Validation),
144}
145
146impl<C> Outcome<C> {
147 const fn other(check: Validation) -> Self {
148 match check {
149 Validation::Valid => Self::Other,
150 error => Self::Rejected(error),
151 }
152 }
153
154 pub const fn validation(&self) -> Validation {
155 match self {
156 Self::Initial(_) | Self::Other => Validation::Valid,
157 Self::Rejected(check) => *check,
158 }
159 }
160
161 pub fn into_parts(self) -> (Option<C>, Validation) {
162 match self {
163 Self::Initial(control) => (Some(control), Validation::Valid),
164 Self::Other => (None, Validation::Valid),
165 Self::Rejected(check) => (None, check),
166 }
167 }
168}
169
170#[derive(Clone, Copy, PartialEq, Eq)]
172pub struct Named<'a> {
173 span: Span<'a>,
174 proof: Name,
175}
176
177impl<'a> Named<'a> {
178 pub fn from_span(span: Span<'a>) -> Self {
179 Self::from_scanned(span, Name::from_span(span))
180 }
181
182 pub const fn from_slice(bytes: &'a [u8]) -> Self {
183 Self::from_scanned(Span::from_slice(bytes), Name::classify(bytes))
184 }
185
186 pub(crate) const fn from_scanned(span: Span<'a>, proof: Name) -> Self {
187 Self { span, proof }
188 }
189
190 pub const fn span(self) -> Span<'a> {
191 self.span
192 }
193
194 pub const fn proof(self) -> Name {
195 self.proof
196 }
197}
198
199#[derive(Clone, Copy, PartialEq, Eq)]
202pub struct Valued<'a> {
203 span: Span<'a>,
204 validation: Validation,
205}
206
207impl<'a> Valued<'a> {
208 pub fn from_span(span: Span<'a>) -> Self {
209 Self::from_scanned(span, Value::from_span(span).finish())
210 }
211
212 pub(crate) const fn from_scanned(span: Span<'a>, check: Validation) -> Self {
213 Self {
214 span,
215 validation: check,
216 }
217 }
218
219 pub const fn span(self) -> Span<'a> {
220 self.span
221 }
222
223 pub const fn validation(self) -> Validation {
224 self.validation
225 }
226}
227
228#[derive(Clone, Copy, Debug, PartialEq, Eq)]
229#[repr(u8)]
230pub enum Kind {
231 Initial,
232 Trailers,
233 PushPromise,
234}
235
236#[derive(Clone, Copy, PartialEq, Eq)]
238#[repr(u8)]
239pub enum Connect {
240 Disabled,
241 Enabled,
242}
243
244#[derive(Clone, Copy, PartialEq, Eq)]
246#[repr(u8)]
247pub enum Version {
248 Two,
249 Three,
250}
251
252#[derive(Clone, Copy, PartialEq, Eq, Debug)]
253#[repr(u8)]
254pub enum Validation {
255 Valid,
256 Malformed,
257 RejectPush,
258}
259
260#[derive(Clone, Copy, Debug, PartialEq, Eq)]
262pub enum Response {
263 Informational,
264 Final,
265}
266
267#[derive(Clone, Copy, Debug, PartialEq, Eq)]
268#[repr(u8)]
269pub enum Form {
270 Path,
271 Asterisk,
272 Connect,
273 ExtendedConnect,
274}
275
276#[derive(Clone, Copy, Debug, PartialEq, Eq)]
277#[repr(u8)]
278pub enum Authority {
279 None,
280 Pseudo,
281 Host,
282 Target,
283}
284
285const _: () = {
286 assert!(core::mem::size_of::<Connect>() == core::mem::size_of::<bool>());
287 assert!(core::mem::align_of::<Connect>() == core::mem::align_of::<bool>());
288 assert!(core::mem::size_of::<Version>() == core::mem::size_of::<bool>());
289 assert!(core::mem::align_of::<Version>() == core::mem::align_of::<bool>());
290};