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>
impl<'buf, const MAX_HDRS: usize> ReadRequest<'buf, MAX_HDRS>
Sourcepub const fn prefetch(&self) -> &'buf [u8] ⓘ
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.
Sourcepub const fn body_offset(&self) -> usize
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).
Sourcepub const fn bytes_read(&self) -> usize
pub const fn bytes_read(&self) -> usize
Total number of bytes read into the buffer.
Sourcepub fn stream_body_to(
&self,
reader: &mut impl BufRead,
writer: &mut impl Write,
) -> Result<u64, Error>
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);Sourcepub fn stream_body_to_limited(
&self,
reader: &mut impl BufRead,
writer: &mut impl Write,
max_body_size: u64,
) -> Result<u64, Error>
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.
Sourcepub 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>
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 linesMAX_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.