Files
axum/src/body.rs
T
David Pedersen 2322d39800 Add StreamBody (#237)
This adds `StreamBody` which converts a `Stream` of `Bytes` into a `http_body::Body`.

---

As suggested by Kestrer on Discord it would make sense for axum to provide different kinds of body types other than `Empty`, `Full`, and `hyper::Body`. There is also some talk about [splitting up `hyper::Body`](https://github.com/hyperium/hyper/issues/2345) so this can be seen as getting started on that effort. axum's body types could be moved to hyper or http-body if thats the direction we decide on.

The types I'm thinking about adding are:

- `StreamBody`-  added in this PR
- `AsyncReadBody` - similar to [http-body#41](https://github.com/hyperium/http-body/pull/41/files)
- `ChannelBody` - similar to `hyper::Body::channel`
2021-08-22 14:41:51 +02:00

37 lines
833 B
Rust

//! HTTP body utilities.
use crate::BoxError;
use crate::Error;
mod stream_body;
pub use self::stream_body::StreamBody;
#[doc(no_inline)]
pub use http_body::{Body as HttpBody, Empty, Full};
#[doc(no_inline)]
pub use hyper::body::Body;
#[doc(no_inline)]
pub use bytes::Bytes;
/// A boxed [`Body`] trait object.
///
/// This is used in axum as the response body type for applications. Its
/// necessary to unify multiple response bodies types into one.
pub type BoxBody = http_body::combinators::BoxBody<Bytes, Error>;
/// Convert a [`http_body::Body`] into a [`BoxBody`].
pub fn box_body<B>(body: B) -> BoxBody
where
B: http_body::Body<Data = Bytes> + Send + Sync + 'static,
B::Error: Into<BoxError>,
{
body.map_err(Error::new).boxed()
}
pub(crate) fn empty() -> BoxBody {
box_body(http_body::Empty::new())
}