Skip to main content

ReadRequest

Struct ReadRequest 

Source
pub struct ReadRequest<'buf, const MAX_HDRS: usize = DEFAULT_MAX_HEADERS> { /* private fields */ }
Expand description

Result of Request::read: the parsed request plus metadata about how many bytes were consumed from the reader.

Body bytes that arrived in the same read as the headers are stored internally — pass them (along with the original reader) to stream_body_to to write the decoded body to any Write destination.

Implementations§

Source§

impl<'buf, const MAX_HDRS: usize> ReadRequest<'buf, MAX_HDRS>

Source

pub const fn request(&self) -> &Request<'buf, MAX_HDRS>

The parsed HTTP request.

Source

pub const fn prefetch(&self) -> &'buf [u8]

Body bytes already in the header buffer (from the read overshoot).

When using stream_body_to, these bytes are consumed automatically. This accessor is useful for manual body handling or inspection.

Source

pub const fn body_offset(&self) -> usize

Byte offset in the buffer where the request body begins (immediately after the \r\n\r\n header terminator).

Source

pub const fn bytes_read(&self) -> usize

Total number of bytes read into the buffer.

Source

pub fn stream_body_to( &self, reader: &mut impl BufRead, writer: &mut impl Write, ) -> Result<u64, Error>

Stream the request body from reader to writer.

Uses DEFAULT_MAX_BODY_SIZE (8 MiB) as the body size limit. Use stream_body_to_limited to specify a custom limit.

Writes any body bytes already in the header buffer (the prefetch), then continues reading from reader and writing decoded body data to writer. For chunked Transfer-Encoding, chunk framing is stripped on the fly — only decoded data reaches the writer.

For best performance, wrap unbuffered readers (e.g. raw TcpStream) in a BufReader before calling this method.

Returns the number of decoded body bytes written.

§Errors

Returns Error if the body is malformed, exceeds the default limit, or the connection closes prematurely.

§Examples
use xocomil::request::Request;

let raw = b"POST / HTTP/1.1\r\nHost: h\r\nContent-Length: 5\r\n\r\nhello";
let mut buf = [0u8; 1024];
let rr = Request::<32>::read(&mut &raw[..], &mut buf).unwrap();

let mut body = Vec::new();
let n = rr.stream_body_to(&mut &b""[..], &mut body).unwrap();
assert_eq!(body, b"hello");
assert_eq!(n, 5);
Source

pub fn stream_body_to_limited( &self, reader: &mut impl BufRead, writer: &mut impl Write, max_body_size: u64, ) -> Result<u64, Error>

Stream the request body with an explicit size limit.

Like stream_body_to but with a caller-specified maximum body size. Pass UNLIMITED_BODY_SIZE to disable the limit.

§Errors

Returns Error::Body if the body exceeds max_body_size.

Source

pub fn stream_body_to_with<const CHUNK_LINE_BUF: usize, const MAX_BODY_SIZE: u64>( &self, reader: &mut impl BufRead, writer: &mut impl Write, ) -> Result<u64, Error>

Stream the request body with compile-time configuration.

Like stream_body_to but all tunables are const generics:

  • CHUNK_LINE_BUF — stack buffer size for chunk size lines
  • MAX_BODY_SIZE — maximum decoded body bytes

See stream_body_with for details.

§Errors

Returns Error if the body is malformed, exceeds MAX_BODY_SIZE, or the connection closes prematurely.

Trait Implementations§

Source§

impl<'buf, const MAX_HDRS: usize> Debug for ReadRequest<'buf, MAX_HDRS>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'buf, const MAX_HDRS: usize> Freeze for ReadRequest<'buf, MAX_HDRS>

§

impl<'buf, const MAX_HDRS: usize> RefUnwindSafe for ReadRequest<'buf, MAX_HDRS>

§

impl<'buf, const MAX_HDRS: usize> Send for ReadRequest<'buf, MAX_HDRS>

§

impl<'buf, const MAX_HDRS: usize> Sync for ReadRequest<'buf, MAX_HDRS>

§

impl<'buf, const MAX_HDRS: usize> Unpin for ReadRequest<'buf, MAX_HDRS>

§

impl<'buf, const MAX_HDRS: usize> UnsafeUnpin for ReadRequest<'buf, MAX_HDRS>

§

impl<'buf, const MAX_HDRS: usize> UnwindSafe for ReadRequest<'buf, MAX_HDRS>

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