Skip to main content

sark_core/http/response/
fixed.rs

1use http::StatusCode;
2use o3::buffer::{Owned, Shared};
3
4use super::wire_emit::{ContentLength, HeadWrite, Out};
5use super::{HeadInner, HeadersInner, InlineHeaderValue};
6
7#[derive(Clone, Debug)]
8pub struct FixedResponseInner<'req> {
9    pub(super) status: StatusCode,
10    pub(super) head: HeadInner<'req>,
11    pub(super) body: Shared,
12    pub(super) body_len_ascii: InlineHeaderValue,
13}
14
15pub type FixedResponse = FixedResponseInner<'static>;
16
17impl<'req> FixedResponseInner<'req> {
18    pub fn direct<B>(
19        status: StatusCode,
20        static_headers: &'static [u8],
21        headers: HeadersInner<'req>,
22        body: B,
23    ) -> Self
24    where
25        B: Into<Shared>,
26    {
27        let body = body.into();
28        Self {
29            status,
30            head: HeadInner::new(static_headers, headers),
31            body_len_ascii: InlineHeaderValue::from_decimal(body.len()),
32            body,
33        }
34    }
35
36    pub fn status(&self) -> StatusCode {
37        self.status
38    }
39
40    pub fn body_ref(&self) -> &[u8] {
41        self.body.as_ref()
42    }
43
44    pub fn has_content_encoding(&self) -> bool {
45        self.head.headers().has_content_encoding()
46    }
47
48    pub fn wire_headers(&self) -> Shared {
49        let mut out = Owned::with_capacity(self.head.wire_len());
50        self.head.write_into(&mut out);
51        out.freeze()
52    }
53
54    pub fn write_preserialized(
55        out: &mut [u8],
56        template: &[u8],
57        date_offset: usize,
58        date: &[u8; 29],
59    ) -> Option<usize> {
60        let total = template.len();
61        if out.len() < total {
62            return None;
63        }
64        out[..total].copy_from_slice(template);
65        out[date_offset..date_offset + 29].copy_from_slice(date);
66        Some(total)
67    }
68
69    pub fn preserialize(&self) -> (Vec<u8>, usize) {
70        let dummy_date: &[u8; 29] = b"Mon, 01 Jan 2000 00:00:00 GMT";
71        let mut buf = vec![0u8; 4096];
72        let n = self
73            .write_into_slice(&mut buf, dummy_date)
74            .expect("preserialize: 4096 must be enough for any response");
75        buf.truncate(n);
76        let date_offset = buf
77            .windows(29)
78            .position(|w| w == dummy_date)
79            .expect("preserialize: dummy date must appear in output");
80        (buf, date_offset)
81    }
82
83    pub fn write_into_slice(&self, out: &mut [u8], date: &[u8; 29]) -> Option<usize> {
84        let status_str = self.status.as_str().as_bytes();
85        let reason = self
86            .status
87            .canonical_reason()
88            .map(str::as_bytes)
89            .unwrap_or(b"");
90        let cl_body = self.body_len_ascii.as_bytes();
91        let body = self.body.as_ref();
92
93        let head = HeadWrite {
94            status_str,
95            reason,
96            headers: &self.head,
97            framing: ContentLength(cl_body),
98        };
99        let total = head.wire_len() + body.len();
100        if out.len() < total {
101            return None;
102        }
103
104        let mut off = 0usize;
105        head.write(out, &mut off, date);
106        Out::put(out, &mut off, body);
107        Some(off)
108    }
109
110    pub fn write_head_split(self, out: &mut [u8], date: &[u8; 29]) -> Option<(usize, Shared)> {
111        let status_str = self.status.as_str().as_bytes();
112        let reason = self
113            .status
114            .canonical_reason()
115            .map(str::as_bytes)
116            .unwrap_or(b"");
117        let cl_body = self.body_len_ascii.as_bytes();
118        let head = HeadWrite {
119            status_str,
120            reason,
121            headers: &self.head,
122            framing: ContentLength(cl_body),
123        };
124        if out.len() < head.wire_len() {
125            return None;
126        }
127        let mut off = 0usize;
128        head.write(out, &mut off, date);
129        Some((off, self.body))
130    }
131
132    pub fn apply_gzip(&mut self, compressed: Shared) {
133        const GZIP_EXTRA: &[u8] = b"Content-Encoding: gzip\r\nVary: Accept-Encoding\r\n";
134        self.body_len_ascii = InlineHeaderValue::from_decimal(compressed.len());
135        self.body = compressed;
136        self.head.set_extra_static(GZIP_EXTRA);
137    }
138}