Skip to main content

sark_http/payload/response/headers/
mod.rs

1//! Admitted application response headers and their emission contract.
2
3pub mod list;
4
5pub use list::List;
6
7use std::marker::PhantomData;
8
9use crate::{field, payload::compress::dynamic};
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub enum Error {
13    Managed,
14    Syntax(field::Error),
15}
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum Application {}
19
20pub type Name<T> = field::storage::Lower<T, Application>;
21pub type Entry<'a> = field::Entry<'a, Application>;
22
23/// Immutable fields paired with their exact name/value line encoding.
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25pub struct Group<'a, Scope = Application> {
26    entries: &'a [field::Entry<'a, Scope>],
27    wire: &'a [u8],
28}
29
30/// The first byte where a field sequence and its wire encoding differ.
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub struct Mismatch {
33    pub offset: usize,
34    pub expected: Option<u8>,
35    pub actual: Option<u8>,
36}
37
38struct Cursor<'a> {
39    wire: &'a [u8],
40    offset: usize,
41}
42
43impl<'a, Scope: Copy> Group<'a, Scope> {
44    pub const fn try_new(
45        entries: &'a [field::Entry<'a, Scope>],
46        wire: &'a [u8],
47    ) -> Result<Self, Mismatch> {
48        let mut cursor = Cursor { wire, offset: 0 };
49        let mut index = 0;
50        while index < entries.len() {
51            let entry = entries[index];
52            let parts = [entry.name(), b": ", entry.value().get(), b"\r\n"];
53            let mut part = 0;
54            while part < parts.len() {
55                if let Err(error) = cursor.check(parts[part]) {
56                    return Err(error);
57                }
58                part += 1;
59            }
60            index += 1;
61        }
62        if let Some((&actual, _)) = cursor.wire.split_first() {
63            return Err(Mismatch {
64                offset: cursor.offset,
65                expected: None,
66                actual: Some(actual),
67            });
68        }
69        Ok(Self { entries, wire })
70    }
71
72    pub const fn entries(&self) -> &'a [field::Entry<'a, Scope>] {
73        self.entries
74    }
75
76    pub const fn wire(&self) -> &'a [u8] {
77        self.wire
78    }
79
80    pub const fn section_size(&self) -> Option<usize> {
81        let extra = dynamic::ENTRY_OVERHEAD - b": \r\n".len();
82        let Some(extra) = self.entries.len().checked_mul(extra) else {
83            return None;
84        };
85        self.wire.len().checked_add(extra)
86    }
87}
88
89impl Cursor<'_> {
90    const fn check(&mut self, expected: &[u8]) -> Result<(), Mismatch> {
91        let mut index = 0;
92        while index < expected.len() {
93            let byte = expected[index];
94            let (actual, rest) = match self.wire.split_first() {
95                Some((&actual, rest)) => (Some(actual), rest),
96                None => (None, self.wire),
97            };
98            if !matches!(actual, Some(actual) if actual == byte) {
99                return Err(Mismatch {
100                    offset: self.offset,
101                    expected: Some(byte),
102                    actual,
103                });
104            }
105            self.wire = rest;
106            self.offset += 1;
107            index += 1;
108        }
109        Ok(())
110    }
111}
112
113pub trait Fields: Sized {
114    const FIELDS: Group<'static>;
115
116    fn emit<'field, Out: field::Sink<'field, Application>>(out: Out) -> Result<Out, Out::Error> {
117        out.group(&Self::FIELDS)
118    }
119}
120
121pub struct Line<D, const I: usize>(PhantomData<fn() -> D>);
122
123impl<D: Fields, const I: usize> field::Constant<Application> for Line<D, I> {
124    const ENTRY: Entry<'static> = D::FIELDS.entries()[I];
125}
126
127impl<D: Fields> field::Source<Application> for PhantomData<D> {
128    const SECTION_SIZE: Option<usize> = D::FIELDS.section_size();
129
130    fn emit<'field, Out: field::Sink<'field, Application>>(
131        &'field self,
132        out: Out,
133    ) -> Result<Out, Out::Error> {
134        field::Sink::<Application>::declared::<D>(out)
135    }
136}
137
138impl<Scope: Copy> field::Source<Scope> for Group<'_, Scope> {
139    fn emit<'field, Out: field::Sink<'field, Scope>>(
140        &'field self,
141        sink: Out,
142    ) -> Result<Out, Out::Error> {
143        sink.group(self)
144    }
145}