Skip to main content

HeadersParser

Struct HeadersParser 

Source
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>

Source

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).

Source

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).

Source

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
Source

pub fn content_length(&self) -> Option<usize>

Returns the Content-Length value if present.

Source

pub fn is_chunked(&self) -> bool

Returns true if Transfer-Encoding: chunked.

Source

pub fn bytes_consumed(&self) -> usize

Returns the number of bytes consumed (including final \r\n\r\n).

Source

pub fn iter(&self) -> HeadersIter<'a>

Returns an iterator over all headers.

Source

pub fn get(&self, name: &str) -> Option<Header<'a>>

Finds a header by name (case-insensitive).

Source

pub fn get_all<'b>( &'b self, name: &'b str, ) -> impl Iterator<Item = Header<'a>> + 'b

Returns all headers matching a name (case-insensitive).

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ResponseProduces<T> for T