Skip to main content

sark_http/h1/codec/decode/response/
head.rs

1use std::num::NonZero;
2
3use o3::alloc::{self, BoxSliceExt as _};
4
5use crate::{
6    h1::{framing, head::response},
7    payload::response::{Response, headers::List, status},
8};
9
10use super::Collected;
11
12pub struct Head<F> {
13    pub version: u8,
14    pub status: status::Code,
15    pub fields: F,
16    pub header_len: NonZero<u32>,
17    pub payload: framing::ResponseBody,
18    pub control: framing::Control,
19}
20
21impl<F> Head<F> {
22    pub(super) fn from(head: response::Admitted, fields: F) -> Self {
23        Self {
24            version: head.version(),
25            status: head.status(),
26            fields,
27            header_len: head.wire_len(),
28            payload: head.body(),
29            control: head.control(),
30        }
31    }
32}
33
34impl<'field> Head<List<'field>> {
35    pub(super) fn try_copy(self, body: &[u8]) -> Result<Collected<'field>, alloc::Exhausted> {
36        Ok(self.complete(Box::try_copy(body)?.into_vec(), List::new()))
37    }
38
39    pub(super) fn complete<B>(self, body: B, trailers: List<'field>) -> Collected<'field, B> {
40        Response {
41            status: self.status,
42            headers: self.fields,
43            trailers,
44            body,
45        }
46    }
47}