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