sark_core/http/response/
fixed.rs1use http::StatusCode;
2use o3::buffer::{Owned, Shared};
3
4use super::wire_emit::{ContentLength, DATE_LEN, HeadWrite, Out, PLACEHOLDER_DATE};
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: Option<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 if let Some(off) = date_offset {
66 out[off..off + DATE_LEN].copy_from_slice(date);
67 }
68 Some(total)
69 }
70
71 fn head_write(&self) -> (HeadWrite<'_, HeadInner<'req>, ContentLength<'_>>, &[u8]) {
72 let head = HeadWrite {
73 status_str: self.status.as_str().as_bytes(),
74 reason: self
75 .status
76 .canonical_reason()
77 .map(str::as_bytes)
78 .unwrap_or(b""),
79 headers: &self.head,
80 framing: ContentLength(self.body_len_ascii.as_bytes()),
81 };
82 (head, self.body.as_ref())
83 }
84
85 pub fn preserialize(&self) -> (Vec<u8>, usize) {
86 let (head, body) = self.head_write();
87 let mut buf = vec![0u8; head.wire_len() + body.len()];
88 let mut off = 0usize;
89 let date_offset = head.write(&mut buf, &mut off, PLACEHOLDER_DATE);
90 Out::put(&mut buf, &mut off, body);
91 (buf, date_offset)
92 }
93
94 pub fn write_into_slice(&self, out: &mut [u8], date: &[u8; 29]) -> Option<usize> {
95 let (head, body) = self.head_write();
96 if out.len() < head.wire_len() + body.len() {
97 return None;
98 }
99 let mut off = 0usize;
100 head.write(out, &mut off, date);
101 Out::put(out, &mut off, body);
102 Some(off)
103 }
104
105 pub fn write_head_split(self, out: &mut [u8], date: &[u8; 29]) -> Option<(usize, Shared)> {
106 let (head, _) = self.head_write();
107 if out.len() < head.wire_len() {
108 return None;
109 }
110 let mut off = 0usize;
111 head.write(out, &mut off, date);
112 Some((off, self.body))
113 }
114
115 pub fn apply_gzip(&mut self, compressed: Shared) {
116 const GZIP_EXTRA: &[u8] = b"Content-Encoding: gzip\r\nVary: Accept-Encoding\r\n";
117 self.body_len_ascii = InlineHeaderValue::from_decimal(compressed.len());
118 self.body = compressed;
119 self.head.set_extra_static(GZIP_EXTRA);
120 }
121}