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