sark_http/payload/response/
mod.rs1use o3::buffer::write::{Sink, consume};
4
5use crate::payload::response::status::Code;
6
7mod body;
8pub mod bounded;
9pub mod declared;
10pub mod dispatch;
11pub mod encoded;
12pub mod exact;
13pub mod headers;
14pub mod output;
15pub mod status;
16
17pub use body::Body;
18
19pub type Fixed<H, B> = output::Output<H, B, output::Fixed>;
20pub type Static<'body, H> = output::Output<H, crate::payload::fixed::Body<'body>, output::Static>;
21pub type SizedStatic<'body, H, const N: usize> = output::Output<H, &'body [u8; N], output::Static>;
23pub type Exact<H, B> = output::Output<H, B, output::Encoded<true>>;
24pub type Bounded<H, B> = output::Output<H, B, output::Encoded<false>>;
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28#[repr(u8)]
29pub enum Method {
30 Other,
31 Head,
32 Connect,
33}
34
35impl Method {
36 pub fn from_bytes(method: &[u8]) -> Self {
37 match method.len() {
38 4 if method == b"HEAD" => Self::Head,
39 7 if method == b"CONNECT" => Self::Connect,
40 0..=3 | 5..=6 | 8.. => Self::Other,
41 4 | 7 => Self::Other,
42 }
43 }
44}
45
46#[derive(Clone, Copy, Debug, PartialEq, Eq)]
48pub enum Content {
49 Forbidden,
51 Suppressed,
53 Tunnel,
55 Empty,
57 Allowed,
58}
59
60impl Content {
61 pub const fn new(method: Method, status: u16) -> Self {
62 if matches!(method, Method::Connect) && status >= 200 && status < 300 {
63 Self::Tunnel
64 } else if status < 200 || status == 204 {
65 Self::Forbidden
66 } else if matches!(method, Method::Head) || status == 304 {
67 Self::Suppressed
68 } else if status == 205 {
69 Self::Empty
70 } else {
71 Self::Allowed
72 }
73 }
74}
75
76pub trait Encode: Sized {
78 fn encode<W>(self, out: &mut W) -> Result<(), consume::Error>
79 where
80 W: Sink<Error = consume::Error>;
81}
82
83use std::borrow::Cow;
84
85use headers::{List, Name};
86
87pub struct Response<'field, B = [u8; 0], T = (), S = Code, N = Name<Cow<'field, str>>> {
88 pub(crate) status: S,
89 pub(crate) headers: List<'field, N>,
90 pub(crate) trailers: T,
91 pub(crate) body: B,
92}
93
94impl<'field, B, T, S, N> Response<'field, B, T, S, N> {
95 pub fn status(&self) -> S
96 where
97 S: Copy,
98 {
99 self.status
100 }
101
102 pub fn headers(&self) -> &List<'field, N> {
103 &self.headers
104 }
105
106 pub fn body(&self) -> &B {
107 &self.body
108 }
109
110 pub fn body_mut(&mut self) -> &mut B {
111 &mut self.body
112 }
113
114 pub fn into_body(self) -> B {
115 self.body
116 }
117
118 pub fn with_body<U>(self, body: U) -> Response<'field, U, T, S, N> {
119 Response {
120 status: self.status,
121 headers: self.headers,
122 trailers: self.trailers,
123 body,
124 }
125 }
126}
127
128impl<'field, B, S, N> Response<'field, B, List<'field>, S, N> {
129 pub fn trailers(&self) -> &List<'field> {
130 &self.trailers
131 }
132}
133
134impl<'field> Response<'field, [u8; 0]> {
135 pub fn new(status: Code) -> Self {
136 Self {
137 status,
138 headers: List::new(),
139 trailers: (),
140 body: [],
141 }
142 }
143}
144
145impl<'field, 'req, 'd, D, B> dispatch::Response<'req, 'd, D> for Response<'field, B, (), Code>
146where
147 B: Body<'d>,
148{
149 type Kind = dispatch::Sync;
150 type Stream = dispatch::Never;
151 type Bound = o3::buffer::resident::bound::Unbounded;
152
153 const BODY: crate::payload::Kind = crate::payload::Kind::Inline;
154
155 fn consume<W: dispatch::Transaction<'req, 'd, Stream = dispatch::Never>>(
156 self,
157 writer: W,
158 ) -> W::Done {
159 let Self {
160 status,
161 headers,
162 trailers: (),
163 body,
164 } = self;
165 writer.fixed(status, headers, body)
166 }
167}
168
169const _: () = assert!(
170 core::mem::size_of::<Response<'static, [u8; 0]>>()
171 < core::mem::size_of::<Response<'static, [u8; 0], List<'static>, status::Code>>()
172);