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};
53use crate::timeout::ResponseDeadline;
54
55// ---------------------------------------------------------------------------
56// Request body
57// ---------------------------------------------------------------------------
58
59/// A type-erased async stream of `Result<Bytes>` chunks.
60pub type BoxBytesStream = Pin<Box<dyn Stream<Item = Result<Bytes>> + Send>>;
61
62/// Request body.
63///
64/// Supports empty bodies, fixed-size byte buffers, and async streams.
65/// `Stream` bodies are **piped through** to the transport incrementally:
66/// each chunk is sent as soon as it is produced, with no eager buffering.
67/// The optional `length` hint drives `Content-Length` for known-size
68/// streams; unknown-length streams use HTTP/1.1 chunked transfer encoding
69/// (selected by hyper).
70#[derive(Default)]
71pub enum RequestBody {
72    /// Empty body.
73    #[default]
74    Empty,
75    /// Byte body with known length.
76    Bytes(Bytes),
77    /// Async stream body.
78    ///
79    /// Chunks are sent incrementally as the stream produces them. If
80    /// `length` is `Some(n)`, a `Content-Length: n` header is set when
81    /// the user has not supplied a conflicting value. If `length` is
82    /// `None`, no `Content-Length` is set; the transport selects a
83    /// safe transfer mode (chunked for HTTP/1.1).
84    ///
85    /// Note: stream bodies are never replayable, even with
86    /// `length = Some(n)` — the bytes are consumed on first send and
87    /// retry/redirect degrades to a single attempt
88    /// (`BodyNotReplayableForRedirect` / no retry). Callers needing
89    /// retried uploads must buffer into `Bytes` first.
90    Stream {
91        /// The stream of body chunks.
92        stream: BoxBytesStream,
93        /// Known length, if available.
94        length: Option<usize>,
95    },
96}
97
98/// Replayability classification for request bodies.
99///
100/// Used by redirect and retry code to determine whether a body can be
101/// re-sent without re-consuming the original source.
102#[derive(Debug, Clone, Copy, PartialEq, Eq)]
103pub enum ReplayClass {
104    /// Empty or immutable byte body — always replayable.
105    Immutable,
106    /// Seekable body that can be replayed by resetting position.
107    Seekable,
108    /// One-shot stream — cannot be replayed.
109    OneShot,
110    /// Body has been consumed.
111    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            // Render only the length: byte bodies routinely carry form
119            // passwords and tokens, which must not appear in logs or
120            // panic messages (see `crate::redact`).
121            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    /// Returns `true` if the body is empty.
132    ///
133    /// For `Stream` bodies the length hint is not authoritative — a
134    /// `Some(0)` stream may still produce bytes and an unbounded stream
135    /// may be empty. This method returns `false` for all stream bodies;
136    /// callers must not use it to decide `Content-Length` (see
137    /// [`Self::len`] and [`Self::has_known_length`]).
138    #[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    /// Returns the known length of the body, if available.
148    ///
149    /// Stream bodies may not have a known length until fully consumed.
150    /// Returns `0` for unknown-length streams; callers that need to
151    /// distinguish "empty" from "unknown length" must check
152    /// [`has_known_length`](Self::has_known_length) first and must not use
153    /// `len() == 0` to decide emptiness (use [`is_empty`](Self::is_empty),
154    /// which returns `false` for streams). Internal Content-Length logic
155    /// matches on the `Stream { length }` variant directly, never on `len()`.
156    #[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    /// Returns `true` if the body length is known.
166    #[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    /// Returns `true` if this body is replayable (can be sent multiple times).
175    ///
176    /// Only byte bodies are replayable. Streams are consumed on use.
177    #[must_use]
178    pub fn is_replayable(&self) -> bool {
179        matches!(self, Self::Empty | Self::Bytes(_))
180    }
181
182    /// Returns the replayability classification for this body.
183    #[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    /// Attempt to clone this body for use in a redirect request.
192    ///
193    /// Returns a cloned body if the body is replayable (empty or bytes),
194    /// or an error if the body is a live stream that cannot be resent.
195    ///
196    /// # Errors
197    ///
198    /// Returns [`Error::BodyNotReplayableForRedirect`] if the body is a
199    /// live stream that cannot be replayed for a redirect.
200    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    /// Attempt to clone this body for use in a retry attempt.
209    ///
210    /// Only available with the `logical-retry` feature (or `proxy`, whose
211    /// CONNECT multi-address fallback reuses the same replay check). This
212    /// is the retry counterpart to [`Self::try_clone_for_redirect`]:
213    /// only replayable bodies (`Empty`/`Bytes`) succeed. One-shot streams
214    /// return the retry-specific error classification so callers preserve
215    /// the existing retry error taxonomy.
216    ///
217    /// # Errors
218    ///
219    /// Returns [`Error::BodyNotReplayableForRetry`] if the body is a
220    /// live stream that cannot be replayed for another attempt.
221    #[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    /// Consume the body and return all bytes.
231    ///
232    /// For byte bodies, returns the bytes directly. For stream bodies,
233    /// collects all chunks. For empty bodies, returns empty bytes.
234    ///
235    /// # Errors
236    ///
237    /// Returns an error if a stream chunk fails.
238    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    /// Create a stream body with known length.
254    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    /// Convert into a hyper-compatible boxed body.
265    ///
266    /// Stream bodies are wrapped as a `StreamBody` so that chunks are
267    /// piped through to the transport incrementally. Bytes and empty
268    /// bodies are wrapped as `Full` and `Empty` respectively. No eager
269    /// buffering is performed.
270    #[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    /// Yield the next body frame.
337    ///
338    /// An empty body ends the stream immediately, a byte body yields its
339    /// single data frame, and a stream body yields one data frame per
340    /// produced chunk. The body is consumed as it is polled, matching the
341    /// single-consumption contract of the high-level request path.
342    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            // A stream body may still produce frames, so it is never
369            // reported as finished before it actually ends.
370            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    /// Type-erased native request body with per-frame write deadlines.
393    ///
394    /// The deadline starts when Hyper first polls the body, so connection and
395    /// TLS setup do not consume the write budget. It is reset after every
396    /// successfully produced frame and covers stalled body producers; the
397    /// established transport I/O timeout remains a separate concern.
398    #[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
491// ---------------------------------------------------------------------------
492// Response body
493// ---------------------------------------------------------------------------
494
495/// A handle to a pool permit. Cloning the handle is cheap; dropping the
496/// last handle releases the permit back to the pool.
497pub(crate) type PoolGuardArc = Arc<crate::pool::PoolGuard>;
498
499pin_project! {
500    /// A native frame-preserving response body.
501    ///
502    /// Unlike [`ResponseBody`], this type exposes the `http_body` frame
503    /// boundary directly. DATA and trailer frames are returned unchanged,
504    /// while the internal pool lease remains held until EOF, an error, or
505    /// drop. The type is opaque so Hyper's `Incoming` does not become part
506    /// of eggfetch's public API.
507    ///
508    /// Enforces the per-frame read inactivity timeout (starts on first
509    /// frame poll, resets per frame) and the absolute native total deadline
510    /// (starts with the logical request, never resets, may already be
511    /// expired on first poll). Total wins when already expired or when both
512    /// deadlines fire together. Timers are created on first poll in the
513    /// consuming runtime; no Tokio sleep crosses a runtime boundary.
514    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    /// Construct a native body around an eggfetch-owned response body.
538    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        // First poll in the consuming runtime: materialize timers here so
581        // no Tokio sleep crosses a runtime boundary. An already-expired
582        // total deadline wins without polling the inner transport.
583        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        // Absolute deadline is authoritative: a ready frame at or after the
600        // deadline must not extend the request.
601        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                // Same-poll race: frame and absolute deadline observable
614                // together; the outer lifecycle cap wins.
615                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                // Total wins when both deadlines fire together.
644                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    /// Wrap a Hyper response body while converting transport errors to the
719    /// stable eggfetch error type.
720    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/// Shared trailer store populated during body streaming.
736///
737/// Hyper yields trailers as a final `Frame::trailers` after data frames
738/// (HTTP/1.1 chunked trailers and HTTP/2 trailing HEADERS). The store is
739/// filled exactly once when that frame arrives; it stays `None` until the
740/// body has advanced far enough, when no trailers were sent, or when a body
741/// error occurred before trailers. It never buffers the body to obtain
742/// trailers and never fabricates trailer state on errors.
743///
744/// Cloning is cheap (`Arc`); the [`crate::Response`] holds one clone while
745/// the body stream holds another, so `Response::trailers()` remains
746/// available after `bytes()` collection. Dropping the body early leaves the
747/// store as `None` and releases the pool permit without waiting for
748/// trailers.
749#[derive(Debug, Clone, Default)]
750pub struct SharedTrailers {
751    inner: Arc<std::sync::Mutex<Option<http::HeaderMap>>>,
752}
753
754impl SharedTrailers {
755    /// Create an empty trailer store.
756    #[must_use]
757    pub fn new() -> Self {
758        Self {
759            inner: Arc::new(std::sync::Mutex::new(None)),
760        }
761    }
762
763    /// Store trailers received from the wire (first write wins).
764    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    /// Returns cloned trailers if they have arrived, else `None`.
773    ///
774    /// `None` means either the body has not been fully consumed yet, the
775    /// response carried no trailers, or a body error occurred before
776    /// trailers. Callers must fully consume the body stream (or `bytes()`)
777    /// before expecting trailers.
778    #[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    /// A response stream that keeps the pool lease alive while it is in use.
789    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            // Release the permit as soon as the stream reaches a terminal
807            // state, rather than waiting for the caller to drop the iterator.
808            this._lease.take();
809        }
810        item
811    }
812}
813
814pin_project! {
815    /// A response stream that bounds the total decoded bytes collected.
816    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        // Once the limit trips, fuse without polling the inner transport
834        // again (releases the connection earlier and avoids unbounded
835        // inner buffering under hostile streams). Returning `None`
836        // (rather than repeating the error) keeps combinators like
837        // `collect` terminating: callers observe the first error, then EOF.
838        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
856/// Response body handle.
857///
858/// Supports two consumption modes:
859///
860/// - **Buffered**: the entire body is held in memory. Created by
861///   `ResponseBody::buffered()`. Calling `bytes()` or `text()` is O(1).
862/// - **Streaming**: body chunks are yielded incrementally. Created by
863///   `ResponseBody::streaming()`. The stream must be fully consumed or
864///   dropped before the attached pool lease is released.
865///
866/// # Single-consumption semantics
867///
868/// A response body can be consumed once via `bytes()`, `text()`,
869/// `bytes_stream()`, or `raw_bytes_stream()`. For compressed streaming bodies,
870/// raw and decoded selection are mutually exclusive. After consumption,
871/// further reads return an error.
872pub enum ResponseBody {
873    /// Fully buffered body.
874    Buffered {
875        /// The buffered body bytes.
876        bytes: Bytes,
877    },
878    /// Streaming body.
879    Streaming {
880        /// The body stream.
881        stream: BoxBytesStream,
882        /// Optional pool permit holder. Released on drop.
883        ///
884        /// The guard privately carries the response read/total lifecycle
885        /// policy; see [`crate::pool::PoolGuard`]. Manually constructed
886        /// lease-free bodies carry no implicit client timeout.
887        lease: Option<PoolGuardArc>,
888    },
889    /// A compressed streaming body whose encoded source is retained until
890    /// the caller selects raw or decoded consumption.
891    EncodedStreaming {
892        /// The encoded body stream.
893        stream: BoxBytesStream,
894        /// Optional pool permit holder. Released on drop.
895        ///
896        /// As with [`ResponseBody::Streaming`], the guard privately carries
897        /// the read/total policy applied to the final selected stream.
898        lease: Option<PoolGuardArc>,
899        /// The original `Content-Encoding` header value.
900        content_encoding: String,
901        /// Limits applied when decoded mode is selected.
902        limit: DecompressionLimit,
903    },
904    /// The streaming body has already been consumed.
905    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    /// Create a buffered response body.
926    #[must_use]
927    pub fn buffered(bytes: Bytes) -> Self {
928        Self::Buffered { bytes }
929    }
930
931    /// Create a streaming response body from a type-erased stream.
932    #[must_use]
933    pub fn streaming(stream: BoxBytesStream) -> Self {
934        Self::Streaming {
935            stream,
936            lease: None,
937        }
938    }
939
940    /// Create a streaming response body with an attached pool permit.
941    /// The permit is released when the body is dropped.
942    #[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    /// Attach the pool lease to a streaming or encoded-streaming body.
954    ///
955    /// This is the single finalization ownership point: transports create
956    /// lease-free bodies, finalization installs the read/total policy behind
957    /// the guard before it is placed behind the lease `Arc`, and consumption
958    /// enforces that private policy at the final stream boundary.
959    /// Buffered/consumed bodies drop the lease; they are already complete.
960    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    /// Create a deferred-decode streaming response body.
985    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    /// Create a deferred-decode streaming response body with an attached
999    /// pool permit.
1000    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    /// Returns `true` if the body is empty.
1015    #[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, // unknown until consumed
1020            Self::Consumed => true,
1021        }
1022    }
1023
1024    /// Returns the buffered body length, if available.
1025    ///
1026    /// For streaming bodies, returns `None` until the body is fully
1027    /// collected.
1028    #[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    /// Returns `true` if this is a buffered body.
1038    #[must_use]
1039    pub fn is_buffered(&self) -> bool {
1040        matches!(self, Self::Buffered { .. })
1041    }
1042
1043    /// Consume the body and return all bytes.
1044    ///
1045    /// For buffered bodies, this is O(1). For streaming bodies, this
1046    /// collects all chunks into a single `Bytes` buffer.
1047    ///
1048    /// # Limits
1049    ///
1050    /// The configured decoded-body limit is enforced for both encoded and
1051    /// unencoded response bodies.
1052    ///
1053    /// [`DecompressionLimit`]: crate::compression::DecompressionLimit
1054    ///
1055    /// # Errors
1056    ///
1057    /// Returns an error if the body has already been consumed or if a
1058    /// stream chunk fails.
1059    pub async fn bytes(&mut self) -> Result<Bytes> {
1060        // Buffered fast path stays O(1); streaming paths share the final
1061        // stream boundary (`take_stream`) so timeouts, decompression,
1062        // limits, and lease release cannot diverge between `bytes()` and
1063        // `bytes_stream()`.
1064        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    /// Enforce a maximum decoded size for this body.
1080    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    /// Consume the body and return it as a UTF-8 string.
1116    ///
1117    /// # Errors
1118    ///
1119    /// Returns an error if the body is not valid UTF-8 or has already
1120    /// been consumed.
1121    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    /// Consume the body and return a stream of byte chunks.
1127    ///
1128    /// For buffered bodies, yields the entire body as a single chunk.
1129    /// For streaming bodies, yields chunks incrementally.
1130    ///
1131    /// # Errors
1132    ///
1133    /// Returns an error if the body has already been consumed.
1134    pub fn bytes_stream(&mut self) -> Result<BoxBytesStream> {
1135        self.take_stream(false)
1136    }
1137
1138    /// Consume the body and return the encoded response byte stream.
1139    ///
1140    /// For compressed streaming responses, this selects the original encoded
1141    /// transport body and bypasses the existing decoder chain. The selection
1142    /// is one-shot; a body cannot subsequently be consumed in decoded mode.
1143    /// Buffered bodies follow the ordinary streaming path because their
1144    /// encoded representation is no longer available.
1145    ///
1146    /// # Errors
1147    ///
1148    /// Returns an error if the body has already been consumed or if the
1149    /// decoder configuration is invalid while selecting a stream.
1150    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                    // Final stream boundary: read the private lifecycle
1167                    // policy from the lease and cover it with one unified
1168                    // timeout wrapper; the lease wrapper then releases the
1169                    // pool permit on terminal timeout/EOF/error.
1170                    // Lease-free manually constructed bodies carry no
1171                    // implicit client timeout.
1172                    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                    // Defensive: the outer match guarantees this arm, but
1187                    // a future variant addition must surface as an error,
1188                    // not a panic.
1189                    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                    // Same single mechanism on the final selected stream:
1219                    // raw encoded bytes and decoded bytes share total/read
1220                    // semantics; decoder buffering cannot bypass the outer
1221                    // absolute deadline because it sits outside the decoder.
1222                    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                    // Defensive: the outer match guarantees this arm, but
1230                    // a future variant addition must surface as an error,
1231                    // not a panic.
1232                    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// ---------------------------------------------------------------------------
1241// Tests
1242// ---------------------------------------------------------------------------
1243
1244#[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        // "ab" ok (2 bytes), "cd" trips the limit (4 > 3).
1632        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        // Further polls fuse (EOF) without polling inner again.
1636        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}