@@ -34,10 +34,14 @@ pub mod util {
3434/// from strings.
3535/// * `Body::from_json` for a `Body` from a `Serialize` (requires feature
3636/// `json`)
37+ /// * `From<AsyncInputStream>` for a `Body` with contents given by the
38+ /// contents of a WASI input-stream.
3739/// * `Body::from_stream` or `Body::from_try_stream` for a `Body` from a
3840/// `Stream` of `Into<Bytes>`
3941///
40- /// Consume
42+ /// Consume this HTTP body using:
43+ ///
44+ ///
4145#[ derive( Debug ) ]
4246pub struct Body ( BodyInner ) ;
4347
@@ -107,6 +111,10 @@ impl Body {
107111 }
108112 }
109113
114+ /// Convert this `Body` into an `UnsyncBoxBody<Bytes, Error>`, which
115+ /// exists to implement the `http_body::Body` trait. Consume the contents
116+ /// using `http_body_utils::BodyExt`, or anywhere else an impl of
117+ /// `http_body::Body` is accepted.
110118 pub fn into_boxed_body ( self ) -> UnsyncBoxBody < Bytes , Error > {
111119 fn map_e ( _: std:: convert:: Infallible ) -> Error {
112120 unreachable ! ( )
@@ -121,6 +129,9 @@ impl Body {
121129 }
122130 }
123131
132+ /// Collect the entire contents of this `Body`, and expose them as a
133+ /// byte slice. This async fn will be pending until the entire `Body` is
134+ /// copied into memory, or an error occurs.
124135 pub async fn contents ( & mut self ) -> Result < & [ u8 ] , Error > {
125136 match & mut self . 0 {
126137 BodyInner :: Complete { ref data, .. } => Ok ( data. as_ref ( ) ) ,
@@ -149,6 +160,11 @@ impl Body {
149160 }
150161 }
151162
163+ /// Get a value for the length of this `Body`'s content, in bytes, if
164+ /// known. This value can come from either the Content-Length header
165+ /// recieved in the incoming request or response assocated with the body,
166+ /// or be provided by an exact `http_body::Body::size_hint` if the `Body`
167+ /// is constructed from an `http_body::Body` impl.
152168 pub fn content_length ( & self ) -> Option < u64 > {
153169 match & self . 0 {
154170 BodyInner :: Boxed ( b) => b. size_hint ( ) . exact ( ) ,
@@ -165,16 +181,25 @@ impl Body {
165181 } )
166182 }
167183
184+ /// Collect the entire contents of this `Body`, and expose them as a
185+ /// string slice. This async fn will be pending until the entire `Body` is
186+ /// copied into memory, or an error occurs. Additonally errors if the
187+ /// contents of the `Body` were not a utf-8 encoded string.
168188 pub async fn str_contents ( & mut self ) -> Result < & str , Error > {
169189 let bs = self . contents ( ) . await ?;
170190 std:: str:: from_utf8 ( bs) . context ( "decoding body contents as string" )
171191 }
172192
193+ /// Construct a `Body` by serializing a type to json. Can fail with a
194+ /// `serde_json::Error` if serilization fails.
173195 #[ cfg( feature = "json" ) ]
174196 pub fn from_json < T : serde:: Serialize > ( data : & T ) -> Result < Self , serde_json:: Error > {
175197 Ok ( Self :: from ( serde_json:: to_vec ( data) ?) )
176198 }
177199
200+ /// Collect the entire contents of this `Body`, and deserialize them from
201+ /// json. Can fail if the body contents are not utf-8 encoded, are not
202+ /// valid json, or the json is not accepted by the `serde::Deserialize` impl.
178203 #[ cfg( feature = "json" ) ]
179204 pub async fn json < T : for < ' a > serde:: Deserialize < ' a > > ( & mut self ) -> Result < T , Error > {
180205 let str = self . str_contents ( ) . await ?;
@@ -185,6 +210,8 @@ impl Body {
185210 Body ( BodyInner :: Incoming ( Incoming { body, size_hint } ) )
186211 }
187212
213+ /// Construct a `Body` backed by a `futures_lite::Stream` impl. The stream
214+ /// will be polled as the body is sent.
188215 pub fn from_stream < S > ( stream : S ) -> Self
189216 where
190217 S : futures_lite:: Stream + Send + ' static ,
@@ -196,17 +223,27 @@ impl Body {
196223 ) )
197224 }
198225
199- pub fn from_try_stream < S , D > ( stream : S ) -> Self
226+ /// Construct a `Body` backed by a `futures_lite::Stream` impl. The stream
227+ /// will be polled as the body is sent. If the stream gives an error, the
228+ /// body will canceled, which closes the underlying connection.
229+ pub fn from_try_stream < S , D , E > ( stream : S ) -> Self
200230 where
201- S : futures_lite:: Stream < Item = Result < D , Error > > + Send + ' static ,
231+ S : futures_lite:: Stream < Item = Result < D , E > > + Send + ' static ,
202232 D : Into < Bytes > ,
233+ E : std:: error:: Error + Send + Sync + ' static ,
203234 {
204235 use futures_lite:: StreamExt ;
205236 Self :: from_http_body ( http_body_util:: StreamBody :: new (
206237 stream. map ( |bs| Ok :: < _ , Error > ( Frame :: data ( bs?. into ( ) ) ) ) ,
207238 ) )
208239 }
209240
241+ /// Construct a `Body` backed by a `http_body::Body`. The http_body will
242+ /// be polled as the body is sent. If the http_body poll gives an error,
243+ /// the body will be canceled, which closes the underlying connection.
244+ ///
245+ /// Note, this is the only constructor which permits adding trailers to
246+ /// the `Body`.
210247 pub fn from_http_body < B > ( http_body : B ) -> Self
211248 where
212249 B : HttpBody + Send + ' static ,
@@ -259,9 +296,12 @@ impl From<String> for Body {
259296
260297impl From < crate :: io:: AsyncInputStream > for Body {
261298 fn from ( r : crate :: io:: AsyncInputStream ) -> Body {
262- // TODO: with another BodyInner variant for a boxed AsyncRead for which
263- // as_input_stream is_some, this could allow for use of
264- // crate::io::copy.
299+ // TODO: this is skipping the wstd::io::copy optimization.
300+ // in future, with another BodyInner variant for a boxed AsyncRead for
301+ // which as_input_stream is_some, this could allow for use of
302+ // crate::io::copy. But, we probably need to redesign AsyncRead to be
303+ // a poll_read func in order to make it possible to use from
304+ // http_body::Body::poll_frame.
265305 use futures_lite:: stream:: StreamExt ;
266306 Body ( BodyInner :: Boxed ( http_body_util:: BodyExt :: boxed_unsync (
267307 http_body_util:: StreamBody :: new ( r. into_stream ( ) . map ( |res| {
0 commit comments