use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::sync::OnceLock;
use crate::body::BoxBytesStream;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, Default)]
pub struct DecompressionLimit {
pub(crate) max_decoded_body_size: Option<usize>,
pub(crate) max_decompression_ratio: Option<f64>,
}
impl DecompressionLimit {
#[must_use]
pub const fn new() -> Self {
Self {
max_decoded_body_size: None,
max_decompression_ratio: None,
}
}
#[must_use]
pub fn is_unlimited(&self) -> bool {
self.max_decoded_body_size.is_none() && self.max_decompression_ratio.is_none()
}
#[must_use]
pub fn max_decoded_body_size(&self) -> Option<usize> {
self.max_decoded_body_size
}
#[must_use]
pub fn max_decompression_ratio(&self) -> Option<f64> {
self.max_decompression_ratio
}
pub fn validate(&self) -> Result<()> {
if let Some(ratio) = self.max_decompression_ratio {
if !ratio.is_finite() || ratio <= 0.0 {
return Err(Error::Decompression(
"invalid max_decompression_ratio: must be finite and positive".into(),
));
}
}
Ok(())
}
pub fn try_new(
max_decoded_body_size: Option<usize>,
max_decompression_ratio: Option<f64>,
) -> crate::error::Result<Self> {
if let Some(ratio) = max_decompression_ratio {
if !ratio.is_finite() || ratio <= 0.0 {
return Err(crate::error::Error::RequestBuild(
"max_decompression_ratio must be finite and positive".into(),
));
}
}
Ok(Self {
max_decoded_body_size,
max_decompression_ratio,
})
}
}
const MAX_NESTING_DEPTH: usize = 4;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContentCoding {
Gzip,
Deflate,
Brotli,
Zstd,
}
impl ContentCoding {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Gzip => "gzip",
Self::Deflate => "deflate",
Self::Brotli => "br",
Self::Zstd => "zstd",
}
}
fn from_wire(s: &str) -> Option<Self> {
match s.trim().to_ascii_lowercase().as_str() {
"gzip" | "x-gzip" => Some(Self::Gzip),
"deflate" => Some(Self::Deflate),
"br" => Some(Self::Brotli),
"zstd" => Some(Self::Zstd),
_ => None,
}
}
fn is_identity(token: &str) -> bool {
token.trim().eq_ignore_ascii_case("identity")
}
}
#[must_use]
pub fn accept_encoding_value() -> Option<&'static str> {
static VALUE: OnceLock<Option<Box<str>>> = OnceLock::new();
VALUE
.get_or_init(|| {
#[allow(
clippy::vec_init_then_push,
reason = "the advertised codings are pushed under per-codec feature gates, so a vec! literal cannot express the compiled-in subset"
)]
{
#[allow(
unused_mut,
reason = "only the feature-gated codec pushes mutate this binding, so it is immutable when no codec is compiled in"
)]
let mut parts: Vec<&str> = Vec::new();
#[cfg(feature = "compression-gzip")]
parts.push("gzip");
#[cfg(feature = "compression-deflate")]
parts.push("deflate");
#[cfg(feature = "compression-brotli")]
parts.push("br");
#[cfg(feature = "compression-zstd")]
parts.push("zstd");
(!parts.is_empty()).then(|| parts.join(", ").into_boxed_str())
}
})
.as_deref()
}
pub(crate) fn parse_content_encodings(header: &str) -> Option<Vec<ContentCoding>> {
let encodings: Vec<ContentCoding> = header
.split(',')
.map(str::trim)
.filter(|s| !s.is_empty() && !ContentCoding::is_identity(s))
.filter_map(ContentCoding::from_wire)
.collect();
if encodings.is_empty() {
None
} else {
Some(encodings)
}
}
pub fn validate_content_encodings(header_value: &str) -> Result<()> {
for token in header_value.split(',') {
let token = token.trim();
if token.is_empty() || ContentCoding::is_identity(token) {
continue;
}
if ContentCoding::from_wire(token).is_none() {
return Err(Error::UnsupportedContentEncoding(token.to_string()));
}
}
Ok(())
}
pub fn decompress_stream(
stream: BoxBytesStream,
content_encoding: Option<&str>,
decompression_enabled: bool,
limit: DecompressionLimit,
) -> Result<BoxBytesStream> {
if !decompression_enabled {
return Ok(stream);
}
let header = match content_encoding {
Some(h) if !h.trim().is_empty() => h,
_ => return Ok(stream),
};
limit.validate()?;
validate_content_encodings(header)?;
let Some(encodings) = parse_content_encodings(header) else {
return Ok(stream);
};
if encodings.len() > MAX_NESTING_DEPTH {
return Err(Error::Decompression(format!(
"content encoding nesting depth {} exceeds maximum {}",
encodings.len(),
MAX_NESTING_DEPTH
)));
}
let mut current = stream;
let mut outer_counter = None;
for encoding in encodings.into_iter().rev() {
let counting = CountingStream::new(current);
let counter = counting.counter();
current = make_decoder(Box::pin(counting), encoding)?;
if outer_counter.is_none() {
outer_counter = Some(Arc::clone(&counter));
}
if let Some(max_ratio) = limit.max_decompression_ratio {
current = Box::pin(LimitingStream::new(
current,
DecompressionLimit {
max_decoded_body_size: None,
max_decompression_ratio: Some(max_ratio),
},
counter,
));
}
}
if limit.is_unlimited() {
return Ok(current);
}
let Some(outer_counter) = outer_counter else {
return Ok(current);
};
Ok(Box::pin(LimitingStream::new(current, limit, outer_counter)))
}
#[allow(
clippy::too_many_lines,
reason = "the buffered decoder keeps the limit validation, the per-coding decode branches and the final size/ratio verdicts in one linear pass"
)]
pub fn decompress_buffered(
data: &[u8],
content_encoding: &str,
limit: DecompressionLimit,
) -> Result<bytes::Bytes> {
limit.validate()?;
validate_content_encodings(content_encoding)?;
let encodings = parse_content_encodings(content_encoding);
let Some(encodings) = encodings else {
return Ok(bytes::Bytes::copy_from_slice(data));
};
if encodings.len() > MAX_NESTING_DEPTH {
return Err(Error::Decompression(format!(
"content encoding nesting depth {} exceeds maximum {}",
encodings.len(),
MAX_NESTING_DEPTH
)));
}
if data.is_empty() {
return Ok(bytes::Bytes::new());
}
let compressed_len = data.len();
let ratio_limit = limit.max_decompression_ratio.and_then(|ratio| {
if ratio.is_finite() && ratio > 0.0 {
#[allow(
clippy::cast_precision_loss,
reason = "the byte count only feeds a ratio budget, and f64 rounding beyond 2^53 bytes cannot change the limit"
)]
let limit = (compressed_len as f64 * ratio).ceil();
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss,
reason = "the finite non-negative ratio is clamped to the usize allocation limit"
)]
Some(if limit >= usize::MAX as f64 {
usize::MAX
} else {
limit.min(usize::MAX as f64) as usize
})
} else {
None
}
});
let output_limit = match (limit.max_decoded_body_size, ratio_limit) {
(Some(size), Some(ratio)) => Some(size.min(ratio)),
(Some(size), None) | (None, Some(size)) => Some(size),
(None, None) => None,
};
let ratio_is_tighter = ratio_limit.is_some_and(|ratio| Some(ratio) == output_limit);
#[allow(
unused_mut,
reason = "the buffer is reassigned only by the feature-gated decode branches, so it stays immutable when no codec is compiled in"
)]
let mut current = bytes::Bytes::copy_from_slice(data);
#[allow(
clippy::never_loop,
reason = "every coding in the list is decoded in order and the body aborts through `?`/`return` instead of an explicit break"
)]
for encoding in encodings.into_iter().rev() {
match encoding {
ContentCoding::Gzip | ContentCoding::Deflate => {
#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
{
let layer_input_len = current.len();
current =
sync_decode_flate2(¤t, encoding, output_limit, ratio_is_tighter)?;
if let Some(max_ratio) = limit.max_decompression_ratio {
if layer_input_len > 0 {
#[allow(
clippy::cast_precision_loss,
reason = "the flate2 layer ratio only compares decoded against compressed bytes, where f64 rounding above 2^53 cannot flip the threshold"
)]
let layer_ratio = current.len() as f64 / layer_input_len as f64;
if layer_ratio > max_ratio {
return Err(Error::DecompressionRatioExceeded);
}
}
}
}
#[cfg(not(any(feature = "compression-gzip", feature = "compression-deflate")))]
{
return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
}
}
ContentCoding::Brotli => {
#[cfg(feature = "compression-brotli")]
{
let layer_input_len = current.len();
current = sync_decode_brotli(¤t, output_limit, ratio_is_tighter)?;
if let Some(max_ratio) = limit.max_decompression_ratio {
if layer_input_len > 0 {
#[allow(
clippy::cast_precision_loss,
reason = "the brotli layer ratio only compares decoded against compressed bytes, where f64 rounding above 2^53 cannot flip the threshold"
)]
let layer_ratio = current.len() as f64 / layer_input_len as f64;
if layer_ratio > max_ratio {
return Err(Error::DecompressionRatioExceeded);
}
}
}
}
#[cfg(not(feature = "compression-brotli"))]
{
return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
}
}
ContentCoding::Zstd => {
#[cfg(feature = "compression-zstd")]
{
let layer_input_len = current.len();
current = sync_decode_zstd(¤t, output_limit, ratio_is_tighter)?;
if let Some(max_ratio) = limit.max_decompression_ratio {
if layer_input_len > 0 {
#[allow(
clippy::cast_precision_loss,
reason = "the zstd layer ratio only compares decoded against compressed bytes, where f64 rounding above 2^53 cannot flip the threshold"
)]
let layer_ratio = current.len() as f64 / layer_input_len as f64;
if layer_ratio > max_ratio {
return Err(Error::DecompressionRatioExceeded);
}
}
}
}
#[cfg(not(feature = "compression-zstd"))]
{
return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
}
}
}
}
let size_violated = limit
.max_decoded_body_size
.is_some_and(|max| current.len() > max);
let ratio_violated = limit.max_decompression_ratio.is_some_and(|max_ratio| {
compressed_len > 0 && {
#[allow(
clippy::cast_precision_loss,
reason = "the final ratio only compares decoded against wire bytes, where f64 rounding above 2^53 cannot flip the threshold"
)]
let ratio = current.len() as f64 / compressed_len as f64;
ratio > max_ratio
}
});
if size_violated && ratio_violated {
if ratio_is_tighter {
return Err(Error::DecompressionRatioExceeded);
}
return Err(Error::DecodedBodyTooLarge);
}
if size_violated {
return Err(Error::DecodedBodyTooLarge);
}
if ratio_violated {
return Err(Error::DecompressionRatioExceeded);
}
Ok(current)
}
#[cfg(any(
feature = "compression-gzip",
feature = "compression-deflate",
feature = "compression-brotli",
feature = "compression-zstd"
))]
fn sync_read_limited<R: std::io::Read>(
mut reader: R,
output_limit: Option<usize>,
ratio_is_tighter: bool,
) -> Result<bytes::Bytes> {
let mut output = Vec::new();
let mut buf = [0u8; 8 * 1024];
loop {
let n = reader
.read(&mut buf)
.map_err(|e| Error::Decompression(e.to_string()))?;
if n == 0 {
break;
}
output.extend_from_slice(&buf[..n]);
if output_limit.is_some_and(|limit| output.len() > limit) {
return Err(if ratio_is_tighter {
Error::DecompressionRatioExceeded
} else {
Error::DecodedBodyTooLarge
});
}
}
Ok(bytes::Bytes::from(output))
}
#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
fn sync_decode_flate2(
data: &bytes::Bytes,
encoding: ContentCoding,
output_limit: Option<usize>,
ratio_is_tighter: bool,
) -> Result<bytes::Bytes> {
match encoding {
ContentCoding::Gzip => {
let decoder = flate2::read::GzDecoder::new(&data[..]);
sync_read_limited(decoder, output_limit, ratio_is_tighter)
}
ContentCoding::Deflate => {
let zlib = flate2::read::ZlibDecoder::new(&data[..]);
if let ok @ Ok(_) = sync_read_limited(zlib, output_limit, ratio_is_tighter) {
ok
} else {
let raw = flate2::read::DeflateDecoder::new(&data[..]);
sync_read_limited(raw, output_limit, ratio_is_tighter)
}
}
_ => Err(Error::UnsupportedContentEncoding(
encoding.as_str().to_owned(),
)),
}
}
#[cfg(feature = "compression-brotli")]
fn sync_decode_brotli(
data: &bytes::Bytes,
output_limit: Option<usize>,
ratio_is_tighter: bool,
) -> Result<bytes::Bytes> {
let decoder = brotli::Decompressor::new(&data[..], 4096);
sync_read_limited(decoder, output_limit, ratio_is_tighter)
}
#[cfg(feature = "compression-zstd")]
fn sync_decode_zstd(
data: &bytes::Bytes,
output_limit: Option<usize>,
ratio_is_tighter: bool,
) -> Result<bytes::Bytes> {
let decoder = zstd::stream::read::Decoder::new(&data[..])
.map_err(|e| Error::Decompression(e.to_string()))?;
sync_read_limited(decoder, output_limit, ratio_is_tighter)
}
#[cfg(feature = "compression-deflate")]
fn is_zlib_header(prefix: &[u8]) -> bool {
let [cmf, flg, ..] = prefix else {
return false;
};
let cmf = u16::from(*cmf);
let flg = u16::from(*flg);
cmf & 0x0F == 8 && (cmf * 256 + flg) % 31 == 0
}
#[cfg(feature = "compression-deflate")]
fn make_deflate_decoder(stream: BoxBytesStream) -> BoxBytesStream {
use async_compression::tokio::bufread::{DeflateDecoder, ZlibDecoder};
use futures_util::StreamExt;
use tokio::io::{AsyncBufReadExt, AsyncRead, BufReader};
use tokio_util::io::{ReaderStream, StreamReader};
enum DeflateState<R> {
Sniffing(R),
Decoding(ReaderStream<std::pin::Pin<Box<dyn AsyncRead + Send>>>),
Done,
}
let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
let reader = StreamReader::new(mapped);
let sniffed = futures_util::stream::unfold(
DeflateState::Sniffing(BufReader::new(reader)),
|mut state| async move {
loop {
match state {
DeflateState::Sniffing(mut reader) => {
let is_zlib = match reader.fill_buf().await {
Ok(buf) => {
if buf.is_empty() {
return None;
}
is_zlib_header(buf)
}
Err(e) => return Some((Err(e), DeflateState::Done)),
};
let decoder: std::pin::Pin<Box<dyn AsyncRead + Send>> = if is_zlib {
Box::pin(ZlibDecoder::new(reader))
} else {
Box::pin(DeflateDecoder::new(reader))
};
state = DeflateState::Decoding(ReaderStream::new(decoder));
}
DeflateState::Decoding(mut inner) => {
return inner
.next()
.await
.map(|item| (item, DeflateState::Decoding(inner)));
}
DeflateState::Done => return None,
}
}
},
);
let stream = sniffed.map(|r| r.map_err(|e| Error::Decompression(e.to_string())));
Box::pin(stream)
}
#[allow(
clippy::unnecessary_wraps,
clippy::needless_pass_by_value,
unused_variables,
reason = "the coding arms are selected at compile time, so an uncompiled codec returns an unsupported-encoding error and ignores its input; the Result and the by-value stream only look redundant when every codec is enabled"
)]
fn make_decoder(stream: BoxBytesStream, encoding: ContentCoding) -> Result<BoxBytesStream> {
match encoding {
ContentCoding::Gzip => {
#[cfg(feature = "compression-gzip")]
{
use async_compression::tokio::bufread::GzipDecoder;
use futures_util::StreamExt;
use tokio::io::BufReader;
use tokio_util::io::{ReaderStream, StreamReader};
let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
let reader = StreamReader::new(mapped);
let decoder = GzipDecoder::new(BufReader::new(reader));
let stream = ReaderStream::new(decoder);
Ok(Box::pin(stream.map(|r| {
r.map_err(|e| Error::Decompression(e.to_string()))
})))
}
#[cfg(not(feature = "compression-gzip"))]
{
Err(Error::UnsupportedContentEncoding("gzip".to_string()))
}
}
ContentCoding::Deflate => {
#[cfg(feature = "compression-deflate")]
{
Ok(make_deflate_decoder(stream))
}
#[cfg(not(feature = "compression-deflate"))]
{
Err(Error::UnsupportedContentEncoding("deflate".to_string()))
}
}
ContentCoding::Brotli => {
#[cfg(feature = "compression-brotli")]
{
use async_compression::tokio::bufread::BrotliDecoder;
use futures_util::StreamExt;
use tokio::io::BufReader;
use tokio_util::io::{ReaderStream, StreamReader};
let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
let reader = StreamReader::new(mapped);
let decoder = BrotliDecoder::new(BufReader::new(reader));
let stream = ReaderStream::new(decoder);
Ok(Box::pin(stream.map(|r| {
r.map_err(|e| Error::Decompression(e.to_string()))
})))
}
#[cfg(not(feature = "compression-brotli"))]
{
Err(Error::UnsupportedContentEncoding("br".to_string()))
}
}
ContentCoding::Zstd => {
#[cfg(feature = "compression-zstd")]
{
use async_compression::tokio::bufread::ZstdDecoder;
use futures_util::StreamExt;
use tokio::io::BufReader;
use tokio_util::io::{ReaderStream, StreamReader};
let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
let reader = StreamReader::new(mapped);
let decoder = ZstdDecoder::new(BufReader::new(reader));
let stream = ReaderStream::new(decoder);
Ok(Box::pin(stream.map(|r| {
r.map_err(|e| Error::Decompression(e.to_string()))
})))
}
#[cfg(not(feature = "compression-zstd"))]
{
Err(Error::UnsupportedContentEncoding("zstd".to_string()))
}
}
}
}
struct CountingStream {
inner: BoxBytesStream,
count: Arc<AtomicUsize>,
}
impl CountingStream {
fn new(inner: BoxBytesStream) -> Self {
Self {
inner,
count: Arc::new(AtomicUsize::new(0)),
}
}
fn counter(&self) -> Arc<AtomicUsize> {
Arc::clone(&self.count)
}
}
fn saturating_fetch_add(counter: &AtomicUsize, amount: usize) {
let mut current = counter.load(Ordering::Acquire);
loop {
let Some(next) = current.checked_add(amount) else {
counter.store(usize::MAX, Ordering::Release);
return;
};
match counter.compare_exchange_weak(current, next, Ordering::AcqRel, Ordering::Acquire) {
Ok(_) => return,
Err(actual) => current = actual,
}
}
}
impl futures_core::Stream for CountingStream {
type Item = <BoxBytesStream as futures_core::Stream>::Item;
fn poll_next(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
use std::pin::Pin;
match Pin::new(&mut self.inner).poll_next(cx) {
std::task::Poll::Ready(Some(Ok(chunk))) => {
saturating_fetch_add(&self.count, chunk.len());
std::task::Poll::Ready(Some(Ok(chunk)))
}
other => other,
}
}
}
struct LimitingStream {
inner: BoxBytesStream,
limit: DecompressionLimit,
decoded_bytes: usize,
compressed_counter: Arc<AtomicUsize>,
limit_exceeded: Option<LimitExceededKind>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LimitExceededKind {
Size,
Ratio,
}
impl LimitingStream {
fn new(
inner: BoxBytesStream,
limit: DecompressionLimit,
compressed_counter: Arc<AtomicUsize>,
) -> Self {
Self {
inner,
limit,
decoded_bytes: 0,
compressed_counter,
limit_exceeded: None,
}
}
#[allow(
clippy::cast_precision_loss,
reason = "the byte counters are only divided to compare against a ratio threshold, where f64 rounding above 2^53 cannot flip the verdict"
)]
fn check_limit(&self) -> std::result::Result<(), LimitExceededKind> {
if let Some(max) = self.limit.max_decoded_body_size {
if self.decoded_bytes > max {
return Err(LimitExceededKind::Size);
}
}
if let Some(max_ratio) = self.limit.max_decompression_ratio {
if !max_ratio.is_finite() || max_ratio <= 0.0 {
return Err(LimitExceededKind::Ratio);
}
let compressed = self.compressed_counter.load(Ordering::Acquire);
if compressed == 0 {
if self.decoded_bytes > 0 {
return Err(LimitExceededKind::Ratio);
}
} else {
let ratio = self.decoded_bytes as f64 / compressed as f64;
if ratio > max_ratio {
return Err(LimitExceededKind::Ratio);
}
}
}
Ok(())
}
fn kind_to_error(kind: LimitExceededKind, limit: &DecompressionLimit) -> Error {
match kind {
LimitExceededKind::Size => Error::DecodedBodyTooLarge,
LimitExceededKind::Ratio => {
if limit
.max_decompression_ratio
.is_some_and(|r| !r.is_finite() || r <= 0.0)
{
Error::Decompression(
"invalid max_decompression_ratio: must be finite and positive".into(),
)
} else {
Error::DecompressionRatioExceeded
}
}
}
}
}
impl futures_core::Stream for LimitingStream {
type Item = Result<bytes::Bytes>;
fn poll_next(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
use std::pin::Pin;
if self.limit_exceeded.is_some() {
return std::task::Poll::Ready(None);
}
match Pin::new(&mut self.inner).poll_next(cx) {
std::task::Poll::Ready(Some(Ok(chunk))) => {
self.decoded_bytes = self.decoded_bytes.saturating_add(chunk.len());
if let Err(kind) = self.check_limit() {
self.limit_exceeded = Some(kind);
let err = Self::kind_to_error(kind, &self.limit);
return std::task::Poll::Ready(Some(Err(err)));
}
std::task::Poll::Ready(Some(Ok(chunk)))
}
std::task::Poll::Ready(Some(Err(e))) => std::task::Poll::Ready(Some(Err(e))),
std::task::Poll::Ready(None) => std::task::Poll::Ready(None),
std::task::Poll::Pending => std::task::Poll::Pending,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use futures_util::FutureExt;
#[cfg(feature = "compression-gzip")]
use futures_util::StreamExt;
#[test]
fn content_coding_from_wire() {
assert_eq!(ContentCoding::from_wire("gzip"), Some(ContentCoding::Gzip));
assert_eq!(
ContentCoding::from_wire("x-gzip"),
Some(ContentCoding::Gzip)
);
assert_eq!(
ContentCoding::from_wire("deflate"),
Some(ContentCoding::Deflate)
);
assert_eq!(ContentCoding::from_wire("br"), Some(ContentCoding::Brotli));
assert_eq!(ContentCoding::from_wire("zstd"), Some(ContentCoding::Zstd));
assert_eq!(ContentCoding::from_wire("identity"), None);
assert_eq!(ContentCoding::from_wire("unknown"), None);
assert_eq!(ContentCoding::from_wire("GZIP"), Some(ContentCoding::Gzip));
assert_eq!(
ContentCoding::from_wire(" Br "),
Some(ContentCoding::Brotli)
);
}
#[test]
fn content_coding_as_str() {
assert_eq!(ContentCoding::Gzip.as_str(), "gzip");
assert_eq!(ContentCoding::Deflate.as_str(), "deflate");
assert_eq!(ContentCoding::Brotli.as_str(), "br");
assert_eq!(ContentCoding::Zstd.as_str(), "zstd");
}
#[test]
fn parse_content_encodings_single() {
let encs = parse_content_encodings("gzip").unwrap();
assert_eq!(encs, vec![ContentCoding::Gzip]);
}
#[test]
fn parse_content_encodings_multiple() {
let encs = parse_content_encodings("gzip, br").unwrap();
assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
}
#[test]
fn parse_content_encodings_with_whitespace() {
let encs = parse_content_encodings(" gzip , br ").unwrap();
assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
}
#[test]
fn parse_content_encodings_empty() {
assert!(parse_content_encodings("").is_none());
assert!(parse_content_encodings(" ").is_none());
}
#[test]
fn parse_content_encodings_unknown_filtered() {
let encs = parse_content_encodings("gzip, identity, br").unwrap();
assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
}
#[test]
fn validate_content_encodings_supported() {
assert!(validate_content_encodings("gzip").is_ok());
assert!(validate_content_encodings("gzip, br").is_ok());
assert!(validate_content_encodings("").is_ok());
}
#[test]
fn validate_content_encodings_accepts_identity() {
assert!(validate_content_encodings("identity").is_ok());
assert!(validate_content_encodings("IDENTITY").is_ok());
assert!(validate_content_encodings("identity, gzip").is_ok());
assert!(validate_content_encodings("gzip, identity").is_ok());
}
#[test]
fn parse_content_encodings_identity_only_is_none() {
assert!(parse_content_encodings("identity").is_none());
let encs = parse_content_encodings("gzip, identity, br").unwrap();
assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
}
#[test]
fn validate_content_encodings_unsupported() {
let err = validate_content_encodings("gzip, weird").unwrap_err();
assert_eq!(err.kind(), "unsupported_content_encoding");
assert!(err.to_string().contains("weird"));
}
#[test]
fn accept_encoding_value_requires_compiled_features() {
let _ = accept_encoding_value();
}
#[cfg(not(any(
feature = "compression-gzip",
feature = "compression-deflate",
feature = "compression-brotli",
feature = "compression-zstd"
)))]
#[test]
fn accept_encoding_value_none_without_compression_features() {
assert!(
accept_encoding_value().is_none(),
"accept_encoding_value() should return None when no compression features are compiled"
);
}
#[cfg(all(
feature = "compression-gzip",
not(feature = "compression-brotli"),
not(feature = "compression-zstd")
))]
#[test]
fn accept_encoding_value_gzip_only() {
let val = accept_encoding_value().unwrap();
assert!(val.contains("gzip"), "should contain gzip, got: {val}");
assert!(
!val.contains("br"),
"should not contain br without brotli feature, got: {val}"
);
assert!(
!val.contains("zstd"),
"should not contain zstd without zstd feature, got: {val}"
);
}
#[test]
fn accept_encoding_value_deterministic_order() {
let val1 = accept_encoding_value();
let val2 = accept_encoding_value();
assert_eq!(
val1, val2,
"accept_encoding_value() should return the same value across calls"
);
}
#[test]
fn accept_encoding_advertises_only_supported_decoders() {
let Some(value) = accept_encoding_value() else {
return;
};
for token in value.split(", ") {
let coding =
ContentCoding::from_wire(token).unwrap_or_else(|| panic!("invalid token {token}"));
let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
let result = make_decoder(stream, coding);
assert!(
result.is_ok(),
"advertised encoding '{token}' has no compiled-in decoder"
);
}
}
#[test]
fn decompress_stream_no_encoding_returns_original() {
let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
let result = decompress_stream(stream, None, true, DecompressionLimit::new()).unwrap();
drop(result);
}
#[test]
fn decompress_stream_disabled_returns_original() {
let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
let result =
decompress_stream(stream, Some("gzip"), false, DecompressionLimit::new()).unwrap();
drop(result);
}
#[test]
fn decompress_stream_empty_header_returns_original() {
let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
let result =
decompress_stream(stream, Some(" "), true, DecompressionLimit::new()).unwrap();
drop(result);
}
#[test]
fn decompress_buffered_identity_passthrough() {
assert_eq!(
&decompress_buffered(b"plain body", "identity", DecompressionLimit::new()).unwrap()[..],
b"plain body"
);
let err = decompress_buffered(b"x", "weird", DecompressionLimit::new()).unwrap_err();
assert_eq!(err.kind(), "unsupported_content_encoding");
}
#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
#[test]
fn sync_decode_flate2_rejects_non_flate_encoding() {
let err = sync_decode_flate2(&bytes::Bytes::new(), ContentCoding::Brotli, None, false)
.unwrap_err();
assert_eq!(err.kind(), "unsupported_content_encoding");
assert!(err.to_string().contains("br"));
}
#[test]
fn decompression_limit_new_is_unlimited() {
let limit = DecompressionLimit::new();
assert!(limit.is_unlimited());
assert!(limit.max_decoded_body_size.is_none());
assert!(limit.max_decompression_ratio.is_none());
}
#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
#[test]
fn decompression_limit_max_size_rejects() {
let data = gzip_compress(b"hello world");
let limit = DecompressionLimit {
max_decoded_body_size: Some(5),
max_decompression_ratio: None,
};
let err = decompress_buffered(&data, "gzip", limit).unwrap_err();
assert_eq!(err.kind(), "decoded_body_too_large");
}
#[cfg(feature = "compression-deflate")]
#[test]
fn deflate_buffered_decodes_zlib_wrapper_and_raw() {
use std::io::Write;
let plain = b"hello deflate world, hello again";
let mut zlib = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
zlib.write_all(plain).unwrap();
let zlib = zlib.finish().unwrap();
assert!(is_zlib_header(&zlib));
let result = decompress_buffered(&zlib, "deflate", DecompressionLimit::new()).unwrap();
assert_eq!(&result[..], &plain[..]);
let mut raw =
flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default());
raw.write_all(plain).unwrap();
let raw = raw.finish().unwrap();
let result = decompress_buffered(&raw, "deflate", DecompressionLimit::new()).unwrap();
assert_eq!(&result[..], &plain[..]);
}
#[cfg(feature = "compression-deflate")]
#[test]
fn zlib_header_sniff_classifies_framing() {
assert!(!is_zlib_header(&[]));
assert!(!is_zlib_header(&[0x78]));
assert!(!is_zlib_header(&[0x1f, 0x8b])); assert!(is_zlib_header(&[0x78, 0x9c, 0x03])); }
#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
#[test]
fn decompression_limit_max_size_allows_within() {
let data = gzip_compress(b"hi");
let limit = DecompressionLimit {
max_decoded_body_size: Some(1024),
max_decompression_ratio: None,
};
let result = decompress_buffered(&data, "gzip", limit).unwrap();
assert_eq!(&result[..], b"hi");
}
#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
#[test]
fn decompression_limit_ratio_rejects() {
let data = gzip_compress(b"hello world, this is a test of compression");
let limit = DecompressionLimit {
max_decoded_body_size: None,
max_decompression_ratio: Some(0.5),
};
let err = decompress_buffered(&data, "gzip", limit).unwrap_err();
assert_eq!(err.kind(), "decompression_ratio_exceeded");
}
#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
#[test]
fn decompression_limit_ratio_allows_within() {
let data = gzip_compress(b"hello world, this is a test of compression");
let limit = DecompressionLimit {
max_decoded_body_size: None,
max_decompression_ratio: Some(100.0),
};
let result = decompress_buffered(&data, "gzip", limit).unwrap();
assert_eq!(&result[..], b"hello world, this is a test of compression");
}
#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
#[test]
fn decompression_limit_unlimited_allows() {
let data = gzip_compress(b"hello world");
let limit = DecompressionLimit::new();
let result = decompress_buffered(&data, "gzip", limit).unwrap();
assert_eq!(&result[..], b"hello world");
}
#[cfg(feature = "compression-gzip")]
#[test]
fn decompression_limit_ratio_enforced_per_layer() {
let payload = vec![b'a'; 5000];
let inner = gzip_compress(&payload);
let outer = gzip_compress(&inner);
#[allow(
clippy::cast_precision_loss,
reason = "the test derives the per-layer ratios from small in-memory buffers, far below the f64 exact-integer range"
)]
let (total_ratio, inner_step) = {
(
payload.len() as f64 / outer.len() as f64,
payload.len() as f64 / inner.len() as f64,
)
};
assert!(
inner_step > total_ratio + 1.0,
"expected the inner step to dominate: {inner_step} vs {total_ratio}"
);
let limit_value = total_ratio.midpoint(inner_step).ceil();
let limit = DecompressionLimit {
max_decoded_body_size: None,
max_decompression_ratio: Some(limit_value),
};
let err = decompress_buffered(&outer, "gzip, gzip", limit).unwrap_err();
assert_eq!(err.kind(), "decompression_ratio_exceeded");
let ok = decompress_buffered(&outer, "gzip, gzip", DecompressionLimit::default()).unwrap();
assert_eq!(&ok[..], &payload[..]);
}
#[cfg(feature = "compression-gzip")]
#[tokio::test]
async fn decompression_stream_enforces_ratio_per_layer() {
let payload = vec![b'a'; 5000];
let inner = gzip_compress(&payload);
let outer = gzip_compress(&inner);
#[allow(
clippy::cast_precision_loss,
reason = "the test derives the per-layer ratios from small in-memory buffers, far below the f64 exact-integer range"
)]
let limit_value = (payload.len() as f64 / outer.len() as f64)
.midpoint(payload.len() as f64 / inner.len() as f64);
let limit = DecompressionLimit {
max_decoded_body_size: None,
max_decompression_ratio: Some(limit_value.ceil()),
};
let input: BoxBytesStream = Box::pin(futures_util::stream::once(async move {
Ok(bytes::Bytes::from(outer))
}));
let decoded = decompress_stream(input, Some("gzip, gzip"), true, limit).unwrap();
let err = decoded
.collect::<Vec<_>>()
.await
.into_iter()
.find_map(std::result::Result::err)
.expect("streaming ratio limit must reject the inner layer");
assert_eq!(err.kind(), "decompression_ratio_exceeded");
}
#[cfg(feature = "compression-gzip")]
#[tokio::test]
async fn decompression_stream_enforces_ratio_against_wire_size() {
let payload: Vec<u8> = (0usize..5000)
.map(|i| u8::try_from((i * 73 + i / 17) % 251).expect("value is below 251"))
.collect();
let inner = gzip_compress(&payload);
let outer = gzip_compress(&inner);
#[allow(
clippy::cast_precision_loss,
reason = "the test derives the per-layer ratios from small in-memory buffers, far below the f64 exact-integer range"
)]
let (first_layer, second_layer, total) = (
inner.len() as f64 / outer.len() as f64,
payload.len() as f64 / inner.len() as f64,
payload.len() as f64 / outer.len() as f64,
);
let per_layer_limit = first_layer.max(second_layer);
assert!(total > per_layer_limit);
let limit = DecompressionLimit {
max_decoded_body_size: None,
max_decompression_ratio: Some(per_layer_limit.midpoint(total)),
};
let input: BoxBytesStream = Box::pin(futures_util::stream::once(async move {
Ok(bytes::Bytes::from(outer))
}));
let decoded = decompress_stream(input, Some("gzip, gzip"), true, limit).unwrap();
let err = decoded
.collect::<Vec<_>>()
.await
.into_iter()
.find_map(std::result::Result::err)
.expect("streaming ratio limit must include the wire size");
assert_eq!(err.kind(), "decompression_ratio_exceeded");
}
#[test]
fn limiting_stream_enforces_max_decoded_body_size() {
let limit = DecompressionLimit {
max_decoded_body_size: Some(3),
max_decompression_ratio: None,
};
let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![
Ok(bytes::Bytes::from("ab")),
Ok(bytes::Bytes::from("cd")),
]));
let counter = Arc::new(AtomicUsize::new(0));
let mut limited = LimitingStream::new(stream, limit, counter);
let chunk1 = futures_util::StreamExt::next(&mut limited)
.now_or_never()
.unwrap()
.unwrap()
.unwrap();
assert_eq!(chunk1, "ab");
let err = futures_util::StreamExt::next(&mut limited)
.now_or_never()
.unwrap()
.unwrap()
.unwrap_err();
assert_eq!(err.kind(), "decoded_body_too_large");
}
#[test]
fn limiting_stream_allows_within_limit() {
let limit = DecompressionLimit {
max_decoded_body_size: Some(1024),
max_decompression_ratio: None,
};
let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![
Ok(bytes::Bytes::from("a")),
Ok(bytes::Bytes::from("b")),
]));
let counter = Arc::new(AtomicUsize::new(0));
let mut limited = LimitingStream::new(stream, limit, counter);
let chunk1 = futures_util::StreamExt::next(&mut limited)
.now_or_never()
.unwrap()
.unwrap()
.unwrap();
assert_eq!(chunk1, "a");
let chunk2 = futures_util::StreamExt::next(&mut limited)
.now_or_never()
.unwrap()
.unwrap()
.unwrap();
assert_eq!(chunk2, "b");
}
#[test]
fn saturating_fetch_add_saturates_instead_of_wrapping() {
let counter = AtomicUsize::new(usize::MAX - 2);
saturating_fetch_add(&counter, 1);
assert_eq!(counter.load(Ordering::Relaxed), usize::MAX - 1);
saturating_fetch_add(&counter, 5);
assert_eq!(counter.load(Ordering::Relaxed), usize::MAX);
saturating_fetch_add(&counter, 5);
assert_eq!(counter.load(Ordering::Relaxed), usize::MAX);
}
#[test]
fn limiting_stream_saturated_counter_still_enforces_size_limit() {
let limit = DecompressionLimit {
max_decoded_body_size: Some(10),
max_decompression_ratio: None,
};
let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![Ok(
bytes::Bytes::from("x"),
)]));
let counter = Arc::new(AtomicUsize::new(0));
let mut limited = LimitingStream::new(stream, limit, counter);
limited.decoded_bytes = usize::MAX;
let err = futures_util::StreamExt::next(&mut limited)
.now_or_never()
.unwrap()
.unwrap()
.unwrap_err();
assert_eq!(err.kind(), "decoded_body_too_large");
}
#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
fn gzip_compress(data: &[u8]) -> Vec<u8> {
use flate2::write::GzEncoder;
use flate2::Compression;
use std::io::Write;
let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
encoder.write_all(data).unwrap();
encoder.finish().unwrap()
}
}