use std::future::Future as _;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use bytes::{Bytes, BytesMut};
use futures_core::Stream;
use futures_util::StreamExt;
use http_body::Frame;
use pin_project_lite::pin_project;
use crate::compression::DecompressionLimit;
use crate::error::{Error, Result};
use crate::timeout::ResponseDeadline;
pub type BoxBytesStream = Pin<Box<dyn Stream<Item = Result<Bytes>> + Send>>;
#[derive(Default)]
pub enum RequestBody {
#[default]
Empty,
Bytes(Bytes),
Stream {
stream: BoxBytesStream,
length: Option<usize>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReplayClass {
Immutable,
Seekable,
OneShot,
Consumed,
}
impl std::fmt::Debug for RequestBody {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Empty => f.debug_tuple("Empty").finish(),
Self::Bytes(b) => f.debug_tuple("Bytes").field(&b.len()).finish(),
Self::Stream { length, .. } => f
.debug_struct("Stream")
.field("length", length)
.finish_non_exhaustive(),
}
}
}
impl RequestBody {
#[must_use]
pub fn is_empty(&self) -> bool {
match self {
Self::Empty => true,
Self::Bytes(b) => b.is_empty(),
Self::Stream { .. } => false,
}
}
#[must_use]
pub fn len(&self) -> usize {
match self {
Self::Empty => 0,
Self::Bytes(b) => b.len(),
Self::Stream { length, .. } => length.unwrap_or(0),
}
}
#[must_use]
pub fn has_known_length(&self) -> bool {
match self {
Self::Empty | Self::Bytes(_) => true,
Self::Stream { length, .. } => length.is_some(),
}
}
#[must_use]
pub fn is_replayable(&self) -> bool {
matches!(self, Self::Empty | Self::Bytes(_))
}
#[must_use]
pub fn replay_class(&self) -> ReplayClass {
match self {
Self::Empty | Self::Bytes(_) => ReplayClass::Immutable,
Self::Stream { .. } => ReplayClass::OneShot,
}
}
pub fn try_clone_for_redirect(&self) -> Result<Self> {
match self {
Self::Empty => Ok(Self::Empty),
Self::Bytes(b) => Ok(Self::Bytes(b.clone())),
Self::Stream { .. } => Err(Error::BodyNotReplayableForRedirect),
}
}
#[cfg(any(feature = "logical-retry", feature = "proxy"))]
pub(crate) fn try_clone_for_retry(&self) -> Result<Self> {
match self {
Self::Empty => Ok(Self::Empty),
Self::Bytes(b) => Ok(Self::Bytes(b.clone())),
Self::Stream { .. } => Err(Error::BodyNotReplayableForRetry),
}
}
pub async fn into_bytes(self) -> Result<Bytes> {
use futures_util::StreamExt;
match self {
Self::Empty => Ok(Bytes::new()),
Self::Bytes(b) => Ok(b),
Self::Stream { mut stream, length } => {
let mut buf = BytesMut::with_capacity(length.unwrap_or(0));
while let Some(chunk) = stream.next().await {
buf.extend_from_slice(&chunk?);
}
Ok(buf.freeze())
}
}
}
pub fn from_stream<S>(stream: S, length: Option<usize>) -> Self
where
S: Stream<Item = Result<Bytes>> + Send + 'static,
{
Self::Stream {
stream: Box::pin(stream),
length,
}
}
#[allow(
clippy::type_complexity,
reason = "the Hyper body type is fixed by the `http-body-util` API and cannot be named more simply without changing the transport contract"
)]
pub(crate) fn into_http_body(
self,
) -> http_body_util::combinators::UnsyncBoxBody<Bytes, Box<dyn std::error::Error + Send + Sync>>
{
use http_body_util::combinators::UnsyncBoxBody;
use http_body_util::{BodyExt, Empty, Full, StreamBody};
match self {
Self::Empty => Empty::<Bytes>::new()
.map_err(|err| -> Box<dyn std::error::Error + Send + Sync> { match err {} })
.boxed_unsync(),
Self::Bytes(b) => Full::new(b)
.map_err(|err| -> Box<dyn std::error::Error + Send + Sync> { match err {} })
.boxed_unsync(),
Self::Stream { stream, .. } => {
let framed = stream.map(|chunk_result| {
chunk_result
.map(Frame::data)
.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { Box::new(e) })
});
let body: UnsyncBoxBody<Bytes, Box<dyn std::error::Error + Send + Sync>> =
BodyExt::boxed_unsync(StreamBody::new(framed));
body
}
}
}
}
impl From<Bytes> for RequestBody {
fn from(b: Bytes) -> Self {
Self::Bytes(b)
}
}
impl From<Vec<u8>> for RequestBody {
fn from(v: Vec<u8>) -> Self {
Self::Bytes(Bytes::from(v))
}
}
impl From<&[u8]> for RequestBody {
fn from(s: &[u8]) -> Self {
Self::Bytes(Bytes::copy_from_slice(s))
}
}
impl From<String> for RequestBody {
fn from(s: String) -> Self {
Self::Bytes(Bytes::from(s))
}
}
impl From<&str> for RequestBody {
fn from(s: &str) -> Self {
Self::Bytes(Bytes::copy_from_slice(s.as_bytes()))
}
}
pin_project! {
#[must_use = "request bodies do nothing unless polled"]
pub(crate) struct NativeRequestBody {
#[pin]
inner: Pin<Box<dyn http_body::Body<Data = Bytes, Error = Box<dyn std::error::Error + Send + Sync>> + Send>>,
write_timeout: Option<Duration>,
#[pin]
timer: tokio::time::Sleep,
started: bool,
done: bool,
}
}
impl NativeRequestBody {
pub(crate) fn new(
inner: Pin<
Box<
dyn http_body::Body<Data = Bytes, Error = Box<dyn std::error::Error + Send + Sync>>
+ Send,
>,
>,
write_timeout: Option<Duration>,
) -> Self {
Self {
inner,
write_timeout,
timer: tokio::time::sleep(Duration::MAX),
started: false,
done: false,
}
}
}
impl http_body::Body for NativeRequestBody {
type Data = Bytes;
type Error = Box<dyn std::error::Error + Send + Sync>;
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<std::result::Result<Frame<Self::Data>, Self::Error>>> {
let mut this = self.project();
if *this.done {
return Poll::Ready(None);
}
if let Some(timeout) = *this.write_timeout {
if !*this.started {
*this.started = true;
let next = crate::timeout::saturating_tokio_now_plus(timeout);
this.timer.as_mut().reset(next);
}
}
match this.inner.as_mut().poll_frame(cx) {
Poll::Ready(Some(Ok(frame))) => {
if let Some(timeout) = *this.write_timeout {
let next = crate::timeout::saturating_tokio_now_plus(timeout);
this.timer.as_mut().reset(next);
}
Poll::Ready(Some(Ok(frame)))
}
Poll::Ready(Some(Err(error))) => {
*this.done = true;
Poll::Ready(Some(Err(error)))
}
Poll::Ready(None) => {
*this.done = true;
Poll::Ready(None)
}
Poll::Pending => {
if let Some(timeout) = *this.write_timeout {
if this.timer.as_mut().poll(cx).is_ready() {
*this.done = true;
return Poll::Ready(Some(Err(Box::new(Error::Timeout {
phase: crate::timeout::TimeoutPhase::Write,
elapsed: timeout,
}))));
}
}
Poll::Pending
}
}
}
fn is_end_stream(&self) -> bool {
self.done || self.inner.as_ref().is_end_stream()
}
fn size_hint(&self) -> http_body::SizeHint {
self.inner.as_ref().size_hint()
}
}
pub(crate) type PoolGuardArc = Arc<crate::pool::PoolGuard>;
pin_project! {
pub struct NativeResponseBody {
#[pin]
inner: Pin<Box<dyn http_body::Body<Data = Bytes, Error = Error> + Send>>,
lease: Option<PoolGuardArc>,
read_timeout: Option<Duration>,
total_deadline: Option<ResponseDeadline>,
read_sleep: Option<Pin<Box<tokio::time::Sleep>>>,
total_sleep: Option<Pin<Box<tokio::time::Sleep>>>,
started: bool,
done: bool,
}
}
impl std::fmt::Debug for NativeResponseBody {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NativeResponseBody")
.field("read_timeout", &self.read_timeout)
.field("done", &self.done)
.finish_non_exhaustive()
}
}
impl NativeResponseBody {
pub(crate) fn new<B>(
body: B,
lease: Option<PoolGuardArc>,
read_timeout: Option<Duration>,
total_deadline: Option<ResponseDeadline>,
) -> Self
where
B: http_body::Body<Data = Bytes, Error = Error> + Send + 'static,
{
Self {
inner: Box::pin(body),
lease,
read_timeout,
total_deadline,
read_sleep: None,
total_sleep: None,
started: false,
done: false,
}
}
fn total_timeout_error(total: &ResponseDeadline) -> Error {
Error::Timeout {
phase: crate::timeout::TimeoutPhase::Total,
elapsed: total.elapsed(),
}
}
}
impl http_body::Body for NativeResponseBody {
type Data = Bytes;
type Error = Error;
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>>>> {
let mut this = self.project();
if *this.done {
return Poll::Ready(None);
}
if !*this.started {
*this.started = true;
if let Some(total) = this.total_deadline.as_ref() {
if total.is_expired() {
*this.done = true;
this.lease.take();
return Poll::Ready(Some(Err(Self::total_timeout_error(total))));
}
let tokio_deadline = tokio::time::Instant::from_std(total.deadline());
*this.total_sleep = Some(Box::pin(tokio::time::sleep_until(tokio_deadline)));
}
if let Some(timeout) = *this.read_timeout {
*this.read_sleep = Some(Box::pin(tokio::time::sleep(timeout)));
}
}
if let Some(total) = this.total_deadline.as_ref() {
if total.is_expired() {
*this.read_sleep = None;
*this.total_sleep = None;
*this.done = true;
this.lease.take();
return Poll::Ready(Some(Err(Self::total_timeout_error(total))));
}
}
match this.inner.as_mut().poll_frame(cx) {
Poll::Ready(Some(Ok(frame))) => {
if let Some(total) = this.total_deadline.as_ref() {
if total.is_expired() {
*this.read_sleep = None;
*this.total_sleep = None;
*this.done = true;
this.lease.take();
return Poll::Ready(Some(Err(Self::total_timeout_error(total))));
}
}
if let (Some(read_sleep), Some(timeout)) =
(this.read_sleep.as_mut(), this.read_timeout.as_ref())
{
let next = crate::timeout::saturating_tokio_now_plus(*timeout);
read_sleep.as_mut().reset(next);
}
Poll::Ready(Some(Ok(frame)))
}
Poll::Ready(Some(Err(error))) => {
*this.done = true;
this.lease.take();
Poll::Ready(Some(Err(error)))
}
Poll::Ready(None) => {
*this.done = true;
this.lease.take();
Poll::Ready(None)
}
Poll::Pending => {
let total_fired = match this.total_sleep.as_mut() {
Some(sleep) => sleep.as_mut().poll(cx).is_ready(),
None => false,
};
if total_fired {
if let Some(total) = this.total_deadline.as_ref() {
let err = Self::total_timeout_error(total);
*this.read_sleep = None;
*this.total_sleep = None;
*this.done = true;
this.lease.take();
return Poll::Ready(Some(Err(err)));
}
}
if let (Some(read_sleep), Some(timeout)) =
(this.read_sleep.as_mut(), this.read_timeout.as_ref())
{
if read_sleep.as_mut().poll(cx).is_ready() {
*this.read_sleep = None;
*this.total_sleep = None;
*this.done = true;
this.lease.take();
return Poll::Ready(Some(Err(Error::Timeout {
phase: crate::timeout::TimeoutPhase::Read,
elapsed: *timeout,
})));
}
}
Poll::Pending
}
}
}
fn is_end_stream(&self) -> bool {
self.done || self.inner.as_ref().is_end_stream()
}
fn size_hint(&self) -> http_body::SizeHint {
self.inner.as_ref().size_hint()
}
}
pin_project! {
struct IncomingErrorBody {
#[pin]
inner: hyper::body::Incoming,
}
}
impl http_body::Body for IncomingErrorBody {
type Data = Bytes;
type Error = Error;
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>>>> {
self.project()
.inner
.poll_frame(cx)
.map_ok(|frame| frame)
.map_err(|error| Error::Hyper(Arc::new(error)))
}
fn is_end_stream(&self) -> bool {
self.inner.is_end_stream()
}
fn size_hint(&self) -> http_body::SizeHint {
self.inner.size_hint()
}
}
impl NativeResponseBody {
pub(crate) fn from_incoming(
incoming: hyper::body::Incoming,
lease: Option<PoolGuardArc>,
read_timeout: Option<Duration>,
total_deadline: Option<ResponseDeadline>,
) -> Self {
Self::new(
IncomingErrorBody { inner: incoming },
lease,
read_timeout,
total_deadline,
)
}
}
#[derive(Debug, Clone, Default)]
pub struct SharedTrailers {
inner: Arc<std::sync::Mutex<Option<http::HeaderMap>>>,
}
impl SharedTrailers {
#[must_use]
pub fn new() -> Self {
Self {
inner: Arc::new(std::sync::Mutex::new(None)),
}
}
pub(crate) fn store(&self, trailers: http::HeaderMap) {
if let Ok(mut guard) = self.inner.lock() {
if guard.is_none() {
*guard = Some(trailers);
}
}
}
#[must_use]
pub fn get(&self) -> Option<http::HeaderMap> {
self.inner
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
}
}
pin_project! {
struct LeasedResponseStream {
#[pin]
inner: BoxBytesStream,
_lease: Option<PoolGuardArc>,
}
}
impl Stream for LeasedResponseStream {
type Item = Result<Bytes>;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
let mut this = self.project();
let item = this.inner.as_mut().poll_next(cx);
if matches!(item, std::task::Poll::Ready(Some(Err(_)) | None)) {
this._lease.take();
}
item
}
}
pin_project! {
struct LimitedResponseStream {
#[pin]
inner: BoxBytesStream,
max: usize,
decoded_bytes: usize,
limit_exceeded: bool,
}
}
impl Stream for LimitedResponseStream {
type Item = Result<Bytes>;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
let mut this = self.project();
if *this.limit_exceeded {
return std::task::Poll::Ready(None);
}
match this.inner.as_mut().poll_next(cx) {
std::task::Poll::Ready(Some(Ok(chunk))) => {
*this.decoded_bytes = this.decoded_bytes.saturating_add(chunk.len());
if *this.decoded_bytes > *this.max {
*this.limit_exceeded = true;
std::task::Poll::Ready(Some(Err(Error::DecodedBodyTooLarge)))
} else {
std::task::Poll::Ready(Some(Ok(chunk)))
}
}
other => other,
}
}
}
pub enum ResponseBody {
Buffered {
bytes: Bytes,
},
Streaming {
stream: BoxBytesStream,
lease: Option<PoolGuardArc>,
},
EncodedStreaming {
stream: BoxBytesStream,
lease: Option<PoolGuardArc>,
content_encoding: String,
limit: DecompressionLimit,
},
Consumed,
}
impl std::fmt::Debug for ResponseBody {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Buffered { bytes } => f
.debug_struct("Buffered")
.field("len", &bytes.len())
.finish(),
Self::Streaming { .. } => f.debug_struct("Streaming").finish_non_exhaustive(),
Self::EncodedStreaming { .. } => {
f.debug_struct("EncodedStreaming").finish_non_exhaustive()
}
Self::Consumed => f.debug_struct("Consumed").finish(),
}
}
}
impl ResponseBody {
#[must_use]
pub fn buffered(bytes: Bytes) -> Self {
Self::Buffered { bytes }
}
#[must_use]
pub fn streaming(stream: BoxBytesStream) -> Self {
Self::Streaming {
stream,
lease: None,
}
}
#[allow(
dead_code,
reason = "the lease-carrying constructor is only called by the pool-lease test; production bodies attach their lease through `attach_lease`"
)]
pub(crate) fn streaming_with_lease(stream: BoxBytesStream, lease: PoolGuardArc) -> Self {
Self::Streaming {
stream,
lease: Some(lease),
}
}
pub(crate) fn attach_lease(self, lease: PoolGuardArc) -> Self {
match self {
Self::Streaming { stream, .. } => Self::Streaming {
stream,
lease: Some(lease),
},
Self::EncodedStreaming {
stream,
content_encoding,
limit,
..
} => Self::EncodedStreaming {
stream,
lease: Some(lease),
content_encoding,
limit,
},
other => {
drop(lease);
other
}
}
}
pub(crate) fn encoded_streaming(
stream: BoxBytesStream,
content_encoding: String,
limit: DecompressionLimit,
) -> Self {
Self::EncodedStreaming {
stream,
lease: None,
content_encoding,
limit,
}
}
pub(crate) fn encoded_streaming_with_lease(
stream: BoxBytesStream,
lease: PoolGuardArc,
content_encoding: String,
limit: DecompressionLimit,
) -> Self {
Self::EncodedStreaming {
stream,
lease: Some(lease),
content_encoding,
limit,
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
match self {
Self::Buffered { bytes } => bytes.is_empty(),
Self::Streaming { .. } | Self::EncodedStreaming { .. } => false, Self::Consumed => true,
}
}
#[must_use]
pub fn len(&self) -> Option<usize> {
match self {
Self::Buffered { bytes } => Some(bytes.len()),
Self::Streaming { .. } | Self::EncodedStreaming { .. } => None,
Self::Consumed => Some(0),
}
}
#[must_use]
pub fn is_buffered(&self) -> bool {
matches!(self, Self::Buffered { .. })
}
pub async fn bytes(&mut self) -> Result<Bytes> {
if matches!(self, Self::Buffered { .. }) {
let old = std::mem::replace(self, Self::Consumed);
if let Self::Buffered { bytes } = old {
return Ok(bytes);
}
return Err(Error::Body("response body state mismatch".into()));
}
let mut stream = self.take_stream(false)?;
let mut buf = BytesMut::new();
while let Some(chunk) = stream.next().await {
buf.extend_from_slice(&chunk?);
}
Ok(buf.freeze())
}
pub(crate) fn limit_decoded_size(self, max: usize) -> Result<Self> {
match self {
Self::Buffered { bytes } if bytes.len() > max => Err(Error::DecodedBodyTooLarge),
Self::Buffered { bytes } => Ok(Self::Buffered { bytes }),
Self::Streaming { stream, lease } => Ok(Self::Streaming {
stream: Box::pin(LimitedResponseStream {
inner: stream,
max,
decoded_bytes: 0,
limit_exceeded: false,
}),
lease,
}),
Self::EncodedStreaming {
stream,
lease,
content_encoding,
limit: mut existing_limit,
} => {
existing_limit.max_decoded_body_size = Some(
existing_limit
.max_decoded_body_size
.map_or(max, |current| current.min(max)),
);
Ok(Self::EncodedStreaming {
stream,
lease,
content_encoding,
limit: existing_limit,
})
}
body @ Self::Consumed => Ok(body),
}
}
pub async fn text(&mut self) -> Result<String> {
let bytes = self.bytes().await?;
String::from_utf8(bytes.to_vec()).map_err(|e| Error::Body(e.to_string()))
}
pub fn bytes_stream(&mut self) -> Result<BoxBytesStream> {
self.take_stream(false)
}
pub fn raw_bytes_stream(&mut self) -> Result<BoxBytesStream> {
self.take_stream(true)
}
fn take_stream(&mut self, raw: bool) -> Result<BoxBytesStream> {
match self {
Self::Buffered { bytes } => {
let bytes = std::mem::take(bytes);
*self = Self::Consumed;
Ok(Box::pin(futures_util::stream::once(
async move { Ok(bytes) },
)))
}
Self::Streaming { .. } => {
let old = std::mem::replace(self, Self::Consumed);
if let Self::Streaming { stream, lease } = old {
let (read_timeout, total_deadline) =
lease.as_ref().map_or((None, None), |guard| {
(
guard.response_read_timeout(),
guard.response_total_deadline(),
)
});
let timed =
crate::stream::body_timeout_stream(stream, read_timeout, total_deadline);
Ok(Box::pin(LeasedResponseStream {
inner: timed,
_lease: lease,
}))
} else {
Err(Error::Body("response body state mismatch".into()))
}
}
Self::EncodedStreaming { .. } => {
let old = std::mem::replace(self, Self::Consumed);
if let Self::EncodedStreaming {
stream,
lease,
content_encoding,
limit,
} = old
{
let (read_timeout, total_deadline) =
lease.as_ref().map_or((None, None), |guard| {
(
guard.response_read_timeout(),
guard.response_total_deadline(),
)
});
let stream = if raw {
Ok(stream)
} else {
crate::compression::decompress_stream(
stream,
Some(&content_encoding),
true,
limit,
)
}?;
let timed =
crate::stream::body_timeout_stream(stream, read_timeout, total_deadline);
Ok(Box::pin(LeasedResponseStream {
inner: timed,
_lease: lease,
}))
} else {
Err(Error::Body("response body state mismatch".into()))
}
}
Self::Consumed => Err(Error::Body("streaming body already consumed".into())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn request_body_empty() {
let body = RequestBody::Empty;
assert!(body.is_empty());
assert_eq!(body.len(), 0);
assert!(body.has_known_length());
assert!(body.is_replayable());
}
#[test]
fn request_body_bytes() {
let body = RequestBody::from(Bytes::from("hello"));
assert!(!body.is_empty());
assert_eq!(body.len(), 5);
assert!(body.has_known_length());
assert!(body.is_replayable());
}
#[test]
fn request_body_stream_unknown_length() {
let stream = futures_util::stream::empty::<Result<Bytes>>();
let body = RequestBody::from_stream(stream, None);
assert!(!body.is_empty());
assert_eq!(body.len(), 0);
assert!(!body.has_known_length());
assert!(!body.is_replayable());
}
#[test]
fn request_body_stream_known_length() {
let stream = futures_util::stream::empty::<Result<Bytes>>();
let body = RequestBody::from_stream(stream, Some(1024));
assert!(!body.is_empty());
assert_eq!(body.len(), 1024);
assert!(body.has_known_length());
assert!(!body.is_replayable());
}
#[test]
fn try_clone_for_redirect_empty() {
let body = RequestBody::Empty;
let cloned = body.try_clone_for_redirect().unwrap();
assert!(cloned.is_empty());
}
#[test]
fn try_clone_for_redirect_bytes() {
let body = RequestBody::from(Bytes::from("hello"));
let cloned = body.try_clone_for_redirect().unwrap();
match cloned {
RequestBody::Bytes(b) => assert_eq!(b, "hello"),
_ => panic!("expected bytes"),
}
}
#[test]
fn try_clone_for_redirect_stream_errors() {
let stream = futures_util::stream::empty::<Result<Bytes>>();
let body = RequestBody::from_stream(stream, None);
assert!(body.try_clone_for_redirect().is_err());
}
#[test]
fn request_body_bytes_debug_redacts_contents() {
let body = RequestBody::from(Bytes::from("form-password=hunter2"));
let debug = format!("{body:?}");
assert!(
!debug.contains("hunter2"),
"RequestBody Debug must not leak byte contents: {debug}"
);
assert!(
debug.contains("Bytes(21)"),
"RequestBody Debug reports the length only: {debug}"
);
}
#[test]
fn response_body_buffered() {
let body = ResponseBody::buffered(Bytes::from("hello"));
assert!(!body.is_empty());
assert_eq!(body.len(), Some(5));
assert!(body.is_buffered());
}
#[test]
fn response_body_streaming() {
let stream = Box::pin(futures_util::stream::empty());
let body = ResponseBody::streaming(stream);
assert!(!body.is_buffered());
}
#[tokio::test]
async fn response_body_buffered_bytes() {
let mut body = ResponseBody::buffered(Bytes::from("hello world"));
let bytes = body.bytes().await.unwrap();
assert_eq!(bytes, "hello world");
}
#[tokio::test]
async fn response_body_buffered_text() {
let mut body = ResponseBody::buffered(Bytes::from("hello"));
let text = body.text().await.unwrap();
assert_eq!(text, "hello");
}
#[tokio::test]
async fn response_body_buffered_stream_yields_single_chunk() {
let mut body = ResponseBody::buffered(Bytes::from("data"));
let mut stream = body.bytes_stream().unwrap();
let chunk = stream.next().await.unwrap().unwrap();
assert_eq!(chunk, "data");
assert!(stream.next().await.is_none());
}
#[tokio::test]
async fn response_body_streaming_yields_chunks() {
let chunks = vec![
Ok(Bytes::from("a")),
Ok(Bytes::from("bb")),
Ok(Bytes::from("ccc")),
];
let stream = Box::pin(futures_util::stream::iter(chunks));
let mut body = ResponseBody::streaming(stream);
let mut stream = body.bytes_stream().unwrap();
assert_eq!(stream.next().await.unwrap().unwrap(), "a");
assert_eq!(stream.next().await.unwrap().unwrap(), "bb");
assert_eq!(stream.next().await.unwrap().unwrap(), "ccc");
assert!(stream.next().await.is_none());
}
#[tokio::test]
async fn response_body_streaming_collect() {
let chunks = vec![Ok(Bytes::from("hello ")), Ok(Bytes::from("world"))];
let stream = Box::pin(futures_util::stream::iter(chunks));
let mut body = ResponseBody::streaming(stream);
let bytes = body.bytes().await.unwrap();
assert_eq!(bytes, "hello world");
}
#[tokio::test]
async fn response_body_unencoded_size_limit_is_enforced() {
let stream = Box::pin(futures_util::stream::iter(vec![
Ok(Bytes::from("ab")),
Ok(Bytes::from("cd")),
]));
let mut body = ResponseBody::streaming(stream)
.limit_decoded_size(3)
.unwrap();
assert_eq!(
body.bytes().await.unwrap_err().kind(),
"decoded_body_too_large"
);
}
#[test]
fn response_body_buffered_size_limit_is_enforced() {
let body = ResponseBody::buffered(Bytes::from_static(b"body"));
assert_eq!(
body.limit_decoded_size(3).unwrap_err().kind(),
"decoded_body_too_large"
);
}
#[tokio::test]
async fn response_body_double_consume_replaces() {
let mut body = ResponseBody::buffered(Bytes::from("data"));
let _ = body.bytes().await.unwrap();
assert!(body.bytes().await.is_err());
}
#[tokio::test]
async fn response_body_double_stream_errors() {
let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("x"))]));
let mut body = ResponseBody::streaming(stream);
let _ = body.bytes_stream().unwrap();
let err = body.bytes_stream();
assert!(err.is_err());
}
#[cfg(feature = "compression-gzip")]
fn gzip_bytes(input: &[u8]) -> Bytes {
use std::io::Write;
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast());
encoder.write_all(input).unwrap();
Bytes::from(encoder.finish().unwrap())
}
#[cfg(feature = "compression-gzip")]
#[tokio::test]
async fn encoded_streaming_selects_raw_once() {
let encoded = gzip_bytes(b"compressed body");
let expected = encoded.clone();
let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
let mut body = ResponseBody::encoded_streaming(
stream,
"gzip".to_owned(),
DecompressionLimit::default(),
);
let mut raw = body.raw_bytes_stream().unwrap();
assert_eq!(raw.next().await.unwrap().unwrap(), expected);
assert!(raw.next().await.is_none());
assert!(body.bytes_stream().is_err());
}
#[cfg(feature = "compression-gzip")]
#[tokio::test]
async fn encoded_streaming_selects_decoded_once() {
let encoded = gzip_bytes(b"compressed body");
let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
let mut body = ResponseBody::encoded_streaming(
stream,
"gzip".to_owned(),
DecompressionLimit::default(),
);
let mut decoded = body.bytes_stream().unwrap();
assert_eq!(decoded.next().await.unwrap().unwrap(), "compressed body");
assert!(decoded.next().await.is_none());
assert!(body.raw_bytes_stream().is_err());
}
#[cfg(feature = "compression-gzip")]
#[tokio::test]
async fn encoded_streaming_limit_is_applied_when_added_after_construction() {
let encoded = gzip_bytes(b"compressed body");
let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
let mut body = ResponseBody::encoded_streaming(
stream,
"gzip".to_owned(),
DecompressionLimit::default(),
)
.limit_decoded_size(3)
.unwrap();
assert_eq!(
body.bytes().await.unwrap_err().kind(),
"decoded_body_too_large"
);
}
#[tokio::test]
async fn uncompressed_raw_selection_is_passthrough() {
let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("body"))]));
let mut body = ResponseBody::streaming(stream);
let mut raw = body.raw_bytes_stream().unwrap();
assert_eq!(raw.next().await.unwrap().unwrap(), "body");
assert!(raw.next().await.is_none());
}
#[tokio::test]
async fn response_body_buffered_double_bytes_errors() {
let mut body = ResponseBody::buffered(Bytes::from("data"));
let first = body.bytes().await.unwrap();
assert_eq!(first, "data");
assert!(body.bytes().await.is_err());
}
#[tokio::test]
async fn response_body_buffered_text_invalid_utf8() {
let mut body = ResponseBody::buffered(Bytes::from(vec![0xFF, 0xFE]));
let err = body.text().await;
assert!(err.is_err());
}
#[tokio::test]
async fn response_body_streaming_bytes_then_stream_returns_empty() {
let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("x"))]));
let mut body = ResponseBody::streaming(stream);
let _ = body.bytes().await.unwrap();
assert!(body.bytes_stream().is_err());
}
#[tokio::test]
async fn response_body_consumed_is_empty() {
let body = ResponseBody::Consumed;
assert!(body.is_empty());
assert_eq!(body.len(), Some(0));
}
#[tokio::test]
async fn response_body_buffered_bytes_stream_then_bytes_returns_empty() {
let mut body = ResponseBody::buffered(Bytes::from("data"));
let mut stream = body.bytes_stream().unwrap();
let chunk = stream.next().await.unwrap().unwrap();
assert_eq!(chunk, "data");
assert!(stream.next().await.is_none());
assert!(body.bytes().await.is_err());
}
#[tokio::test]
async fn into_http_body_empty_produces_empty_body() {
use http_body::Body;
use http_body_util::BodyExt;
let body = RequestBody::Empty;
let mut hyper_body = body.into_http_body();
let frame = futures_util::future::poll_fn(|cx| {
use std::pin::Pin;
Pin::new(&mut hyper_body).poll_frame(cx)
})
.await;
assert!(frame.is_none());
let _ = hyper_body.collect().await;
}
#[tokio::test]
async fn into_http_body_bytes_produces_one_data_frame() {
use http_body::Body;
use std::pin::Pin;
let body = RequestBody::from(Bytes::from("hello"));
let mut hyper_body = body.into_http_body();
let frame = futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx))
.await
.expect("frame present")
.expect("frame ok");
assert!(frame.is_data());
let data = frame.into_data().unwrap();
assert_eq!(data, "hello");
let end =
futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx)).await;
assert!(end.is_none());
}
#[tokio::test]
async fn into_http_body_stream_pipes_through_without_eager_buffering() {
use http_body::Body;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
let polled = Arc::new(AtomicUsize::new(0));
let polled_inner = polled.clone();
let chunks = vec![
Ok(Bytes::from("a")),
Ok(Bytes::from("b")),
Ok(Bytes::from("c")),
];
let chunk_stream = futures_util::stream::iter(chunks).inspect(move |_| {
polled_inner.fetch_add(1, Ordering::SeqCst);
});
let body = RequestBody::from_stream(chunk_stream.map(|r| -> Result<Bytes> { r }), Some(3));
let mut hyper_body = body.into_http_body();
let frame = futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx))
.await
.expect("frame present")
.expect("frame ok");
let data = frame.into_data().unwrap();
assert_eq!(data, "a");
assert_eq!(
polled.load(Ordering::SeqCst),
1,
"stream should not be eagerly polled"
);
}
#[tokio::test]
async fn limited_stream_fuses_after_limit_trips() {
use futures_util::StreamExt;
use std::sync::atomic::{AtomicUsize, Ordering};
let polls = Arc::new(AtomicUsize::new(0));
let polls_inner = polls.clone();
let tracked = futures_util::stream::iter(vec![
Ok(Bytes::from("ab")),
Ok(Bytes::from("cd")),
Ok(Bytes::from("ef")),
])
.inspect(move |_| {
polls_inner.fetch_add(1, Ordering::SeqCst);
});
let inner: super::BoxBytesStream = Box::pin(tracked);
let mut limited = super::LimitedResponseStream {
inner,
max: 3,
decoded_bytes: 0,
limit_exceeded: false,
};
assert!(limited.next().await.unwrap().is_ok());
assert!(limited.next().await.unwrap().is_err());
let polls_after_error = polls.load(Ordering::SeqCst);
assert!(limited.next().await.is_none());
assert_eq!(
polls.load(Ordering::SeqCst),
polls_after_error,
"inner stream must not be re-polled after the limit trips"
);
}
#[tokio::test]
async fn leased_stream_holds_pool_slot_until_drop() {
let pool = crate::pool::Pool::new(crate::pool::PoolConfig {
max_connections_per_host: Some(1),
..Default::default()
});
let origin = crate::pool::OriginKey::from_parts("http", "example.com", 80);
let guard = pool.acquire(Some(&origin)).await.unwrap();
let mut body = ResponseBody::streaming_with_lease(
Box::pin(futures_util::stream::pending()),
Arc::new(guard),
);
let stream = body.bytes_stream().unwrap();
assert!(tokio::time::timeout(
std::time::Duration::from_millis(10),
pool.acquire(Some(&origin))
)
.await
.is_err());
drop(stream);
assert!(tokio::time::timeout(
std::time::Duration::from_millis(100),
pool.acquire(Some(&origin))
)
.await
.is_ok());
}
}