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(
271 clippy::type_complexity,
272 reason = "the Hyper body type is fixed by the `http-body-util` API and cannot be named more simply without changing the transport contract"
273 )]
274 pub(crate) fn into_http_body(
275 self,
276 ) -> http_body_util::combinators::UnsyncBoxBody<Bytes, Box<dyn std::error::Error + Send + Sync>>
277 {
278 use http_body_util::combinators::UnsyncBoxBody;
279 use http_body_util::{BodyExt, Empty, Full, StreamBody};
280
281 match self {
282 Self::Empty => Empty::<Bytes>::new()
283 .map_err(|err| -> Box<dyn std::error::Error + Send + Sync> { match err {} })
284 .boxed_unsync(),
285 Self::Bytes(b) => Full::new(b)
286 .map_err(|err| -> Box<dyn std::error::Error + Send + Sync> { match err {} })
287 .boxed_unsync(),
288 Self::Stream { stream, .. } => {
289 let framed = stream.map(|chunk_result| {
290 chunk_result
291 .map(Frame::data)
292 .map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { Box::new(e) })
293 });
294 let body: UnsyncBoxBody<Bytes, Box<dyn std::error::Error + Send + Sync>> =
295 BodyExt::boxed_unsync(StreamBody::new(framed));
296 body
297 }
298 }
299 }
300}
301
302impl From<Bytes> for RequestBody {
303 fn from(b: Bytes) -> Self {
304 Self::Bytes(b)
305 }
306}
307
308impl From<Vec<u8>> for RequestBody {
309 fn from(v: Vec<u8>) -> Self {
310 Self::Bytes(Bytes::from(v))
311 }
312}
313
314impl From<&[u8]> for RequestBody {
315 fn from(s: &[u8]) -> Self {
316 Self::Bytes(Bytes::copy_from_slice(s))
317 }
318}
319
320impl From<String> for RequestBody {
321 fn from(s: String) -> Self {
322 Self::Bytes(Bytes::from(s))
323 }
324}
325
326impl From<&str> for RequestBody {
327 fn from(s: &str) -> Self {
328 Self::Bytes(Bytes::copy_from_slice(s.as_bytes()))
329 }
330}
331
332impl http_body::Body for RequestBody {
333 type Data = Bytes;
334 type Error = Error;
335
336 fn poll_frame(
343 mut self: Pin<&mut Self>,
344 cx: &mut Context<'_>,
345 ) -> Poll<Option<std::result::Result<Frame<Self::Data>, Self::Error>>> {
346 match &mut *self {
347 Self::Empty => Poll::Ready(None),
348 Self::Bytes(bytes) => {
349 if bytes.is_empty() {
350 return Poll::Ready(None);
351 }
352 let frame = std::mem::replace(bytes, Bytes::new());
353 Poll::Ready(Some(Ok(Frame::data(frame))))
354 }
355 Self::Stream { stream, .. } => match stream.as_mut().poll_next(cx) {
356 Poll::Ready(Some(Ok(chunk))) => Poll::Ready(Some(Ok(Frame::data(chunk)))),
357 Poll::Ready(Some(Err(error))) => Poll::Ready(Some(Err(error))),
358 Poll::Ready(None) => Poll::Ready(None),
359 Poll::Pending => Poll::Pending,
360 },
361 }
362 }
363
364 fn is_end_stream(&self) -> bool {
365 match self {
366 Self::Empty => true,
367 Self::Bytes(bytes) => bytes.is_empty(),
368 Self::Stream { .. } => false,
371 }
372 }
373
374 fn size_hint(&self) -> http_body::SizeHint {
375 let mut hint = http_body::SizeHint::new();
376 match self {
377 Self::Empty => hint.set_exact(0),
378 Self::Bytes(bytes) => hint.set_exact(u64::try_from(bytes.len()).unwrap_or(u64::MAX)),
379 Self::Stream {
380 length: Some(length),
381 ..
382 } => {
383 hint.set_exact(u64::try_from(*length).unwrap_or(u64::MAX));
384 }
385 Self::Stream { length: None, .. } => {}
386 }
387 hint
388 }
389}
390
391pin_project! {
392 #[must_use = "request bodies do nothing unless polled"]
399 pub(crate) struct NativeRequestBody {
400 #[pin]
401 inner: Pin<Box<dyn http_body::Body<Data = Bytes, Error = Box<dyn std::error::Error + Send + Sync>> + Send>>,
402 write_timeout: Option<Duration>,
403 #[pin]
404 timer: tokio::time::Sleep,
405 started: bool,
406 done: bool,
407 }
408}
409
410impl NativeRequestBody {
411 pub(crate) fn new(
412 inner: Pin<
413 Box<
414 dyn http_body::Body<Data = Bytes, Error = Box<dyn std::error::Error + Send + Sync>>
415 + Send,
416 >,
417 >,
418 write_timeout: Option<Duration>,
419 ) -> Self {
420 Self {
421 inner,
422 write_timeout,
423 timer: tokio::time::sleep(Duration::MAX),
424 started: false,
425 done: false,
426 }
427 }
428}
429
430impl http_body::Body for NativeRequestBody {
431 type Data = Bytes;
432 type Error = Box<dyn std::error::Error + Send + Sync>;
433
434 fn poll_frame(
435 self: Pin<&mut Self>,
436 cx: &mut Context<'_>,
437 ) -> Poll<Option<std::result::Result<Frame<Self::Data>, Self::Error>>> {
438 let mut this = self.project();
439 if *this.done {
440 return Poll::Ready(None);
441 }
442
443 if let Some(timeout) = *this.write_timeout {
444 if !*this.started {
445 *this.started = true;
446 let next = crate::timeout::saturating_tokio_now_plus(timeout);
447 this.timer.as_mut().reset(next);
448 }
449 }
450
451 match this.inner.as_mut().poll_frame(cx) {
452 Poll::Ready(Some(Ok(frame))) => {
453 if let Some(timeout) = *this.write_timeout {
454 let next = crate::timeout::saturating_tokio_now_plus(timeout);
455 this.timer.as_mut().reset(next);
456 }
457 Poll::Ready(Some(Ok(frame)))
458 }
459 Poll::Ready(Some(Err(error))) => {
460 *this.done = true;
461 Poll::Ready(Some(Err(error)))
462 }
463 Poll::Ready(None) => {
464 *this.done = true;
465 Poll::Ready(None)
466 }
467 Poll::Pending => {
468 if let Some(timeout) = *this.write_timeout {
469 if this.timer.as_mut().poll(cx).is_ready() {
470 *this.done = true;
471 return Poll::Ready(Some(Err(Box::new(Error::Timeout {
472 phase: crate::timeout::TimeoutPhase::Write,
473 elapsed: timeout,
474 }))));
475 }
476 }
477 Poll::Pending
478 }
479 }
480 }
481
482 fn is_end_stream(&self) -> bool {
483 self.done || self.inner.as_ref().is_end_stream()
484 }
485
486 fn size_hint(&self) -> http_body::SizeHint {
487 self.inner.as_ref().size_hint()
488 }
489}
490
491pub(crate) type PoolGuardArc = Arc<crate::pool::PoolGuard>;
498
499pin_project! {
500 pub struct NativeResponseBody {
515 #[pin]
516 inner: Pin<Box<dyn http_body::Body<Data = Bytes, Error = Error> + Send>>,
517 lease: Option<PoolGuardArc>,
518 read_timeout: Option<Duration>,
519 total_deadline: Option<ResponseDeadline>,
520 read_sleep: Option<Pin<Box<tokio::time::Sleep>>>,
521 total_sleep: Option<Pin<Box<tokio::time::Sleep>>>,
522 started: bool,
523 done: bool,
524 }
525}
526
527impl std::fmt::Debug for NativeResponseBody {
528 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
529 f.debug_struct("NativeResponseBody")
530 .field("read_timeout", &self.read_timeout)
531 .field("done", &self.done)
532 .finish_non_exhaustive()
533 }
534}
535
536impl NativeResponseBody {
537 pub(crate) fn new<B>(
539 body: B,
540 lease: Option<PoolGuardArc>,
541 read_timeout: Option<Duration>,
542 total_deadline: Option<ResponseDeadline>,
543 ) -> Self
544 where
545 B: http_body::Body<Data = Bytes, Error = Error> + Send + 'static,
546 {
547 Self {
548 inner: Box::pin(body),
549 lease,
550 read_timeout,
551 total_deadline,
552 read_sleep: None,
553 total_sleep: None,
554 started: false,
555 done: false,
556 }
557 }
558
559 fn total_timeout_error(total: &ResponseDeadline) -> Error {
560 Error::Timeout {
561 phase: crate::timeout::TimeoutPhase::Total,
562 elapsed: total.elapsed(),
563 }
564 }
565}
566
567impl http_body::Body for NativeResponseBody {
568 type Data = Bytes;
569 type Error = Error;
570
571 fn poll_frame(
572 self: Pin<&mut Self>,
573 cx: &mut Context<'_>,
574 ) -> Poll<Option<Result<Frame<Self::Data>>>> {
575 let mut this = self.project();
576 if *this.done {
577 return Poll::Ready(None);
578 }
579
580 if !*this.started {
584 *this.started = true;
585 if let Some(total) = this.total_deadline.as_ref() {
586 if total.is_expired() {
587 *this.done = true;
588 this.lease.take();
589 return Poll::Ready(Some(Err(Self::total_timeout_error(total))));
590 }
591 let tokio_deadline = tokio::time::Instant::from_std(total.deadline());
592 *this.total_sleep = Some(Box::pin(tokio::time::sleep_until(tokio_deadline)));
593 }
594 if let Some(timeout) = *this.read_timeout {
595 *this.read_sleep = Some(Box::pin(tokio::time::sleep(timeout)));
596 }
597 }
598
599 if let Some(total) = this.total_deadline.as_ref() {
602 if total.is_expired() {
603 *this.read_sleep = None;
604 *this.total_sleep = None;
605 *this.done = true;
606 this.lease.take();
607 return Poll::Ready(Some(Err(Self::total_timeout_error(total))));
608 }
609 }
610
611 match this.inner.as_mut().poll_frame(cx) {
612 Poll::Ready(Some(Ok(frame))) => {
613 if let Some(total) = this.total_deadline.as_ref() {
616 if total.is_expired() {
617 *this.read_sleep = None;
618 *this.total_sleep = None;
619 *this.done = true;
620 this.lease.take();
621 return Poll::Ready(Some(Err(Self::total_timeout_error(total))));
622 }
623 }
624 if let (Some(read_sleep), Some(timeout)) =
625 (this.read_sleep.as_mut(), this.read_timeout.as_ref())
626 {
627 let next = crate::timeout::saturating_tokio_now_plus(*timeout);
628 read_sleep.as_mut().reset(next);
629 }
630 Poll::Ready(Some(Ok(frame)))
631 }
632 Poll::Ready(Some(Err(error))) => {
633 *this.done = true;
634 this.lease.take();
635 Poll::Ready(Some(Err(error)))
636 }
637 Poll::Ready(None) => {
638 *this.done = true;
639 this.lease.take();
640 Poll::Ready(None)
641 }
642 Poll::Pending => {
643 let total_fired = match this.total_sleep.as_mut() {
645 Some(sleep) => sleep.as_mut().poll(cx).is_ready(),
646 None => false,
647 };
648 if total_fired {
649 if let Some(total) = this.total_deadline.as_ref() {
650 let err = Self::total_timeout_error(total);
651 *this.read_sleep = None;
652 *this.total_sleep = None;
653 *this.done = true;
654 this.lease.take();
655 return Poll::Ready(Some(Err(err)));
656 }
657 }
658 if let (Some(read_sleep), Some(timeout)) =
659 (this.read_sleep.as_mut(), this.read_timeout.as_ref())
660 {
661 if read_sleep.as_mut().poll(cx).is_ready() {
662 *this.read_sleep = None;
663 *this.total_sleep = None;
664 *this.done = true;
665 this.lease.take();
666 return Poll::Ready(Some(Err(Error::Timeout {
667 phase: crate::timeout::TimeoutPhase::Read,
668 elapsed: *timeout,
669 })));
670 }
671 }
672 Poll::Pending
673 }
674 }
675 }
676
677 fn is_end_stream(&self) -> bool {
678 self.done || self.inner.as_ref().is_end_stream()
679 }
680
681 fn size_hint(&self) -> http_body::SizeHint {
682 self.inner.as_ref().size_hint()
683 }
684}
685
686pin_project! {
687 struct IncomingErrorBody {
688 #[pin]
689 inner: hyper::body::Incoming,
690 }
691}
692
693impl http_body::Body for IncomingErrorBody {
694 type Data = Bytes;
695 type Error = Error;
696
697 fn poll_frame(
698 self: Pin<&mut Self>,
699 cx: &mut Context<'_>,
700 ) -> Poll<Option<Result<Frame<Self::Data>>>> {
701 self.project()
702 .inner
703 .poll_frame(cx)
704 .map_ok(|frame| frame)
705 .map_err(|error| Error::Hyper(Arc::new(error)))
706 }
707
708 fn is_end_stream(&self) -> bool {
709 self.inner.is_end_stream()
710 }
711
712 fn size_hint(&self) -> http_body::SizeHint {
713 self.inner.size_hint()
714 }
715}
716
717impl NativeResponseBody {
718 pub(crate) fn from_incoming(
721 incoming: hyper::body::Incoming,
722 lease: Option<PoolGuardArc>,
723 read_timeout: Option<Duration>,
724 total_deadline: Option<ResponseDeadline>,
725 ) -> Self {
726 Self::new(
727 IncomingErrorBody { inner: incoming },
728 lease,
729 read_timeout,
730 total_deadline,
731 )
732 }
733}
734
735#[derive(Debug, Clone, Default)]
750pub struct SharedTrailers {
751 inner: Arc<std::sync::Mutex<Option<http::HeaderMap>>>,
752}
753
754impl SharedTrailers {
755 #[must_use]
757 pub fn new() -> Self {
758 Self {
759 inner: Arc::new(std::sync::Mutex::new(None)),
760 }
761 }
762
763 pub(crate) fn store(&self, trailers: http::HeaderMap) {
765 if let Ok(mut guard) = self.inner.lock() {
766 if guard.is_none() {
767 *guard = Some(trailers);
768 }
769 }
770 }
771
772 #[must_use]
779 pub fn get(&self) -> Option<http::HeaderMap> {
780 self.inner
781 .lock()
782 .unwrap_or_else(std::sync::PoisonError::into_inner)
783 .clone()
784 }
785}
786
787pin_project! {
788 struct LeasedResponseStream {
790 #[pin]
791 inner: BoxBytesStream,
792 _lease: Option<PoolGuardArc>,
793 }
794}
795
796impl Stream for LeasedResponseStream {
797 type Item = Result<Bytes>;
798
799 fn poll_next(
800 self: Pin<&mut Self>,
801 cx: &mut std::task::Context<'_>,
802 ) -> std::task::Poll<Option<Self::Item>> {
803 let mut this = self.project();
804 let item = this.inner.as_mut().poll_next(cx);
805 if matches!(item, std::task::Poll::Ready(Some(Err(_)) | None)) {
806 this._lease.take();
809 }
810 item
811 }
812}
813
814pin_project! {
815 struct LimitedResponseStream {
817 #[pin]
818 inner: BoxBytesStream,
819 max: usize,
820 decoded_bytes: usize,
821 limit_exceeded: bool,
822 }
823}
824
825impl Stream for LimitedResponseStream {
826 type Item = Result<Bytes>;
827
828 fn poll_next(
829 self: Pin<&mut Self>,
830 cx: &mut std::task::Context<'_>,
831 ) -> std::task::Poll<Option<Self::Item>> {
832 let mut this = self.project();
833 if *this.limit_exceeded {
839 return std::task::Poll::Ready(None);
840 }
841 match this.inner.as_mut().poll_next(cx) {
842 std::task::Poll::Ready(Some(Ok(chunk))) => {
843 *this.decoded_bytes = this.decoded_bytes.saturating_add(chunk.len());
844 if *this.decoded_bytes > *this.max {
845 *this.limit_exceeded = true;
846 std::task::Poll::Ready(Some(Err(Error::DecodedBodyTooLarge)))
847 } else {
848 std::task::Poll::Ready(Some(Ok(chunk)))
849 }
850 }
851 other => other,
852 }
853 }
854}
855
856pub enum ResponseBody {
873 Buffered {
875 bytes: Bytes,
877 },
878 Streaming {
880 stream: BoxBytesStream,
882 lease: Option<PoolGuardArc>,
888 },
889 EncodedStreaming {
892 stream: BoxBytesStream,
894 lease: Option<PoolGuardArc>,
899 content_encoding: String,
901 limit: DecompressionLimit,
903 },
904 Consumed,
906}
907
908impl std::fmt::Debug for ResponseBody {
909 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
910 match self {
911 Self::Buffered { bytes } => f
912 .debug_struct("Buffered")
913 .field("len", &bytes.len())
914 .finish(),
915 Self::Streaming { .. } => f.debug_struct("Streaming").finish_non_exhaustive(),
916 Self::EncodedStreaming { .. } => {
917 f.debug_struct("EncodedStreaming").finish_non_exhaustive()
918 }
919 Self::Consumed => f.debug_struct("Consumed").finish(),
920 }
921 }
922}
923
924impl ResponseBody {
925 #[must_use]
927 pub fn buffered(bytes: Bytes) -> Self {
928 Self::Buffered { bytes }
929 }
930
931 #[must_use]
933 pub fn streaming(stream: BoxBytesStream) -> Self {
934 Self::Streaming {
935 stream,
936 lease: None,
937 }
938 }
939
940 #[allow(
943 dead_code,
944 reason = "the lease-carrying constructor is only called by the pool-lease test; production bodies attach their lease through `attach_lease`"
945 )]
946 pub(crate) fn streaming_with_lease(stream: BoxBytesStream, lease: PoolGuardArc) -> Self {
947 Self::Streaming {
948 stream,
949 lease: Some(lease),
950 }
951 }
952
953 pub(crate) fn attach_lease(self, lease: PoolGuardArc) -> Self {
961 match self {
962 Self::Streaming { stream, .. } => Self::Streaming {
963 stream,
964 lease: Some(lease),
965 },
966 Self::EncodedStreaming {
967 stream,
968 content_encoding,
969 limit,
970 ..
971 } => Self::EncodedStreaming {
972 stream,
973 lease: Some(lease),
974 content_encoding,
975 limit,
976 },
977 other => {
978 drop(lease);
979 other
980 }
981 }
982 }
983
984 pub(crate) fn encoded_streaming(
986 stream: BoxBytesStream,
987 content_encoding: String,
988 limit: DecompressionLimit,
989 ) -> Self {
990 Self::EncodedStreaming {
991 stream,
992 lease: None,
993 content_encoding,
994 limit,
995 }
996 }
997
998 pub(crate) fn encoded_streaming_with_lease(
1001 stream: BoxBytesStream,
1002 lease: PoolGuardArc,
1003 content_encoding: String,
1004 limit: DecompressionLimit,
1005 ) -> Self {
1006 Self::EncodedStreaming {
1007 stream,
1008 lease: Some(lease),
1009 content_encoding,
1010 limit,
1011 }
1012 }
1013
1014 #[must_use]
1016 pub fn is_empty(&self) -> bool {
1017 match self {
1018 Self::Buffered { bytes } => bytes.is_empty(),
1019 Self::Streaming { .. } | Self::EncodedStreaming { .. } => false, Self::Consumed => true,
1021 }
1022 }
1023
1024 #[must_use]
1029 pub fn len(&self) -> Option<usize> {
1030 match self {
1031 Self::Buffered { bytes } => Some(bytes.len()),
1032 Self::Streaming { .. } | Self::EncodedStreaming { .. } => None,
1033 Self::Consumed => Some(0),
1034 }
1035 }
1036
1037 #[must_use]
1039 pub fn is_buffered(&self) -> bool {
1040 matches!(self, Self::Buffered { .. })
1041 }
1042
1043 pub async fn bytes(&mut self) -> Result<Bytes> {
1060 if matches!(self, Self::Buffered { .. }) {
1065 let old = std::mem::replace(self, Self::Consumed);
1066 if let Self::Buffered { bytes } = old {
1067 return Ok(bytes);
1068 }
1069 return Err(Error::Body("response body state mismatch".into()));
1070 }
1071 let mut stream = self.take_stream(false)?;
1072 let mut buf = BytesMut::new();
1073 while let Some(chunk) = stream.next().await {
1074 buf.extend_from_slice(&chunk?);
1075 }
1076 Ok(buf.freeze())
1077 }
1078
1079 pub(crate) fn limit_decoded_size(self, max: usize) -> Result<Self> {
1081 match self {
1082 Self::Buffered { bytes } if bytes.len() > max => Err(Error::DecodedBodyTooLarge),
1083 Self::Buffered { bytes } => Ok(Self::Buffered { bytes }),
1084 Self::Streaming { stream, lease } => Ok(Self::Streaming {
1085 stream: Box::pin(LimitedResponseStream {
1086 inner: stream,
1087 max,
1088 decoded_bytes: 0,
1089 limit_exceeded: false,
1090 }),
1091 lease,
1092 }),
1093 Self::EncodedStreaming {
1094 stream,
1095 lease,
1096 content_encoding,
1097 limit: mut existing_limit,
1098 } => {
1099 existing_limit.max_decoded_body_size = Some(
1100 existing_limit
1101 .max_decoded_body_size
1102 .map_or(max, |current| current.min(max)),
1103 );
1104 Ok(Self::EncodedStreaming {
1105 stream,
1106 lease,
1107 content_encoding,
1108 limit: existing_limit,
1109 })
1110 }
1111 body @ Self::Consumed => Ok(body),
1112 }
1113 }
1114
1115 pub async fn text(&mut self) -> Result<String> {
1122 let bytes = self.bytes().await?;
1123 String::from_utf8(bytes.to_vec()).map_err(|e| Error::Body(e.to_string()))
1124 }
1125
1126 pub fn bytes_stream(&mut self) -> Result<BoxBytesStream> {
1135 self.take_stream(false)
1136 }
1137
1138 pub fn raw_bytes_stream(&mut self) -> Result<BoxBytesStream> {
1151 self.take_stream(true)
1152 }
1153
1154 fn take_stream(&mut self, raw: bool) -> Result<BoxBytesStream> {
1155 match self {
1156 Self::Buffered { bytes } => {
1157 let bytes = std::mem::take(bytes);
1158 *self = Self::Consumed;
1159 Ok(Box::pin(futures_util::stream::once(
1160 async move { Ok(bytes) },
1161 )))
1162 }
1163 Self::Streaming { .. } => {
1164 let old = std::mem::replace(self, Self::Consumed);
1165 if let Self::Streaming { stream, lease } = old {
1166 let (read_timeout, total_deadline) =
1173 lease.as_ref().map_or((None, None), |guard| {
1174 (
1175 guard.response_read_timeout(),
1176 guard.response_total_deadline(),
1177 )
1178 });
1179 let timed =
1180 crate::stream::body_timeout_stream(stream, read_timeout, total_deadline);
1181 Ok(Box::pin(LeasedResponseStream {
1182 inner: timed,
1183 _lease: lease,
1184 }))
1185 } else {
1186 Err(Error::Body("response body state mismatch".into()))
1190 }
1191 }
1192 Self::EncodedStreaming { .. } => {
1193 let old = std::mem::replace(self, Self::Consumed);
1194 if let Self::EncodedStreaming {
1195 stream,
1196 lease,
1197 content_encoding,
1198 limit,
1199 } = old
1200 {
1201 let (read_timeout, total_deadline) =
1202 lease.as_ref().map_or((None, None), |guard| {
1203 (
1204 guard.response_read_timeout(),
1205 guard.response_total_deadline(),
1206 )
1207 });
1208 let stream = if raw {
1209 Ok(stream)
1210 } else {
1211 crate::compression::decompress_stream(
1212 stream,
1213 Some(&content_encoding),
1214 true,
1215 limit,
1216 )
1217 }?;
1218 let timed =
1223 crate::stream::body_timeout_stream(stream, read_timeout, total_deadline);
1224 Ok(Box::pin(LeasedResponseStream {
1225 inner: timed,
1226 _lease: lease,
1227 }))
1228 } else {
1229 Err(Error::Body("response body state mismatch".into()))
1233 }
1234 }
1235 Self::Consumed => Err(Error::Body("streaming body already consumed".into())),
1236 }
1237 }
1238}
1239
1240#[cfg(test)]
1245mod tests {
1246 use super::*;
1247
1248 #[test]
1249 fn request_body_empty() {
1250 let body = RequestBody::Empty;
1251 assert!(body.is_empty());
1252 assert_eq!(body.len(), 0);
1253 assert!(body.has_known_length());
1254 assert!(body.is_replayable());
1255 }
1256
1257 #[test]
1258 fn request_body_bytes() {
1259 let body = RequestBody::from(Bytes::from("hello"));
1260 assert!(!body.is_empty());
1261 assert_eq!(body.len(), 5);
1262 assert!(body.has_known_length());
1263 assert!(body.is_replayable());
1264 }
1265
1266 #[test]
1267 fn request_body_stream_unknown_length() {
1268 let stream = futures_util::stream::empty::<Result<Bytes>>();
1269 let body = RequestBody::from_stream(stream, None);
1270 assert!(!body.is_empty());
1271 assert_eq!(body.len(), 0);
1272 assert!(!body.has_known_length());
1273 assert!(!body.is_replayable());
1274 }
1275
1276 #[test]
1277 fn request_body_stream_known_length() {
1278 let stream = futures_util::stream::empty::<Result<Bytes>>();
1279 let body = RequestBody::from_stream(stream, Some(1024));
1280 assert!(!body.is_empty());
1281 assert_eq!(body.len(), 1024);
1282 assert!(body.has_known_length());
1283 assert!(!body.is_replayable());
1284 }
1285
1286 #[test]
1287 fn try_clone_for_redirect_empty() {
1288 let body = RequestBody::Empty;
1289 let cloned = body.try_clone_for_redirect().unwrap();
1290 assert!(cloned.is_empty());
1291 }
1292
1293 #[test]
1294 fn try_clone_for_redirect_bytes() {
1295 let body = RequestBody::from(Bytes::from("hello"));
1296 let cloned = body.try_clone_for_redirect().unwrap();
1297 match cloned {
1298 RequestBody::Bytes(b) => assert_eq!(b, "hello"),
1299 _ => panic!("expected bytes"),
1300 }
1301 }
1302
1303 #[test]
1304 fn try_clone_for_redirect_stream_errors() {
1305 let stream = futures_util::stream::empty::<Result<Bytes>>();
1306 let body = RequestBody::from_stream(stream, None);
1307 assert!(body.try_clone_for_redirect().is_err());
1308 }
1309
1310 #[test]
1311 fn request_body_bytes_debug_redacts_contents() {
1312 let body = RequestBody::from(Bytes::from("form-password=hunter2"));
1313 let debug = format!("{body:?}");
1314 assert!(
1315 !debug.contains("hunter2"),
1316 "RequestBody Debug must not leak byte contents: {debug}"
1317 );
1318 assert!(
1319 debug.contains("Bytes(21)"),
1320 "RequestBody Debug reports the length only: {debug}"
1321 );
1322 }
1323
1324 #[test]
1325 fn response_body_buffered() {
1326 let body = ResponseBody::buffered(Bytes::from("hello"));
1327 assert!(!body.is_empty());
1328 assert_eq!(body.len(), Some(5));
1329 assert!(body.is_buffered());
1330 }
1331
1332 #[test]
1333 fn response_body_streaming() {
1334 let stream = Box::pin(futures_util::stream::empty());
1335 let body = ResponseBody::streaming(stream);
1336 assert!(!body.is_buffered());
1337 }
1338
1339 #[tokio::test]
1340 async fn response_body_buffered_bytes() {
1341 let mut body = ResponseBody::buffered(Bytes::from("hello world"));
1342 let bytes = body.bytes().await.unwrap();
1343 assert_eq!(bytes, "hello world");
1344 }
1345
1346 #[tokio::test]
1347 async fn response_body_buffered_text() {
1348 let mut body = ResponseBody::buffered(Bytes::from("hello"));
1349 let text = body.text().await.unwrap();
1350 assert_eq!(text, "hello");
1351 }
1352
1353 #[tokio::test]
1354 async fn response_body_buffered_stream_yields_single_chunk() {
1355 let mut body = ResponseBody::buffered(Bytes::from("data"));
1356 let mut stream = body.bytes_stream().unwrap();
1357 let chunk = stream.next().await.unwrap().unwrap();
1358 assert_eq!(chunk, "data");
1359 assert!(stream.next().await.is_none());
1360 }
1361
1362 #[tokio::test]
1363 async fn response_body_streaming_yields_chunks() {
1364 let chunks = vec![
1365 Ok(Bytes::from("a")),
1366 Ok(Bytes::from("bb")),
1367 Ok(Bytes::from("ccc")),
1368 ];
1369 let stream = Box::pin(futures_util::stream::iter(chunks));
1370 let mut body = ResponseBody::streaming(stream);
1371 let mut stream = body.bytes_stream().unwrap();
1372 assert_eq!(stream.next().await.unwrap().unwrap(), "a");
1373 assert_eq!(stream.next().await.unwrap().unwrap(), "bb");
1374 assert_eq!(stream.next().await.unwrap().unwrap(), "ccc");
1375 assert!(stream.next().await.is_none());
1376 }
1377
1378 #[tokio::test]
1379 async fn response_body_streaming_collect() {
1380 let chunks = vec![Ok(Bytes::from("hello ")), Ok(Bytes::from("world"))];
1381 let stream = Box::pin(futures_util::stream::iter(chunks));
1382 let mut body = ResponseBody::streaming(stream);
1383 let bytes = body.bytes().await.unwrap();
1384 assert_eq!(bytes, "hello world");
1385 }
1386
1387 #[tokio::test]
1388 async fn response_body_unencoded_size_limit_is_enforced() {
1389 let stream = Box::pin(futures_util::stream::iter(vec![
1390 Ok(Bytes::from("ab")),
1391 Ok(Bytes::from("cd")),
1392 ]));
1393 let mut body = ResponseBody::streaming(stream)
1394 .limit_decoded_size(3)
1395 .unwrap();
1396 assert_eq!(
1397 body.bytes().await.unwrap_err().kind(),
1398 "decoded_body_too_large"
1399 );
1400 }
1401
1402 #[test]
1403 fn response_body_buffered_size_limit_is_enforced() {
1404 let body = ResponseBody::buffered(Bytes::from_static(b"body"));
1405 assert_eq!(
1406 body.limit_decoded_size(3).unwrap_err().kind(),
1407 "decoded_body_too_large"
1408 );
1409 }
1410
1411 #[tokio::test]
1412 async fn response_body_double_consume_replaces() {
1413 let mut body = ResponseBody::buffered(Bytes::from("data"));
1414 let _ = body.bytes().await.unwrap();
1415 assert!(body.bytes().await.is_err());
1416 }
1417
1418 #[tokio::test]
1419 async fn response_body_double_stream_errors() {
1420 let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("x"))]));
1421 let mut body = ResponseBody::streaming(stream);
1422 let _ = body.bytes_stream().unwrap();
1423 let err = body.bytes_stream();
1424 assert!(err.is_err());
1425 }
1426
1427 #[cfg(feature = "compression-gzip")]
1428 fn gzip_bytes(input: &[u8]) -> Bytes {
1429 use std::io::Write;
1430
1431 let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast());
1432 encoder.write_all(input).unwrap();
1433 Bytes::from(encoder.finish().unwrap())
1434 }
1435
1436 #[cfg(feature = "compression-gzip")]
1437 #[tokio::test]
1438 async fn encoded_streaming_selects_raw_once() {
1439 let encoded = gzip_bytes(b"compressed body");
1440 let expected = encoded.clone();
1441 let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
1442 let mut body = ResponseBody::encoded_streaming(
1443 stream,
1444 "gzip".to_owned(),
1445 DecompressionLimit::default(),
1446 );
1447
1448 let mut raw = body.raw_bytes_stream().unwrap();
1449 assert_eq!(raw.next().await.unwrap().unwrap(), expected);
1450 assert!(raw.next().await.is_none());
1451 assert!(body.bytes_stream().is_err());
1452 }
1453
1454 #[cfg(feature = "compression-gzip")]
1455 #[tokio::test]
1456 async fn encoded_streaming_selects_decoded_once() {
1457 let encoded = gzip_bytes(b"compressed body");
1458 let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
1459 let mut body = ResponseBody::encoded_streaming(
1460 stream,
1461 "gzip".to_owned(),
1462 DecompressionLimit::default(),
1463 );
1464
1465 let mut decoded = body.bytes_stream().unwrap();
1466 assert_eq!(decoded.next().await.unwrap().unwrap(), "compressed body");
1467 assert!(decoded.next().await.is_none());
1468 assert!(body.raw_bytes_stream().is_err());
1469 }
1470
1471 #[cfg(feature = "compression-gzip")]
1472 #[tokio::test]
1473 async fn encoded_streaming_limit_is_applied_when_added_after_construction() {
1474 let encoded = gzip_bytes(b"compressed body");
1475 let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
1476 let mut body = ResponseBody::encoded_streaming(
1477 stream,
1478 "gzip".to_owned(),
1479 DecompressionLimit::default(),
1480 )
1481 .limit_decoded_size(3)
1482 .unwrap();
1483
1484 assert_eq!(
1485 body.bytes().await.unwrap_err().kind(),
1486 "decoded_body_too_large"
1487 );
1488 }
1489
1490 #[tokio::test]
1491 async fn uncompressed_raw_selection_is_passthrough() {
1492 let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("body"))]));
1493 let mut body = ResponseBody::streaming(stream);
1494 let mut raw = body.raw_bytes_stream().unwrap();
1495 assert_eq!(raw.next().await.unwrap().unwrap(), "body");
1496 assert!(raw.next().await.is_none());
1497 }
1498
1499 #[tokio::test]
1500 async fn response_body_buffered_double_bytes_errors() {
1501 let mut body = ResponseBody::buffered(Bytes::from("data"));
1502 let first = body.bytes().await.unwrap();
1503 assert_eq!(first, "data");
1504 assert!(body.bytes().await.is_err());
1505 }
1506
1507 #[tokio::test]
1508 async fn response_body_buffered_text_invalid_utf8() {
1509 let mut body = ResponseBody::buffered(Bytes::from(vec![0xFF, 0xFE]));
1510 let err = body.text().await;
1511 assert!(err.is_err());
1512 }
1513
1514 #[tokio::test]
1515 async fn response_body_streaming_bytes_then_stream_returns_empty() {
1516 let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("x"))]));
1517 let mut body = ResponseBody::streaming(stream);
1518 let _ = body.bytes().await.unwrap();
1519 assert!(body.bytes_stream().is_err());
1520 }
1521
1522 #[tokio::test]
1523 async fn response_body_consumed_is_empty() {
1524 let body = ResponseBody::Consumed;
1525 assert!(body.is_empty());
1526 assert_eq!(body.len(), Some(0));
1527 }
1528
1529 #[tokio::test]
1530 async fn response_body_buffered_bytes_stream_then_bytes_returns_empty() {
1531 let mut body = ResponseBody::buffered(Bytes::from("data"));
1532 let mut stream = body.bytes_stream().unwrap();
1533 let chunk = stream.next().await.unwrap().unwrap();
1534 assert_eq!(chunk, "data");
1535 assert!(stream.next().await.is_none());
1536 assert!(body.bytes().await.is_err());
1537 }
1538
1539 #[tokio::test]
1540 async fn into_http_body_empty_produces_empty_body() {
1541 use http_body::Body;
1542 use http_body_util::BodyExt;
1543
1544 let body = RequestBody::Empty;
1545 let mut hyper_body = body.into_http_body();
1546 let frame = futures_util::future::poll_fn(|cx| {
1547 use std::pin::Pin;
1548 Pin::new(&mut hyper_body).poll_frame(cx)
1549 })
1550 .await;
1551 assert!(frame.is_none());
1552 let _ = hyper_body.collect().await;
1553 }
1554
1555 #[tokio::test]
1556 async fn into_http_body_bytes_produces_one_data_frame() {
1557 use http_body::Body;
1558 use std::pin::Pin;
1559
1560 let body = RequestBody::from(Bytes::from("hello"));
1561 let mut hyper_body = body.into_http_body();
1562
1563 let frame = futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx))
1564 .await
1565 .expect("frame present")
1566 .expect("frame ok");
1567 assert!(frame.is_data());
1568 let data = frame.into_data().unwrap();
1569 assert_eq!(data, "hello");
1570
1571 let end =
1572 futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx)).await;
1573 assert!(end.is_none());
1574 }
1575
1576 #[tokio::test]
1577 async fn into_http_body_stream_pipes_through_without_eager_buffering() {
1578 use http_body::Body;
1579 use std::pin::Pin;
1580 use std::sync::atomic::{AtomicUsize, Ordering};
1581
1582 let polled = Arc::new(AtomicUsize::new(0));
1583 let polled_inner = polled.clone();
1584
1585 let chunks = vec![
1586 Ok(Bytes::from("a")),
1587 Ok(Bytes::from("b")),
1588 Ok(Bytes::from("c")),
1589 ];
1590 let chunk_stream = futures_util::stream::iter(chunks).inspect(move |_| {
1591 polled_inner.fetch_add(1, Ordering::SeqCst);
1592 });
1593 let body = RequestBody::from_stream(chunk_stream.map(|r| -> Result<Bytes> { r }), Some(3));
1594 let mut hyper_body = body.into_http_body();
1595
1596 let frame = futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx))
1597 .await
1598 .expect("frame present")
1599 .expect("frame ok");
1600 let data = frame.into_data().unwrap();
1601 assert_eq!(data, "a");
1602 assert_eq!(
1603 polled.load(Ordering::SeqCst),
1604 1,
1605 "stream should not be eagerly polled"
1606 );
1607 }
1608
1609 #[tokio::test]
1610 async fn limited_stream_fuses_after_limit_trips() {
1611 use futures_util::StreamExt;
1612 use std::sync::atomic::{AtomicUsize, Ordering};
1613
1614 let polls = Arc::new(AtomicUsize::new(0));
1615 let polls_inner = polls.clone();
1616 let tracked = futures_util::stream::iter(vec![
1617 Ok(Bytes::from("ab")),
1618 Ok(Bytes::from("cd")),
1619 Ok(Bytes::from("ef")),
1620 ])
1621 .inspect(move |_| {
1622 polls_inner.fetch_add(1, Ordering::SeqCst);
1623 });
1624 let inner: super::BoxBytesStream = Box::pin(tracked);
1625 let mut limited = super::LimitedResponseStream {
1626 inner,
1627 max: 3,
1628 decoded_bytes: 0,
1629 limit_exceeded: false,
1630 };
1631 assert!(limited.next().await.unwrap().is_ok());
1633 assert!(limited.next().await.unwrap().is_err());
1634 let polls_after_error = polls.load(Ordering::SeqCst);
1635 assert!(limited.next().await.is_none());
1637 assert_eq!(
1638 polls.load(Ordering::SeqCst),
1639 polls_after_error,
1640 "inner stream must not be re-polled after the limit trips"
1641 );
1642 }
1643
1644 #[tokio::test]
1645 async fn leased_stream_holds_pool_slot_until_drop() {
1646 let pool = crate::pool::Pool::new(crate::pool::PoolConfig {
1647 max_connections_per_host: Some(1),
1648 ..Default::default()
1649 });
1650 let origin = crate::pool::OriginKey::from_parts("http", "example.com", 80);
1651 let guard = pool.acquire(Some(&origin)).await.unwrap();
1652 let mut body = ResponseBody::streaming_with_lease(
1653 Box::pin(futures_util::stream::pending()),
1654 Arc::new(guard),
1655 );
1656 let stream = body.bytes_stream().unwrap();
1657
1658 assert!(tokio::time::timeout(
1659 std::time::Duration::from_millis(10),
1660 pool.acquire(Some(&origin))
1661 )
1662 .await
1663 .is_err());
1664
1665 drop(stream);
1666 assert!(tokio::time::timeout(
1667 std::time::Duration::from_millis(100),
1668 pool.acquire(Some(&origin))
1669 )
1670 .await
1671 .is_ok());
1672 }
1673}