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