Skip to main content

ResponseReader

Struct ResponseReader 

Source
pub struct ResponseReader { /* private fields */ }
Expand description

Sans-IO HTTP/1.x response parser.

§Usage

use nexus_web::http::ResponseReader;

let mut reader = ResponseReader::new(4096);
reader.read(b"HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\n\r\n").unwrap();
let resp = reader.next().unwrap().unwrap();
assert_eq!(resp.status, 101);
assert_eq!(resp.header("Upgrade"), Some("websocket"));

Implementations§

Source§

impl ResponseReader

Source

pub fn new(capacity: usize) -> Self

Create with the given buffer capacity.

Source

pub fn max_headers(self, n: usize) -> Self

Set maximum number of headers. Default: 64.

Source

pub fn max_head_size(self, n: usize) -> Self

Set maximum head size. Default: 8KB.

Source

pub fn max_body_size(self, n: usize) -> Self

Set maximum response body size. Default: 0 (no limit).

When set, responses with Content-Length exceeding this value will be rejected during validation.

Source

pub fn max_body_size_limit(&self) -> usize

Configured maximum body size (0 = no limit).

Source

pub fn read(&mut self, src: &[u8]) -> Result<(), HttpError>

Buffer wire bytes.

Source

pub fn spare(&mut self) -> &mut [u8]

Writable region for direct in-buffer writes. Pair with filled() to commit bytes after the write. Used by crate::WireStream to deliver bytes from a transport without a slice intermediate.

Source

pub fn filled(&mut self, n: usize)

Commit n bytes written into spare().

Source

pub fn read_from<R: Read>(&mut self, src: &mut R) -> Result<usize>

Read bytes from a source directly into the internal buffer.

Returns bytes read, or 0 on EOF.

Source

pub fn body_remaining(&self) -> usize

Bytes of data buffered beyond the parsed headers (body bytes). Available after next() returns Some.

Source

pub fn header(&self, name: &str) -> Option<&str>

Look up a parsed response header by name (case-insensitive).

Returns None if headers haven’t been parsed yet or the header is not found. Only valid after next() returns Some.

Source

pub fn next(&mut self) -> Result<Option<Response<'_>>, HttpError>

Parse the next response.

Source

pub fn remainder(&self) -> &[u8]

Bytes after the parsed head.

Source

pub fn status(&self) -> u16

HTTP status code from parsed headers.

Source

pub fn header_count(&self) -> usize

Number of parsed headers.

Source

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

Cached Content-Length from parsed headers. None = header absent, Some(Ok(n)) = valid, Some(Err(())) = present but malformed.

Source

pub fn is_chunked(&self) -> bool

Whether Transfer-Encoding includes “chunked” (cached from parse).

Source

pub fn set_body_consumed(&mut self, raw_bytes: usize)

Set the raw wire bytes consumed for the response body.

For Content-Length responses: equals Content-Length. For chunked responses: includes chunk framing overhead. For bodyless (1xx/204/304): 0.

Must be called before consume_response().

Source

pub fn consume_response(&mut self)

Advance past a consumed response, preserving any pipelined bytes.

Uses last_raw_body_bytes (set via set_body_consumed) to determine how many wire bytes to skip. Call before parsing the next response on a keep-alive connection.

Source

pub fn reset(&mut self)

Reset for a new response. Discards all buffered data.

Trait Implementations§

Source§

impl ParserSink for ResponseReader

Lets a WireStream feed bytes directly into the ResponseReader’s spare region — one fewer copy than going through a slice intermediary.

Source§

fn spare(&mut self) -> &mut [u8]

Writable region where new bytes go.
Source§

fn filled(&mut self, n: usize)

Commit n bytes written into spare(). Read more

Auto Trait Implementations§

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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V