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};
53
54pub type BoxBytesStream = Pin<Box<dyn Stream<Item = Result<Bytes>> + Send>>;
60
61#[derive(Default)]
70pub enum RequestBody {
71 #[default]
73 Empty,
74 Bytes(Bytes),
76 Stream {
84 stream: BoxBytesStream,
86 length: Option<usize>,
88 },
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
96pub enum ReplayClass {
97 Immutable,
99 Seekable,
101 OneShot,
103 Consumed,
105}
106
107impl std::fmt::Debug for RequestBody {
108 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109 match self {
110 Self::Empty => f.debug_tuple("Empty").finish(),
111 Self::Bytes(b) => f.debug_tuple("Bytes").field(&b.len()).finish(),
115 Self::Stream { length, .. } => f
116 .debug_struct("Stream")
117 .field("length", length)
118 .finish_non_exhaustive(),
119 }
120 }
121}
122
123impl RequestBody {
124 #[must_use]
132 pub fn is_empty(&self) -> bool {
133 match self {
134 Self::Empty => true,
135 Self::Bytes(b) => b.is_empty(),
136 Self::Stream { .. } => false,
137 }
138 }
139
140 #[must_use]
147 pub fn len(&self) -> usize {
148 match self {
149 Self::Empty => 0,
150 Self::Bytes(b) => b.len(),
151 Self::Stream { length, .. } => length.unwrap_or(0),
152 }
153 }
154
155 #[must_use]
157 pub fn has_known_length(&self) -> bool {
158 match self {
159 Self::Empty | Self::Bytes(_) => true,
160 Self::Stream { length, .. } => length.is_some(),
161 }
162 }
163
164 #[must_use]
168 pub fn is_replayable(&self) -> bool {
169 matches!(self, Self::Empty | Self::Bytes(_))
170 }
171
172 #[must_use]
174 pub fn replay_class(&self) -> ReplayClass {
175 match self {
176 Self::Empty | Self::Bytes(_) => ReplayClass::Immutable,
177 Self::Stream { .. } => ReplayClass::OneShot,
178 }
179 }
180
181 pub fn try_clone_for_redirect(&self) -> Result<Self> {
191 match self {
192 Self::Empty => Ok(Self::Empty),
193 Self::Bytes(b) => Ok(Self::Bytes(b.clone())),
194 Self::Stream { .. } => Err(Error::BodyNotReplayableForRedirect),
195 }
196 }
197
198 pub(crate) fn try_clone_for_retry(&self) -> Result<Self> {
210 match self {
211 Self::Empty => Ok(Self::Empty),
212 Self::Bytes(b) => Ok(Self::Bytes(b.clone())),
213 Self::Stream { .. } => Err(Error::BodyNotReplayableForRetry),
214 }
215 }
216
217 pub async fn into_bytes(self) -> Result<Bytes> {
226 use futures_util::StreamExt;
227 match self {
228 Self::Empty => Ok(Bytes::new()),
229 Self::Bytes(b) => Ok(b),
230 Self::Stream { mut stream, length } => {
231 let mut buf = BytesMut::with_capacity(length.unwrap_or(0));
232 while let Some(chunk) = stream.next().await {
233 buf.extend_from_slice(&chunk?);
234 }
235 Ok(buf.freeze())
236 }
237 }
238 }
239
240 pub fn from_stream<S>(stream: S, length: Option<usize>) -> Self
242 where
243 S: Stream<Item = Result<Bytes>> + Send + 'static,
244 {
245 Self::Stream {
246 stream: Box::pin(stream),
247 length,
248 }
249 }
250
251 #[allow(clippy::type_complexity)]
258 pub(crate) fn into_http_body(
259 self,
260 ) -> http_body_util::combinators::UnsyncBoxBody<Bytes, Box<dyn std::error::Error + Send + Sync>>
261 {
262 use http_body_util::combinators::UnsyncBoxBody;
263 use http_body_util::{BodyExt, Empty, Full, StreamBody};
264
265 match self {
266 Self::Empty => Empty::<Bytes>::new()
267 .map_err(|err| -> Box<dyn std::error::Error + Send + Sync> { match err {} })
268 .boxed_unsync(),
269 Self::Bytes(b) => Full::new(b)
270 .map_err(|err| -> Box<dyn std::error::Error + Send + Sync> { match err {} })
271 .boxed_unsync(),
272 Self::Stream { stream, .. } => {
273 let framed = stream.map(|chunk_result| {
274 chunk_result
275 .map(Frame::data)
276 .map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { Box::new(e) })
277 });
278 let body: UnsyncBoxBody<Bytes, Box<dyn std::error::Error + Send + Sync>> =
279 BodyExt::boxed_unsync(StreamBody::new(framed));
280 body
281 }
282 }
283 }
284}
285
286impl From<Bytes> for RequestBody {
287 fn from(b: Bytes) -> Self {
288 Self::Bytes(b)
289 }
290}
291
292impl From<Vec<u8>> for RequestBody {
293 fn from(v: Vec<u8>) -> Self {
294 Self::Bytes(Bytes::from(v))
295 }
296}
297
298impl From<&[u8]> for RequestBody {
299 fn from(s: &[u8]) -> Self {
300 Self::Bytes(Bytes::copy_from_slice(s))
301 }
302}
303
304impl From<String> for RequestBody {
305 fn from(s: String) -> Self {
306 Self::Bytes(Bytes::from(s))
307 }
308}
309
310impl From<&str> for RequestBody {
311 fn from(s: &str) -> Self {
312 Self::Bytes(Bytes::copy_from_slice(s.as_bytes()))
313 }
314}
315
316pin_project! {
317 #[must_use = "request bodies do nothing unless polled"]
324 pub(crate) struct NativeRequestBody {
325 #[pin]
326 inner: Pin<Box<dyn http_body::Body<Data = Bytes, Error = Box<dyn std::error::Error + Send + Sync>> + Send>>,
327 write_timeout: Option<Duration>,
328 #[pin]
329 timer: tokio::time::Sleep,
330 started: bool,
331 done: bool,
332 }
333}
334
335impl NativeRequestBody {
336 pub(crate) fn new(
337 inner: Pin<
338 Box<
339 dyn http_body::Body<Data = Bytes, Error = Box<dyn std::error::Error + Send + Sync>>
340 + Send,
341 >,
342 >,
343 write_timeout: Option<Duration>,
344 ) -> Self {
345 Self {
346 inner,
347 write_timeout,
348 timer: tokio::time::sleep(Duration::MAX),
349 started: false,
350 done: false,
351 }
352 }
353}
354
355impl http_body::Body for NativeRequestBody {
356 type Data = Bytes;
357 type Error = Box<dyn std::error::Error + Send + Sync>;
358
359 fn poll_frame(
360 self: Pin<&mut Self>,
361 cx: &mut Context<'_>,
362 ) -> Poll<Option<std::result::Result<Frame<Self::Data>, Self::Error>>> {
363 let mut this = self.project();
364 if *this.done {
365 return Poll::Ready(None);
366 }
367
368 if let Some(timeout) = *this.write_timeout {
369 if !*this.started {
370 *this.started = true;
371 this.timer
372 .as_mut()
373 .reset(tokio::time::Instant::now() + timeout);
374 }
375 }
376
377 match this.inner.as_mut().poll_frame(cx) {
378 Poll::Ready(Some(Ok(frame))) => {
379 if let Some(timeout) = *this.write_timeout {
380 this.timer
381 .as_mut()
382 .reset(tokio::time::Instant::now() + timeout);
383 }
384 Poll::Ready(Some(Ok(frame)))
385 }
386 Poll::Ready(Some(Err(error))) => {
387 *this.done = true;
388 Poll::Ready(Some(Err(error)))
389 }
390 Poll::Ready(None) => {
391 *this.done = true;
392 Poll::Ready(None)
393 }
394 Poll::Pending => {
395 if let Some(timeout) = *this.write_timeout {
396 if this.timer.as_mut().poll(cx).is_ready() {
397 *this.done = true;
398 return Poll::Ready(Some(Err(Box::new(Error::Timeout {
399 phase: crate::timeout::TimeoutPhase::Write,
400 elapsed: timeout,
401 }))));
402 }
403 }
404 Poll::Pending
405 }
406 }
407 }
408
409 fn is_end_stream(&self) -> bool {
410 self.done || self.inner.as_ref().is_end_stream()
411 }
412
413 fn size_hint(&self) -> http_body::SizeHint {
414 self.inner.as_ref().size_hint()
415 }
416}
417
418pub(crate) type PoolGuardArc = Arc<crate::pool::PoolGuard>;
425
426pin_project! {
427 pub struct NativeResponseBody {
435 #[pin]
436 inner: Pin<Box<dyn http_body::Body<Data = Bytes, Error = Error> + Send>>,
437 lease: Option<PoolGuardArc>,
438 read_timeout: Option<Duration>,
439 #[pin]
440 timer: tokio::time::Sleep,
441 started: bool,
442 done: bool,
443 }
444}
445
446impl std::fmt::Debug for NativeResponseBody {
447 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
448 f.debug_struct("NativeResponseBody")
449 .field("read_timeout", &self.read_timeout)
450 .field("done", &self.done)
451 .finish_non_exhaustive()
452 }
453}
454
455impl NativeResponseBody {
456 pub(crate) fn new<B>(body: B, lease: PoolGuardArc, read_timeout: Option<Duration>) -> Self
458 where
459 B: http_body::Body<Data = Bytes, Error = Error> + Send + 'static,
460 {
461 Self {
462 inner: Box::pin(body),
463 lease: Some(lease),
464 read_timeout,
465 timer: tokio::time::sleep(Duration::MAX),
466 started: false,
467 done: false,
468 }
469 }
470}
471
472impl http_body::Body for NativeResponseBody {
473 type Data = Bytes;
474 type Error = Error;
475
476 fn poll_frame(
477 self: Pin<&mut Self>,
478 cx: &mut Context<'_>,
479 ) -> Poll<Option<Result<Frame<Self::Data>>>> {
480 let mut this = self.project();
481 if *this.done {
482 return Poll::Ready(None);
483 }
484
485 if let Some(timeout) = *this.read_timeout {
486 if !*this.started {
487 *this.started = true;
488 this.timer
489 .as_mut()
490 .reset(tokio::time::Instant::now() + timeout);
491 }
492 }
493
494 match this.inner.as_mut().poll_frame(cx) {
495 Poll::Ready(Some(Ok(frame))) => {
496 if let Some(timeout) = *this.read_timeout {
497 this.timer
498 .as_mut()
499 .reset(tokio::time::Instant::now() + timeout);
500 }
501 Poll::Ready(Some(Ok(frame)))
502 }
503 Poll::Ready(Some(Err(error))) => {
504 *this.done = true;
505 this.lease.take();
506 Poll::Ready(Some(Err(error)))
507 }
508 Poll::Ready(None) => {
509 *this.done = true;
510 this.lease.take();
511 Poll::Ready(None)
512 }
513 Poll::Pending => {
514 if let Some(timeout) = *this.read_timeout {
515 if this.timer.as_mut().poll(cx).is_ready() {
516 *this.done = true;
517 this.lease.take();
518 return Poll::Ready(Some(Err(Error::Timeout {
519 phase: crate::timeout::TimeoutPhase::Read,
520 elapsed: timeout,
521 })));
522 }
523 }
524 Poll::Pending
525 }
526 }
527 }
528
529 fn is_end_stream(&self) -> bool {
530 self.done || self.inner.as_ref().is_end_stream()
531 }
532
533 fn size_hint(&self) -> http_body::SizeHint {
534 self.inner.as_ref().size_hint()
535 }
536}
537
538pin_project! {
539 struct IncomingErrorBody {
540 #[pin]
541 inner: hyper::body::Incoming,
542 }
543}
544
545impl http_body::Body for IncomingErrorBody {
546 type Data = Bytes;
547 type Error = Error;
548
549 fn poll_frame(
550 self: Pin<&mut Self>,
551 cx: &mut Context<'_>,
552 ) -> Poll<Option<Result<Frame<Self::Data>>>> {
553 self.project()
554 .inner
555 .poll_frame(cx)
556 .map_ok(|frame| frame)
557 .map_err(|error| Error::Hyper(Arc::new(error)))
558 }
559
560 fn is_end_stream(&self) -> bool {
561 self.inner.is_end_stream()
562 }
563
564 fn size_hint(&self) -> http_body::SizeHint {
565 self.inner.size_hint()
566 }
567}
568
569impl NativeResponseBody {
570 pub(crate) fn from_incoming(
573 incoming: hyper::body::Incoming,
574 lease: PoolGuardArc,
575 read_timeout: Option<Duration>,
576 ) -> Self {
577 Self::new(IncomingErrorBody { inner: incoming }, lease, read_timeout)
578 }
579}
580
581#[derive(Debug, Clone, Default)]
596pub struct SharedTrailers {
597 inner: Arc<std::sync::Mutex<Option<http::HeaderMap>>>,
598}
599
600impl SharedTrailers {
601 #[must_use]
603 pub fn new() -> Self {
604 Self {
605 inner: Arc::new(std::sync::Mutex::new(None)),
606 }
607 }
608
609 pub(crate) fn store(&self, trailers: http::HeaderMap) {
611 if let Ok(mut guard) = self.inner.lock() {
612 if guard.is_none() {
613 *guard = Some(trailers);
614 }
615 }
616 }
617
618 #[must_use]
625 pub fn get(&self) -> Option<http::HeaderMap> {
626 self.inner.lock().ok().and_then(|guard| guard.clone())
627 }
628}
629
630pin_project! {
631 struct LeasedResponseStream {
633 #[pin]
634 inner: BoxBytesStream,
635 _lease: Option<PoolGuardArc>,
636 }
637}
638
639impl Stream for LeasedResponseStream {
640 type Item = Result<Bytes>;
641
642 fn poll_next(
643 self: Pin<&mut Self>,
644 cx: &mut std::task::Context<'_>,
645 ) -> std::task::Poll<Option<Self::Item>> {
646 let mut this = self.project();
647 let item = this.inner.as_mut().poll_next(cx);
648 if matches!(item, std::task::Poll::Ready(Some(Err(_)) | None)) {
649 this._lease.take();
652 }
653 item
654 }
655}
656
657pin_project! {
658 struct LimitedResponseStream {
660 #[pin]
661 inner: BoxBytesStream,
662 max: usize,
663 decoded_bytes: usize,
664 limit_exceeded: bool,
665 }
666}
667
668impl Stream for LimitedResponseStream {
669 type Item = Result<Bytes>;
670
671 fn poll_next(
672 self: Pin<&mut Self>,
673 cx: &mut std::task::Context<'_>,
674 ) -> std::task::Poll<Option<Self::Item>> {
675 let mut this = self.project();
676 if *this.limit_exceeded {
682 return std::task::Poll::Ready(None);
683 }
684 match this.inner.as_mut().poll_next(cx) {
685 std::task::Poll::Ready(Some(Ok(chunk))) => {
686 *this.decoded_bytes = this.decoded_bytes.saturating_add(chunk.len());
687 if *this.decoded_bytes > *this.max {
688 *this.limit_exceeded = true;
689 std::task::Poll::Ready(Some(Err(Error::DecodedBodyTooLarge)))
690 } else {
691 std::task::Poll::Ready(Some(Ok(chunk)))
692 }
693 }
694 other => other,
695 }
696 }
697}
698
699pub enum ResponseBody {
716 Buffered {
718 bytes: Bytes,
720 },
721 Streaming {
723 stream: BoxBytesStream,
725 lease: Option<PoolGuardArc>,
727 },
728 EncodedStreaming {
731 stream: BoxBytesStream,
733 lease: Option<PoolGuardArc>,
735 content_encoding: String,
737 limit: DecompressionLimit,
739 },
740 Consumed,
742}
743
744impl std::fmt::Debug for ResponseBody {
745 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
746 match self {
747 Self::Buffered { bytes } => f
748 .debug_struct("Buffered")
749 .field("len", &bytes.len())
750 .finish(),
751 Self::Streaming { .. } => f.debug_struct("Streaming").finish_non_exhaustive(),
752 Self::EncodedStreaming { .. } => {
753 f.debug_struct("EncodedStreaming").finish_non_exhaustive()
754 }
755 Self::Consumed => f.debug_struct("Consumed").finish(),
756 }
757 }
758}
759
760impl ResponseBody {
761 #[must_use]
763 pub fn buffered(bytes: Bytes) -> Self {
764 Self::Buffered { bytes }
765 }
766
767 #[must_use]
769 pub fn streaming(stream: BoxBytesStream) -> Self {
770 Self::Streaming {
771 stream,
772 lease: None,
773 }
774 }
775
776 pub(crate) fn streaming_with_lease(stream: BoxBytesStream, lease: PoolGuardArc) -> Self {
779 Self::Streaming {
780 stream,
781 lease: Some(lease),
782 }
783 }
784
785 pub(crate) fn encoded_streaming(
787 stream: BoxBytesStream,
788 content_encoding: String,
789 limit: DecompressionLimit,
790 ) -> Self {
791 Self::EncodedStreaming {
792 stream,
793 lease: None,
794 content_encoding,
795 limit,
796 }
797 }
798
799 pub(crate) fn encoded_streaming_with_lease(
802 stream: BoxBytesStream,
803 lease: PoolGuardArc,
804 content_encoding: String,
805 limit: DecompressionLimit,
806 ) -> Self {
807 Self::EncodedStreaming {
808 stream,
809 lease: Some(lease),
810 content_encoding,
811 limit,
812 }
813 }
814
815 #[must_use]
817 pub fn is_empty(&self) -> bool {
818 match self {
819 Self::Buffered { bytes } => bytes.is_empty(),
820 Self::Streaming { .. } | Self::EncodedStreaming { .. } => false, Self::Consumed => true,
822 }
823 }
824
825 #[must_use]
830 pub fn len(&self) -> Option<usize> {
831 match self {
832 Self::Buffered { bytes } => Some(bytes.len()),
833 Self::Streaming { .. } | Self::EncodedStreaming { .. } => None,
834 Self::Consumed => Some(0),
835 }
836 }
837
838 #[must_use]
840 pub fn is_buffered(&self) -> bool {
841 matches!(self, Self::Buffered { .. })
842 }
843
844 pub async fn bytes(&mut self) -> Result<Bytes> {
861 let old = std::mem::replace(self, Self::Consumed);
862 match old {
863 Self::Buffered { bytes } => Ok(bytes),
864 Self::Streaming {
865 mut stream, lease, ..
866 } => {
867 let mut buf = BytesMut::new();
868 while let Some(chunk) = stream.next().await {
869 buf.extend_from_slice(&chunk?);
870 }
871 drop(lease);
872 Ok(buf.freeze())
873 }
874 Self::EncodedStreaming {
875 stream,
876 lease,
877 content_encoding,
878 limit,
879 } => {
880 let mut stream = crate::compression::decompress_stream(
881 stream,
882 Some(&content_encoding),
883 true,
884 limit,
885 )?;
886 let mut buf = BytesMut::new();
887 while let Some(chunk) = stream.next().await {
888 buf.extend_from_slice(&chunk?);
889 }
890 drop(lease);
891 Ok(buf.freeze())
892 }
893 Self::Consumed => Err(Error::Body("body already consumed".into())),
894 }
895 }
896
897 pub(crate) fn limit_decoded_size(self, max: usize) -> Result<Self> {
899 match self {
900 Self::Buffered { bytes } if bytes.len() > max => Err(Error::DecodedBodyTooLarge),
901 Self::Buffered { bytes } => Ok(Self::Buffered { bytes }),
902 Self::Streaming { stream, lease } => Ok(Self::Streaming {
903 stream: Box::pin(LimitedResponseStream {
904 inner: stream,
905 max,
906 decoded_bytes: 0,
907 limit_exceeded: false,
908 }),
909 lease,
910 }),
911 Self::EncodedStreaming {
912 stream,
913 lease,
914 content_encoding,
915 limit: mut existing_limit,
916 } => {
917 existing_limit.max_decoded_body_size = Some(
918 existing_limit
919 .max_decoded_body_size
920 .map_or(max, |current| current.min(max)),
921 );
922 Ok(Self::EncodedStreaming {
923 stream,
924 lease,
925 content_encoding,
926 limit: existing_limit,
927 })
928 }
929 body @ Self::Consumed => Ok(body),
930 }
931 }
932
933 pub async fn text(&mut self) -> Result<String> {
940 let bytes = self.bytes().await?;
941 String::from_utf8(bytes.to_vec()).map_err(|e| Error::Body(e.to_string()))
942 }
943
944 pub fn bytes_stream(&mut self) -> Result<BoxBytesStream> {
953 self.take_stream(false)
954 }
955
956 pub fn raw_bytes_stream(&mut self) -> Result<BoxBytesStream> {
969 self.take_stream(true)
970 }
971
972 fn take_stream(&mut self, raw: bool) -> Result<BoxBytesStream> {
973 match self {
974 Self::Buffered { bytes } => {
975 let bytes = std::mem::take(bytes);
976 *self = Self::Consumed;
977 Ok(Box::pin(futures_util::stream::once(
978 async move { Ok(bytes) },
979 )))
980 }
981 Self::Streaming { .. } => {
982 let old = std::mem::replace(self, Self::Consumed);
983 if let Self::Streaming { stream, lease } = old {
984 Ok(Box::pin(LeasedResponseStream {
985 inner: stream,
986 _lease: lease,
987 }))
988 } else {
989 Err(Error::Body("response body state mismatch".into()))
993 }
994 }
995 Self::EncodedStreaming { .. } => {
996 let old = std::mem::replace(self, Self::Consumed);
997 if let Self::EncodedStreaming {
998 stream,
999 lease,
1000 content_encoding,
1001 limit,
1002 } = old
1003 {
1004 let stream = if raw {
1005 Ok(stream)
1006 } else {
1007 crate::compression::decompress_stream(
1008 stream,
1009 Some(&content_encoding),
1010 true,
1011 limit,
1012 )
1013 }?;
1014 Ok(Box::pin(LeasedResponseStream {
1015 inner: stream,
1016 _lease: lease,
1017 }))
1018 } else {
1019 Err(Error::Body("response body state mismatch".into()))
1023 }
1024 }
1025 Self::Consumed => Err(Error::Body("streaming body already consumed".into())),
1026 }
1027 }
1028}
1029
1030#[cfg(test)]
1035mod tests {
1036 use super::*;
1037
1038 #[test]
1039 fn request_body_empty() {
1040 let body = RequestBody::Empty;
1041 assert!(body.is_empty());
1042 assert_eq!(body.len(), 0);
1043 assert!(body.has_known_length());
1044 assert!(body.is_replayable());
1045 }
1046
1047 #[test]
1048 fn request_body_bytes() {
1049 let body = RequestBody::from(Bytes::from("hello"));
1050 assert!(!body.is_empty());
1051 assert_eq!(body.len(), 5);
1052 assert!(body.has_known_length());
1053 assert!(body.is_replayable());
1054 }
1055
1056 #[test]
1057 fn request_body_stream_unknown_length() {
1058 let stream = futures_util::stream::empty::<Result<Bytes>>();
1059 let body = RequestBody::from_stream(stream, None);
1060 assert!(!body.is_empty());
1061 assert_eq!(body.len(), 0);
1062 assert!(!body.has_known_length());
1063 assert!(!body.is_replayable());
1064 }
1065
1066 #[test]
1067 fn request_body_stream_known_length() {
1068 let stream = futures_util::stream::empty::<Result<Bytes>>();
1069 let body = RequestBody::from_stream(stream, Some(1024));
1070 assert!(!body.is_empty());
1071 assert_eq!(body.len(), 1024);
1072 assert!(body.has_known_length());
1073 assert!(!body.is_replayable());
1074 }
1075
1076 #[test]
1077 fn try_clone_for_redirect_empty() {
1078 let body = RequestBody::Empty;
1079 let cloned = body.try_clone_for_redirect().unwrap();
1080 assert!(cloned.is_empty());
1081 }
1082
1083 #[test]
1084 fn try_clone_for_redirect_bytes() {
1085 let body = RequestBody::from(Bytes::from("hello"));
1086 let cloned = body.try_clone_for_redirect().unwrap();
1087 match cloned {
1088 RequestBody::Bytes(b) => assert_eq!(b, "hello"),
1089 _ => panic!("expected bytes"),
1090 }
1091 }
1092
1093 #[test]
1094 fn try_clone_for_redirect_stream_errors() {
1095 let stream = futures_util::stream::empty::<Result<Bytes>>();
1096 let body = RequestBody::from_stream(stream, None);
1097 assert!(body.try_clone_for_redirect().is_err());
1098 }
1099
1100 #[test]
1101 fn request_body_bytes_debug_redacts_contents() {
1102 let body = RequestBody::from(Bytes::from("form-password=hunter2"));
1103 let debug = format!("{body:?}");
1104 assert!(
1105 !debug.contains("hunter2"),
1106 "RequestBody Debug must not leak byte contents: {debug}"
1107 );
1108 assert!(
1109 debug.contains("Bytes(21)"),
1110 "RequestBody Debug reports the length only: {debug}"
1111 );
1112 }
1113
1114 #[test]
1115 fn response_body_buffered() {
1116 let body = ResponseBody::buffered(Bytes::from("hello"));
1117 assert!(!body.is_empty());
1118 assert_eq!(body.len(), Some(5));
1119 assert!(body.is_buffered());
1120 }
1121
1122 #[test]
1123 fn response_body_streaming() {
1124 let stream = Box::pin(futures_util::stream::empty());
1125 let body = ResponseBody::streaming(stream);
1126 assert!(!body.is_buffered());
1127 }
1128
1129 #[tokio::test]
1130 async fn response_body_buffered_bytes() {
1131 let mut body = ResponseBody::buffered(Bytes::from("hello world"));
1132 let bytes = body.bytes().await.unwrap();
1133 assert_eq!(bytes, "hello world");
1134 }
1135
1136 #[tokio::test]
1137 async fn response_body_buffered_text() {
1138 let mut body = ResponseBody::buffered(Bytes::from("hello"));
1139 let text = body.text().await.unwrap();
1140 assert_eq!(text, "hello");
1141 }
1142
1143 #[tokio::test]
1144 async fn response_body_buffered_stream_yields_single_chunk() {
1145 let mut body = ResponseBody::buffered(Bytes::from("data"));
1146 let mut stream = body.bytes_stream().unwrap();
1147 let chunk = stream.next().await.unwrap().unwrap();
1148 assert_eq!(chunk, "data");
1149 assert!(stream.next().await.is_none());
1150 }
1151
1152 #[tokio::test]
1153 async fn response_body_streaming_yields_chunks() {
1154 let chunks = vec![
1155 Ok(Bytes::from("a")),
1156 Ok(Bytes::from("bb")),
1157 Ok(Bytes::from("ccc")),
1158 ];
1159 let stream = Box::pin(futures_util::stream::iter(chunks));
1160 let mut body = ResponseBody::streaming(stream);
1161 let mut stream = body.bytes_stream().unwrap();
1162 assert_eq!(stream.next().await.unwrap().unwrap(), "a");
1163 assert_eq!(stream.next().await.unwrap().unwrap(), "bb");
1164 assert_eq!(stream.next().await.unwrap().unwrap(), "ccc");
1165 assert!(stream.next().await.is_none());
1166 }
1167
1168 #[tokio::test]
1169 async fn response_body_streaming_collect() {
1170 let chunks = vec![Ok(Bytes::from("hello ")), Ok(Bytes::from("world"))];
1171 let stream = Box::pin(futures_util::stream::iter(chunks));
1172 let mut body = ResponseBody::streaming(stream);
1173 let bytes = body.bytes().await.unwrap();
1174 assert_eq!(bytes, "hello world");
1175 }
1176
1177 #[tokio::test]
1178 async fn response_body_unencoded_size_limit_is_enforced() {
1179 let stream = Box::pin(futures_util::stream::iter(vec![
1180 Ok(Bytes::from("ab")),
1181 Ok(Bytes::from("cd")),
1182 ]));
1183 let mut body = ResponseBody::streaming(stream)
1184 .limit_decoded_size(3)
1185 .unwrap();
1186 assert_eq!(
1187 body.bytes().await.unwrap_err().kind(),
1188 "decoded_body_too_large"
1189 );
1190 }
1191
1192 #[test]
1193 fn response_body_buffered_size_limit_is_enforced() {
1194 let body = ResponseBody::buffered(Bytes::from_static(b"body"));
1195 assert_eq!(
1196 body.limit_decoded_size(3).unwrap_err().kind(),
1197 "decoded_body_too_large"
1198 );
1199 }
1200
1201 #[tokio::test]
1202 async fn response_body_double_consume_replaces() {
1203 let mut body = ResponseBody::buffered(Bytes::from("data"));
1204 let _ = body.bytes().await.unwrap();
1205 assert!(body.bytes().await.is_err());
1206 }
1207
1208 #[tokio::test]
1209 async fn response_body_double_stream_errors() {
1210 let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("x"))]));
1211 let mut body = ResponseBody::streaming(stream);
1212 let _ = body.bytes_stream().unwrap();
1213 let err = body.bytes_stream();
1214 assert!(err.is_err());
1215 }
1216
1217 #[cfg(feature = "compression-gzip")]
1218 fn gzip_bytes(input: &[u8]) -> Bytes {
1219 use std::io::Write;
1220
1221 let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast());
1222 encoder.write_all(input).unwrap();
1223 Bytes::from(encoder.finish().unwrap())
1224 }
1225
1226 #[cfg(feature = "compression-gzip")]
1227 #[tokio::test]
1228 async fn encoded_streaming_selects_raw_once() {
1229 let encoded = gzip_bytes(b"compressed body");
1230 let expected = encoded.clone();
1231 let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
1232 let mut body = ResponseBody::encoded_streaming(
1233 stream,
1234 "gzip".to_owned(),
1235 DecompressionLimit::default(),
1236 );
1237
1238 let mut raw = body.raw_bytes_stream().unwrap();
1239 assert_eq!(raw.next().await.unwrap().unwrap(), expected);
1240 assert!(raw.next().await.is_none());
1241 assert!(body.bytes_stream().is_err());
1242 }
1243
1244 #[cfg(feature = "compression-gzip")]
1245 #[tokio::test]
1246 async fn encoded_streaming_selects_decoded_once() {
1247 let encoded = gzip_bytes(b"compressed body");
1248 let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
1249 let mut body = ResponseBody::encoded_streaming(
1250 stream,
1251 "gzip".to_owned(),
1252 DecompressionLimit::default(),
1253 );
1254
1255 let mut decoded = body.bytes_stream().unwrap();
1256 assert_eq!(decoded.next().await.unwrap().unwrap(), "compressed body");
1257 assert!(decoded.next().await.is_none());
1258 assert!(body.raw_bytes_stream().is_err());
1259 }
1260
1261 #[cfg(feature = "compression-gzip")]
1262 #[tokio::test]
1263 async fn encoded_streaming_limit_is_applied_when_added_after_construction() {
1264 let encoded = gzip_bytes(b"compressed body");
1265 let stream = Box::pin(futures_util::stream::iter(vec![Ok(encoded)]));
1266 let mut body = ResponseBody::encoded_streaming(
1267 stream,
1268 "gzip".to_owned(),
1269 DecompressionLimit::default(),
1270 )
1271 .limit_decoded_size(3)
1272 .unwrap();
1273
1274 assert_eq!(
1275 body.bytes().await.unwrap_err().kind(),
1276 "decoded_body_too_large"
1277 );
1278 }
1279
1280 #[tokio::test]
1281 async fn uncompressed_raw_selection_is_passthrough() {
1282 let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("body"))]));
1283 let mut body = ResponseBody::streaming(stream);
1284 let mut raw = body.raw_bytes_stream().unwrap();
1285 assert_eq!(raw.next().await.unwrap().unwrap(), "body");
1286 assert!(raw.next().await.is_none());
1287 }
1288
1289 #[tokio::test]
1290 async fn response_body_buffered_double_bytes_errors() {
1291 let mut body = ResponseBody::buffered(Bytes::from("data"));
1292 let first = body.bytes().await.unwrap();
1293 assert_eq!(first, "data");
1294 assert!(body.bytes().await.is_err());
1295 }
1296
1297 #[tokio::test]
1298 async fn response_body_buffered_text_invalid_utf8() {
1299 let mut body = ResponseBody::buffered(Bytes::from(vec![0xFF, 0xFE]));
1300 let err = body.text().await;
1301 assert!(err.is_err());
1302 }
1303
1304 #[tokio::test]
1305 async fn response_body_streaming_bytes_then_stream_returns_empty() {
1306 let stream = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from("x"))]));
1307 let mut body = ResponseBody::streaming(stream);
1308 let _ = body.bytes().await.unwrap();
1309 assert!(body.bytes_stream().is_err());
1310 }
1311
1312 #[tokio::test]
1313 async fn response_body_consumed_is_empty() {
1314 let body = ResponseBody::Consumed;
1315 assert!(body.is_empty());
1316 assert_eq!(body.len(), Some(0));
1317 }
1318
1319 #[tokio::test]
1320 async fn response_body_buffered_bytes_stream_then_bytes_returns_empty() {
1321 let mut body = ResponseBody::buffered(Bytes::from("data"));
1322 let mut stream = body.bytes_stream().unwrap();
1323 let chunk = stream.next().await.unwrap().unwrap();
1324 assert_eq!(chunk, "data");
1325 assert!(stream.next().await.is_none());
1326 assert!(body.bytes().await.is_err());
1327 }
1328
1329 #[tokio::test]
1330 async fn into_http_body_empty_produces_empty_body() {
1331 use http_body::Body;
1332 use http_body_util::BodyExt;
1333
1334 let body = RequestBody::Empty;
1335 let mut hyper_body = body.into_http_body();
1336 let frame = futures_util::future::poll_fn(|cx| {
1337 use std::pin::Pin;
1338 Pin::new(&mut hyper_body).poll_frame(cx)
1339 })
1340 .await;
1341 assert!(frame.is_none());
1342 let _ = hyper_body.collect().await;
1343 }
1344
1345 #[tokio::test]
1346 async fn into_http_body_bytes_produces_one_data_frame() {
1347 use http_body::Body;
1348 use std::pin::Pin;
1349
1350 let body = RequestBody::from(Bytes::from("hello"));
1351 let mut hyper_body = body.into_http_body();
1352
1353 let frame = futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx))
1354 .await
1355 .expect("frame present")
1356 .expect("frame ok");
1357 assert!(frame.is_data());
1358 let data = frame.into_data().unwrap();
1359 assert_eq!(data, "hello");
1360
1361 let end =
1362 futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx)).await;
1363 assert!(end.is_none());
1364 }
1365
1366 #[tokio::test]
1367 async fn into_http_body_stream_pipes_through_without_eager_buffering() {
1368 use http_body::Body;
1369 use std::pin::Pin;
1370 use std::sync::atomic::{AtomicUsize, Ordering};
1371
1372 let polled = Arc::new(AtomicUsize::new(0));
1373 let polled_inner = polled.clone();
1374
1375 let chunks = vec![
1376 Ok(Bytes::from("a")),
1377 Ok(Bytes::from("b")),
1378 Ok(Bytes::from("c")),
1379 ];
1380 let chunk_stream = futures_util::stream::iter(chunks).inspect(move |_| {
1381 polled_inner.fetch_add(1, Ordering::SeqCst);
1382 });
1383 let body = RequestBody::from_stream(chunk_stream.map(|r| -> Result<Bytes> { r }), Some(3));
1384 let mut hyper_body = body.into_http_body();
1385
1386 let frame = futures_util::future::poll_fn(|cx| Pin::new(&mut hyper_body).poll_frame(cx))
1387 .await
1388 .expect("frame present")
1389 .expect("frame ok");
1390 let data = frame.into_data().unwrap();
1391 assert_eq!(data, "a");
1392 assert_eq!(
1393 polled.load(Ordering::SeqCst),
1394 1,
1395 "stream should not be eagerly polled"
1396 );
1397 }
1398
1399 #[tokio::test]
1400 async fn limited_stream_fuses_after_limit_trips() {
1401 use futures_util::StreamExt;
1402 use std::sync::atomic::{AtomicUsize, Ordering};
1403
1404 let polls = Arc::new(AtomicUsize::new(0));
1405 let polls_inner = polls.clone();
1406 let tracked = futures_util::stream::iter(vec![
1407 Ok(Bytes::from("ab")),
1408 Ok(Bytes::from("cd")),
1409 Ok(Bytes::from("ef")),
1410 ])
1411 .inspect(move |_| {
1412 polls_inner.fetch_add(1, Ordering::SeqCst);
1413 });
1414 let inner: super::BoxBytesStream = Box::pin(tracked);
1415 let mut limited = super::LimitedResponseStream {
1416 inner,
1417 max: 3,
1418 decoded_bytes: 0,
1419 limit_exceeded: false,
1420 };
1421 assert!(limited.next().await.unwrap().is_ok());
1423 assert!(limited.next().await.unwrap().is_err());
1424 let polls_after_error = polls.load(Ordering::SeqCst);
1425 assert!(limited.next().await.is_none());
1427 assert_eq!(
1428 polls.load(Ordering::SeqCst),
1429 polls_after_error,
1430 "inner stream must not be re-polled after the limit trips"
1431 );
1432 }
1433
1434 #[tokio::test]
1435 async fn leased_stream_holds_pool_slot_until_drop() {
1436 let pool = crate::pool::Pool::new(crate::pool::PoolConfig {
1437 max_connections_per_host: Some(1),
1438 ..Default::default()
1439 });
1440 let origin = crate::pool::OriginKey::from_parts("http", "example.com", 80);
1441 let guard = pool.acquire(Some(&origin)).await.unwrap();
1442 let mut body = ResponseBody::streaming_with_lease(
1443 Box::pin(futures_util::stream::pending()),
1444 Arc::new(guard),
1445 );
1446 let stream = body.bytes_stream().unwrap();
1447
1448 assert!(tokio::time::timeout(
1449 std::time::Duration::from_millis(10),
1450 pool.acquire(Some(&origin))
1451 )
1452 .await
1453 .is_err());
1454
1455 drop(stream);
1456 assert!(tokio::time::timeout(
1457 std::time::Duration::from_millis(100),
1458 pool.acquire(Some(&origin))
1459 )
1460 .await
1461 .is_ok());
1462 }
1463}