pub struct ReceivedBody<'conn, Transport> { /* private fields */ }Expand description
A received http body
This type represents a body that will be read from the underlying transport, which it may either
borrow from a Conn or own.
let app = HttpTest::new(|mut conn| async move {
let body = conn.request_body();
let body_string = body.read_string().await.unwrap();
conn.with_response_body(format!("received: {body_string}"))
});
app.get("/").block().assert_body("received: ");
app.post("/")
.with_body("hello")
.block()
.assert_body("received: hello");§Bounds checking
Every ReceivedBody has a maximum length beyond which it will return an error, expressed as a
u64. To override this on the specific ReceivedBody, use ReceivedBody::with_max_len or
ReceivedBody::set_max_len
The default maximum length is 10mb; see HttpConfig::received_body_max_len to configure
this server-wide.
§Large chunks, small read buffers
Attempting to read a chunked body with a buffer that is shorter than the chunk size in hex will result in an error.
Implementations§
Source§impl<'conn, Transport> ReceivedBody<'conn, Transport>
impl<'conn, Transport> ReceivedBody<'conn, Transport>
Sourcepub fn content_length(&self) -> Option<u64>
pub fn content_length(&self) -> Option<u64>
Returns a copy of The content-length of this body, if available. This usually is derived from the content-length header. If the http request or response that this body is attached to uses transfer-encoding chunked, this will be None.
HttpTest::new(|mut conn| async move {
let body = conn.request_body();
assert_eq!(body.content_length(), Some(5));
let body_string = body.read_string().await.unwrap();
conn.with_status(200)
.with_response_body(format!("received: {body_string}"))
})
.post("/")
.with_body("hello")
.block()
.assert_ok()
.assert_body("received: hello");Sourcepub fn encoding(&self) -> &'static Encoding
pub fn encoding(&self) -> &'static Encoding
Borrows the character encoding of this body, usually determined from the content type (mime-type) of the associated Conn.
Sourcepub fn max_len(&self) -> u64
pub fn max_len(&self) -> u64
Returns a copy of The maximum length that can be read from this body before error
See also HttpConfig::received_body_max_len
Sourcepub fn set_max_len(&mut self, max_len: u64) -> &mut Self
pub fn set_max_len(&mut self, max_len: u64) -> &mut Self
Sets The maximum length that can be read from this body before error, returning &mut Self for chaining
See also HttpConfig::received_body_max_len
Sourcepub fn with_max_len(self, max_len: u64) -> Self
pub fn with_max_len(self, max_len: u64) -> Self
Owned chainable setter for The maximum length that can be read from this body before error, returning Self
See also HttpConfig::received_body_max_len
Sourcepub fn initial_len(&self) -> usize
pub fn initial_len(&self) -> usize
Returns a copy of The initial buffer capacity allocated when reading the body to bytes or a string
Sourcepub fn set_initial_len(&mut self, initial_len: usize) -> &mut Self
pub fn set_initial_len(&mut self, initial_len: usize) -> &mut Self
Sets The initial buffer capacity allocated when reading the body to bytes or a string, returning &mut Self for chaining
Sourcepub fn with_initial_len(self, initial_len: usize) -> Self
pub fn with_initial_len(self, initial_len: usize) -> Self
Owned chainable setter for The initial buffer capacity allocated when reading the body to bytes or a string, returning Self
Sourcepub fn copy_loops_per_yield(&self) -> usize
pub fn copy_loops_per_yield(&self) -> usize
Returns a copy of The maximum number of read loops that reading this received body will perform before yielding back to the runtime
Sourcepub fn set_copy_loops_per_yield(
&mut self,
copy_loops_per_yield: usize,
) -> &mut Self
pub fn set_copy_loops_per_yield( &mut self, copy_loops_per_yield: usize, ) -> &mut Self
Sets The maximum number of read loops that reading this received body will perform before, returning &mut Self for chaining
yielding back to the runtime
Sourcepub fn with_copy_loops_per_yield(self, copy_loops_per_yield: usize) -> Self
pub fn with_copy_loops_per_yield(self, copy_loops_per_yield: usize) -> Self
Owned chainable setter for The maximum number of read loops that reading this received body will perform before, returning Self
yielding back to the runtime
Sourcepub fn max_preallocate(&self) -> usize
pub fn max_preallocate(&self) -> usize
Returns a copy of Maximum size to pre-allocate based on content-length for buffering this received body
Sourcepub fn set_max_preallocate(&mut self, max_preallocate: usize) -> &mut Self
pub fn set_max_preallocate(&mut self, max_preallocate: usize) -> &mut Self
Sets Maximum size to pre-allocate based on content-length for buffering this received body, returning &mut Self for chaining
Sourcepub fn with_max_preallocate(self, max_preallocate: usize) -> Self
pub fn with_max_preallocate(self, max_preallocate: usize) -> Self
Owned chainable setter for Maximum size to pre-allocate based on content-length for buffering this received body, returning Self
Source§impl<'conn, Transport> ReceivedBody<'conn, Transport>
impl<'conn, Transport> ReceivedBody<'conn, Transport>
Sourcepub async fn read_string(self) -> Result<String>
pub async fn read_string(self) -> Result<String>
§Reads 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 ReceivedBody::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.
§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 override this
value on this specific body, use ReceivedBody::with_max_len or
ReceivedBody::set_max_len
Sourcepub async fn read_bytes(self) -> Result<Vec<u8>>
pub async fn read_bytes(self) -> Result<Vec<u8>>
Similar to ReceivedBody::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 ReceivedBody::read_string.
§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
received_body_max_len. To override this value on
this specific body, use ReceivedBody::with_max_len or ReceivedBody::set_max_len
Sourcepub async fn drain(self) -> Result<u64>
pub async fn drain(self) -> Result<u64>
Consumes the remainder of this body from the underlying transport by reading it to the end and discarding the contents. This is important for http1.1 keepalive, but most of the time you do not need to directly call this. It returns the number of bytes consumed.
§Errors
This will return an std::io::Result::Err if there is an io error on the underlying
transport, such as a disconnect
Source§impl<T> ReceivedBody<'static, T>
impl<T> ReceivedBody<'static, T>
Sourcepub fn take_transport(&mut self) -> Option<T>
pub fn take_transport(&mut self) -> Option<T>
takes the static transport from this received body
Trait Implementations§
Source§impl<Transport> AsyncRead for ReceivedBody<'_, Transport>
impl<Transport> AsyncRead for ReceivedBody<'_, Transport>
Source§impl<Transport> Debug for ReceivedBody<'_, Transport>
impl<Transport> Debug for ReceivedBody<'_, Transport>
Source§impl<Transport> From<ReceivedBody<'static, Transport>> for Body
impl<Transport> From<ReceivedBody<'static, Transport>> for Body
Source§fn from(rb: ReceivedBody<'static, Transport>) -> Self
fn from(rb: ReceivedBody<'static, Transport>) -> Self
Auto Trait Implementations§
impl<'conn, Transport> Freeze for ReceivedBody<'conn, Transport>where
Transport: Freeze,
impl<'conn, Transport> !RefUnwindSafe for ReceivedBody<'conn, Transport>
impl<'conn, Transport> Send for ReceivedBody<'conn, Transport>where
Transport: Send,
impl<'conn, Transport> Sync for ReceivedBody<'conn, Transport>where
Transport: Sync,
impl<'conn, Transport> Unpin for ReceivedBody<'conn, Transport>where
Transport: Unpin,
impl<'conn, Transport> UnsafeUnpin for ReceivedBody<'conn, Transport>where
Transport: UnsafeUnpin,
impl<'conn, Transport> !UnwindSafe for ReceivedBody<'conn, Transport>
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