1use o3::buffer::{
4 resident::bound::{AtMost, Unbounded},
5 segments, storage,
6 write::{Sink, consume},
7};
8
9use std::{borrow::Cow, convert::Infallible, marker::PhantomData};
10
11use crate::{
12 field,
13 payload::{self, fixed},
14};
15use headers::{Application, List, Name};
16use status::Code;
17
18mod body;
19pub mod bounded;
20pub mod declared;
21pub mod dispatch;
22pub mod encoded;
23pub mod exact;
24pub mod headers;
25pub mod status;
26
27pub use body::Body;
28
29pub trait Destination<'place, 'd, S, Ranges: Copy + 'd = ()> {
30 type Place;
31 type Stored;
32
33 fn empty(place: Self::Place) -> Self::Stored;
34 fn owned(place: Self::Place, body: storage::NonEmpty<Vec<u8>>) -> Self::Stored;
35 fn allocated(place: Self::Place, body: storage::NonEmpty<storage::Owned>) -> Self::Stored;
36 fn shared<const COPIED: bool>(
37 place: Self::Place,
38 body: storage::NonEmpty<storage::Shared>,
39 ) -> Self::Stored;
40 fn retained<const INPUT: bool>(
41 place: Self::Place,
42 body: segments::raw::Data<'d, (), Ranges>,
43 ) -> Self::Stored;
44 fn fixed<const DEFERRED: bool>(
45 place: Self::Place,
46 body: storage::NonEmpty<&'d [u8]>,
47 ) -> Self::Stored;
48}
49
50pub struct Output<H, B, F = Fixed> {
51 pub status: Code,
52 pub fields: H,
53 pub body: B,
54 form: PhantomData<fn() -> F>,
55}
56
57impl<H: Copy> Copy for Output<H, fixed::Body<'_>, Static> {}
58impl<H: Copy> Clone for Output<H, fixed::Body<'_>, Static> {
59 fn clone(&self) -> Self {
60 *self
61 }
62}
63
64impl<H, B, F> Output<H, B, F> {
65 pub fn new(status: Code, fields: H, body: B) -> Self {
66 Self {
67 status,
68 fields,
69 body,
70 form: PhantomData,
71 }
72 }
73
74 pub fn status(&self) -> Code {
75 self.status
76 }
77
78 pub fn body(&self) -> &B {
79 &self.body
80 }
81}
82
83pub enum Fixed {}
84pub enum Static {}
85pub struct Encoded<const EXACT: bool>;
86
87impl<'req, 'd, Ranges: Copy + 'd, D, H: field::Source<Application>, B: Body<'d, Ranges>>
88 dispatch::Emit<'req, 'd, D, Ranges> for Output<H, B, Fixed>
89{
90 type Kind = dispatch::Sync;
91 type Stream = dispatch::Never;
92 type Bound = Unbounded;
93
94 const BODY: payload::Kind = payload::Kind::Inline;
95
96 fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
97 self,
98 writer: W,
99 ) -> W::Done {
100 writer.fixed::<Code, _, _>(self.status, self.fields, self.body)
101 }
102}
103
104impl<'body: 'd, 'req, 'd, Ranges: Copy + 'd, D, H: field::Source<Application>>
105 dispatch::Emit<'req, 'd, D, Ranges> for Output<H, fixed::Body<'body>, Static>
106{
107 type Kind = dispatch::Sync;
108 type Stream = dispatch::Never;
109 type Bound = AtMost<0>;
110
111 const BODY: payload::Kind = payload::Kind::Static;
112
113 fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
114 self,
115 writer: W,
116 ) -> W::Done {
117 writer.statics::<Code, _>(self.status, self.fields, self.body)
118 }
119}
120
121impl<'body: 'd, 'req, 'd, Ranges: Copy + 'd, D, H: field::Source<Application>, const N: usize>
123 dispatch::Emit<'req, 'd, D, Ranges> for Output<H, &'body [u8; N], Static>
124{
125 type Kind = dispatch::Sync;
126 type Stream = dispatch::Never;
127 type Bound = AtMost<0>;
128
129 const BODY: payload::Kind = payload::Kind::Static;
130
131 fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
132 self,
133 writer: W,
134 ) -> W::Done {
135 writer.sized_static::<Code, _, N>(self.status, self.fields, self.body)
136 }
137}
138
139impl<
140 'req,
141 'd,
142 Ranges: Copy + 'd,
143 D,
144 H: field::Source<Application>,
145 B: encoded::Source<EXACT>,
146 const EXACT: bool,
147> dispatch::Emit<'req, 'd, D, Ranges> for Output<H, B, Encoded<EXACT>>
148{
149 type Kind = dispatch::Sync;
150 type Stream = dispatch::Never;
151 type Bound = Unbounded;
152
153 const BODY: payload::Kind = payload::Kind::Inline;
154
155 fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
156 self,
157 writer: W,
158 ) -> W::Done {
159 writer.encoded::<EXACT, Code>(self.status, self.fields, self.body)
160 }
161}
162
163#[derive(Clone, Copy, Debug, PartialEq, Eq)]
165#[repr(u8)]
166pub enum Method {
167 Other,
168 Head,
169 Connect,
170}
171
172impl Method {
173 pub fn from_bytes(method: &[u8]) -> Self {
174 match method.len() {
175 4 if method == b"HEAD" => Self::Head,
176 7 if method == b"CONNECT" => Self::Connect,
177 0..=3 | 5..=6 | 8.. => Self::Other,
178 4 | 7 => Self::Other,
179 }
180 }
181}
182
183#[derive(Clone, Copy, Debug, PartialEq, Eq)]
185pub enum Content {
186 Forbidden,
188 Suppressed,
190 Tunnel,
192 Empty,
194 Allowed,
195}
196
197impl Content {
198 pub const fn new(method: Method, status: u16) -> Self {
199 if matches!(method, Method::Connect) && status >= 200 && status < 300 {
200 Self::Tunnel
201 } else if status < 200 || status == 204 {
202 Self::Forbidden
203 } else if matches!(method, Method::Head) || status == 304 {
204 Self::Suppressed
205 } else if status == 205 {
206 Self::Empty
207 } else {
208 Self::Allowed
209 }
210 }
211
212 pub fn length<S: dispatch::Source<Kind>, Kind>(
213 self,
214 status: Code,
215 source: Option<&S>,
216 ) -> Option<u64> {
217 if S::remaining(&mut S::Count::default()).is_some()
218 && (self == Self::Allowed || self == Self::Suppressed && status != Code::RESET_CONTENT)
219 {
220 Some(source.map_or(0, S::length))
221 } else {
222 None
223 }
224 }
225}
226
227pub trait Encode: Sized {
229 const MAX_LEN: usize = usize::MAX;
231
232 fn encode<W>(self, out: &mut W) -> Result<(), consume::Error>
233 where
234 W: Sink<Error = consume::Error>;
235}
236
237impl Encode for Infallible {
238 fn encode<W: Sink<Error = consume::Error>>(self, _: &mut W) -> Result<(), consume::Error> {
239 match self {}
240 }
241}
242
243pub struct Response<'field, B = [u8; 0], T = (), S = Code, N = Name<Cow<'field, str>>> {
244 pub(crate) status: S,
245 pub(crate) headers: List<'field, N>,
246 pub(crate) trailers: T,
247 pub(crate) body: B,
248}
249
250impl<'field, B, T, S, N> Response<'field, B, T, S, N> {
251 pub fn status(&self) -> S
252 where
253 S: Copy,
254 {
255 self.status
256 }
257
258 pub fn headers(&self) -> &List<'field, N> {
259 &self.headers
260 }
261
262 pub fn body(&self) -> &B {
263 &self.body
264 }
265
266 pub fn body_mut(&mut self) -> &mut B {
267 &mut self.body
268 }
269
270 pub fn into_body(self) -> B {
271 self.body
272 }
273
274 pub fn with_body<U>(self, body: U) -> Response<'field, U, T, S, N> {
275 Response {
276 status: self.status,
277 headers: self.headers,
278 trailers: self.trailers,
279 body,
280 }
281 }
282}
283
284impl<'field, B, S, N> Response<'field, B, List<'field>, S, N> {
285 pub fn trailers(&self) -> &List<'field> {
286 &self.trailers
287 }
288}
289
290impl<'field> Response<'field, [u8; 0]> {
291 pub fn new(status: Code) -> Self {
292 Self {
293 status,
294 headers: List::new(),
295 trailers: (),
296 body: [],
297 }
298 }
299}
300
301impl<'field, 'req, 'd, Ranges: Copy + 'd, D, B> dispatch::Emit<'req, 'd, D, Ranges>
302 for Response<'field, B, (), Code>
303where
304 B: Body<'d, Ranges>,
305{
306 type Kind = dispatch::Sync;
307 type Stream = dispatch::Never;
308 type Bound = Unbounded;
309
310 const BODY: payload::Kind = payload::Kind::Inline;
311
312 fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
313 self,
314 writer: W,
315 ) -> W::Done {
316 let Self {
317 status,
318 headers,
319 trailers: (),
320 body,
321 } = self;
322 writer.fixed::<Code, _, _>(status, headers, body)
323 }
324}
325
326const _: () = assert!(
327 core::mem::size_of::<Response<'static, [u8; 0]>>()
328 < core::mem::size_of::<Response<'static, [u8; 0], List<'static>, status::Code>>()
329);