Struct trillium_http::ReceivedBody
source · 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 mut conn = Conn::new_synthetic(Method::Get, "/", "hello");
let body = conn.request_body().await;
assert_eq!(body.read_string().await?, "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 currently set to 500mb. In the next semver-minor release, this value will decrease substantially.
§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. This limitation is temporary.
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 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.
let mut conn = Conn::new_synthetic(Method::Get, "/", "hello");
let body = conn.request_body().await;
assert_eq!(body.content_length(), Some(5));
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 fn set_max_len(&mut self, max_len: u64)
pub fn set_max_len(&mut self, max_len: u64)
Set the maximum length that can be read from this body before error
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
chainable setter for the maximum length that can be read from this body before error
See also HttpConfig::received_body_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 fn encoding(&self) -> &'static Encoding
pub fn encoding(&self) -> &'static Encoding
returns the character encoding of this body, usually determined from the content type (mime-type) of the associated Conn.
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<'conn, Transport> AsyncRead for ReceivedBody<'conn, Transport>
impl<'conn, Transport> AsyncRead for ReceivedBody<'conn, Transport>
source§impl<'conn, Transport> Debug for ReceivedBody<'conn, Transport>
impl<'conn, Transport> Debug for ReceivedBody<'conn, 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
source§impl<'a, Transport> IntoFuture for ReceivedBody<'a, Transport>
impl<'a, Transport> IntoFuture for ReceivedBody<'a, Transport>
§type IntoFuture = Pin<Box<dyn Future<Output = <ReceivedBody<'a, Transport> as IntoFuture>::Output> + Send + 'a>>
type IntoFuture = Pin<Box<dyn Future<Output = <ReceivedBody<'a, Transport> as IntoFuture>::Output> + Send + 'a>>
source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
source§impl<'conn, Transport> Stream for ReceivedBody<'conn, Transport>
impl<'conn, Transport> Stream for ReceivedBody<'conn, Transport>
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> !UnwindSafe for ReceivedBody<'conn, Transport>
Blanket Implementations§
§impl<R> AsyncReadExt for Rwhere
R: AsyncRead + ?Sized,
impl<R> AsyncReadExt for Rwhere
R: AsyncRead + ?Sized,
§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,
§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,
§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,
§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,
§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 more§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 moresource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<S> StreamExt for Swhere
S: Stream + ?Sized,
impl<S> StreamExt for Swhere
S: Stream + ?Sized,
§fn next(&mut self) -> NextFuture<'_, Self>where
Self: Unpin,
fn next(&mut self) -> NextFuture<'_, Self>where
Self: Unpin,
§fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self>
fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self>
§fn count(self) -> CountFuture<Self>where
Self: Sized,
fn count(self) -> CountFuture<Self>where
Self: Sized,
§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
§fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
§fn flatten(self) -> Flatten<Self>where
Self: Sized,
Self::Item: Stream,
fn flatten(self) -> Flatten<Self>where
Self: Sized,
Self::Item: Stream,
§fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut>
fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut>
§fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n
items of the stream. Read more§fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
items of the stream. Read more§fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
§fn chain<U>(self, other: U) -> Chain<Self, U>where
Self: Sized,
U: Stream<Item = Self::Item>,
fn chain<U>(self, other: U) -> Chain<Self, U>where
Self: Sized,
U: Stream<Item = Self::Item>,
§fn collect<C>(self) -> CollectFuture<Self, C>
fn collect<C>(self) -> CollectFuture<Self, C>
§fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C>
fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C>
§fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B>
fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B>
predicate
is true
and those for which it is
false
, and then collects them into two collections. Read more§fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T>
fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T>
§fn try_fold<T, E, F, B>(
&mut self,
init: B,
f: F,
) -> TryFoldFuture<'_, Self, F, B>
fn try_fold<T, E, F, B>( &mut self, init: B, f: F, ) -> TryFoldFuture<'_, Self, F, B>
§fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
(index, item)
. Read more§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
§fn nth(&mut self, n: usize) -> NthFuture<'_, Self>where
Self: Unpin,
fn nth(&mut self, n: usize) -> NthFuture<'_, Self>where
Self: Unpin,
n
th item of the stream. Read more§fn for_each<F>(self, f: F) -> ForEachFuture<Self, F>
fn for_each<F>(self, f: F) -> ForEachFuture<Self, F>
§fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F>
fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F>
§fn zip<U>(self, other: U) -> Zip<Self, U>where
Self: Sized,
U: Stream,
fn zip<U>(self, other: U) -> Zip<Self, U>where
Self: Sized,
U: Stream,
§fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>
fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>
§fn race<S>(self, other: S) -> Race<Self, S>where
Self: Sized,
S: Stream<Item = Self::Item>,
fn race<S>(self, other: S) -> Race<Self, S>where
Self: Sized,
S: Stream<Item = Self::Item>,
other
stream, with no preference for either stream when both are ready. Read more