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