Skip to main content

eggfetch_core/
compression.rs

1//! Response decompression.
2//!
3//! Provides streaming decompression of response bodies based on
4//! `Content-Encoding` headers. Supports gzip, deflate, brotli, and
5//! zstd via feature-gated `async-compression` decoders.
6//!
7//! # Policy
8//!
9//! When automatic decompression is enabled (the default when any
10//! compression feature is compiled in), the client:
11//!
12//! 1. Advertises supported encodings in `Accept-Encoding`.
13//! 2. Decodes compressed response bodies transparently.
14//! 3. Strips `Content-Encoding` and `Content-Length` from decoded
15//!    response headers.
16//!
17//! If the server responds with an unsupported encoding and automatic
18//! decompression is enabled, an `Error::UnsupportedContentEncoding`
19//! is returned. If decompression is disabled, the raw bytes pass
20//! through unchanged.
21
22use std::sync::atomic::{AtomicUsize, Ordering};
23use std::sync::Arc;
24use std::sync::OnceLock;
25
26use crate::body::BoxBytesStream;
27use crate::error::{Error, Result};
28
29/// Limits applied during response body decompression.
30///
31/// Nested content encodings are additionally capped at 4 layers
32/// (`MAX_NESTING_DEPTH`); `max_decompression_ratio` remains the primary
33/// defense against large expansion ratios.
34#[derive(Debug, Clone, Copy, Default)]
35pub struct DecompressionLimit {
36    /// Hard limit on total decoded bytes.
37    pub(crate) max_decoded_body_size: Option<usize>,
38    /// Ratio of decoded bytes to compressed bytes after which
39    /// decompression is rejected.
40    pub(crate) max_decompression_ratio: Option<f64>,
41}
42
43impl DecompressionLimit {
44    /// Create a new limit with both fields set to `None` (unlimited).
45    #[must_use]
46    pub const fn new() -> Self {
47        Self {
48            max_decoded_body_size: None,
49            max_decompression_ratio: None,
50        }
51    }
52
53    /// Returns `true` if neither limit is set.
54    #[must_use]
55    pub fn is_unlimited(&self) -> bool {
56        self.max_decoded_body_size.is_none() && self.max_decompression_ratio.is_none()
57    }
58
59    /// Returns the configured maximum decoded body size, if any.
60    #[must_use]
61    pub fn max_decoded_body_size(&self) -> Option<usize> {
62        self.max_decoded_body_size
63    }
64
65    /// Returns the configured maximum decompression ratio, if any.
66    #[must_use]
67    pub fn max_decompression_ratio(&self) -> Option<f64> {
68        self.max_decompression_ratio
69    }
70
71    /// Validate the decompression ratio.
72    ///
73    /// # Errors
74    ///
75    /// Returns [`Error::Decompression`] if `max_decompression_ratio` is not
76    /// finite and positive. This is the runtime counterpart to
77    /// [`Self::try_new`] for limits constructed via struct literal.
78    pub fn validate(&self) -> Result<()> {
79        if let Some(ratio) = self.max_decompression_ratio {
80            if !ratio.is_finite() || ratio <= 0.0 {
81                return Err(Error::Decompression(
82                    "invalid max_decompression_ratio: must be finite and positive".into(),
83                ));
84            }
85        }
86        Ok(())
87    }
88
89    /// Create a limit with validation for the decompression ratio.
90    ///
91    /// # Errors
92    ///
93    /// Returns an error if `max_decompression_ratio` is not finite and
94    /// positive.
95    pub fn try_new(
96        max_decoded_body_size: Option<usize>,
97        max_decompression_ratio: Option<f64>,
98    ) -> crate::error::Result<Self> {
99        if let Some(ratio) = max_decompression_ratio {
100            if !ratio.is_finite() || ratio <= 0.0 {
101                return Err(crate::error::Error::RequestBuild(
102                    "max_decompression_ratio must be finite and positive".into(),
103                ));
104            }
105        }
106        Ok(Self {
107            max_decoded_body_size,
108            max_decompression_ratio,
109        })
110    }
111}
112
113/// Maximum number of nested content encodings we will attempt to
114/// decode. This prevents stack overflow or excessive resource use
115/// from adversarial multi-layer compression.
116const MAX_NESTING_DEPTH: usize = 4;
117
118/// A content coding identifier.
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub enum ContentCoding {
121    /// gzip (RFC 1952)
122    Gzip,
123    /// deflate (RFC 9110 §8.4.1.2: the zlib wrapper, RFC 1950; raw
124    /// deflate streams, RFC 1951, are accepted as a lenient fallback)
125    Deflate,
126    /// brotli (RFC 7932)
127    Brotli,
128    /// zstd (RFC 8478)
129    Zstd,
130}
131
132impl ContentCoding {
133    /// Return the wire identifier for this encoding.
134    #[must_use]
135    pub fn as_str(self) -> &'static str {
136        match self {
137            Self::Gzip => "gzip",
138            Self::Deflate => "deflate",
139            Self::Brotli => "br",
140            Self::Zstd => "zstd",
141        }
142    }
143
144    /// Parse a wire identifier into a `ContentCoding`.
145    fn from_wire(s: &str) -> Option<Self> {
146        match s.trim().to_ascii_lowercase().as_str() {
147            "gzip" | "x-gzip" => Some(Self::Gzip),
148            "deflate" => Some(Self::Deflate),
149            "br" => Some(Self::Brotli),
150            "zstd" => Some(Self::Zstd),
151            _ => None,
152        }
153    }
154
155    /// Returns `true` when the token is the RFC 9110 §8.4 `identity`
156    /// content-coding, which means "no encoding" and decodes to itself.
157    fn is_identity(token: &str) -> bool {
158        token.trim().eq_ignore_ascii_case("identity")
159    }
160}
161
162/// Generate the `Accept-Encoding` header value for the compiled-in
163/// features. Returns `None` if no compression features are enabled.
164#[must_use]
165pub fn accept_encoding_value() -> Option<&'static str> {
166    static VALUE: OnceLock<Option<Box<str>>> = OnceLock::new();
167
168    VALUE
169        .get_or_init(|| {
170            // Order matches mainstream client defaults (most common first).
171            //
172            // Only advertise codings whose streaming decoder is compiled
173            // in; advertising an encoding without its decoder makes a
174            // compliant server produce responses we cannot decode.
175            // The pushes are feature-gated, so `vec![]` cannot express
176            // this sequence.
177            #[allow(clippy::vec_init_then_push)]
178            {
179                #[allow(unused_mut)]
180                let mut parts: Vec<&str> = Vec::new();
181
182                #[cfg(feature = "compression-gzip")]
183                parts.push("gzip");
184                #[cfg(feature = "compression-deflate")]
185                parts.push("deflate");
186                #[cfg(feature = "compression-brotli")]
187                parts.push("br");
188                #[cfg(feature = "compression-zstd")]
189                parts.push("zstd");
190
191                (!parts.is_empty()).then(|| parts.join(", ").into_boxed_str())
192            }
193        })
194        .as_deref()
195}
196
197/// Parse a `Content-Encoding` header value into an ordered list of
198/// content codings. The list is in wire order (outermost first).
199///
200/// `identity` tokens (RFC 9110 §8.4, meaning "no encoding") are no-ops
201/// and are skipped.
202///
203/// Returns `None` if the header is empty, contains only whitespace, or
204/// contains only no-op codings such as bare `identity`.
205pub(crate) fn parse_content_encodings(header: &str) -> Option<Vec<ContentCoding>> {
206    let encodings: Vec<ContentCoding> = header
207        .split(',')
208        .map(str::trim)
209        .filter(|s| !s.is_empty() && !ContentCoding::is_identity(s))
210        .filter_map(ContentCoding::from_wire)
211        .collect();
212
213    if encodings.is_empty() {
214        None
215    } else {
216        Some(encodings)
217    }
218}
219
220/// Check whether a `Content-Encoding` header value contains only
221/// supported encodings.
222///
223/// `identity` (RFC 9110 §8.4) is a valid no-op coding and is accepted.
224///
225/// Returns `Ok(())` if the header is absent, empty, or all encodings
226/// are supported.
227///
228/// # Errors
229///
230/// Returns [`Error::UnsupportedContentEncoding`] if any encoding is
231/// unsupported.
232pub fn validate_content_encodings(header_value: &str) -> Result<()> {
233    for token in header_value.split(',') {
234        let token = token.trim();
235        if token.is_empty() || ContentCoding::is_identity(token) {
236            continue;
237        }
238        if ContentCoding::from_wire(token).is_none() {
239            return Err(Error::UnsupportedContentEncoding(token.to_string()));
240        }
241    }
242    Ok(())
243}
244
245/// Wrap a byte stream in decompression decoders based on the
246/// `Content-Encoding` header.
247///
248/// Decoders are applied in reverse order (innermost encoding first).
249/// For example, `Content-Encoding: gzip, br` means decode brotli
250/// first, then gzip.
251///
252/// Returns the original stream unchanged if there are no encodings
253/// to decode or if decompression is disabled.
254///
255/// # Errors
256///
257/// Returns [`Error::Decompression`] if the nesting depth exceeds the
258/// maximum or if a decoder fails. Returns
259/// [`Error::UnsupportedContentEncoding`] if any encoding is not
260/// supported by the compiled-in features. Returns
261/// [`Error::DecodedBodyTooLarge`] or
262/// [`Error::DecompressionRatioExceeded`] if the decoded body exceeds
263/// the configured limits.
264pub fn decompress_stream(
265    stream: BoxBytesStream,
266    content_encoding: Option<&str>,
267    decompression_enabled: bool,
268    limit: DecompressionLimit,
269) -> Result<BoxBytesStream> {
270    if !decompression_enabled {
271        return Ok(stream);
272    }
273
274    let header = match content_encoding {
275        Some(h) if !h.trim().is_empty() => h,
276        _ => return Ok(stream),
277    };
278
279    // Centralized validation so buffered and streaming paths handle invalid
280    // ratios identically (previously buffered treated NaN/inf/negative as
281    // unlimited while streaming errored per poll).
282    limit.validate()?;
283
284    // Validate the complete wire value before parsing recognized codings.
285    // This keeps unsupported encodings and nesting-depth errors ordered the
286    // same way for streaming and buffered bodies.
287    validate_content_encodings(header)?;
288
289    let Some(encodings) = parse_content_encodings(header) else {
290        return Ok(stream);
291    };
292
293    if encodings.len() > MAX_NESTING_DEPTH {
294        return Err(Error::Decompression(format!(
295            "content encoding nesting depth {} exceeds maximum {}",
296            encodings.len(),
297            MAX_NESTING_DEPTH
298        )));
299    }
300
301    // Apply decoders in reverse order (innermost encoding first).
302    //
303    // Threat model for stacked encodings (e.g., `gzip, gzip`):
304    // - Each layer's `CountingStream` measures that layer's compressed
305    //   input. The first decoder's counter is the wire size; inner
306    //   decoders' counters measure the outer layer's *decompressed* output.
307    // - Per-layer `LimitingStream`s enforce `max_decompression_ratio`
308    //   against each layer's own input (catches `gzip(gzip(huge))` where
309    //   inner expansion is huge but outer is small).
310    // - The final `LimitingStream` enforces both `max_decoded_body_size`
311    //   and `max_decompression_ratio` against the outermost wire size
312    //   (catches cumulative expansion that stays under per-layer limits).
313    // Both checks are required; removing either would let an attacker
314    // craft sizes that stay under the remaining threshold.
315    let mut current = stream;
316    let mut outer_counter = None;
317    for encoding in encodings.into_iter().rev() {
318        let counting = CountingStream::new(current);
319        let counter = counting.counter();
320        current = make_decoder(Box::pin(counting), encoding)?;
321        // The first decoder consumes the wire stream, so its counter is the
322        // only one that measures the original compressed body size.
323        if outer_counter.is_none() {
324            outer_counter = Some(Arc::clone(&counter));
325        }
326
327        // Check every decoder against the bytes consumed for its own input
328        // layer. Comparing only the final output with the outermost wire
329        // size lets stacked encodings hide a large intermediate expansion.
330        if let Some(max_ratio) = limit.max_decompression_ratio {
331            current = Box::pin(LimitingStream::new(
332                current,
333                DecompressionLimit {
334                    max_decoded_body_size: None,
335                    max_decompression_ratio: Some(max_ratio),
336                },
337                counter,
338            ));
339        }
340    }
341
342    if limit.is_unlimited() {
343        return Ok(current);
344    }
345
346    let Some(outer_counter) = outer_counter else {
347        return Ok(current);
348    };
349    Ok(Box::pin(LimitingStream::new(current, limit, outer_counter)))
350}
351
352/// Decompress a fully buffered byte slice using synchronous decoding.
353///
354/// Used for response bodies that have already been collected into memory.
355/// Supports gzip/deflate via `flate2` and, when the corresponding
356/// features are enabled, brotli (`brotli` crate) and zstd (`zstd` crate).
357///
358/// # Errors
359///
360/// Returns an error if the content encoding is unsupported or
361/// decompression fails. Returns [`Error::DecodedBodyTooLarge`] or
362/// [`Error::DecompressionRatioExceeded`] if the decoded body exceeds
363/// the configured limits.
364#[allow(clippy::too_many_lines)]
365pub fn decompress_buffered(
366    data: &[u8],
367    content_encoding: &str,
368    limit: DecompressionLimit,
369) -> Result<bytes::Bytes> {
370    // Validate decompression limit first so invalid ratios (NaN/inf/negative)
371    // are handled identically to the streaming path (previously buffered
372    // treated them as unlimited).
373    limit.validate()?;
374    // Validate before the pass-through so unsupported codings still fail.
375    validate_content_encodings(content_encoding)?;
376
377    let encodings = parse_content_encodings(content_encoding);
378    let Some(encodings) = encodings else {
379        // Validation accepted every token; reaching this arm means only
380        // no-op codings (e.g. bare `identity`) were present, so the body
381        // is served unchanged.
382        return Ok(bytes::Bytes::copy_from_slice(data));
383    };
384
385    if encodings.len() > MAX_NESTING_DEPTH {
386        return Err(Error::Decompression(format!(
387            "content encoding nesting depth {} exceeds maximum {}",
388            encodings.len(),
389            MAX_NESTING_DEPTH
390        )));
391    }
392
393    // Preserve the existing empty-body behavior while still routing the
394    // body through the same validation path as non-empty buffered bodies.
395    if data.is_empty() {
396        return Ok(bytes::Bytes::new());
397    }
398
399    let compressed_len = data.len();
400    let ratio_limit = limit.max_decompression_ratio.and_then(|ratio| {
401        if ratio.is_finite() && ratio > 0.0 {
402            #[allow(clippy::cast_precision_loss)]
403            let limit = (compressed_len as f64 * ratio).ceil();
404            #[allow(
405                clippy::cast_possible_truncation,
406                clippy::cast_sign_loss,
407                clippy::cast_precision_loss,
408                reason = "the finite non-negative ratio is clamped to the usize allocation limit"
409            )]
410            // `usize::MAX as f64` rounds up to 2^64, and float→int casts
411            // saturate, which would yield `usize::MAX - 1`. Saturate
412            // explicitly so the extreme budget keeps the full limit.
413            Some(if limit >= usize::MAX as f64 {
414                usize::MAX
415            } else {
416                limit.min(usize::MAX as f64) as usize
417            })
418        } else {
419            None
420        }
421    });
422    let output_limit = match (limit.max_decoded_body_size, ratio_limit) {
423        (Some(size), Some(ratio)) => Some(size.min(ratio)),
424        (Some(size), None) | (None, Some(size)) => Some(size),
425        (None, None) => None,
426    };
427    let ratio_is_tighter = ratio_limit.is_some_and(|ratio| Some(ratio) == output_limit);
428
429    #[allow(unused_mut)]
430    let mut current = bytes::Bytes::copy_from_slice(data);
431    #[allow(clippy::never_loop)]
432    for encoding in encodings.into_iter().rev() {
433        match encoding {
434            ContentCoding::Gzip | ContentCoding::Deflate => {
435                #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
436                {
437                    let layer_input_len = current.len();
438                    current =
439                        sync_decode_flate2(&current, encoding, output_limit, ratio_is_tighter)?;
440                    if let Some(max_ratio) = limit.max_decompression_ratio {
441                        if layer_input_len > 0 {
442                            #[allow(clippy::cast_precision_loss)]
443                            let layer_ratio = current.len() as f64 / layer_input_len as f64;
444                            if layer_ratio > max_ratio {
445                                return Err(Error::DecompressionRatioExceeded);
446                            }
447                        }
448                    }
449                }
450                #[cfg(not(any(feature = "compression-gzip", feature = "compression-deflate")))]
451                {
452                    return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
453                }
454            }
455            ContentCoding::Brotli => {
456                #[cfg(feature = "compression-brotli")]
457                {
458                    let layer_input_len = current.len();
459                    current = sync_decode_brotli(&current, output_limit, ratio_is_tighter)?;
460                    if let Some(max_ratio) = limit.max_decompression_ratio {
461                        if layer_input_len > 0 {
462                            #[allow(clippy::cast_precision_loss)]
463                            let layer_ratio = current.len() as f64 / layer_input_len as f64;
464                            if layer_ratio > max_ratio {
465                                return Err(Error::DecompressionRatioExceeded);
466                            }
467                        }
468                    }
469                }
470                #[cfg(not(feature = "compression-brotli"))]
471                {
472                    return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
473                }
474            }
475            ContentCoding::Zstd => {
476                #[cfg(feature = "compression-zstd")]
477                {
478                    let layer_input_len = current.len();
479                    current = sync_decode_zstd(&current, output_limit, ratio_is_tighter)?;
480                    if let Some(max_ratio) = limit.max_decompression_ratio {
481                        if layer_input_len > 0 {
482                            #[allow(clippy::cast_precision_loss)]
483                            let layer_ratio = current.len() as f64 / layer_input_len as f64;
484                            if layer_ratio > max_ratio {
485                                return Err(Error::DecompressionRatioExceeded);
486                            }
487                        }
488                    }
489                }
490                #[cfg(not(feature = "compression-zstd"))]
491                {
492                    return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
493                }
494            }
495        }
496    }
497
498    // Final wire-size checks are mutually exclusive with the per-layer
499    // `output_limit` enforcement to keep the error kind stable when both
500    // limits are tight. When `output_limit` was ratio-derived, the size
501    // check is redundant (ratio*compressed < size) and would otherwise
502    // flip the error kind from `DecompressionRatioExceeded` to
503    // `DecodedBodyTooLarge` for the same logical violation.
504    let size_violated = limit
505        .max_decoded_body_size
506        .is_some_and(|max| current.len() > max);
507    let ratio_violated = limit.max_decompression_ratio.is_some_and(|max_ratio| {
508        compressed_len > 0 && {
509            #[allow(clippy::cast_precision_loss)]
510            let ratio = current.len() as f64 / compressed_len as f64;
511            ratio > max_ratio
512        }
513    });
514    if size_violated && ratio_violated {
515        if ratio_is_tighter {
516            return Err(Error::DecompressionRatioExceeded);
517        }
518        return Err(Error::DecodedBodyTooLarge);
519    }
520    if size_violated {
521        return Err(Error::DecodedBodyTooLarge);
522    }
523    if ratio_violated {
524        return Err(Error::DecompressionRatioExceeded);
525    }
526
527    Ok(current)
528}
529
530/// Incrementally drain a sync decoder, enforcing `output_limit` per chunk.
531///
532/// Replaces `take(limit+1)` + `read_to_end` so a large `output_limit` does
533/// not grow the output `Vec` to the full budget in one call: reads use a
534/// fixed 8 KiB stack buffer and fail fast once the cumulative output
535/// exceeds the limit.
536#[cfg(any(
537    feature = "compression-gzip",
538    feature = "compression-deflate",
539    feature = "compression-brotli",
540    feature = "compression-zstd"
541))]
542fn sync_read_limited<R: std::io::Read>(
543    mut reader: R,
544    output_limit: Option<usize>,
545    ratio_is_tighter: bool,
546) -> Result<bytes::Bytes> {
547    let mut output = Vec::new();
548    let mut buf = [0u8; 8 * 1024];
549    loop {
550        let n = reader
551            .read(&mut buf)
552            .map_err(|e| Error::Decompression(e.to_string()))?;
553        if n == 0 {
554            break;
555        }
556        output.extend_from_slice(&buf[..n]);
557        if output_limit.is_some_and(|limit| output.len() > limit) {
558            return Err(if ratio_is_tighter {
559                Error::DecompressionRatioExceeded
560            } else {
561                Error::DecodedBodyTooLarge
562            });
563        }
564    }
565    Ok(bytes::Bytes::from(output))
566}
567
568/// Synchronously decode a single layer of gzip or deflate compression using flate2.
569#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
570fn sync_decode_flate2(
571    data: &bytes::Bytes,
572    encoding: ContentCoding,
573    output_limit: Option<usize>,
574    ratio_is_tighter: bool,
575) -> Result<bytes::Bytes> {
576    match encoding {
577        ContentCoding::Gzip => {
578            let decoder = flate2::read::GzDecoder::new(&data[..]);
579            sync_read_limited(decoder, output_limit, ratio_is_tighter)
580        }
581        ContentCoding::Deflate => {
582            // HTTP `deflate` (RFC 9110 §8.4.1.2) is the zlib wrapper
583            // (RFC 1950), but some servers send raw deflate (RFC 1951).
584            // Try the wrapper first, then fall back to the raw stream so
585            // lenient receivers decode both framings.
586            let zlib = flate2::read::ZlibDecoder::new(&data[..]);
587            if let ok @ Ok(_) = sync_read_limited(zlib, output_limit, ratio_is_tighter) {
588                ok
589            } else {
590                let raw = flate2::read::DeflateDecoder::new(&data[..]);
591                sync_read_limited(raw, output_limit, ratio_is_tighter)
592            }
593        }
594        _ => Err(Error::UnsupportedContentEncoding(
595            encoding.as_str().to_owned(),
596        )),
597    }
598}
599
600#[cfg(feature = "compression-brotli")]
601fn sync_decode_brotli(
602    data: &bytes::Bytes,
603    output_limit: Option<usize>,
604    ratio_is_tighter: bool,
605) -> Result<bytes::Bytes> {
606    let decoder = brotli::Decompressor::new(&data[..], 4096);
607    sync_read_limited(decoder, output_limit, ratio_is_tighter)
608}
609
610#[cfg(feature = "compression-zstd")]
611fn sync_decode_zstd(
612    data: &bytes::Bytes,
613    output_limit: Option<usize>,
614    ratio_is_tighter: bool,
615) -> Result<bytes::Bytes> {
616    let decoder = zstd::stream::read::Decoder::new(&data[..])
617        .map_err(|e| Error::Decompression(e.to_string()))?;
618    sync_read_limited(decoder, output_limit, ratio_is_tighter)
619}
620
621/// Returns `true` when `prefix` looks like an RFC 1950 zlib wrapper
622/// header: compression method 8 (deflate) with a valid FCHECK checksum.
623/// A raw deflate stream can pass this check by coincidence, so the check
624/// only selects the first decoder tried on the streaming path (the
625/// buffered path tries the wrapper and falls back to raw on failure).
626#[cfg(feature = "compression-deflate")]
627fn is_zlib_header(prefix: &[u8]) -> bool {
628    let [cmf, flg, ..] = prefix else {
629        return false;
630    };
631    let cmf = u16::from(*cmf);
632    let flg = u16::from(*flg);
633    cmf & 0x0F == 8 && (cmf * 256 + flg) % 31 == 0
634}
635
636/// Build the streaming `deflate` decoder with lazy framing selection.
637///
638/// HTTP `deflate` (RFC 9110 §8.4.1.2) is the zlib wrapper (RFC 1950), but
639/// some servers send raw deflate (RFC 1951). The first two bytes are
640/// sniffed once they arrive ([`is_zlib_header`]) and the decoder is
641/// selected then, so streaming never buffers the whole body for a decode
642/// retry. Split out of [`make_decoder`] to keep that function small.
643#[cfg(feature = "compression-deflate")]
644fn make_deflate_decoder(stream: BoxBytesStream) -> BoxBytesStream {
645    use async_compression::tokio::bufread::{DeflateDecoder, ZlibDecoder};
646    use futures_util::StreamExt;
647    use tokio::io::{AsyncBufReadExt, AsyncRead, BufReader};
648    use tokio_util::io::{ReaderStream, StreamReader};
649
650    enum DeflateState<R> {
651        Sniffing(R),
652        Decoding(ReaderStream<std::pin::Pin<Box<dyn AsyncRead + Send>>>),
653        Done,
654    }
655
656    let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
657    let reader = StreamReader::new(mapped);
658    let sniffed = futures_util::stream::unfold(
659        DeflateState::Sniffing(BufReader::new(reader)),
660        |mut state| async move {
661            loop {
662                match state {
663                    DeflateState::Sniffing(mut reader) => {
664                        let is_zlib = match reader.fill_buf().await {
665                            Ok(buf) => {
666                                if buf.is_empty() {
667                                    return None;
668                                }
669                                is_zlib_header(buf)
670                            }
671                            Err(e) => return Some((Err(e), DeflateState::Done)),
672                        };
673                        let decoder: std::pin::Pin<Box<dyn AsyncRead + Send>> = if is_zlib {
674                            Box::pin(ZlibDecoder::new(reader))
675                        } else {
676                            Box::pin(DeflateDecoder::new(reader))
677                        };
678                        state = DeflateState::Decoding(ReaderStream::new(decoder));
679                    }
680                    DeflateState::Decoding(mut inner) => {
681                        return inner
682                            .next()
683                            .await
684                            .map(|item| (item, DeflateState::Decoding(inner)));
685                    }
686                    DeflateState::Done => return None,
687                }
688            }
689        },
690    );
691    let stream = sniffed.map(|r| r.map_err(|e| Error::Decompression(e.to_string())));
692    Box::pin(stream)
693}
694
695/// Create a decoder for a single content coding.
696#[allow(
697    clippy::unnecessary_wraps,
698    clippy::needless_pass_by_value,
699    unused_variables
700)]
701fn make_decoder(stream: BoxBytesStream, encoding: ContentCoding) -> Result<BoxBytesStream> {
702    match encoding {
703        ContentCoding::Gzip => {
704            #[cfg(feature = "compression-gzip")]
705            {
706                use async_compression::tokio::bufread::GzipDecoder;
707                use futures_util::StreamExt;
708                use tokio::io::BufReader;
709                use tokio_util::io::{ReaderStream, StreamReader};
710
711                // Map locally to keep the conversion private; the decoder
712                // boundary keeps its explicit `Error::Decompression` text.
713                let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
714                let reader = StreamReader::new(mapped);
715                let decoder = GzipDecoder::new(BufReader::new(reader));
716                let stream = ReaderStream::new(decoder);
717                Ok(Box::pin(stream.map(|r| {
718                    r.map_err(|e| Error::Decompression(e.to_string()))
719                })))
720            }
721            #[cfg(not(feature = "compression-gzip"))]
722            {
723                Err(Error::UnsupportedContentEncoding("gzip".to_string()))
724            }
725        }
726        ContentCoding::Deflate => {
727            #[cfg(feature = "compression-deflate")]
728            {
729                Ok(make_deflate_decoder(stream))
730            }
731            #[cfg(not(feature = "compression-deflate"))]
732            {
733                Err(Error::UnsupportedContentEncoding("deflate".to_string()))
734            }
735        }
736        ContentCoding::Brotli => {
737            #[cfg(feature = "compression-brotli")]
738            {
739                use async_compression::tokio::bufread::BrotliDecoder;
740                use futures_util::StreamExt;
741                use tokio::io::BufReader;
742                use tokio_util::io::{ReaderStream, StreamReader};
743
744                let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
745                let reader = StreamReader::new(mapped);
746                let decoder = BrotliDecoder::new(BufReader::new(reader));
747                let stream = ReaderStream::new(decoder);
748                Ok(Box::pin(stream.map(|r| {
749                    r.map_err(|e| Error::Decompression(e.to_string()))
750                })))
751            }
752            #[cfg(not(feature = "compression-brotli"))]
753            {
754                Err(Error::UnsupportedContentEncoding("br".to_string()))
755            }
756        }
757        ContentCoding::Zstd => {
758            #[cfg(feature = "compression-zstd")]
759            {
760                use async_compression::tokio::bufread::ZstdDecoder;
761                use futures_util::StreamExt;
762                use tokio::io::BufReader;
763                use tokio_util::io::{ReaderStream, StreamReader};
764
765                let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
766                let reader = StreamReader::new(mapped);
767                let decoder = ZstdDecoder::new(BufReader::new(reader));
768                let stream = ReaderStream::new(decoder);
769                Ok(Box::pin(stream.map(|r| {
770                    r.map_err(|e| Error::Decompression(e.to_string()))
771                })))
772            }
773            #[cfg(not(feature = "compression-zstd"))]
774            {
775                Err(Error::UnsupportedContentEncoding("zstd".to_string()))
776            }
777        }
778    }
779}
780
781/// A stream wrapper that counts the compressed bytes yielded by the
782/// underlying stream. The count is exposed via a shared
783/// `Arc<AtomicUsize>` so that downstream limiters can read the
784/// compressed byte count without needing to see the raw stream.
785///
786/// For stacked decoders the *outer* `CountingStream` (first in the chain)
787/// counts wire bytes; inner counters count the outer layer's decompressed
788/// output. The outer counter is used for the final cumulative ratio
789/// check, while each inner counter drives its own per-layer ratio check
790/// (see `decompress_stream` docs).
791struct CountingStream {
792    inner: BoxBytesStream,
793    count: Arc<AtomicUsize>,
794}
795
796impl CountingStream {
797    fn new(inner: BoxBytesStream) -> Self {
798        Self {
799            inner,
800            count: Arc::new(AtomicUsize::new(0)),
801        }
802    }
803
804    fn counter(&self) -> Arc<AtomicUsize> {
805        Arc::clone(&self.count)
806    }
807}
808
809/// Atomically add `amount` to `counter`, saturating at `usize::MAX`
810/// instead of wrapping. A wrapped counter would under-report compressed
811/// bytes and silently disable a configured decompression-ratio limit
812/// mid-stream.
813fn saturating_fetch_add(counter: &AtomicUsize, amount: usize) {
814    let mut current = counter.load(Ordering::Acquire);
815    loop {
816        let Some(next) = current.checked_add(amount) else {
817            counter.store(usize::MAX, Ordering::Release);
818            return;
819        };
820        match counter.compare_exchange_weak(current, next, Ordering::AcqRel, Ordering::Acquire) {
821            Ok(_) => return,
822            Err(actual) => current = actual,
823        }
824    }
825}
826
827impl futures_core::Stream for CountingStream {
828    type Item = <BoxBytesStream as futures_core::Stream>::Item;
829
830    fn poll_next(
831        mut self: std::pin::Pin<&mut Self>,
832        cx: &mut std::task::Context<'_>,
833    ) -> std::task::Poll<Option<Self::Item>> {
834        use std::pin::Pin;
835
836        match Pin::new(&mut self.inner).poll_next(cx) {
837            std::task::Poll::Ready(Some(Ok(chunk))) => {
838                saturating_fetch_add(&self.count, chunk.len());
839                std::task::Poll::Ready(Some(Ok(chunk)))
840            }
841            other => other,
842        }
843    }
844}
845
846struct LimitingStream {
847    inner: BoxBytesStream,
848    limit: DecompressionLimit,
849    decoded_bytes: usize,
850    compressed_counter: Arc<AtomicUsize>,
851    limit_exceeded: Option<LimitExceededKind>,
852}
853
854/// Which limit tripped first; retained so later polls fail fast without
855/// re-polling the inner transport.
856#[derive(Debug, Clone, Copy, PartialEq, Eq)]
857enum LimitExceededKind {
858    Size,
859    Ratio,
860}
861
862impl LimitingStream {
863    fn new(
864        inner: BoxBytesStream,
865        limit: DecompressionLimit,
866        compressed_counter: Arc<AtomicUsize>,
867    ) -> Self {
868        Self {
869            inner,
870            limit,
871            decoded_bytes: 0,
872            compressed_counter,
873            limit_exceeded: None,
874        }
875    }
876
877    #[allow(clippy::cast_precision_loss)]
878    fn check_limit(&self) -> std::result::Result<(), LimitExceededKind> {
879        if let Some(max) = self.limit.max_decoded_body_size {
880            if self.decoded_bytes > max {
881                return Err(LimitExceededKind::Size);
882            }
883        }
884        if let Some(max_ratio) = self.limit.max_decompression_ratio {
885            if !max_ratio.is_finite() || max_ratio <= 0.0 {
886                // Invalid configuration surfaces as a decompression error;
887                // record it as a terminal ratio failure so we still fuse.
888                return Err(LimitExceededKind::Ratio);
889            }
890            let compressed = self.compressed_counter.load(Ordering::Acquire);
891            if compressed > 0 {
892                let ratio = self.decoded_bytes as f64 / compressed as f64;
893                if ratio > max_ratio {
894                    return Err(LimitExceededKind::Ratio);
895                }
896            }
897        }
898        Ok(())
899    }
900
901    fn kind_to_error(kind: LimitExceededKind, limit: &DecompressionLimit) -> Error {
902        match kind {
903            LimitExceededKind::Size => Error::DecodedBodyTooLarge,
904            LimitExceededKind::Ratio => {
905                // Preserve the invalid-ratio configuration message; all
906                // genuine ratio violations map to `DecompressionRatioExceeded`.
907                if limit
908                    .max_decompression_ratio
909                    .is_some_and(|r| !r.is_finite() || r <= 0.0)
910                {
911                    Error::Decompression(
912                        "invalid max_decompression_ratio: must be finite and positive".into(),
913                    )
914                } else {
915                    Error::DecompressionRatioExceeded
916                }
917            }
918        }
919    }
920}
921
922impl futures_core::Stream for LimitingStream {
923    type Item = Result<bytes::Bytes>;
924
925    fn poll_next(
926        mut self: std::pin::Pin<&mut Self>,
927        cx: &mut std::task::Context<'_>,
928    ) -> std::task::Poll<Option<Self::Item>> {
929        use std::pin::Pin;
930
931        // Once the limit trips, fuse without polling inner again (see
932        // `LimitedResponseStream`: `None` keeps `collect` terminating;
933        // callers already observed the first error).
934        if self.limit_exceeded.is_some() {
935            return std::task::Poll::Ready(None);
936        }
937
938        match Pin::new(&mut self.inner).poll_next(cx) {
939            std::task::Poll::Ready(Some(Ok(chunk))) => {
940                // Saturate instead of wrapping: a wrapped counter would
941                // permanently disable the limit mid-stream on targets
942                // where `usize` is narrower than the body size.
943                self.decoded_bytes = self.decoded_bytes.saturating_add(chunk.len());
944                if let Err(kind) = self.check_limit() {
945                    self.limit_exceeded = Some(kind);
946                    let err = Self::kind_to_error(kind, &self.limit);
947                    return std::task::Poll::Ready(Some(Err(err)));
948                }
949                std::task::Poll::Ready(Some(Ok(chunk)))
950            }
951            std::task::Poll::Ready(Some(Err(e))) => std::task::Poll::Ready(Some(Err(e))),
952            std::task::Poll::Ready(None) => std::task::Poll::Ready(None),
953            std::task::Poll::Pending => std::task::Poll::Pending,
954        }
955    }
956}
957
958#[cfg(test)]
959mod tests {
960    use super::*;
961    use futures_util::FutureExt;
962    #[cfg(feature = "compression-gzip")]
963    use futures_util::StreamExt;
964
965    #[test]
966    fn content_coding_from_wire() {
967        assert_eq!(ContentCoding::from_wire("gzip"), Some(ContentCoding::Gzip));
968        assert_eq!(
969            ContentCoding::from_wire("x-gzip"),
970            Some(ContentCoding::Gzip)
971        );
972        assert_eq!(
973            ContentCoding::from_wire("deflate"),
974            Some(ContentCoding::Deflate)
975        );
976        assert_eq!(ContentCoding::from_wire("br"), Some(ContentCoding::Brotli));
977        assert_eq!(ContentCoding::from_wire("zstd"), Some(ContentCoding::Zstd));
978        assert_eq!(ContentCoding::from_wire("identity"), None);
979        assert_eq!(ContentCoding::from_wire("unknown"), None);
980        assert_eq!(ContentCoding::from_wire("GZIP"), Some(ContentCoding::Gzip));
981        assert_eq!(
982            ContentCoding::from_wire(" Br "),
983            Some(ContentCoding::Brotli)
984        );
985    }
986
987    #[test]
988    fn content_coding_as_str() {
989        assert_eq!(ContentCoding::Gzip.as_str(), "gzip");
990        assert_eq!(ContentCoding::Deflate.as_str(), "deflate");
991        assert_eq!(ContentCoding::Brotli.as_str(), "br");
992        assert_eq!(ContentCoding::Zstd.as_str(), "zstd");
993    }
994
995    #[test]
996    fn parse_content_encodings_single() {
997        let encs = parse_content_encodings("gzip").unwrap();
998        assert_eq!(encs, vec![ContentCoding::Gzip]);
999    }
1000
1001    #[test]
1002    fn parse_content_encodings_multiple() {
1003        let encs = parse_content_encodings("gzip, br").unwrap();
1004        assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1005    }
1006
1007    #[test]
1008    fn parse_content_encodings_with_whitespace() {
1009        let encs = parse_content_encodings("  gzip , br  ").unwrap();
1010        assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1011    }
1012
1013    #[test]
1014    fn parse_content_encodings_empty() {
1015        assert!(parse_content_encodings("").is_none());
1016        assert!(parse_content_encodings("  ").is_none());
1017    }
1018
1019    #[test]
1020    fn parse_content_encodings_unknown_filtered() {
1021        let encs = parse_content_encodings("gzip, identity, br").unwrap();
1022        assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1023    }
1024
1025    #[test]
1026    fn validate_content_encodings_supported() {
1027        assert!(validate_content_encodings("gzip").is_ok());
1028        assert!(validate_content_encodings("gzip, br").is_ok());
1029        assert!(validate_content_encodings("").is_ok());
1030    }
1031
1032    #[test]
1033    fn validate_content_encodings_accepts_identity() {
1034        // RFC 9110 §8.4: `identity` is a valid no-op content-coding.
1035        assert!(validate_content_encodings("identity").is_ok());
1036        assert!(validate_content_encodings("IDENTITY").is_ok());
1037        assert!(validate_content_encodings("identity, gzip").is_ok());
1038        assert!(validate_content_encodings("gzip, identity").is_ok());
1039    }
1040
1041    #[test]
1042    fn parse_content_encodings_identity_only_is_none() {
1043        assert!(parse_content_encodings("identity").is_none());
1044        let encs = parse_content_encodings("gzip, identity, br").unwrap();
1045        assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1046    }
1047
1048    #[test]
1049    fn validate_content_encodings_unsupported() {
1050        let err = validate_content_encodings("gzip, weird").unwrap_err();
1051        assert_eq!(err.kind(), "unsupported_content_encoding");
1052        assert!(err.to_string().contains("weird"));
1053    }
1054
1055    #[test]
1056    fn accept_encoding_value_requires_compiled_features() {
1057        let _ = accept_encoding_value();
1058    }
1059
1060    #[cfg(not(any(
1061        feature = "compression-gzip",
1062        feature = "compression-deflate",
1063        feature = "compression-brotli",
1064        feature = "compression-zstd"
1065    )))]
1066    #[test]
1067    fn accept_encoding_value_none_without_compression_features() {
1068        assert!(
1069            accept_encoding_value().is_none(),
1070            "accept_encoding_value() should return None when no compression features are compiled"
1071        );
1072    }
1073
1074    #[cfg(all(
1075        feature = "compression-gzip",
1076        not(feature = "compression-brotli"),
1077        not(feature = "compression-zstd")
1078    ))]
1079    #[test]
1080    fn accept_encoding_value_gzip_only() {
1081        let val = accept_encoding_value().unwrap();
1082        assert!(val.contains("gzip"), "should contain gzip, got: {val}");
1083        assert!(
1084            !val.contains("br"),
1085            "should not contain br without brotli feature, got: {val}"
1086        );
1087        assert!(
1088            !val.contains("zstd"),
1089            "should not contain zstd without zstd feature, got: {val}"
1090        );
1091    }
1092
1093    #[test]
1094    fn accept_encoding_value_deterministic_order() {
1095        let val1 = accept_encoding_value();
1096        let val2 = accept_encoding_value();
1097        assert_eq!(
1098            val1, val2,
1099            "accept_encoding_value() should return the same value across calls"
1100        );
1101    }
1102
1103    #[test]
1104    fn accept_encoding_advertises_only_supported_decoders() {
1105        // Every coding advertised in Accept-Encoding must have a working
1106        // streaming decoder for the compiled-in feature set; otherwise a
1107        // compliant server can produce responses we cannot decode.
1108        let Some(value) = accept_encoding_value() else {
1109            return;
1110        };
1111        for token in value.split(", ") {
1112            let coding =
1113                ContentCoding::from_wire(token).unwrap_or_else(|| panic!("invalid token {token}"));
1114            let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1115            let result = make_decoder(stream, coding);
1116            assert!(
1117                result.is_ok(),
1118                "advertised encoding '{token}' has no compiled-in decoder"
1119            );
1120        }
1121    }
1122
1123    #[test]
1124    fn decompress_stream_no_encoding_returns_original() {
1125        let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1126        let result = decompress_stream(stream, None, true, DecompressionLimit::new()).unwrap();
1127        drop(result);
1128    }
1129
1130    #[test]
1131    fn decompress_stream_disabled_returns_original() {
1132        let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1133        let result =
1134            decompress_stream(stream, Some("gzip"), false, DecompressionLimit::new()).unwrap();
1135        drop(result);
1136    }
1137
1138    #[test]
1139    fn decompress_stream_empty_header_returns_original() {
1140        let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1141        let result =
1142            decompress_stream(stream, Some("  "), true, DecompressionLimit::new()).unwrap();
1143        drop(result);
1144    }
1145
1146    #[test]
1147    fn decompress_buffered_identity_passthrough() {
1148        // RFC 9110 §8.4: `identity` means "no encoding"; the body must be
1149        // served unchanged instead of raising UnsupportedContentEncoding.
1150        assert_eq!(
1151            &decompress_buffered(b"plain body", "identity", DecompressionLimit::new()).unwrap()[..],
1152            b"plain body"
1153        );
1154        let err = decompress_buffered(b"x", "weird", DecompressionLimit::new()).unwrap_err();
1155        assert_eq!(err.kind(), "unsupported_content_encoding");
1156    }
1157
1158    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1159    #[test]
1160    fn sync_decode_flate2_rejects_non_flate_encoding() {
1161        let err = sync_decode_flate2(&bytes::Bytes::new(), ContentCoding::Brotli, None, false)
1162            .unwrap_err();
1163        assert_eq!(err.kind(), "unsupported_content_encoding");
1164        assert!(err.to_string().contains("br"));
1165    }
1166
1167    #[test]
1168    fn decompression_limit_new_is_unlimited() {
1169        let limit = DecompressionLimit::new();
1170        assert!(limit.is_unlimited());
1171        assert!(limit.max_decoded_body_size.is_none());
1172        assert!(limit.max_decompression_ratio.is_none());
1173    }
1174
1175    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1176    #[test]
1177    fn decompression_limit_max_size_rejects() {
1178        let data = gzip_compress(b"hello world");
1179        let limit = DecompressionLimit {
1180            max_decoded_body_size: Some(5),
1181            max_decompression_ratio: None,
1182        };
1183        let err = decompress_buffered(&data, "gzip", limit).unwrap_err();
1184        assert_eq!(err.kind(), "decoded_body_too_large");
1185    }
1186
1187    #[cfg(feature = "compression-deflate")]
1188    #[test]
1189    fn deflate_buffered_decodes_zlib_wrapper_and_raw() {
1190        use std::io::Write;
1191        let plain = b"hello deflate world, hello again";
1192
1193        let mut zlib = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
1194        zlib.write_all(plain).unwrap();
1195        let zlib = zlib.finish().unwrap();
1196        assert!(is_zlib_header(&zlib));
1197        let result = decompress_buffered(&zlib, "deflate", DecompressionLimit::new()).unwrap();
1198        assert_eq!(&result[..], &plain[..]);
1199
1200        let mut raw =
1201            flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default());
1202        raw.write_all(plain).unwrap();
1203        let raw = raw.finish().unwrap();
1204        let result = decompress_buffered(&raw, "deflate", DecompressionLimit::new()).unwrap();
1205        assert_eq!(&result[..], &plain[..]);
1206    }
1207
1208    #[cfg(feature = "compression-deflate")]
1209    #[test]
1210    fn zlib_header_sniff_classifies_framing() {
1211        assert!(!is_zlib_header(&[]));
1212        assert!(!is_zlib_header(&[0x78]));
1213        assert!(!is_zlib_header(&[0x1f, 0x8b])); // gzip magic
1214        assert!(is_zlib_header(&[0x78, 0x9c, 0x03])); // default zlib header
1215    }
1216
1217    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1218    #[test]
1219    fn decompression_limit_max_size_allows_within() {
1220        let data = gzip_compress(b"hi");
1221        let limit = DecompressionLimit {
1222            max_decoded_body_size: Some(1024),
1223            max_decompression_ratio: None,
1224        };
1225        let result = decompress_buffered(&data, "gzip", limit).unwrap();
1226        assert_eq!(&result[..], b"hi");
1227    }
1228
1229    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1230    #[test]
1231    fn decompression_limit_ratio_rejects() {
1232        let data = gzip_compress(b"hello world, this is a test of compression");
1233        let limit = DecompressionLimit {
1234            max_decoded_body_size: None,
1235            max_decompression_ratio: Some(0.5),
1236        };
1237        let err = decompress_buffered(&data, "gzip", limit).unwrap_err();
1238        assert_eq!(err.kind(), "decompression_ratio_exceeded");
1239    }
1240
1241    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1242    #[test]
1243    fn decompression_limit_ratio_allows_within() {
1244        let data = gzip_compress(b"hello world, this is a test of compression");
1245        let limit = DecompressionLimit {
1246            max_decoded_body_size: None,
1247            max_decompression_ratio: Some(100.0),
1248        };
1249        let result = decompress_buffered(&data, "gzip", limit).unwrap();
1250        assert_eq!(&result[..], b"hello world, this is a test of compression");
1251    }
1252
1253    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1254    #[test]
1255    fn decompression_limit_unlimited_allows() {
1256        let data = gzip_compress(b"hello world");
1257        let limit = DecompressionLimit::new();
1258        let result = decompress_buffered(&data, "gzip", limit).unwrap();
1259        assert_eq!(&result[..], b"hello world");
1260    }
1261
1262    #[cfg(feature = "compression-gzip")]
1263    #[test]
1264    fn decompression_limit_ratio_enforced_per_layer() {
1265        // Stacked encodings: the middle layer stores near-incompressible
1266        // data, so the cumulative expansion measured against the wire size
1267        // is *smaller* than the inner layer's own expansion. A per-layer
1268        // limit between the two must reject even though the old
1269        // final-size-vs-outermost-length check would have passed.
1270        let payload = vec![b'a'; 5000];
1271        let inner = gzip_compress(&payload);
1272        let outer = gzip_compress(&inner);
1273        #[allow(clippy::cast_precision_loss)]
1274        let (total_ratio, inner_step) = {
1275            (
1276                payload.len() as f64 / outer.len() as f64,
1277                payload.len() as f64 / inner.len() as f64,
1278            )
1279        };
1280        assert!(
1281            inner_step > total_ratio + 1.0,
1282            "expected the inner step to dominate: {inner_step} vs {total_ratio}"
1283        );
1284
1285        #[allow(clippy::cast_precision_loss)]
1286        let limit_value = total_ratio.midpoint(inner_step).ceil();
1287        let limit = DecompressionLimit {
1288            max_decoded_body_size: None,
1289            max_decompression_ratio: Some(limit_value),
1290        };
1291        let err = decompress_buffered(&outer, "gzip, gzip", limit).unwrap_err();
1292        assert_eq!(err.kind(), "decompression_ratio_exceeded");
1293
1294        // With no ratio limit both layers decode fine.
1295        let ok = decompress_buffered(&outer, "gzip, gzip", DecompressionLimit::default()).unwrap();
1296        assert_eq!(&ok[..], &payload[..]);
1297    }
1298
1299    #[cfg(feature = "compression-gzip")]
1300    #[tokio::test]
1301    async fn decompression_stream_enforces_ratio_per_layer() {
1302        let payload = vec![b'a'; 5000];
1303        let inner = gzip_compress(&payload);
1304        let outer = gzip_compress(&inner);
1305        #[allow(clippy::cast_precision_loss)]
1306        let limit_value = (payload.len() as f64 / outer.len() as f64)
1307            .midpoint(payload.len() as f64 / inner.len() as f64);
1308        let limit = DecompressionLimit {
1309            max_decoded_body_size: None,
1310            max_decompression_ratio: Some(limit_value.ceil()),
1311        };
1312        let input: BoxBytesStream = Box::pin(futures_util::stream::once(async move {
1313            Ok(bytes::Bytes::from(outer))
1314        }));
1315        let decoded = decompress_stream(input, Some("gzip, gzip"), true, limit).unwrap();
1316        let err = decoded
1317            .collect::<Vec<_>>()
1318            .await
1319            .into_iter()
1320            .find_map(std::result::Result::err)
1321            .expect("streaming ratio limit must reject the inner layer");
1322        assert_eq!(err.kind(), "decompression_ratio_exceeded");
1323    }
1324
1325    #[cfg(feature = "compression-gzip")]
1326    #[tokio::test]
1327    async fn decompression_stream_enforces_ratio_against_wire_size() {
1328        // Both individual layers stay below the limit, but their cumulative
1329        // expansion exceeds it. The final check must use the original wire
1330        // size rather than the inner layer's size.
1331        let payload: Vec<u8> = (0usize..5000)
1332            .map(|i| u8::try_from((i * 73 + i / 17) % 251).expect("value is below 251"))
1333            .collect();
1334        let inner = gzip_compress(&payload);
1335        let outer = gzip_compress(&inner);
1336        #[allow(clippy::cast_precision_loss)]
1337        let (first_layer, second_layer, total) = (
1338            inner.len() as f64 / outer.len() as f64,
1339            payload.len() as f64 / inner.len() as f64,
1340            payload.len() as f64 / outer.len() as f64,
1341        );
1342        let per_layer_limit = first_layer.max(second_layer);
1343        assert!(total > per_layer_limit);
1344        let limit = DecompressionLimit {
1345            max_decoded_body_size: None,
1346            max_decompression_ratio: Some(per_layer_limit.midpoint(total)),
1347        };
1348        let input: BoxBytesStream = Box::pin(futures_util::stream::once(async move {
1349            Ok(bytes::Bytes::from(outer))
1350        }));
1351        let decoded = decompress_stream(input, Some("gzip, gzip"), true, limit).unwrap();
1352        let err = decoded
1353            .collect::<Vec<_>>()
1354            .await
1355            .into_iter()
1356            .find_map(std::result::Result::err)
1357            .expect("streaming ratio limit must include the wire size");
1358        assert_eq!(err.kind(), "decompression_ratio_exceeded");
1359    }
1360
1361    #[test]
1362    fn limiting_stream_enforces_max_decoded_body_size() {
1363        let limit = DecompressionLimit {
1364            max_decoded_body_size: Some(3),
1365            max_decompression_ratio: None,
1366        };
1367        let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![
1368            Ok(bytes::Bytes::from("ab")),
1369            Ok(bytes::Bytes::from("cd")),
1370        ]));
1371        let counter = Arc::new(AtomicUsize::new(0));
1372        let mut limited = LimitingStream::new(stream, limit, counter);
1373
1374        let chunk1 = futures_util::StreamExt::next(&mut limited)
1375            .now_or_never()
1376            .unwrap()
1377            .unwrap()
1378            .unwrap();
1379        assert_eq!(chunk1, "ab");
1380
1381        let err = futures_util::StreamExt::next(&mut limited)
1382            .now_or_never()
1383            .unwrap()
1384            .unwrap()
1385            .unwrap_err();
1386        assert_eq!(err.kind(), "decoded_body_too_large");
1387    }
1388
1389    #[test]
1390    fn limiting_stream_allows_within_limit() {
1391        let limit = DecompressionLimit {
1392            max_decoded_body_size: Some(1024),
1393            max_decompression_ratio: None,
1394        };
1395        let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![
1396            Ok(bytes::Bytes::from("a")),
1397            Ok(bytes::Bytes::from("b")),
1398        ]));
1399        let counter = Arc::new(AtomicUsize::new(0));
1400        let mut limited = LimitingStream::new(stream, limit, counter);
1401
1402        let chunk1 = futures_util::StreamExt::next(&mut limited)
1403            .now_or_never()
1404            .unwrap()
1405            .unwrap()
1406            .unwrap();
1407        assert_eq!(chunk1, "a");
1408        let chunk2 = futures_util::StreamExt::next(&mut limited)
1409            .now_or_never()
1410            .unwrap()
1411            .unwrap()
1412            .unwrap();
1413        assert_eq!(chunk2, "b");
1414    }
1415
1416    #[test]
1417    fn saturating_fetch_add_saturates_instead_of_wrapping() {
1418        let counter = AtomicUsize::new(usize::MAX - 2);
1419        saturating_fetch_add(&counter, 1);
1420        assert_eq!(counter.load(Ordering::Relaxed), usize::MAX - 1);
1421        saturating_fetch_add(&counter, 5);
1422        assert_eq!(counter.load(Ordering::Relaxed), usize::MAX);
1423        // Further adds stay pinned at the maximum.
1424        saturating_fetch_add(&counter, 5);
1425        assert_eq!(counter.load(Ordering::Relaxed), usize::MAX);
1426    }
1427
1428    #[test]
1429    fn limiting_stream_saturated_counter_still_enforces_size_limit() {
1430        let limit = DecompressionLimit {
1431            max_decoded_body_size: Some(10),
1432            max_decompression_ratio: None,
1433        };
1434        let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![Ok(
1435            bytes::Bytes::from("x"),
1436        )]));
1437        let counter = Arc::new(AtomicUsize::new(0));
1438        let mut limited = LimitingStream::new(stream, limit, counter);
1439        // Simulate a saturated counter (e.g. >4 GiB decoded on a 32-bit
1440        // target): the size limit must still trip instead of wrapping to a
1441        // small value and passing.
1442        limited.decoded_bytes = usize::MAX;
1443        let err = futures_util::StreamExt::next(&mut limited)
1444            .now_or_never()
1445            .unwrap()
1446            .unwrap()
1447            .unwrap_err();
1448        assert_eq!(err.kind(), "decoded_body_too_large");
1449    }
1450
1451    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1452    fn gzip_compress(data: &[u8]) -> Vec<u8> {
1453        use flate2::write::GzEncoder;
1454        use flate2::Compression;
1455        use std::io::Write;
1456
1457        let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
1458        encoder.write_all(data).unwrap();
1459        encoder.finish().unwrap()
1460    }
1461}