Skip to main content

net_wire/http3/frame/
headers.rs

1//! Typed HTTP/3 HEADERS frame payload view and builder.
2
3use super::envelope::{Http3Frame, Http3FrameBuildError, Http3FrameBuilder};
4use super::payload::{Http3FramePayloadParseError, validate_type};
5use crate::http3::codepoints::Http3FrameType;
6
7/// A validated borrowed HTTP/3 HEADERS frame.
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub struct Http3Headers<'a> {
10    frame: Http3Frame<'a>,
11}
12
13impl<'a> Http3Headers<'a> {
14    /// Assembles a HEADERS view from a builder-validated frame.
15    pub(in crate::http3) const fn from_validated(frame: Http3Frame<'a>) -> Self {
16        Self { frame }
17    }
18
19    /// Parses the first complete HEADERS frame, enforcing the caller-provided payload maximum.
20    pub fn parse(
21        bytes: &'a [u8],
22        maximum_payload: usize,
23    ) -> Result<Self, Http3FramePayloadParseError> {
24        Self::from_frame(
25            Http3Frame::parse(bytes, maximum_payload)
26                .map_err(Http3FramePayloadParseError::Frame)?,
27        )
28    }
29
30    /// Validates a raw HEADERS frame's intrinsic layout without decoding QPACK.
31    pub fn from_frame(frame: Http3Frame<'a>) -> Result<Self, Http3FramePayloadParseError> {
32        validate_type(frame, Http3FrameType::HEADERS)?;
33        Ok(Self { frame })
34    }
35
36    /// Returns the validated raw frame.
37    pub const fn frame(&self) -> Http3Frame<'a> {
38        self.frame
39    }
40
41    /// Returns the exact represented frame bytes.
42    pub const fn as_bytes(&self) -> &'a [u8] {
43        self.frame.as_bytes()
44    }
45
46    /// Returns the complete opaque QPACK-encoded field section.
47    pub fn encoded_field_section(&self) -> &'a [u8] {
48        self.frame.payload()
49    }
50}
51
52/// Builds a typed HTTP/3 HEADERS frame in caller-owned storage.
53pub struct Http3HeadersBuilder<'output, 'section> {
54    destination: &'output mut [u8],
55    encoded_field_section: &'section [u8],
56}
57
58impl<'output, 'section> Http3HeadersBuilder<'output, 'section> {
59    /// Creates a HEADERS builder for an opaque QPACK-encoded field section.
60    pub fn new(destination: &'output mut [u8], encoded_field_section: &'section [u8]) -> Self {
61        Self {
62            destination,
63            encoded_field_section,
64        }
65    }
66
67    /// Canonically encodes and atomically writes the HEADERS frame.
68    pub fn build(self) -> Result<Http3Headers<'output>, Http3FrameBuildError> {
69        let frame = Http3FrameBuilder::new(
70            self.destination,
71            Http3FrameType::HEADERS,
72            self.encoded_field_section,
73        )
74        .build()?;
75        Ok(Http3Headers::from_validated(frame))
76    }
77}