pub struct HeadersParser<'a> { /* private fields */ }Expand description
Parses headers and extracts Content-Length / Transfer-Encoding.
This is a zero-copy parser that yields headers while tracking the body length indicator.
§Example
ⓘ
let buffer = b"Content-Length: 42\r\nHost: example.com\r\n\r\n";
let headers = HeadersParser::parse(buffer)?;
assert_eq!(headers.body_length(), BodyLength::ContentLength(42));
assert_eq!(headers.content_length(), Some(42));Implementations§
Source§impl<'a> HeadersParser<'a>
impl<'a> HeadersParser<'a>
Sourcepub fn parse(buffer: &'a [u8]) -> Result<Self, ParseError>
pub fn parse(buffer: &'a [u8]) -> Result<Self, ParseError>
Parse all headers from a buffer.
Returns the parser with pre-computed Content-Length and Transfer-Encoding. The buffer should start at the first header line (after request line).
Sourcepub fn parse_with_limits(
buffer: &'a [u8],
limits: &ParseLimits,
) -> Result<Self, ParseError>
pub fn parse_with_limits( buffer: &'a [u8], limits: &ParseLimits, ) -> Result<Self, ParseError>
Parse all headers from a buffer with limits.
Enforces header count, line length, and total size limits. Also rejects ambiguous body length indicators (Transfer-Encoding + Content-Length).
Sourcepub fn body_length(&self) -> BodyLength
pub fn body_length(&self) -> BodyLength
Returns the body length indicator.
Per RFC 7230:
- If Transfer-Encoding: chunked is present, use chunked decoding
- Else if Content-Length is present, use that
- Else no body
Sourcepub fn content_length(&self) -> Option<usize>
pub fn content_length(&self) -> Option<usize>
Returns the Content-Length value if present.
Sourcepub fn is_chunked(&self) -> bool
pub fn is_chunked(&self) -> bool
Returns true if Transfer-Encoding: chunked.
Sourcepub fn bytes_consumed(&self) -> usize
pub fn bytes_consumed(&self) -> usize
Returns the number of bytes consumed (including final \r\n\r\n).
Sourcepub fn iter(&self) -> HeadersIter<'a> ⓘ
pub fn iter(&self) -> HeadersIter<'a> ⓘ
Returns an iterator over all headers.
Auto Trait Implementations§
impl<'a> Freeze for HeadersParser<'a>
impl<'a> RefUnwindSafe for HeadersParser<'a>
impl<'a> Send for HeadersParser<'a>
impl<'a> Sync for HeadersParser<'a>
impl<'a> Unpin for HeadersParser<'a>
impl<'a> UnwindSafe for HeadersParser<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).