|
3 | 3 | use crate::{BoxError, Error}; |
4 | 4 | use bytes::Bytes; |
5 | 5 | use bytes::{Buf, BufMut}; |
6 | | -use http_body::Body; |
| 6 | +use futures_util::stream::Stream; |
| 7 | +use http::HeaderMap; |
| 8 | +use http_body::Body as _; |
| 9 | +use std::pin::Pin; |
| 10 | +use std::task::{Context, Poll}; |
7 | 11 |
|
8 | 12 | /// A boxed [`Body`] trait object. |
9 | 13 | /// |
|
55 | 59 | // THE SOFTWARE. |
56 | 60 | pub(crate) async fn to_bytes<T>(body: T) -> Result<Bytes, T::Error> |
57 | 61 | where |
58 | | - T: Body, |
| 62 | + T: http_body::Body, |
59 | 63 | { |
60 | 64 | futures_util::pin_mut!(body); |
61 | 65 |
|
|
85 | 89 | Ok(vec.into()) |
86 | 90 | } |
87 | 91 |
|
| 92 | +/// The body type used in axum requests and responses. |
| 93 | +#[derive(Debug)] |
| 94 | +pub struct Body(BoxBody); |
| 95 | + |
| 96 | +impl Body { |
| 97 | + /// Create a new `Body` that wraps another [`http_body::Body`]. |
| 98 | + pub fn new<B>(body: B) -> Self |
| 99 | + where |
| 100 | + B: http_body::Body<Data = Bytes> + Send + 'static, |
| 101 | + B::Error: Into<BoxError>, |
| 102 | + { |
| 103 | + try_downcast(body).unwrap_or_else(|body| Self(boxed(body))) |
| 104 | + } |
| 105 | + |
| 106 | + /// Create an empty body. |
| 107 | + pub fn empty() -> Self { |
| 108 | + Self::new(http_body::Empty::new()) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl Default for Body { |
| 113 | + fn default() -> Self { |
| 114 | + Self::empty() |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +macro_rules! body_from_impl { |
| 119 | + ($ty:ty) => { |
| 120 | + impl From<$ty> for Body { |
| 121 | + fn from(buf: $ty) -> Self { |
| 122 | + Self::new(http_body::Full::from(buf)) |
| 123 | + } |
| 124 | + } |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +body_from_impl!(&'static [u8]); |
| 129 | +body_from_impl!(std::borrow::Cow<'static, [u8]>); |
| 130 | +body_from_impl!(Vec<u8>); |
| 131 | + |
| 132 | +body_from_impl!(&'static str); |
| 133 | +body_from_impl!(std::borrow::Cow<'static, str>); |
| 134 | +body_from_impl!(String); |
| 135 | + |
| 136 | +body_from_impl!(Bytes); |
| 137 | + |
| 138 | +impl http_body::Body for Body { |
| 139 | + type Data = Bytes; |
| 140 | + type Error = Error; |
| 141 | + |
| 142 | + #[inline] |
| 143 | + fn poll_data( |
| 144 | + mut self: Pin<&mut Self>, |
| 145 | + cx: &mut Context<'_>, |
| 146 | + ) -> std::task::Poll<Option<Result<Self::Data, Self::Error>>> { |
| 147 | + Pin::new(&mut self.0).poll_data(cx) |
| 148 | + } |
| 149 | + |
| 150 | + #[inline] |
| 151 | + fn poll_trailers( |
| 152 | + mut self: Pin<&mut Self>, |
| 153 | + cx: &mut Context<'_>, |
| 154 | + ) -> std::task::Poll<Result<Option<HeaderMap>, Self::Error>> { |
| 155 | + Pin::new(&mut self.0).poll_trailers(cx) |
| 156 | + } |
| 157 | + |
| 158 | + #[inline] |
| 159 | + fn size_hint(&self) -> http_body::SizeHint { |
| 160 | + self.0.size_hint() |
| 161 | + } |
| 162 | + |
| 163 | + #[inline] |
| 164 | + fn is_end_stream(&self) -> bool { |
| 165 | + self.0.is_end_stream() |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +impl Stream for Body { |
| 170 | + type Item = Result<Bytes, Error>; |
| 171 | + |
| 172 | + #[inline] |
| 173 | + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
| 174 | + self.poll_data(cx) |
| 175 | + } |
| 176 | +} |
| 177 | + |
88 | 178 | #[test] |
89 | 179 | fn test_try_downcast() { |
90 | 180 | assert_eq!(try_downcast::<i32, _>(5_u32), Err(5_u32)); |
|
0 commit comments