1use std::future::Future as _;
40use std::pin::Pin;
41use std::sync::Arc;
42use std::task::{Context, Poll};
43use std::time::Duration;
44
45use bytes::{Bytes, BytesMut};
46use futures_core::Stream;
47use futures_util::StreamExt;
48use http_body::Frame;
49use pin_project_lite::pin_project;
50
51use crate::compression::DecompressionLimit;
52use crate::error::{Error, Result};
53use crate::timeout::ResponseDeadline;
54
55pub type BoxBytesStream = Pin<Box<dyn Stream<Item = Result<Bytes>> + Send>>;
61
62#[derive(Default)]
71pub enum RequestBody {
72 #[default]
74 Empty,
75 Bytes(Bytes),
77 Stream {
91 stream: BoxBytesStream,
93 length: Option<usize>,
95 },
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq)]
103pub enum ReplayClass {
104 Immutable,
106 Seekable,
108 OneShot,
110 Consumed,
112}
113
114impl std::fmt::Debug for RequestBody {
115 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116 match self {
117 Self::Empty => f.debug_tuple("Empty").finish(),
118 Self::Bytes(b) => f.debug_tuple("Bytes").field(&b.len()).finish(),
122 Self::Stream { length, .. } => f
123 .debug_struct("Stream")
124 .field("length", length)
125 .finish_non_exhaustive(),
126 }
127 }
128}
129
130impl RequestBody {
131 #[must_use]
139 pub fn is_empty(&self) -> bool {
140 match self {
141 Self::Empty => true,
142 Self::Bytes(b) => b.is_empty(),
143 Self::Stream { .. } => false,
144 }
145 }
146
147 #[must_use]
157 pub fn len(&self) -> usize {
158 match self {
159 Self::Empty => 0,
160 Self::Bytes(b) => b.len(),
161 Self::Stream { length, .. } => length.unwrap_or(0),
162 }
163 }
164
165 #[must_use]
167 pub fn has_known_length(&self) -> bool {
168 match self {
169 Self::Empty | Self::Bytes(_) => true,
170 Self::Stream { length, .. } => length.is_some(),
171 }
172 }
173
174 #[must_use]
178 pub fn is_replayable(&self) -> bool {
179 matches!(self, Self::Empty | Self::Bytes(_))
180 }
181
182 #[must_use]
184 pub fn replay_class(&self) -> ReplayClass {
185 match self {
186 Self::Empty | Self::Bytes(_) => ReplayClass::Immutable,
187 Self::Stream { .. } => ReplayClass::OneShot,
188 }
189 }
190
191 pub fn try_clone_for_redirect(&self) -> Result<Self> {
201 match self {
202 Self::Empty => Ok(Self::Empty),
203 Self::Bytes(b) => Ok(Self::Bytes(b.clone())),
204 Self::Stream { .. } => Err(Error::BodyNotReplayableForRedirect),
205 }
206 }
207
208 #[cfg(any(feature = "logical-retry", feature = "proxy"))]
222 pub(crate) fn try_clone_for_retry(&self) -> Result<Self> {
223 match self {
224 Self::Empty => Ok(Self::Empty),
225 Self::Bytes(b) => Ok(Self::Bytes(b.clone())),
226 Self::Stream { .. } => Err(Error::BodyNotReplayableForRetry),
227 }
228 }
229
230 pub async fn into_bytes(self) -> Result<Bytes> {
239 use futures_util::StreamExt;
240 match self {
241 Self::Empty => Ok(Bytes::new()),
242 Self::Bytes(b) => Ok(b),
243 Self::Stream { mut stream, length } => {
244 let mut buf = BytesMut::with_capacity(length.unwrap_or(0));
245 while let Some(chunk) = stream.next().await {
246 buf.extend_from_slice(&chunk?);
247 }
248 Ok(buf.freeze())
249 }
250 }
251 }
252
253 pub fn from_stream<S>(stream: S, length: Option<usize>) -> Self
255 where
256 S: Stream<Item = Result<Bytes>> + Send + 'static,
257 {
258 Self::Stream {
259 stream: Box::pin(stream),
260 length,
261 }
262 }
263
264 #[allow(clippy::type_complexity)]
271 pub(crate) fn into_http_body(
272 self,
273 ) -> http_body_util::combinators::UnsyncBoxBody<Bytes, Box<dyn std::error::Error + Send + Sync>>
274 {
275 use http_body_util::combinators::UnsyncBoxBody;
276 use http_body_util::{BodyExt, Empty, Full, StreamBody};
277
278 match self {
279 Self::Empty => Empty::<Bytes>::new()
280 .map_err(|err| -> Box<dyn std::error::Error + Send + Sync> { match err {} })
281 .boxed_unsync(),
282 Self::Bytes(b) => Full::new(b)
283 .map_err(|err| -> Box<dyn std::error::Error + Send + Sync> { match err {} })
284 .boxed_unsync(),
285 Self::Stream { stream, .. } => {
286 let framed = stream.map(|chunk_result| {
287 chunk_result
288 .map(Frame::data)
289 .map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { Box::new(e) })
290 });
291 let body: UnsyncBoxBody<Bytes, Box<dyn std::error::Error + Send + Sync>> =
292 BodyExt::boxed_unsync(StreamBody::new(framed));
293 body
294 }
295 }
296 }
297}
298
299impl From<Bytes> for RequestBody {
300 fn from(b: Bytes) -> Self {
301 Self::Bytes(b)
302 }
303}
304
305impl From<Vec<u8>> for RequestBody {
306 fn from(v: Vec<u8>) -> Self {
307 Self::Bytes(Bytes::from(v))
308 }
309}
310
311impl From<&[u8]> for RequestBody {
312 fn from(s: &[u8]) -> Self {
313 Self::Bytes(Bytes::copy_from_slice(s))
314 }
315}
316
317impl From<String> for RequestBody {
318 fn from(s: String) -> Self {
319 Self::Bytes(Bytes::from(s))
320 }
321}
322
323impl From<&str> for RequestBody {
324 fn from(s: &str) -> Self {
325 Self::Bytes(Bytes::copy_from_slice(s.as_bytes()))
326 }
327}
328
329pin_project! {
330 #[must_use = "request bodies do nothing unless polled"]
337 pub(crate) struct NativeRequestBody {
338 #[pin]
339 inner: Pin<Box<dyn http_body::Body<Data = Bytes, Error = Box<dyn std::error::Error + Send + Sync>> + Send>>,
340 write_timeout: Option<Duration>,
341 #[pin]
342 timer: tokio::time::Sleep,
343 started: bool,
344 done: bool,
345 }
346}
347
348impl NativeRequestBody {
349 pub(crate) fn new(
350 inner: Pin<
351 Box<
352 dyn http_body::Body<Data = Bytes, Error = Box<dyn std::error::Error + Send + Sync>>
353 + Send,
354 >,
355 >,
356 write_timeout: Option<Duration>,
357 ) -> Self {
358 Self {
359 inner,
360 write_timeout,
361 timer: tokio::time::sleep(Duration::MAX),
362 started: false,
363 done: false,
364 }
365 }
366}
367
368impl http_body::Body for NativeRequestBody {
369 type Data = Bytes;
370 type Error = Box<dyn std::error::Error + Send + Sync>;
371
372 fn poll_frame(
373 self: Pin<&mut Self>,
374 cx: &mut Context<'_>,
375 ) -> Poll<Option<std::result::Result<Frame<Self::Data>, Self::Error>>> {
376 let mut this = self.project();
377 if *this.done {
378 return Poll::Ready(None);
379 }
380
381 if let Some(timeout) = *this.write_timeout {
382 if !*this.started {
383 *this.started = true;
384 let next = crate::timeout::saturating_tokio_now_plus(timeout);
385 this.timer.as_mut().reset(next);
386 }
387 }
388
389 match this.inner.as_mut().poll_frame(cx) {
390 Poll::Ready(Some(Ok(frame))) => {
391 if let Some(timeout) = *this.write_timeout {
392 let next = crate::timeout::saturating_tokio_now_plus(timeout);
393 this.timer.as_mut().reset(next);
394 }
395 Poll::Ready(Some(Ok(frame)))
396 }
397 Poll::Ready(Some(Err(error))) => {
398 *this.done = true;
399 Poll::Ready(Some(Err(error)))
400 }
401 Poll::Ready(None) => {
402 *this.done = true;
403 Poll::Ready(None)
404 }
405 Poll::Pending => {
406 if let Some(timeout) = *this.write_timeout {
407 if this.timer.as_mut().poll(cx).is_ready() {
408 *this.done = true;
409 return Poll::Ready(Some(Err(Box::new(Error::Timeout {
410 phase: crate::timeout::TimeoutPhase::Write,
411 elapsed: timeout,
412 }))));
413 }
414 }
415 Poll::Pending
416 }
417 }
418 }
419
420 fn is_end_stream(&self) -> bool {
421 self.done || self.inner.as_ref().is_end_stream()
422 }
423
424 fn size_hint(&self) -> http_body::SizeHint {
425 self.inner.as_ref().size_hint()
426 }
427}
428
429pub(crate) type PoolGuardArc = Arc<crate::pool::PoolGuard>;
436
437pin_project! {
438 pub struct NativeResponseBody {
453 #[pin]
454 inner: Pin<Box<dyn http_body::Body<Data = Bytes, Error = Error> + Send>>,
455 lease: Option<PoolGuardArc>,
456 read_timeout: Option<Duration>,
457 total_deadline: Option<ResponseDeadline>,
458 read_sleep: Option<Pin<Box<tokio::time::Sleep>>>,
459 total_sleep: Option<Pin<Box<tokio::time::Sleep>>>,
460 started: bool,
461 done: bool,
462 }
463}
464
465impl std::fmt::Debug for NativeResponseBody {
466 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
467 f.debug_struct("NativeResponseBody")
468 .field("read_timeout", &self.read_timeout)
469 .field("done", &self.done)
470 .finish_non_exhaustive()
471 }
472}
473
474impl NativeResponseBody {
475 pub(crate) fn new<B>(
477 body: B,
478 lease: Option<PoolGuardArc>,
479 read_timeout: Option<Duration>,
480 total_deadline: Option<ResponseDeadline>,
481 ) -> Self
482 where
483 B: http_body::Body<Data = Bytes, Error = Error> + Send + 'static,
484 {
485 Self {
486 inner: Box::pin(body),
487 lease,
488 read_timeout,
489 total_deadline,
490 read_sleep: None,
491 total_sleep: None,
492 started: false,
493 done: false,
494 }
495 }
496
497 fn total_timeout_error(total: &ResponseDeadline) -> Error {
498 Error::Timeout {
499 phase: crate::timeout::TimeoutPhase::Total,
500 elapsed: total.elapsed(),
501 }
502 }
503}
504
505impl http_body::Body for NativeResponseBody {
506 type Data = Bytes;
507 type Error = Error;
508
509 fn poll_frame(
510 self: Pin<&mut Self>,
511 cx: &mut Context<'_>,
512 ) -> Poll<Option<Result<Frame<Self::Data>>>> {
513 let mut this = self.project();
514 if *this.done {
515 return Poll::Ready(None);
516 }
517
518 if !*this.started {
522 *this.started = true;
523 if let Some(total) = this.total_deadline.as_ref() {
524 if total.is_expired() {
525 *this.done = true;
526 this.lease.take();
527 return Poll::Ready(Some(Err(Self::total_timeout_error(total))));
528 }
529 let tokio_deadline = tokio::time::Instant::from_std(total.deadline());
530 *this.total_sleep = Some(Box::pin(tokio::time::sleep_until(tokio_deadline)));
531 }
532 if let Some(timeout) = *this.read_timeout {
533 *this.read_sleep = Some(Box::pin(tokio::time::sleep(timeout)));
534 }
535 }
536
537 if let Some(total) = this.total_deadline.as_ref() {
540 if total.is_expired() {
541 *this.read_sleep = None;
542 *this.total_sleep = None;
543 *this.done = true;
544 this.lease.take();
545 return Poll::Ready(Some(Err(Self::total_timeout_error(total))));
546 }
547 }
548
549 match this.inner.as_mut().poll_frame(cx) {
550 Poll::Ready(Some(Ok(frame))) => {
551 if let Some(total) = this.total_deadline.as_ref() {
554 if total.is_expired() {
555 *this.read_sleep = None;
556 *this.total_sleep = None;
557 *this.done = true;
558 this.lease.take();
559 return Poll::Ready(Some(Err(Self::total_timeout_error(total))));
560 }
561 }
562 if let (Some(read_sleep), Some(timeout)) =
563 (this.read_sleep.as_mut(), this.read_timeout.as_ref())
564 {
565 let next = crate::timeout::saturating_tokio_now_plus(*timeout);
566 read_sleep.as_mut().reset(next);
567 }
568 Poll::Ready(Some(Ok(frame)))
569 }
570 Poll::Ready(Some(Err(error))) => {
571 *this.done = true;
572 this.lease.take();
573 Poll::Ready(Some(Err(error)))
574 }
575 Poll::Ready(None) => {
576 *this.done = true;
577 this.lease.take();
578 Poll::Ready(None)
579 }
580 Poll::Pending => {
581 let total_fired = match this.total_sleep.as_mut() {
583 Some(sleep) => sleep.as_mut().poll(cx).is_ready(),
584 None => false,
585 };
586 if total_fired {
587 if let Some(total) = this.total_deadline.as_ref() {
588 let err = Self::total_timeout_error(total);
589 *this.read_sleep = None;
590 *this.total_sleep = None;
591 *this.done = true;
592 this.lease.take();
593 return Poll::Ready(Some(Err(err)));
594 }
595 }
596 if let (Some(read_sleep), Some(timeout)) =
597 (this.read_sleep.as_mut(), this.read_timeout.as_ref())
598 {
599 if read_sleep.as_mut().poll(cx).is_ready() {
600 *this.read_sleep = None;
601 *this.total_sleep = None;
602 *this.done = true;
603 this.lease.take();
604 return Poll::Ready(Some(Err(Error::Timeout {
605 phase: crate::timeout::TimeoutPhase::Read,
606 elapsed: *timeout,
607 })));
608 }
609 }
610 Poll::Pending
611 }
612 }
613 }
614
615 fn is_end_stream(&self) -> bool {
616 self.done || self.inner.as_ref().is_end_stream()
617 }
618
619 fn size_hint(&self) -> http_body::SizeHint {
620 self.inner.as_ref().size_hint()
621 }
622}
623
624pin_project! {
625 struct IncomingErrorBody {
626 #[pin]
627 inner: hyper::body::Incoming,
628 }
629}
630
631impl http_body::Body for IncomingErrorBody {
632 type Data = Bytes;
633 type Error = Error;
634
635 fn poll_frame(
636 self: Pin<&mut Self>,
637 cx: &mut Context<'_>,
638 ) -> Poll<Option<Result<Frame<Self::Data>>>> {
639 self.project()
640 .inner
641 .poll_frame(cx)
642 .map_ok(|frame| frame)
643 .map_err(|error| Error::Hyper(Arc::new(error)))
644 }
645
646 fn is_end_stream(&self) -> bool {
647 self.inner.is_end_stream()
648 }
649
650 fn size_hint(&self) -> http_body::SizeHint {
651 self.inner.size_hint()
652 }
653}
654
655impl NativeResponseBody {
656 pub(crate) fn from_incoming(
659 incoming: hyper::body::Incoming,
660 lease: Option<PoolGuardArc>,
661 read_timeout: Option<Duration>,
662 total_deadline: Option<ResponseDeadline>,
663 ) -> Self {
664 Self::new(
665 IncomingErrorBody { inner: incoming },
666 lease,
667 read_timeout,
668 total_deadline,
669 )
670 }
671}
672
673#[derive(Debug, Clone, Default)]
688pub struct SharedTrailers {
689 inner: Arc<std::sync::Mutex<Option<http::HeaderMap>>>,
690}
691
692impl SharedTrailers {
693 #[must_use]
695 pub fn new() -> Self {
696 Self {
697 inner: Arc::new(std::sync::Mutex::new(None)),
698 }
699 }
700
701 pub(crate) fn store(&self, trailers: http::HeaderMap) {
703 if let Ok(mut guard) = self.inner.lock() {
704 if guard.is_none() {
705 *guard = Some(trailers);
706 }
707 }
708 }
709
710 #[must_use]
717 pub fn get(&self) -> Option<http::HeaderMap> {
718 self.inner
719 .lock()
720 .unwrap_or_else(std::sync::PoisonError::into_inner)
721 .clone()
722 }
723}
724
725pin_project! {
726 struct LeasedResponseStream {
728 #[pin]
729 inner: BoxBytesStream,
730 _lease: Option<PoolGuardArc>,
731 }
732}
733
734impl Stream for LeasedResponseStream {
735 type Item = Result<Bytes>;
736
737 fn poll_next(
738 self: Pin<&mut Self>,
739 cx: &mut std::task::Context<'_>,
740 ) -> std::task::Poll<Option<Self::Item>> {
741 let mut this = self.project();
742 let item = this.inner.as_mut().poll_next(cx);
743 if matches!(item, std::task::Poll::Ready(Some(Err(_)) | None)) {
744 this._lease.take();
747 }
748 item
749 }
750}
751
752pin_project! {
753 struct LimitedResponseStream {
755 #[pin]
756 inner: BoxBytesStream,
757 max: usize,
758 decoded_bytes: usize,
759 limit_exceeded: bool,
760 }
761}
762
763impl Stream for LimitedResponseStream {
764 type Item = Result<Bytes>;
765
766 fn poll_next(
767 self: Pin<&mut Self>,
768 cx: &mut std::task::Context<'_>,
769 ) -> std::task::Poll<Option<Self::Item>> {
770 let mut this = self.project();
771 if *this.limit_exceeded {
777 return std::task::Poll::Ready(None);
778 }
779 match this.inner.as_mut().poll_next(cx) {
780 std::task::Poll::Ready(Some(Ok(chunk))) => {
781 *this.decoded_bytes = this.decoded_bytes.saturating_add(chunk.len());
782 if *this.decoded_bytes > *this.max {
783 *this.limit_exceeded = true;
784 std::task::Poll::Ready(Some(Err(Error::DecodedBodyTooLarge)))
785 } else {
786 std::task::Poll::Ready(Some(Ok(chunk)))
787 }
788 }
789 other => other,
790 }
791 }
792}
793
794pub enum ResponseBody {
811 Buffered {
813 bytes: Bytes,
815 },
816 Streaming {
818 stream: BoxBytesStream,
820 lease: Option<PoolGuardArc>,
826 },
827 EncodedStreaming {
830 stream: BoxBytesStream,
832 lease: Option<PoolGuardArc>,
837 content_encoding: String,
839 limit: DecompressionLimit,
841 },
842 Consumed,
844}
845
846impl std::fmt::Debug for ResponseBody {
847 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
848 match self {
849 Self::Buffered { bytes } => f
850 .debug_struct("Buffered")
851 .field("len", &bytes.len())
852 .finish(),
853 Self::Streaming { .. } => f.debug_struct("Streaming").finish_non_exhaustive(),
854 Self::EncodedStreaming { .. } => {
855 f.debug_struct("EncodedStreaming").finish_non_exhaustive()
856 }
857 Self::Consumed => f.debug_struct("Consumed").finish(),
858 }
859 }
860}
861
862impl ResponseBody {
863 #[must_use]
865 pub fn buffered(bytes: Bytes) -> Self {
866 Self::Buffered { bytes }
867 }
868
869 #[must_use]
871 pub fn streaming(stream: BoxBytesStream) -> Self {
872 Self::Streaming {
873 stream,
874 lease: None,
875 }
876 }
877
878 #[allow(dead_code)]
881 pub(crate) fn streaming_with_lease(stream: BoxBytesStream, lease: PoolGuardArc) -> Self {
882 Self::Streaming {
883 stream,
884 lease: Some(lease),
885 }
886 }
887
888 pub(crate) fn attach_lease(self, lease: PoolGuardArc) -> Self {
896 match self {
897 Self::Streaming { stream, .. } => Self::Streaming {
898 stream,
899 lease: Some(lease),
900 },
901 Self::EncodedStreaming {
902 stream,
903 content_encoding,
904 limit,
905 ..
906 } => Self::EncodedStreaming {
907 stream,
908 lease: Some(lease),
909 content_encoding,
910 limit,
911 },
912 other => {
913 drop(lease);
914 other
915 }
916 }
917 }
918
919 pub(crate) fn encoded_streaming(
921 stream: BoxBytesStream,
922 content_encoding: String,
923 limit: DecompressionLimit,
924 ) -> Self {
925 Self::EncodedStreaming {
926 stream,
927 lease: None,
928 content_encoding,
929 limit,
930 }
931 }
932
933 pub(crate) fn encoded_streaming_with_lease(
936 stream: BoxBytesStream,
937 lease: PoolGuardArc,
938 content_encoding: String,
939 limit: DecompressionLimit,
940 ) -> Self {
941 Self::EncodedStreaming {
942 stream,
943 lease: Some(lease),
944 content_encoding,
945 limit,
946 }
947 }
948
949 #[must_use]
951 pub fn is_empty(&self) -> bool {
952 match self {
953 Self::Buffered { bytes } => bytes.is_empty(),
954 Self::Streaming { .. } | Self::EncodedStreaming { .. } => false, Self::Consumed => true,
956 }
957 }
958
959 #[must_use]
964 pub fn len(&self) -> Option<usize> {
965 match self {
966 Self::Buffered { bytes } => Some(bytes.len()),
967 Self::Streaming { .. } | Self::EncodedStreaming { .. } => None,
968 Self::Consumed => Some(0),
969 }
970 }
971
972 #[must_use]
974 pub fn is_buffered(&self) -> bool {
975 matches!(self, Self::Buffered { .. })
976 }
977
978 pub async fn bytes(&mut self) -> Result<Bytes> {
995 if matches!(self, Self::Buffered { .. }) {
1000 let old = std::mem::replace(self, Self::Consumed);
1001 if let Self::Buffered { bytes } = old {
1002 return Ok(bytes);
1003 }
1004 return Err(Error::Body("response body state mismatch".into()));
1005 }
1006 let mut stream = self.take_stream(false)?;
1007 let mut buf = BytesMut::new();
1008 while let Some(chunk) = stream.next().await {
1009 buf.extend_from_slice(&chunk?);
1010 }
1011 Ok(buf.freeze())
1012 }
1013
1014 pub(crate) fn limit_decoded_size(self, max: usize) -> Result<Self> {
1016 match self {
1017 Self::Buffered { bytes } if bytes.len() > max => Err(Error::DecodedBodyTooLarge),
1018 Self::Buffered { bytes } => Ok(Self::Buffered { bytes }),
1019 Self::Streaming { stream, lease } => Ok(Self::Streaming {
1020 stream: Box::pin(LimitedResponseStream {
1021 inner: stream,
1022 max,
1023 decoded_bytes: 0,
1024 limit_exceeded: false,
1025 }),
1026 lease,
1027 }),
1028 Self::EncodedStreaming {
1029 stream,
1030 lease,
1031 content_encoding,
1032 limit: mut existing_limit,
1033 } => {
1034 existing_limit.max_decoded_body_size = Some(
1035 existing_limit
1036 .max_decoded_body_size
1037 .map_or(max, |current| current.min(max)),
1038 );
1039 Ok(Self::EncodedStreaming {
1040 stream,
1041 lease,
1042 content_encoding,
1043 limit: existing_limit,
1044 })
1045 }
1046 body @ Self::Consumed => Ok(body),
1047 }
1048 }
1049
1050 pub async fn text(&mut self) -> Result<String> {
1057 let bytes = self.bytes().await?;
1058 String::from_utf8(bytes.to_vec()).map_err(|e| Error::Body(e.to_string()))
1059 }
1060
1061 pub fn bytes_stream(&mut self) -> Result<BoxBytesStream> {
1070 self.take_stream(false)
1071 }
1072
1073 pub fn raw_bytes_stream(&mut self) -> Result<BoxBytesStream> {
1086 self.take_stream(true)
1087 }
1088
1089 fn take_stream(&mut self, raw: bool) -> Result<BoxBytesStream> {
1090 match self {
1091 Self::Buffered { bytes } => {
1092 let bytes = std::mem::take(bytes);
1093 *self = Self::Consumed;
1094 Ok(Box::pin(futures_util::stream::once(
1095 async move { Ok(bytes) },
1096 )))
1097 }
1098 Self::Streaming { .. } => {
1099 let old = std::mem::replace(self, Self::Consumed);
1100 if let Self::Streaming { stream, lease } = old {
1101 let (read_timeout, total_deadline) =
1108 lease.as_ref().map_or((None, None), |guard| {
1109 (
1110 guard.response_read_timeout(),
1111 guard.response_total_deadline(),
1112 )
1113 });
1114 let timed =
1115 crate::stream::body_timeout_stream(stream, read_timeout, total_deadline);
1116 Ok(Box::pin(LeasedResponseStream {
1117 inner: timed,
1118 _lease: lease,
1119 }))
1120 } else {
1121 Err(Error::Body("response body state mismatch".into()))
1125 }
1126 }
1127 Self::EncodedStreaming { .. } => {
1128 let old = std::mem::replace(self, Self::Consumed);
1129 if let Self::EncodedStreaming {
1130 stream,
1131 lease,
1132 content_encoding,
1133 limit,
1134 } = old
1135 {
1136 let (read_timeout, total_deadline) =
1137 lease.as_ref().map_or((None, None), |guard| {
1138 (
1139 guard.response_read_timeout(),
1140 guard.response_total_deadline(),
1141 )
1142 });
1143 let stream = if raw {
1144 Ok(stream)
1145 } else {
1146 crate::compression::decompress_stream(
1147 stream,
1148 Some(&content_encoding),
1149 true,
1150 limit,
1151 )
1152 }?;
1153 let timed =
1158 crate::stream::body_timeout_stream(stream, read_timeout, total_deadline);
1159 Ok(Box::pin(LeasedResponseStream {
1160 inner: timed,
1161 _lease: lease,
1162 }))
1163 } else {
1164 Err(Error::Body("response body state mismatch".into()))
1168 }
1169 }
1170 Self::Consumed => Err(Error::Body("streaming body already consumed".into())),
1171 }
1172 }
1173}
1174
1175#[cfg(test)]
1180mod tests {
1181 use super::*;
1182
1183 #[test]
1184 fn request_body_empty() {
1185 let body = RequestBody::Empty;
1186 assert!(body.is_empty());
1187 assert_eq!(body.len(), 0);
1188 assert!(body.has_known_length());
1189 assert!(body.is_replayable());
1190 }
1191
1192 #[test]
1193 fn request_body_bytes() {
1194 let body = RequestBody::from(Bytes::from("hello"));
1195 assert!(!body.is_empty());
1196 assert_eq!(body.len(), 5);
1197 assert!(body.has_known_length());
1198 assert!(body.is_replayable());
1199 }
1200
1201 #[test]
1202 fn request_body_stream_unknown_length() {
1203 let stream = futures_util::stream::empty::<Result<Bytes>>();
1204 let body = RequestBody::from_stream(stream, None);
1205 assert!(!body.is_empty());
1206 assert_eq!(body.len(), 0);
1207 assert!(!body.has_known_length());
1208 assert!(!body.is_replayable());
1209 }
1210
1211 #[test]
1212 fn request_body_stream_known_length() {
1213 let stream = futures_util::stream::empty::<Result<Bytes>>();
1214 let body = RequestBody::from_stream(stream, Some(1024));
1215 assert!(!body.is_empty());
1216 assert_eq!(body.len(), 1024);
1217 assert!(body.has_known_length());
1218 assert!(!body.is_replayable());
1219 }
1220
1221 #[test]
1222 fn try_clone_for_redirect_empty() {
1223 let body = RequestBody::Empty;
1224 let cloned = body.try_clone_for_redirect().unwrap();
1225 assert!(cloned.is_empty());
1226 }
1227
1228 #[test]
1229 fn try_clone_for_redirect_bytes() {
1230 let body = RequestBody::from(Bytes::from("hello"));
1231 let cloned = body.try_clone_for_redirect().unwrap();
1232 match cloned {
1233 RequestBody::Bytes(b) => assert_eq!(b, "hello"),
1234 _ => panic!("expected bytes"),
1235 }
1236 }
1237
1238 #[test]
1239 fn try_clone_for_redirect_stream_errors() {
1240 let stream = futures_util::stream::empty::<Result<Bytes>>();
1241 let body = RequestBody::from_stream(stream, None);
1242 assert!(body.try_clone_for_redirect().is_err());
1243 }
1244
1245 #[test]
1246 fn request_body_bytes_debug_redacts_contents() {
1247 let body = RequestBody::from(Bytes::from("form-password=hunter2"));
1248 let debug = format!("{body:?}");
1249 assert!(
1250 !debug.contains("hunter2"),
1251 "RequestBody Debug must not leak byte contents: {debug}"
1252 );
1253 assert!(
1254 debug.contains("Bytes(21)"),
1255 "RequestBody Debug reports the length only: {debug}"
1256 );
1257 }
1258
1259 #[test]
1260 fn response_body_buffered() {
1261 let body = ResponseBody::buffered(Bytes::from("hello"));
1262 assert!(!body.is_empty());
1263 assert_eq!(body.len(), Some(5));
1264 assert!(body.is_buffered());
1265 }
1266
1267 #[test]
1268 fn response_body_streaming() {
1269 let stream = Box::pin(futures_util::stream::empty());
1270 let body = ResponseBody::streaming(stream);
1271 assert!(!body.is_buffered());
1272 }
1273
1274 #[tokio::test]
1275 async fn response_body_buffered_bytes() {
1276 let mut body = ResponseBody::buffered(Bytes::from("hello world"));
1277 let bytes = body.bytes().await.unwrap();
1278 assert_eq!(bytes, "hello world");
1279 }
1280
1281 #[tokio::test]
1282 async fn response_body_buffered_text() {
1283 let mut body = ResponseBody::buffered(Bytes::from("hello"));
1284 let text = body.text().await.unwrap();
1285 assert_eq!(text, "hello");
1286 }
1287
1288 #[tokio::test]
1289 async fn response_body_buffered_stream_yields_single_chunk() {
1290 let mut body = ResponseBody::buffered(Bytes::from("data"));
1291 let mut stream = body.bytes_stream().unwrap();
1292 let chunk = stream.next().await.unwrap().unwrap();
1293 assert_eq!(chunk, "data");
1294 assert!(stream.next().await.is_none());
1295 }
1296
1297 #[tokio::test]
1298 async fn response_body_streaming_yields_chunks() {
1299 let chunks = vec![
1300 Ok(Bytes::from("a")),
1301 Ok(Bytes::from("bb")),
1302 Ok(Bytes::from("ccc")),
1303 ];
1304 let stream = Box::pin(futures_util::stream::iter(chunks));
1305 let mut body = ResponseBody::streaming(stream);
1306 let mut stream = body.bytes_stream().unwrap();
1307 assert_eq!(stream.next().await.unwrap().unwrap(), "a");
1308 assert_eq!(stream.next().await.unwrap().unwrap(), "bb");
1309 assert_eq!(stream.next().await.unwrap().unwrap(), "ccc");
1310 assert!(stream.next().await.is_none());
1311 }
1312
1313 #[tokio::test]
1314 async fn response_body_streaming_collect() {
1315 let chunks = vec![Ok(Bytes::from("hello ")), Ok(Bytes::from("world"))];
1316 let stream = Box::pin(futures_util::stream::iter(chunks));
1317 let mut body = ResponseBody::streaming(stream);
1318 let bytes = body.bytes().await.unwrap();
1319 assert_eq!(bytes, "hello world");
1320 }
1321
1322 #[tokio::test]
1323 async fn response_body_unencoded_size_limit_is_enforced() {
1324 let stream = Box::pin(futures_util::stream::iter(vec![
1325 Ok(Bytes::from("ab")),
1326 Ok(Bytes::from("cd")),
1327 ]));
1328 let mut body = ResponseBody::streaming(stream)
1329 .limit_decoded_size(3)
1330 .unwrap();
1331 assert_eq!(
1332 body.bytes().await.unwrap_err().kind(),
1333 "decoded_body_too_large"
1334 );
1335 }
1336
1337 #[test]
1338 fn response_body_buffered_size_limit_is_enforced() {
1339 let body = ResponseBody::buffered(Bytes::from_static(b"body"));
1340 assert_eq!(
1341 body.limit_decoded_size(3).unwrap_err().kind(),
1342 "decoded_body_too_large"
1343 );
1344 }
1345
1346 #[tokio::test]
1347 async fn response_body_double_consume_replaces() {
1348 let mut body = ResponseBody::buffered(Bytes::from("data"));
1349 let _ = body.bytes().await.unwrap();
1350 assert!(body.bytes().await.is_err());
1351 }
1352
1353 #[tokio::test]
1354 async fn response_body_double_stream_errors() {
1355 let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("x"))]));
1356 let mut body = ResponseBody::streaming(stream);
1357 let _ = body.bytes_stream().unwrap();
1358 let err = body.bytes_stream();
1359 assert!(err.is_err());
1360 }
1361
1362 #[cfg(feature = "compression-gzip")]
1363 fn gzip_bytes(input: &[u8]) -> Bytes {
1364 use std::io::Write;
1365
1366 let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast());
1367 encoder.write_all(input).unwrap();
1368 Bytes::from(encoder.finish().unwrap())
1369 }
1370
1371 #[cfg(feature = "compression-gzip")]
1372 #[tokio::test]
1373 async fn encoded_streaming_selects_raw_once() {
1374 let encoded = gzip_bytes(b"compressed body");
1375 let expected = encoded.clone();
1376 let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
1377 let mut body = ResponseBody::encoded_streaming(
1378 stream,
1379 "gzip".to_owned(),
1380 DecompressionLimit::default(),
1381 );
1382
1383 let mut raw = body.raw_bytes_stream().unwrap();
1384 assert_eq!(raw.next().await.unwrap().unwrap(), expected);
1385 assert!(raw.next().await.is_none());
1386 assert!(body.bytes_stream().is_err());
1387 }
1388
1389 #[cfg(feature = "compression-gzip")]
1390 #[tokio::test]
1391 async fn encoded_streaming_selects_decoded_once() {
1392 let encoded = gzip_bytes(b"compressed body");
1393 let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
1394 let mut body = ResponseBody::encoded_streaming(
1395 stream,
1396 "gzip".to_owned(),
1397 DecompressionLimit::default(),
1398 );
1399
1400 let mut decoded = body.bytes_stream().unwrap();
1401 assert_eq!(decoded.next().await.unwrap().unwrap(), "compressed body");
1402 assert!(decoded.next().await.is_none());
1403 assert!(body.raw_bytes_stream().is_err());
1404 }
1405
1406 #[cfg(feature = "compression-gzip")]
1407 #[tokio::test]
1408 async fn encoded_streaming_limit_is_applied_when_added_after_construction() {
1409 let encoded = gzip_bytes(b"compressed body");
1410 let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
1411 let mut body = ResponseBody::encoded_streaming(
1412 stream,
1413 "gzip".to_owned(),
1414 DecompressionLimit::default(),
1415 )
1416 .limit_decoded_size(3)
1417 .unwrap();
1418
1419 assert_eq!(
1420 body.bytes().await.unwrap_err().kind(),
1421 "decoded_body_too_large"
1422 );
1423 }
1424
1425 #[tokio::test]
1426 async fn uncompressed_raw_selection_is_passthrough() {
1427 let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("body"))]));
1428 let mut body = ResponseBody::streaming(stream);
1429 let mut raw = body.raw_bytes_stream().unwrap();
1430 assert_eq!(raw.next().await.unwrap().unwrap(), "body");
1431 assert!(raw.next().await.is_none());
1432 }
1433
1434 #[tokio::test]
1435 async fn response_body_buffered_double_bytes_errors() {
1436 let mut body = ResponseBody::buffered(Bytes::from("data"));
1437 let first = body.bytes().await.unwrap();
1438 assert_eq!(first, "data");
1439 assert!(body.bytes().await.is_err());
1440 }
1441
1442 #[tokio::test]
1443 async fn response_body_buffered_text_invalid_utf8() {
1444 let mut body = ResponseBody::buffered(Bytes::from(vec![0xFF, 0xFE]));
1445 let err = body.text().await;
1446 assert!(err.is_err());
1447 }
1448
1449 #[tokio::test]
1450 async fn response_body_streaming_bytes_then_stream_returns_empty() {
1451 let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("x"))]));
1452 let mut body = ResponseBody::streaming(stream);
1453 let _ = body.bytes().await.unwrap();
1454 assert!(body.bytes_stream().is_err());
1455 }
1456
1457 #[tokio::test]
1458 async fn response_body_consumed_is_empty() {
1459 let body = ResponseBody::Consumed;
1460 assert!(body.is_empty());
1461 assert_eq!(body.len(), Some(0));
1462 }
1463
1464 #[tokio::test]
1465 async fn response_body_buffered_bytes_stream_then_bytes_returns_empty() {
1466 let mut body = ResponseBody::buffered(Bytes::from("data"));
1467 let mut stream = body.bytes_stream().unwrap();
1468 let chunk = stream.next().await.unwrap().unwrap();
1469 assert_eq!(chunk, "data");
1470 assert!(stream.next().await.is_none());
1471 assert!(body.bytes().await.is_err());
1472 }
1473
1474 #[tokio::test]
1475 async fn into_http_body_empty_produces_empty_body() {
1476 use http_body::Body;
1477 use http_body_util::BodyExt;
1478
1479 let body = RequestBody::Empty;
1480 let mut hyper_body = body.into_http_body();
1481 let frame = futures_util::future::poll_fn(|cx| {
1482 use std::pin::Pin;
1483 Pin::new(&mut hyper_body).poll_frame(cx)
1484 })
1485 .await;
1486 assert!(frame.is_none());
1487 let _ = hyper_body.collect().await;
1488 }
1489
1490 #[tokio::test]
1491 async fn into_http_body_bytes_produces_one_data_frame() {
1492 use http_body::Body;
1493 use std::pin::Pin;
1494
1495 let body = RequestBody::from(Bytes::from("hello"));
1496 let mut hyper_body = body.into_http_body();
1497
1498 let frame = futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx))
1499 .await
1500 .expect("frame present")
1501 .expect("frame ok");
1502 assert!(frame.is_data());
1503 let data = frame.into_data().unwrap();
1504 assert_eq!(data, "hello");
1505
1506 let end =
1507 futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx)).await;
1508 assert!(end.is_none());
1509 }
1510
1511 #[tokio::test]
1512 async fn into_http_body_stream_pipes_through_without_eager_buffering() {
1513 use http_body::Body;
1514 use std::pin::Pin;
1515 use std::sync::atomic::{AtomicUsize, Ordering};
1516
1517 let polled = Arc::new(AtomicUsize::new(0));
1518 let polled_inner = polled.clone();
1519
1520 let chunks = vec![
1521 Ok(Bytes::from("a")),
1522 Ok(Bytes::from("b")),
1523 Ok(Bytes::from("c")),
1524 ];
1525 let chunk_stream = futures_util::stream::iter(chunks).inspect(move |_| {
1526 polled_inner.fetch_add(1, Ordering::SeqCst);
1527 });
1528 let body = RequestBody::from_stream(chunk_stream.map(|r| -> Result<Bytes> { r }), Some(3));
1529 let mut hyper_body = body.into_http_body();
1530
1531 let frame = futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx))
1532 .await
1533 .expect("frame present")
1534 .expect("frame ok");
1535 let data = frame.into_data().unwrap();
1536 assert_eq!(data, "a");
1537 assert_eq!(
1538 polled.load(Ordering::SeqCst),
1539 1,
1540 "stream should not be eagerly polled"
1541 );
1542 }
1543
1544 #[tokio::test]
1545 async fn limited_stream_fuses_after_limit_trips() {
1546 use futures_util::StreamExt;
1547 use std::sync::atomic::{AtomicUsize, Ordering};
1548
1549 let polls = Arc::new(AtomicUsize::new(0));
1550 let polls_inner = polls.clone();
1551 let tracked = futures_util::stream::iter(vec![
1552 Ok(Bytes::from("ab")),
1553 Ok(Bytes::from("cd")),
1554 Ok(Bytes::from("ef")),
1555 ])
1556 .inspect(move |_| {
1557 polls_inner.fetch_add(1, Ordering::SeqCst);
1558 });
1559 let inner: super::BoxBytesStream = Box::pin(tracked);
1560 let mut limited = super::LimitedResponseStream {
1561 inner,
1562 max: 3,
1563 decoded_bytes: 0,
1564 limit_exceeded: false,
1565 };
1566 assert!(limited.next().await.unwrap().is_ok());
1568 assert!(limited.next().await.unwrap().is_err());
1569 let polls_after_error = polls.load(Ordering::SeqCst);
1570 assert!(limited.next().await.is_none());
1572 assert_eq!(
1573 polls.load(Ordering::SeqCst),
1574 polls_after_error,
1575 "inner stream must not be re-polled after the limit trips"
1576 );
1577 }
1578
1579 #[tokio::test]
1580 async fn leased_stream_holds_pool_slot_until_drop() {
1581 let pool = crate::pool::Pool::new(crate::pool::PoolConfig {
1582 max_connections_per_host: Some(1),
1583 ..Default::default()
1584 });
1585 let origin = crate::pool::OriginKey::from_parts("http", "example.com", 80);
1586 let guard = pool.acquire(Some(&origin)).await.unwrap();
1587 let mut body = ResponseBody::streaming_with_lease(
1588 Box::pin(futures_util::stream::pending()),
1589 Arc::new(guard),
1590 );
1591 let stream = body.bytes_stream().unwrap();
1592
1593 assert!(tokio::time::timeout(
1594 std::time::Duration::from_millis(10),
1595 pool.acquire(Some(&origin))
1596 )
1597 .await
1598 .is_err());
1599
1600 drop(stream);
1601 assert!(tokio::time::timeout(
1602 std::time::Duration::from_millis(100),
1603 pool.acquire(Some(&origin))
1604 )
1605 .await
1606 .is_ok());
1607 }
1608}