|
1 | 1 | //! Provides an ALB / API Gateway oriented request and response body entity interface
|
2 | 2 |
|
3 |
| -use std::{borrow::Cow, ops::Deref}; |
4 |
| - |
| 3 | +use crate::Error; |
5 | 4 | use base64::display::Base64Display;
|
| 5 | +use bytes::Bytes; |
| 6 | +use http_body::{Body as HttpBody, SizeHint}; |
6 | 7 | use serde::ser::{Error as SerError, Serialize, Serializer};
|
| 8 | +use std::{borrow::Cow, mem::take, ops::Deref, pin::Pin, task::Poll}; |
7 | 9 |
|
8 | 10 | /// Representation of http request and response bodies as supported
|
9 | 11 | /// by API Gateway and ALBs.
|
@@ -175,6 +177,45 @@ impl<'a> Serialize for Body {
|
175 | 177 | }
|
176 | 178 | }
|
177 | 179 |
|
| 180 | +impl HttpBody for Body { |
| 181 | + type Data = Bytes; |
| 182 | + type Error = Error; |
| 183 | + |
| 184 | + fn poll_data( |
| 185 | + self: Pin<&mut Self>, |
| 186 | + _cx: &mut std::task::Context<'_>, |
| 187 | + ) -> Poll<Option<Result<Self::Data, Self::Error>>> { |
| 188 | + let body = take(self.get_mut()); |
| 189 | + Poll::Ready(match body { |
| 190 | + Body::Empty => None, |
| 191 | + Body::Text(s) => Some(Ok(s.into())), |
| 192 | + Body::Binary(b) => Some(Ok(b.into())), |
| 193 | + }) |
| 194 | + } |
| 195 | + |
| 196 | + fn poll_trailers( |
| 197 | + self: Pin<&mut Self>, |
| 198 | + _cx: &mut std::task::Context<'_>, |
| 199 | + ) -> Poll<Result<Option<http::HeaderMap>, Self::Error>> { |
| 200 | + Poll::Ready(Ok(None)) |
| 201 | + } |
| 202 | + |
| 203 | + fn is_end_stream(&self) -> bool { |
| 204 | + match self { |
| 205 | + Body::Empty => true, |
| 206 | + _ => false, |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + fn size_hint(&self) -> SizeHint { |
| 211 | + match self { |
| 212 | + Body::Empty => SizeHint::default(), |
| 213 | + Body::Text(ref s) => SizeHint::with_exact(s.len() as u64), |
| 214 | + Body::Binary(ref b) => SizeHint::with_exact(b.len() as u64), |
| 215 | + } |
| 216 | + } |
| 217 | +} |
| 218 | + |
178 | 219 | #[cfg(test)]
|
179 | 220 | mod tests {
|
180 | 221 | use super::*;
|
|
0 commit comments