sark_http/h1/head/request/
mod.rs1pub mod offers;
4mod parts;
5mod plan;
6pub mod pool;
7mod route;
8pub mod scan;
9pub mod trace;
10
11pub use parts::Parts;
12pub(in crate::h1::head::request) use plan::Kind;
13pub use plan::Plan;
14pub use route::Route;
15
16use crate::{
17 field::head::Name,
18 h1::{
19 field::{self, Connection, Transfer, expect},
20 framing,
21 head::{Semantic, line},
22 switch, target,
23 },
24 payload::response::Method,
25 request::{self, span},
26};
27
28#[derive(Clone, Copy, Default)]
29pub struct Fields {
30 pub message: Message,
31 pub routing: Routing,
32}
33
34impl Fields {
35 pub fn apply(mut self, field: &field::Entry<'_>, version: line::Version) -> Result<Self, Semantic> {
36 let value = field.value();
37 match field.kind() {
38 Name::ContentLength => {
39 self.message.framing = self
40 .message
41 .framing
42 .try_length(value)
43 .map_err(Semantic::Field)?;
44 }
45 Name::TransferEncoding => {
46 self.message = self.message.try_coding(value).map_err(Semantic::Field)?;
47 }
48 Name::Host => {
49 self.routing.authority = self
50 .routing
51 .authority
52 .try_host(value)
53 .map_err(Semantic::Host)?;
54 self.routing.host = Some(field.value_span());
55 }
56 Name::Expect => {
57 self.message = self.message.with_expect(value);
58 }
59 Name::Connection => {
60 self.routing.connection = self
61 .routing
62 .connection
63 .try_push(value)
64 .map_err(Semantic::Connection)?;
65 }
66 Name::Upgrade => {
67 self.routing = self
68 .routing
69 .try_upgrade(value, version)
70 .map_err(Semantic::Upgrade)?;
71 }
72 Name::Invalid
73 | Name::Method
74 | Name::Scheme
75 | Name::Path
76 | Name::Status
77 | Name::Authority
78 | Name::Protocol
79 | Name::AcceptEncoding
80 | Name::Te
81 | Name::KeepAlive
82 | Name::ProxyConnection
83 | Name::Regular => {}
84 }
85 Ok(self)
86 }
87
88 pub fn admit_target(
89 self,
90 target: target::Target,
91 version: line::Version,
92 policy: target::Policy,
93 ) -> Result<framing::Admission, Semantic> {
94 framing::Admission::admit(
95 self.message.framing,
96 self.routing.authority,
97 self.message.expectation,
98 target,
99 version,
100 policy,
101 )
102 .map_err(Semantic::Admission)
103 }
104
105 pub fn transition(
106 self,
107 method: Method,
108 version: line::Version,
109 ) -> Result<switch::Intent, Semantic> {
110 switch::Intent::from_request(
111 method,
112 self.routing.connection,
113 self.routing.upgrades,
114 version,
115 )
116 .map_err(Semantic::Transition)
117 }
118
119 pub fn admit_parts(
120 self,
121 target: target::Target,
122 range: span::Span,
123 version: line::Version,
124 ) -> Result<(framing::Admission, request::Layout, span::Span), Semantic> {
125 let admit = self.admit_target(target, version, target::Policy::Require)?;
126 let auth = admit
127 .authority_span(self.routing.host)
128 .map_err(Semantic::Authority)?;
129 let request = target.layout(range, auth).map_err(Semantic::Authority)?;
130 Ok((admit, request, auth))
131 }
132
133 pub(super) const fn connection(self) -> Connection {
134 self.routing.connection
135 }
136
137 pub(super) const fn upgrades(self) -> switch::Offers {
138 self.routing.upgrades
139 }
140}
141
142const _: () = assert!(core::mem::size_of::<Fields>() <= 6 * core::mem::size_of::<usize>());
143
144#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
145#[repr(u8)]
146pub enum Coding {
147 #[default]
148 Plain,
149 Gzip,
150 GzipOnly,
151 Refused,
152}
153
154impl Coding {
155 pub const fn identity(self) -> bool {
156 matches!(self, Self::Plain | Self::Gzip)
157 }
158
159 pub const fn gzip(self) -> bool {
160 matches!(self, Self::Gzip | Self::GzipOnly)
161 }
162
163 pub(crate) const fn from_acceptance(identity: bool, gzip: bool) -> Self {
164 match (identity, gzip) {
165 (true, false) => Self::Plain,
166 (true, true) => Self::Gzip,
167 (false, true) => Self::GzipOnly,
168 (false, false) => Self::Refused,
169 }
170 }
171}
172
173const _: () = assert!(core::mem::size_of::<Coding>() == core::mem::size_of::<bool>());
174
175#[derive(Clone, Copy, Default)]
176pub struct Message {
177 framing: framing::Fields,
178 expectation: expect::State,
179 coding: Coding,
180}
181
182impl Message {
183 pub fn try_length(mut self, length: usize) -> Result<Self, field::Error> {
184 self.framing = self.framing.try_set(length)?;
185 Ok(self)
186 }
187
188 pub fn try_coding(mut self, value: &[u8]) -> Result<Self, field::Error> {
189 self.framing = self.framing.try_coding(value)?;
190 Ok(self)
191 }
192
193 pub fn try_append(mut self, value: Transfer) -> Result<Self, field::Error> {
194 self.framing = self.framing.try_append(value)?;
195 Ok(self)
196 }
197
198 pub fn with_expect(mut self, value: &[u8]) -> Self {
199 self.expectation = self.expectation.push(value);
200 self
201 }
202
203 pub const fn length(self) -> Option<usize> {
204 self.framing.length()
205 }
206
207 pub const fn has_coding(self) -> bool {
208 self.framing.has_coding()
209 }
210
211 pub const fn is_chunked(self) -> bool {
212 self.framing.is_chunked()
213 }
214
215 pub const fn has_other_transfer_coding(self) -> bool {
217 self.framing.has_other()
218 }
219
220 pub(crate) fn set_coding(&mut self, coding: Coding) {
221 self.coding = coding;
222 }
223
224 pub const fn coding(self) -> Coding {
225 self.coding
226 }
227}
228
229#[derive(Clone, Copy, Default)]
230pub struct Routing {
231 pub(crate) authority: target::Request,
232 pub(crate) connection: Connection,
233 pub(in crate::h1::head::request) host: Option<span::Span>,
234 pub(in crate::h1::head::request) upgrades: switch::Offers,
235}
236
237impl Routing {
238 pub const fn connection(self) -> Connection {
239 self.connection
240 }
241
242 pub fn try_host(mut self, span: span::Span) -> Result<Self, target::Invalid> {
243 if !self.authority.has_host() || self.host.is_some() {
244 return Err(target::Invalid);
245 }
246 self.host = Some(span);
247 Ok(self)
248 }
249
250 pub const fn host_span(self) -> Option<span::Span> {
251 self.host
252 }
253
254 pub fn try_upgrade(
255 mut self,
256 value: &[u8],
257 version: line::Version,
258 ) -> Result<Self, switch::UpgradeError> {
259 if version == line::Version::Ten {
260 return Ok(self);
261 }
262 let next = self.upgrades.try_push(value)?;
263 self.upgrades = next;
264 Ok(self)
265 }
266}