Skip to main content

sark_core/http/response/direct/
plan.rs

1use http::StatusCode;
2use o3::buffer::{Borrowed, Bytes, Retained, Shared};
3
4use super::super::{Body, HotHeadInner, MonoResponseInner};
5use super::head::HeadInner;
6use super::headers::{HeaderNameToken, HeaderStaticValueToken, Headers};
7use super::value::InlineHeaderValue;
8
9#[derive(Clone, Debug)]
10pub struct ResponsePlan<'req> {
11    pub(in crate::http::response) status: StatusCode,
12    pub(in crate::http::response) head: HeadInner<'req>,
13}
14
15impl<'req> ResponsePlan<'req> {
16    pub fn with_capacity(status: StatusCode) -> Self {
17        Self {
18            status,
19            head: HeadInner::new(b"", Headers::new()),
20        }
21    }
22
23    pub fn from_static(status: StatusCode, static_headers: &'static [u8]) -> Self {
24        Self {
25            status,
26            head: HeadInner::new(static_headers, Headers::new()),
27        }
28    }
29
30    pub fn new(status: StatusCode) -> Self {
31        Self::with_capacity(status)
32    }
33
34    pub fn ok() -> Self {
35        Self::new(StatusCode::OK)
36    }
37
38    pub fn not_found() -> Self {
39        Self::new(StatusCode::NOT_FOUND)
40    }
41
42    pub fn status(&self) -> StatusCode {
43        self.status
44    }
45
46    pub fn wire_headers(&self) -> Shared {
47        self.head.wire_headers()
48    }
49
50    pub fn push_static(&mut self, name: &'static str, value: &'static str) -> &mut Self {
51        let name = HeaderNameToken::new(name);
52        let value = HeaderStaticValueToken::new(value);
53        self.head.headers_mut().push_static(name, value);
54        self
55    }
56
57    pub fn push(&mut self, name: &'static str, value: &str) -> &mut Self {
58        self.push_str_value(HeaderNameToken::new(name), value)
59    }
60
61    pub fn push_token_static(
62        &mut self,
63        name: HeaderNameToken,
64        value: HeaderStaticValueToken,
65    ) -> &mut Self {
66        self.head.headers_mut().push_static(name, value);
67        self
68    }
69
70    pub fn push_token(&mut self, name: HeaderNameToken, value: &str) -> &mut Self {
71        self.push_str_value(name, value)
72    }
73
74    fn push_str_value(&mut self, name: HeaderNameToken, value: &str) -> &mut Self {
75        HeaderStaticValueToken::validate(value);
76        if value.len() <= 31 {
77            self.head
78                .headers_mut()
79                .push_inline(name, InlineHeaderValue::new(value.as_bytes()));
80        } else {
81            self.head
82                .headers_mut()
83                .push_shared(name, Shared::copy_from_slice(value.as_bytes()));
84        }
85        self
86    }
87
88    pub fn push_borrowed_token(
89        &mut self,
90        name: HeaderNameToken,
91        value: Bytes<Borrowed<'req>>,
92    ) -> &mut Self {
93        HeaderStaticValueToken::validate_bytes(value.as_slice());
94        self.head.headers_mut().push_borrowed(name, value);
95        self
96    }
97
98    pub fn push_retained_token(
99        &mut self,
100        name: HeaderNameToken,
101        value: Bytes<Retained>,
102    ) -> &mut Self {
103        HeaderStaticValueToken::validate_bytes(value.as_slice());
104        self.head.headers_mut().push_retained(name, value);
105        self
106    }
107
108    pub fn respond_mono<B>(self, body: B) -> MonoResponseInner<'req>
109    where
110        B: Into<Body<'req>>,
111    {
112        let status = self.status;
113        MonoResponseInner {
114            status,
115            headers: None,
116            head: HotHeadInner::Direct(self.head),
117            body: body.into(),
118        }
119    }
120}