Skip to main content

eggfetch_core/
body.rs

1//! Body model for requests and responses.
2//!
3//! # Body ownership rules
4//!
5//! Both request and response bodies follow single-consumption semantics:
6//! a body can be consumed exactly once (by buffering or streaming). Once
7//! consumed, further reads return an error or produce no data.
8//!
9//! # Streaming vs buffering
10//!
11//! Response bodies can be consumed in two ways:
12//!
13//! - **Buffered**: `bytes()` or `text()` collects the entire body into
14//!   memory. Suitable for small-to-medium responses.
15//! - **Streaming**: `bytes_stream()` yields body chunks incrementally.
16//!   Suitable for large responses or when incremental processing is needed.
17//!
18//! Request bodies can be:
19//!
20//! - **Empty**: no body sent.
21//! - **Bytes**: a fixed-size byte buffer with known length. Sent as a
22//!   `Content-Length`-delimited body.
23//! - **Stream**: an async stream of bytes. Piped through to the transport
24//!   incrementally (no eager buffering). When `length` is `Some(n)`, the
25//!   body is sent as a `Content-Length`-delimited body. When `length` is
26//!   `None`, hyper's HTTP/1.1 machinery selects a safe transfer mode
27//!   (e.g. chunked transfer encoding for HTTP/1.1).
28//!
29//! # Response lease (pool permit lifecycle)
30//!
31//! Streaming response bodies carry an internal `Arc<PoolGuard>` that
32//! holds the pool permits acquired for the request. The permits are
33//! released when the response body is dropped or fully consumed. This
34//! guarantees that per-origin concurrency limits remain meaningful while
35//! response bodies are in flight: a streaming response that is held
36//! but not consumed continues to occupy its pool slot. Buffered and
37//! already-consumed responses do not carry a lease.
38
39use 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
54// ---------------------------------------------------------------------------
55// Request body
56// ---------------------------------------------------------------------------
57
58/// A type-erased async stream of `Result<Bytes>` chunks.
59pub type BoxBytesStream = Pin<Box<dyn Stream<Item = Result<Bytes>> + Send>>;
60
61/// Request body.
62///
63/// Supports empty bodies, fixed-size byte buffers, and async streams.
64/// `Stream` bodies are **piped through** to the transport incrementally:
65/// each chunk is sent as soon as it is produced, with no eager buffering.
66/// The optional `length` hint drives `Content-Length` for known-size
67/// streams; unknown-length streams use HTTP/1.1 chunked transfer encoding
68/// (selected by hyper).
69#[derive(Default)]
70pub enum RequestBody {
71    /// Empty body.
72    #[default]
73    Empty,
74    /// Byte body with known length.
75    Bytes(Bytes),
76    /// Async stream body.
77    ///
78    /// Chunks are sent incrementally as the stream produces them. If
79    /// `length` is `Some(n)`, a `Content-Length: n` header is set when
80    /// the user has not supplied a conflicting value. If `length` is
81    /// `None`, no `Content-Length` is set; the transport selects a
82    /// safe transfer mode (chunked for HTTP/1.1).
83    Stream {
84        /// The stream of body chunks.
85        stream: BoxBytesStream,
86        /// Known length, if available.
87        length: Option<usize>,
88    },
89}
90
91/// Replayability classification for request bodies.
92///
93/// Used by redirect and retry code to determine whether a body can be
94/// re-sent without re-consuming the original source.
95#[derive(Debug, Clone, Copy, PartialEq, Eq)]
96pub enum ReplayClass {
97    /// Empty or immutable byte body — always replayable.
98    Immutable,
99    /// Seekable body that can be replayed by resetting position.
100    Seekable,
101    /// One-shot stream — cannot be replayed.
102    OneShot,
103    /// Body has been consumed.
104    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            // Render only the length: byte bodies routinely carry form
112            // passwords and tokens, which must not appear in logs or
113            // panic messages (see `crate::redact`).
114            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    /// Returns `true` if the body is empty.
125    ///
126    /// For `Stream` bodies the length hint is not authoritative — a
127    /// `Some(0)` stream may still produce bytes and an unbounded stream
128    /// may be empty. This method returns `false` for all stream bodies;
129    /// callers must not use it to decide `Content-Length` (see
130    /// [`Self::len`] and [`Self::has_known_length`]).
131    #[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    /// Returns the known length of the body, if available.
141    ///
142    /// Stream bodies may not have a known length until fully consumed.
143    /// Returns `0` for unknown-length streams; callers that need to
144    /// distinguish "empty" from "unknown length" must check
145    /// [`has_known_length`](Self::has_known_length) first.
146    #[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    /// Returns `true` if the body length is known.
156    #[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    /// Returns `true` if this body is replayable (can be sent multiple times).
165    ///
166    /// Only byte bodies are replayable. Streams are consumed on use.
167    #[must_use]
168    pub fn is_replayable(&self) -> bool {
169        matches!(self, Self::Empty | Self::Bytes(_))
170    }
171
172    /// Returns the replayability classification for this body.
173    #[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    /// Attempt to clone this body for use in a redirect request.
182    ///
183    /// Returns a cloned body if the body is replayable (empty or bytes),
184    /// or an error if the body is a live stream that cannot be resent.
185    ///
186    /// # Errors
187    ///
188    /// Returns [`Error::BodyNotReplayableForRedirect`] if the body is a
189    /// live stream that cannot be replayed for a redirect.
190    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    /// Attempt to clone this body for use in a retry attempt.
199    ///
200    /// This is the retry counterpart to [`Self::try_clone_for_redirect`]:
201    /// only replayable bodies (`Empty`/`Bytes`) succeed. One-shot streams
202    /// return the retry-specific error classification so callers preserve
203    /// the existing retry error taxonomy.
204    ///
205    /// # Errors
206    ///
207    /// Returns [`Error::BodyNotReplayableForRetry`] if the body is a
208    /// live stream that cannot be replayed for another attempt.
209    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    /// Consume the body and return all bytes.
218    ///
219    /// For byte bodies, returns the bytes directly. For stream bodies,
220    /// collects all chunks. For empty bodies, returns empty bytes.
221    ///
222    /// # Errors
223    ///
224    /// Returns an error if a stream chunk fails.
225    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    /// Create a stream body with known length.
241    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    /// Convert into a hyper-compatible boxed body.
252    ///
253    /// Stream bodies are wrapped as a `StreamBody` so that chunks are
254    /// piped through to the transport incrementally. Bytes and empty
255    /// bodies are wrapped as `Full` and `Empty` respectively. No eager
256    /// buffering is performed.
257    #[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    /// Type-erased native request body with per-frame write deadlines.
318    ///
319    /// The deadline starts when Hyper first polls the body, so connection and
320    /// TLS setup do not consume the write budget. It is reset after every
321    /// successfully produced frame and covers stalled body producers; the
322    /// established transport I/O timeout remains a separate concern.
323    #[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
418// ---------------------------------------------------------------------------
419// Response body
420// ---------------------------------------------------------------------------
421
422/// A handle to a pool permit. Cloning the handle is cheap; dropping the
423/// last handle releases the permit back to the pool.
424pub(crate) type PoolGuardArc = Arc<crate::pool::PoolGuard>;
425
426pin_project! {
427    /// A native frame-preserving response body.
428    ///
429    /// Unlike [`ResponseBody`], this type exposes the `http_body` frame
430    /// boundary directly. DATA and trailer frames are returned unchanged,
431    /// while the internal pool lease remains held until EOF, an error, or
432    /// drop. The type is opaque so Hyper's `Incoming` does not become part
433    /// of eggfetch's public API.
434    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    /// Construct a native body around an eggfetch-owned response body.
457    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    /// Wrap a Hyper response body while converting transport errors to the
571    /// stable eggfetch error type.
572    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/// Shared trailer store populated during body streaming.
582///
583/// Hyper yields trailers as a final `Frame::trailers` after data frames
584/// (HTTP/1.1 chunked trailers and HTTP/2 trailing HEADERS). The store is
585/// filled exactly once when that frame arrives; it stays `None` until the
586/// body has advanced far enough, when no trailers were sent, or when a body
587/// error occurred before trailers. It never buffers the body to obtain
588/// trailers and never fabricates trailer state on errors.
589///
590/// Cloning is cheap (`Arc`); the [`crate::Response`] holds one clone while
591/// the body stream holds another, so `Response::trailers()` remains
592/// available after `bytes()` collection. Dropping the body early leaves the
593/// store as `None` and releases the pool permit without waiting for
594/// trailers.
595#[derive(Debug, Clone, Default)]
596pub struct SharedTrailers {
597    inner: Arc<std::sync::Mutex<Option<http::HeaderMap>>>,
598}
599
600impl SharedTrailers {
601    /// Create an empty trailer store.
602    #[must_use]
603    pub fn new() -> Self {
604        Self {
605            inner: Arc::new(std::sync::Mutex::new(None)),
606        }
607    }
608
609    /// Store trailers received from the wire (first write wins).
610    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    /// Returns cloned trailers if they have arrived, else `None`.
619    ///
620    /// `None` means either the body has not been fully consumed yet, the
621    /// response carried no trailers, or a body error occurred before
622    /// trailers. Callers must fully consume the body stream (or `bytes()`)
623    /// before expecting trailers.
624    #[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    /// A response stream that keeps the pool lease alive while it is in use.
632    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            // Release the permit as soon as the stream reaches a terminal
650            // state, rather than waiting for the caller to drop the iterator.
651            this._lease.take();
652        }
653        item
654    }
655}
656
657pin_project! {
658    /// A response stream that bounds the total decoded bytes collected.
659    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        // Once the limit trips, fuse without polling the inner transport
677        // again (releases the connection earlier and avoids unbounded
678        // inner buffering under hostile streams). Returning `None`
679        // (rather than repeating the error) keeps combinators like
680        // `collect` terminating: callers observe the first error, then EOF.
681        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
699/// Response body handle.
700///
701/// Supports two consumption modes:
702///
703/// - **Buffered**: the entire body is held in memory. Created by
704///   `ResponseBody::buffered()`. Calling `bytes()` or `text()` is O(1).
705/// - **Streaming**: body chunks are yielded incrementally. Created by
706///   `ResponseBody::streaming()`. The stream must be fully consumed or
707///   dropped before the attached pool lease is released.
708///
709/// # Single-consumption semantics
710///
711/// A response body can be consumed once via `bytes()`, `text()`,
712/// `bytes_stream()`, or `raw_bytes_stream()`. For compressed streaming bodies,
713/// raw and decoded selection are mutually exclusive. After consumption,
714/// further reads return an error.
715pub enum ResponseBody {
716    /// Fully buffered body.
717    Buffered {
718        /// The buffered body bytes.
719        bytes: Bytes,
720    },
721    /// Streaming body.
722    Streaming {
723        /// The body stream.
724        stream: BoxBytesStream,
725        /// Optional pool permit holder. Released on drop.
726        lease: Option<PoolGuardArc>,
727    },
728    /// A compressed streaming body whose encoded source is retained until
729    /// the caller selects raw or decoded consumption.
730    EncodedStreaming {
731        /// The encoded body stream.
732        stream: BoxBytesStream,
733        /// Optional pool permit holder. Released on drop.
734        lease: Option<PoolGuardArc>,
735        /// The original `Content-Encoding` header value.
736        content_encoding: String,
737        /// Limits applied when decoded mode is selected.
738        limit: DecompressionLimit,
739    },
740    /// The streaming body has already been consumed.
741    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    /// Create a buffered response body.
762    #[must_use]
763    pub fn buffered(bytes: Bytes) -> Self {
764        Self::Buffered { bytes }
765    }
766
767    /// Create a streaming response body from a type-erased stream.
768    #[must_use]
769    pub fn streaming(stream: BoxBytesStream) -> Self {
770        Self::Streaming {
771            stream,
772            lease: None,
773        }
774    }
775
776    /// Create a streaming response body with an attached pool permit.
777    /// The permit is released when the body is dropped.
778    pub(crate) fn streaming_with_lease(stream: BoxBytesStream, lease: PoolGuardArc) -> Self {
779        Self::Streaming {
780            stream,
781            lease: Some(lease),
782        }
783    }
784
785    /// Create a deferred-decode streaming response body.
786    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    /// Create a deferred-decode streaming response body with an attached
800    /// pool permit.
801    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    /// Returns `true` if the body is empty.
816    #[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, // unknown until consumed
821            Self::Consumed => true,
822        }
823    }
824
825    /// Returns the buffered body length, if available.
826    ///
827    /// For streaming bodies, returns `None` until the body is fully
828    /// collected.
829    #[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    /// Returns `true` if this is a buffered body.
839    #[must_use]
840    pub fn is_buffered(&self) -> bool {
841        matches!(self, Self::Buffered { .. })
842    }
843
844    /// Consume the body and return all bytes.
845    ///
846    /// For buffered bodies, this is O(1). For streaming bodies, this
847    /// collects all chunks into a single `Bytes` buffer.
848    ///
849    /// # Limits
850    ///
851    /// The configured decoded-body limit is enforced for both encoded and
852    /// unencoded response bodies.
853    ///
854    /// [`DecompressionLimit`]: crate::compression::DecompressionLimit
855    ///
856    /// # Errors
857    ///
858    /// Returns an error if the body has already been consumed or if a
859    /// stream chunk fails.
860    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    /// Enforce a maximum decoded size for this body.
898    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    /// Consume the body and return it as a UTF-8 string.
934    ///
935    /// # Errors
936    ///
937    /// Returns an error if the body is not valid UTF-8 or has already
938    /// been consumed.
939    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    /// Consume the body and return a stream of byte chunks.
945    ///
946    /// For buffered bodies, yields the entire body as a single chunk.
947    /// For streaming bodies, yields chunks incrementally.
948    ///
949    /// # Errors
950    ///
951    /// Returns an error if the body has already been consumed.
952    pub fn bytes_stream(&mut self) -> Result<BoxBytesStream> {
953        self.take_stream(false)
954    }
955
956    /// Consume the body and return the encoded response byte stream.
957    ///
958    /// For compressed streaming responses, this selects the original encoded
959    /// transport body and bypasses the existing decoder chain. The selection
960    /// is one-shot; a body cannot subsequently be consumed in decoded mode.
961    /// Buffered bodies follow the ordinary streaming path because their
962    /// encoded representation is no longer available.
963    ///
964    /// # Errors
965    ///
966    /// Returns an error if the body has already been consumed or if the
967    /// decoder configuration is invalid while selecting a stream.
968    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                    // Defensive: the outer match guarantees this arm, but
990                    // a future variant addition must surface as an error,
991                    // not a panic.
992                    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                    // Defensive: the outer match guarantees this arm, but
1020                    // a future variant addition must surface as an error,
1021                    // not a panic.
1022                    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// ---------------------------------------------------------------------------
1031// Tests
1032// ---------------------------------------------------------------------------
1033
1034#[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        // "ab" ok (2 bytes), "cd" trips the limit (4 > 3).
1422        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        // Further polls fuse (EOF) without polling inner again.
1426        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}