pub struct RequestBody<'a>(/* private fields */);Expand description
A received request body
This type represents a body that will be read from the underlying transport
let app = TestServer::new(|mut conn: trillium::Conn| async move {
let body = conn.request_body(); // 100-continue sent lazily on first read if needed
let Ok(body_string) = body.with_max_len(1024).read_string().await else {
return conn.with_status(500);
};
conn.with_body(format!("received: {body_string}"))
})
.await;
app.get("/").await.assert_body("received: ");
app.post("/")
.with_body("hello")
.await
.assert_body("received: hello");§Bounds checking
Every RequestBody has a maximum length beyond which it will return an error, expressed as a
u64. To override this on the specific RequestBody, use RequestBody::with_max_len or
RequestBody::set_max_len
Implementations§
Source§impl RequestBody<'_>
impl RequestBody<'_>
Sourcepub async fn read_bytes(self) -> Result<Vec<u8>, Error>
pub async fn read_bytes(self) -> Result<Vec<u8>, Error>
Similar to RequestBody::read_string, but returns the raw bytes. This is useful for
bodies that are not text.
You can use this in conjunction with encoding if you need different handling of malformed
character encoding than the lossy conversion provided by RequestBody::read_string.
An empty or nonexistent body will yield an empty Vec, not an error.
§Errors
This will return an error if there is an IO error on the underlying transport such as a disconnect
This will also return an error if the length exceeds the maximum length. To configure the
value on this specific request body, use RequestBody::with_max_len or
RequestBody::set_max_len
Sourcepub async fn read_string(self) -> Result<String, Error>
pub async fn read_string(self) -> Result<String, Error>
§Reads the entire body to String.
This uses the encoding determined by the content-type (mime) charset. If an encoding problem
is encountered, the String returned by RequestBody::read_string will contain utf8
replacement characters.
Note that this can only be performed once per Conn, as the underlying data is not cached anywhere. This is the only copy of the body contents.
An empty or nonexistent body will yield an empty String, not an error
§Errors
This will return an error if there is an IO error on the underlying transport such as a disconnect
This will also return an error if the length exceeds the maximum length. To configure the
value on this specific request body, use RequestBody::with_max_len or
RequestBody::set_max_len.
Sourcepub fn with_max_len(self, max_len: u64) -> Self
pub fn with_max_len(self, max_len: u64) -> Self
Set the maximum content length to read, returning self
This protects against an memory-use denial-of-service attack wherein an untrusted peer sends
an unbounded request body. This is especially important when using
RequestBody::read_string and RequestBody::read_bytes instead of streaming with
AsyncRead.
The default value can be found documented in the trillium-http crate
Sourcepub fn set_max_len(&mut self, max_len: u64) -> &mut Self
pub fn set_max_len(&mut self, max_len: u64) -> &mut Self
Set the maximum content length to read
This protects against an memory-use denial-of-service attack wherein an untrusted peer sends
an unbounded request body. This is especially important when using
RequestBody::read_string and RequestBody::read_bytes instead of streaming with
AsyncRead.
The default value can be found documented in the trillium-http crate
Sourcepub fn content_length(&self) -> Option<u64>
pub fn content_length(&self) -> Option<u64>
The content-length of this body, if available.
This value usually is derived from the content-length header. If the request that this body is attached to uses transfer-encoding chunked, this will be None.
Trait Implementations§
Source§impl<'a> AsyncRead for RequestBody<'a>where
Self: Unpin,
impl<'a> AsyncRead for RequestBody<'a>where
Self: Unpin,
Source§impl<'a> Debug for RequestBody<'a>
impl<'a> Debug for RequestBody<'a>
Source§impl<'a> From<RequestBody<'a>> for ReceivedBody<'a, Box<dyn Transport>>
impl<'a> From<RequestBody<'a>> for ReceivedBody<'a, Box<dyn Transport>>
Source§fn from(value: RequestBody<'a>) -> Self
fn from(value: RequestBody<'a>) -> Self
Auto Trait Implementations§
impl<'a> Freeze for RequestBody<'a>
impl<'a> !RefUnwindSafe for RequestBody<'a>
impl<'a> Send for RequestBody<'a>
impl<'a> Sync for RequestBody<'a>
impl<'a> Unpin for RequestBody<'a>
impl<'a> UnsafeUnpin for RequestBody<'a>
impl<'a> !UnwindSafe for RequestBody<'a>
Blanket Implementations§
Source§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
Source§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
Source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf. Read moreSource§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit bytes from it. Read more