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");
Implementations
sourceimpl<'conn, Transport> ReceivedBody<'conn, Transport>where
Transport: AsyncRead + Unpin + Send + Sync + 'static,
impl<'conn, Transport> ReceivedBody<'conn, Transport>where
Transport: AsyncRead + Unpin + Send + Sync + 'static,
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 the entire body to string, using the encoding determined by the content-type (mime) charset. If an encoding problem is encountered, the String returned by 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
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 read_string
.
Errors
This will return an error if there is an IO error on the underlying transport such as a disconnect
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
sourceimpl<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
sourceimpl<'conn, Transport> AsyncRead for ReceivedBody<'conn, Transport>where
Transport: AsyncRead + Unpin + Send + Sync + 'static,
impl<'conn, Transport> AsyncRead for ReceivedBody<'conn, Transport>where
Transport: AsyncRead + Unpin + Send + Sync + 'static,
sourceimpl<'conn, Transport> Debug for ReceivedBody<'conn, Transport>
impl<'conn, Transport> Debug for ReceivedBody<'conn, Transport>
sourceimpl<Transport> From<ReceivedBody<'static, Transport>> for Bodywhere
Transport: AsyncRead + AsyncWrite + Send + Sync + Unpin + 'static,
impl<Transport> From<ReceivedBody<'static, Transport>> for Bodywhere
Transport: AsyncRead + AsyncWrite + Send + Sync + Unpin + 'static,
sourcefn from(rb: ReceivedBody<'static, Transport>) -> Self
fn from(rb: ReceivedBody<'static, Transport>) -> Self
sourceimpl<'conn, Transport> Stream for ReceivedBody<'conn, Transport>where
Transport: AsyncRead + Unpin + Send + Sync + 'static,
impl<'conn, Transport> Stream for ReceivedBody<'conn, Transport>where
Transport: AsyncRead + Unpin + Send + Sync + 'static,
Auto Trait Implementations
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 mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf
. Read morefn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read moresourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn 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>where
Self: Stream<Item = Result<T, E>> + Unpin,
fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self>where
Self: Stream<Item = Result<T, E>> + Unpin,
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>where
Self: Sized,
F: FnMut(Self::Item) -> T,
fn map<T, F>(self, f: F) -> Map<Self, F>where
Self: Sized,
F: FnMut(Self::Item) -> T,
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>where
Self: Sized,
U: Stream,
F: FnMut(Self::Item) -> U,
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>where
Self: Sized,
U: Stream,
F: FnMut(Self::Item) -> U,
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>where
Self: Sized,
F: FnMut(Self::Item) -> Fut,
Fut: Future,
fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut>where
Self: Sized,
F: FnMut(Self::Item) -> Fut,
Fut: Future,
fn filter<P>(self, predicate: P) -> Filter<Self, P>where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
fn filter<P>(self, predicate: P) -> Filter<Self, P>where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>where
Self: Sized,
F: FnMut(Self::Item) -> Option<T>,
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>where
Self: Sized,
F: FnMut(Self::Item) -> Option<T>,
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 morefn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
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 morefn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
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 cloned<'a, T>(self) -> Cloned<Self>where
Self: Stream<Item = &'a T> + Sized,
T: 'a + Clone,
fn cloned<'a, T>(self) -> Cloned<Self>where
Self: Stream<Item = &'a T> + Sized,
T: 'a + Clone,
fn copied<'a, T>(self) -> Copied<Self>where
Self: Stream<Item = &'a T> + Sized,
T: 'a + Copy,
fn copied<'a, T>(self) -> Copied<Self>where
Self: Stream<Item = &'a T> + Sized,
T: 'a + Copy,
fn collect<C>(self) -> CollectFuture<Self, C>where
Self: Sized,
C: Default + Extend<Self::Item>,
fn collect<C>(self) -> CollectFuture<Self, C>where
Self: Sized,
C: Default + Extend<Self::Item>,
fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C>where
Self: Stream<Item = Result<T, E>> + Sized,
C: Default + Extend<T>,
fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C>where
Self: Stream<Item = Result<T, E>> + Sized,
C: Default + Extend<T>,
fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B>where
Self: Sized,
B: Default + Extend<Self::Item>,
P: FnMut(&Self::Item) -> bool,
fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B>where
Self: Sized,
B: Default + Extend<Self::Item>,
P: FnMut(&Self::Item) -> bool,
predicate
is true
and those for which it is
false
, and then collects them into two collections. Read morefn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T>where
Self: Sized,
F: FnMut(T, Self::Item) -> T,
fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T>where
Self: Sized,
F: FnMut(T, Self::Item) -> T,
fn try_fold<T, E, F, B>(&mut self, init: B, f: F) -> TryFoldFuture<'_, Self, F, B>where
Self: Stream<Item = Result<T, E>> + Unpin + Sized,
F: FnMut(B, T) -> Result<B, E>,
fn try_fold<T, E, F, B>(&mut self, init: B, f: F) -> TryFoldFuture<'_, Self, F, B>where
Self: Stream<Item = Result<T, E>> + Unpin + Sized,
F: FnMut(B, T) -> Result<B, E>,
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>where
Self: Sized,
F: FnMut(&mut St, Self::Item) -> Option<B>,
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>where
Self: Sized,
F: FnMut(&mut St, Self::Item) -> Option<B>,
fn cycle(self) -> Cycle<Self>where
Self: Clone + Sized,
fn cycle(self) -> Cycle<Self>where
Self: Clone + Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
(index, item)
. Read morefn inspect<F>(self, f: F) -> Inspect<Self, F>where
Self: Sized,
F: FnMut(&Self::Item),
fn inspect<F>(self, f: F) -> Inspect<Self, F>where
Self: Sized,
F: FnMut(&Self::Item),
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 morefn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P>where
Self: Unpin,
P: FnMut(&Self::Item) -> bool,
fn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P>where
Self: Unpin,
P: FnMut(&Self::Item) -> bool,
fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F>where
Self: Unpin,
F: FnMut(Self::Item) -> Option<B>,
fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F>where
Self: Unpin,
F: FnMut(Self::Item) -> Option<B>,
fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P>where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P>where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
fn all<P>(&mut self, predicate: P) -> AllFuture<'_, Self, P>where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
fn all<P>(&mut self, predicate: P) -> AllFuture<'_, Self, P>where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
fn any<P>(&mut self, predicate: P) -> AnyFuture<'_, Self, P>where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
fn any<P>(&mut self, predicate: P) -> AnyFuture<'_, Self, P>where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
fn for_each<F>(self, f: F) -> ForEachFuture<Self, F>where
Self: Sized,
F: FnMut(Self::Item),
fn for_each<F>(self, f: F) -> ForEachFuture<Self, F>where
Self: Sized,
F: FnMut(Self::Item),
fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F>where
Self: Unpin,
F: FnMut(Self::Item) -> Result<(), E>,
fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F>where
Self: Unpin,
F: FnMut(Self::Item) -> Result<(), E>,
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>where
FromA: Default + Extend<A>,
FromB: Default + Extend<B>,
Self: Stream<Item = (A, B)> + Sized,
fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>where
FromA: Default + Extend<A>,
FromB: Default + Extend<B>,
Self: Stream<Item = (A, B)> + Sized,
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