1use 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#[derive(Debug, Clone, Copy, Default)]
31pub struct DecompressionLimit {
32 pub(crate) max_decoded_body_size: Option<usize>,
34 pub(crate) max_decompression_ratio: Option<f64>,
37}
38
39impl DecompressionLimit {
40 #[must_use]
42 pub const fn new() -> Self {
43 Self {
44 max_decoded_body_size: None,
45 max_decompression_ratio: None,
46 }
47 }
48
49 #[must_use]
51 pub fn is_unlimited(&self) -> bool {
52 self.max_decoded_body_size.is_none() && self.max_decompression_ratio.is_none()
53 }
54
55 #[must_use]
57 pub fn max_decoded_body_size(&self) -> Option<usize> {
58 self.max_decoded_body_size
59 }
60
61 #[must_use]
63 pub fn max_decompression_ratio(&self) -> Option<f64> {
64 self.max_decompression_ratio
65 }
66
67 pub fn validate(&self) -> Result<()> {
75 if let Some(ratio) = self.max_decompression_ratio {
76 if !ratio.is_finite() || ratio <= 0.0 {
77 return Err(Error::Decompression(
78 "invalid max_decompression_ratio: must be finite and positive".into(),
79 ));
80 }
81 }
82 Ok(())
83 }
84
85 pub fn try_new(
92 max_decoded_body_size: Option<usize>,
93 max_decompression_ratio: Option<f64>,
94 ) -> crate::error::Result<Self> {
95 if let Some(ratio) = max_decompression_ratio {
96 if !ratio.is_finite() || ratio <= 0.0 {
97 return Err(crate::error::Error::RequestBuild(
98 "max_decompression_ratio must be finite and positive".into(),
99 ));
100 }
101 }
102 Ok(Self {
103 max_decoded_body_size,
104 max_decompression_ratio,
105 })
106 }
107}
108
109const MAX_NESTING_DEPTH: usize = 4;
113
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116pub enum ContentCoding {
117 Gzip,
119 Deflate,
121 Brotli,
123 Zstd,
125}
126
127impl ContentCoding {
128 #[must_use]
130 pub fn as_str(self) -> &'static str {
131 match self {
132 Self::Gzip => "gzip",
133 Self::Deflate => "deflate",
134 Self::Brotli => "br",
135 Self::Zstd => "zstd",
136 }
137 }
138
139 fn from_wire(s: &str) -> Option<Self> {
141 match s.trim().to_ascii_lowercase().as_str() {
142 "gzip" | "x-gzip" => Some(Self::Gzip),
143 "deflate" => Some(Self::Deflate),
144 "br" => Some(Self::Brotli),
145 "zstd" => Some(Self::Zstd),
146 _ => None,
147 }
148 }
149
150 fn is_identity(token: &str) -> bool {
153 token.trim().eq_ignore_ascii_case("identity")
154 }
155}
156
157#[must_use]
160pub fn accept_encoding_value() -> Option<&'static str> {
161 static VALUE: OnceLock<Option<Box<str>>> = OnceLock::new();
162
163 VALUE
164 .get_or_init(|| {
165 #[allow(clippy::vec_init_then_push)]
173 {
174 #[allow(unused_mut)]
175 let mut parts: Vec<&str> = Vec::new();
176
177 #[cfg(feature = "compression-gzip")]
178 parts.push("gzip");
179 #[cfg(feature = "compression-deflate")]
180 parts.push("deflate");
181 #[cfg(feature = "compression-brotli")]
182 parts.push("br");
183 #[cfg(feature = "compression-zstd")]
184 parts.push("zstd");
185
186 (!parts.is_empty()).then(|| parts.join(", ").into_boxed_str())
187 }
188 })
189 .as_deref()
190}
191
192pub(crate) fn parse_content_encodings(header: &str) -> Option<Vec<ContentCoding>> {
201 let encodings: Vec<ContentCoding> = header
202 .split(',')
203 .map(str::trim)
204 .filter(|s| !s.is_empty() && !ContentCoding::is_identity(s))
205 .filter_map(ContentCoding::from_wire)
206 .collect();
207
208 if encodings.is_empty() {
209 None
210 } else {
211 Some(encodings)
212 }
213}
214
215pub fn validate_content_encodings(header_value: &str) -> Result<()> {
228 for token in header_value.split(',') {
229 let token = token.trim();
230 if token.is_empty() || ContentCoding::is_identity(token) {
231 continue;
232 }
233 if ContentCoding::from_wire(token).is_none() {
234 return Err(Error::UnsupportedContentEncoding(token.to_string()));
235 }
236 }
237 Ok(())
238}
239
240pub fn decompress_stream(
260 stream: BoxBytesStream,
261 content_encoding: Option<&str>,
262 decompression_enabled: bool,
263 limit: DecompressionLimit,
264) -> Result<BoxBytesStream> {
265 if !decompression_enabled {
266 return Ok(stream);
267 }
268
269 let header = match content_encoding {
270 Some(h) if !h.trim().is_empty() => h,
271 _ => return Ok(stream),
272 };
273
274 limit.validate()?;
278
279 validate_content_encodings(header)?;
283
284 let Some(encodings) = parse_content_encodings(header) else {
285 return Ok(stream);
286 };
287
288 if encodings.len() > MAX_NESTING_DEPTH {
289 return Err(Error::Decompression(format!(
290 "content encoding nesting depth {} exceeds maximum {}",
291 encodings.len(),
292 MAX_NESTING_DEPTH
293 )));
294 }
295
296 let mut current = stream;
311 let mut outer_counter = None;
312 for encoding in encodings.into_iter().rev() {
313 let counting = CountingStream::new(current);
314 let counter = counting.counter();
315 current = make_decoder(Box::pin(counting), encoding)?;
316 if outer_counter.is_none() {
319 outer_counter = Some(Arc::clone(&counter));
320 }
321
322 if let Some(max_ratio) = limit.max_decompression_ratio {
326 current = Box::pin(LimitingStream::new(
327 current,
328 DecompressionLimit {
329 max_decoded_body_size: None,
330 max_decompression_ratio: Some(max_ratio),
331 },
332 counter,
333 ));
334 }
335 }
336
337 if limit.is_unlimited() {
338 return Ok(current);
339 }
340
341 let Some(outer_counter) = outer_counter else {
342 return Ok(current);
343 };
344 Ok(Box::pin(LimitingStream::new(current, limit, outer_counter)))
345}
346
347#[allow(clippy::too_many_lines)]
360pub fn decompress_buffered(
361 data: &[u8],
362 content_encoding: &str,
363 limit: DecompressionLimit,
364) -> Result<bytes::Bytes> {
365 limit.validate()?;
369 validate_content_encodings(content_encoding)?;
371
372 let encodings = parse_content_encodings(content_encoding);
373 let Some(encodings) = encodings else {
374 return Ok(bytes::Bytes::copy_from_slice(data));
378 };
379
380 if encodings.len() > MAX_NESTING_DEPTH {
381 return Err(Error::Decompression(format!(
382 "content encoding nesting depth {} exceeds maximum {}",
383 encodings.len(),
384 MAX_NESTING_DEPTH
385 )));
386 }
387
388 if data.is_empty() {
391 return Ok(bytes::Bytes::new());
392 }
393
394 let compressed_len = data.len();
395 let ratio_limit = limit.max_decompression_ratio.and_then(|ratio| {
396 if ratio.is_finite() && ratio > 0.0 {
397 #[allow(clippy::cast_precision_loss)]
398 let limit = (compressed_len as f64 * ratio).ceil();
399 #[allow(
400 clippy::cast_possible_truncation,
401 clippy::cast_sign_loss,
402 clippy::cast_precision_loss,
403 reason = "the finite non-negative ratio is clamped to the usize allocation limit"
404 )]
405 Some(limit.min(usize::MAX as f64) as usize)
406 } else {
407 None
408 }
409 });
410 let output_limit = match (limit.max_decoded_body_size, ratio_limit) {
411 (Some(size), Some(ratio)) => Some(size.min(ratio)),
412 (Some(size), None) | (None, Some(size)) => Some(size),
413 (None, None) => None,
414 };
415 let ratio_is_tighter = ratio_limit.is_some_and(|ratio| Some(ratio) == output_limit);
416
417 #[allow(unused_mut)]
418 let mut current = bytes::Bytes::copy_from_slice(data);
419 #[allow(clippy::never_loop)]
420 for encoding in encodings.into_iter().rev() {
421 match encoding {
422 ContentCoding::Gzip | ContentCoding::Deflate => {
423 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
424 {
425 let layer_input_len = current.len();
426 current =
427 sync_decode_flate2(¤t, encoding, output_limit, ratio_is_tighter)?;
428 if let Some(max_ratio) = limit.max_decompression_ratio {
429 if layer_input_len > 0 {
430 #[allow(clippy::cast_precision_loss)]
431 let layer_ratio = current.len() as f64 / layer_input_len as f64;
432 if layer_ratio > max_ratio {
433 return Err(Error::DecompressionRatioExceeded);
434 }
435 }
436 }
437 }
438 #[cfg(not(any(feature = "compression-gzip", feature = "compression-deflate")))]
439 {
440 return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
441 }
442 }
443 ContentCoding::Brotli => {
444 #[cfg(feature = "compression-brotli")]
445 {
446 let layer_input_len = current.len();
447 current = sync_decode_brotli(¤t, output_limit, ratio_is_tighter)?;
448 if let Some(max_ratio) = limit.max_decompression_ratio {
449 if layer_input_len > 0 {
450 #[allow(clippy::cast_precision_loss)]
451 let layer_ratio = current.len() as f64 / layer_input_len as f64;
452 if layer_ratio > max_ratio {
453 return Err(Error::DecompressionRatioExceeded);
454 }
455 }
456 }
457 }
458 #[cfg(not(feature = "compression-brotli"))]
459 {
460 return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
461 }
462 }
463 ContentCoding::Zstd => {
464 #[cfg(feature = "compression-zstd")]
465 {
466 let layer_input_len = current.len();
467 current = sync_decode_zstd(¤t, output_limit, ratio_is_tighter)?;
468 if let Some(max_ratio) = limit.max_decompression_ratio {
469 if layer_input_len > 0 {
470 #[allow(clippy::cast_precision_loss)]
471 let layer_ratio = current.len() as f64 / layer_input_len as f64;
472 if layer_ratio > max_ratio {
473 return Err(Error::DecompressionRatioExceeded);
474 }
475 }
476 }
477 }
478 #[cfg(not(feature = "compression-zstd"))]
479 {
480 return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
481 }
482 }
483 }
484 }
485
486 let size_violated = limit
493 .max_decoded_body_size
494 .is_some_and(|max| current.len() > max);
495 let ratio_violated = limit.max_decompression_ratio.is_some_and(|max_ratio| {
496 compressed_len > 0 && {
497 #[allow(clippy::cast_precision_loss)]
498 let ratio = current.len() as f64 / compressed_len as f64;
499 ratio > max_ratio
500 }
501 });
502 if size_violated && ratio_violated {
503 if ratio_is_tighter {
504 return Err(Error::DecompressionRatioExceeded);
505 }
506 return Err(Error::DecodedBodyTooLarge);
507 }
508 if size_violated {
509 return Err(Error::DecodedBodyTooLarge);
510 }
511 if ratio_violated {
512 return Err(Error::DecompressionRatioExceeded);
513 }
514
515 Ok(current)
516}
517
518#[cfg(any(
525 feature = "compression-gzip",
526 feature = "compression-deflate",
527 feature = "compression-brotli",
528 feature = "compression-zstd"
529))]
530fn sync_read_limited<R: std::io::Read>(
531 mut reader: R,
532 output_limit: Option<usize>,
533 ratio_is_tighter: bool,
534) -> Result<bytes::Bytes> {
535 let mut output = Vec::new();
536 let mut buf = [0u8; 8 * 1024];
537 loop {
538 let n = reader
539 .read(&mut buf)
540 .map_err(|e| Error::Decompression(e.to_string()))?;
541 if n == 0 {
542 break;
543 }
544 output.extend_from_slice(&buf[..n]);
545 if output_limit.is_some_and(|limit| output.len() > limit) {
546 return Err(if ratio_is_tighter {
547 Error::DecompressionRatioExceeded
548 } else {
549 Error::DecodedBodyTooLarge
550 });
551 }
552 }
553 Ok(bytes::Bytes::from(output))
554}
555
556#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
558fn sync_decode_flate2(
559 data: &bytes::Bytes,
560 encoding: ContentCoding,
561 output_limit: Option<usize>,
562 ratio_is_tighter: bool,
563) -> Result<bytes::Bytes> {
564 match encoding {
565 ContentCoding::Gzip => {
566 let decoder = flate2::read::GzDecoder::new(&data[..]);
567 sync_read_limited(decoder, output_limit, ratio_is_tighter)
568 }
569 ContentCoding::Deflate => {
570 let decoder = flate2::read::DeflateDecoder::new(&data[..]);
571 sync_read_limited(decoder, output_limit, ratio_is_tighter)
572 }
573 _ => Err(Error::UnsupportedContentEncoding(
574 encoding.as_str().to_owned(),
575 )),
576 }
577}
578
579#[cfg(feature = "compression-brotli")]
580fn sync_decode_brotli(
581 data: &bytes::Bytes,
582 output_limit: Option<usize>,
583 ratio_is_tighter: bool,
584) -> Result<bytes::Bytes> {
585 let decoder = brotli::Decompressor::new(&data[..], 4096);
586 sync_read_limited(decoder, output_limit, ratio_is_tighter)
587}
588
589#[cfg(feature = "compression-zstd")]
590fn sync_decode_zstd(
591 data: &bytes::Bytes,
592 output_limit: Option<usize>,
593 ratio_is_tighter: bool,
594) -> Result<bytes::Bytes> {
595 let decoder = zstd::stream::read::Decoder::new(&data[..])
596 .map_err(|e| Error::Decompression(e.to_string()))?;
597 sync_read_limited(decoder, output_limit, ratio_is_tighter)
598}
599
600#[allow(
602 clippy::unnecessary_wraps,
603 clippy::needless_pass_by_value,
604 unused_variables
605)]
606fn make_decoder(stream: BoxBytesStream, encoding: ContentCoding) -> Result<BoxBytesStream> {
607 match encoding {
608 ContentCoding::Gzip => {
609 #[cfg(feature = "compression-gzip")]
610 {
611 use async_compression::tokio::bufread::GzipDecoder;
612 use futures_util::StreamExt;
613 use tokio::io::BufReader;
614 use tokio_util::io::ReaderStream;
615
616 let reader = StreamReader::new(stream);
617 let decoder = GzipDecoder::new(BufReader::new(reader));
618 let stream = ReaderStream::new(decoder);
619 Ok(Box::pin(stream.map(|r| {
620 r.map_err(|e| Error::Decompression(e.to_string()))
621 })))
622 }
623 #[cfg(not(feature = "compression-gzip"))]
624 {
625 Err(Error::UnsupportedContentEncoding("gzip".to_string()))
626 }
627 }
628 ContentCoding::Deflate => {
629 #[cfg(feature = "compression-deflate")]
630 {
631 use async_compression::tokio::bufread::DeflateDecoder;
632 use futures_util::StreamExt;
633 use tokio::io::BufReader;
634 use tokio_util::io::ReaderStream;
635
636 let reader = StreamReader::new(stream);
637 let decoder = DeflateDecoder::new(BufReader::new(reader));
638 let stream = ReaderStream::new(decoder);
639 Ok(Box::pin(stream.map(|r| {
640 r.map_err(|e| Error::Decompression(e.to_string()))
641 })))
642 }
643 #[cfg(not(feature = "compression-deflate"))]
644 {
645 Err(Error::UnsupportedContentEncoding("deflate".to_string()))
646 }
647 }
648 ContentCoding::Brotli => {
649 #[cfg(feature = "compression-brotli")]
650 {
651 use async_compression::tokio::bufread::BrotliDecoder;
652 use futures_util::StreamExt;
653 use tokio::io::BufReader;
654 use tokio_util::io::ReaderStream;
655
656 let reader = StreamReader::new(stream);
657 let decoder = BrotliDecoder::new(BufReader::new(reader));
658 let stream = ReaderStream::new(decoder);
659 Ok(Box::pin(stream.map(|r| {
660 r.map_err(|e| Error::Decompression(e.to_string()))
661 })))
662 }
663 #[cfg(not(feature = "compression-brotli"))]
664 {
665 Err(Error::UnsupportedContentEncoding("br".to_string()))
666 }
667 }
668 ContentCoding::Zstd => {
669 #[cfg(feature = "compression-zstd")]
670 {
671 use async_compression::tokio::bufread::ZstdDecoder;
672 use futures_util::StreamExt;
673 use tokio::io::BufReader;
674 use tokio_util::io::ReaderStream;
675
676 let reader = StreamReader::new(stream);
677 let decoder = ZstdDecoder::new(BufReader::new(reader));
678 let stream = ReaderStream::new(decoder);
679 Ok(Box::pin(stream.map(|r| {
680 r.map_err(|e| Error::Decompression(e.to_string()))
681 })))
682 }
683 #[cfg(not(feature = "compression-zstd"))]
684 {
685 Err(Error::UnsupportedContentEncoding("zstd".to_string()))
686 }
687 }
688 }
689}
690
691#[allow(dead_code)]
694struct StreamReader {
695 stream: BoxBytesStream,
696 buffer: bytes::BytesMut,
697 offset: usize,
698}
699
700#[allow(dead_code)]
701impl StreamReader {
702 fn new(stream: BoxBytesStream) -> Self {
703 Self {
704 stream,
705 buffer: bytes::BytesMut::new(),
706 offset: 0,
707 }
708 }
709}
710
711impl tokio::io::AsyncRead for StreamReader {
712 fn poll_read(
713 mut self: std::pin::Pin<&mut Self>,
714 cx: &mut std::task::Context<'_>,
715 buf: &mut tokio::io::ReadBuf<'_>,
716 ) -> std::task::Poll<std::io::Result<()>> {
717 use futures_core::Stream;
718 use std::pin::Pin;
719
720 if self.offset < self.buffer.len() {
722 let remaining = &self.buffer[self.offset..];
723 let to_copy = remaining.len().min(buf.remaining());
724 buf.put_slice(&remaining[..to_copy]);
725 self.offset += to_copy;
726 if self.offset >= self.buffer.len() {
727 self.buffer.clear();
728 self.offset = 0;
729 }
730 return std::task::Poll::Ready(Ok(()));
731 }
732
733 match Pin::new(&mut self.stream).poll_next(cx) {
735 std::task::Poll::Ready(Some(Ok(chunk))) => {
736 self.buffer.extend_from_slice(&chunk);
737 self.offset = 0;
738 let to_copy = self.buffer.len().min(buf.remaining());
739 buf.put_slice(&self.buffer[..to_copy]);
740 self.offset = to_copy;
741 std::task::Poll::Ready(Ok(()))
742 }
743 std::task::Poll::Ready(Some(Err(e))) => {
744 std::task::Poll::Ready(Err(std::io::Error::other(e.to_string())))
745 }
746 std::task::Poll::Ready(None) => std::task::Poll::Ready(Ok(())),
747 std::task::Poll::Pending => std::task::Poll::Pending,
748 }
749 }
750}
751
752struct CountingStream {
763 inner: BoxBytesStream,
764 count: Arc<AtomicUsize>,
765}
766
767impl CountingStream {
768 fn new(inner: BoxBytesStream) -> Self {
769 Self {
770 inner,
771 count: Arc::new(AtomicUsize::new(0)),
772 }
773 }
774
775 fn counter(&self) -> Arc<AtomicUsize> {
776 Arc::clone(&self.count)
777 }
778}
779
780fn saturating_fetch_add(counter: &AtomicUsize, amount: usize) {
785 let mut current = counter.load(Ordering::Acquire);
786 loop {
787 let Some(next) = current.checked_add(amount) else {
788 counter.store(usize::MAX, Ordering::Release);
789 return;
790 };
791 match counter.compare_exchange_weak(current, next, Ordering::AcqRel, Ordering::Acquire) {
792 Ok(_) => return,
793 Err(actual) => current = actual,
794 }
795 }
796}
797
798impl futures_core::Stream for CountingStream {
799 type Item = <BoxBytesStream as futures_core::Stream>::Item;
800
801 fn poll_next(
802 mut self: std::pin::Pin<&mut Self>,
803 cx: &mut std::task::Context<'_>,
804 ) -> std::task::Poll<Option<Self::Item>> {
805 use std::pin::Pin;
806
807 match Pin::new(&mut self.inner).poll_next(cx) {
808 std::task::Poll::Ready(Some(Ok(chunk))) => {
809 saturating_fetch_add(&self.count, chunk.len());
810 std::task::Poll::Ready(Some(Ok(chunk)))
811 }
812 other => other,
813 }
814 }
815}
816
817struct LimitingStream {
818 inner: BoxBytesStream,
819 limit: DecompressionLimit,
820 decoded_bytes: usize,
821 compressed_counter: Arc<AtomicUsize>,
822 limit_exceeded: Option<LimitExceededKind>,
823}
824
825#[derive(Debug, Clone, Copy, PartialEq, Eq)]
828enum LimitExceededKind {
829 Size,
830 Ratio,
831}
832
833impl LimitingStream {
834 fn new(
835 inner: BoxBytesStream,
836 limit: DecompressionLimit,
837 compressed_counter: Arc<AtomicUsize>,
838 ) -> Self {
839 Self {
840 inner,
841 limit,
842 decoded_bytes: 0,
843 compressed_counter,
844 limit_exceeded: None,
845 }
846 }
847
848 #[allow(clippy::cast_precision_loss)]
849 fn check_limit(&self) -> std::result::Result<(), LimitExceededKind> {
850 if let Some(max) = self.limit.max_decoded_body_size {
851 if self.decoded_bytes > max {
852 return Err(LimitExceededKind::Size);
853 }
854 }
855 if let Some(max_ratio) = self.limit.max_decompression_ratio {
856 if !max_ratio.is_finite() || max_ratio <= 0.0 {
857 return Err(LimitExceededKind::Ratio);
860 }
861 let compressed = self.compressed_counter.load(Ordering::Acquire);
862 if compressed > 0 {
863 let ratio = self.decoded_bytes as f64 / compressed as f64;
864 if ratio > max_ratio {
865 return Err(LimitExceededKind::Ratio);
866 }
867 }
868 }
869 Ok(())
870 }
871
872 fn kind_to_error(kind: LimitExceededKind, limit: &DecompressionLimit) -> Error {
873 match kind {
874 LimitExceededKind::Size => Error::DecodedBodyTooLarge,
875 LimitExceededKind::Ratio => {
876 if limit
879 .max_decompression_ratio
880 .is_some_and(|r| !r.is_finite() || r <= 0.0)
881 {
882 Error::Decompression(
883 "invalid max_decompression_ratio: must be finite and positive".into(),
884 )
885 } else {
886 Error::DecompressionRatioExceeded
887 }
888 }
889 }
890 }
891}
892
893impl futures_core::Stream for LimitingStream {
894 type Item = Result<bytes::Bytes>;
895
896 fn poll_next(
897 mut self: std::pin::Pin<&mut Self>,
898 cx: &mut std::task::Context<'_>,
899 ) -> std::task::Poll<Option<Self::Item>> {
900 use std::pin::Pin;
901
902 if self.limit_exceeded.is_some() {
906 return std::task::Poll::Ready(None);
907 }
908
909 match Pin::new(&mut self.inner).poll_next(cx) {
910 std::task::Poll::Ready(Some(Ok(chunk))) => {
911 self.decoded_bytes = self.decoded_bytes.saturating_add(chunk.len());
915 if let Err(kind) = self.check_limit() {
916 self.limit_exceeded = Some(kind);
917 let err = Self::kind_to_error(kind, &self.limit);
918 return std::task::Poll::Ready(Some(Err(err)));
919 }
920 std::task::Poll::Ready(Some(Ok(chunk)))
921 }
922 std::task::Poll::Ready(Some(Err(e))) => std::task::Poll::Ready(Some(Err(e))),
923 std::task::Poll::Ready(None) => std::task::Poll::Ready(None),
924 std::task::Poll::Pending => std::task::Poll::Pending,
925 }
926 }
927}
928
929#[cfg(test)]
930mod tests {
931 use super::*;
932 use futures_util::{FutureExt, StreamExt};
933
934 #[test]
935 fn content_coding_from_wire() {
936 assert_eq!(ContentCoding::from_wire("gzip"), Some(ContentCoding::Gzip));
937 assert_eq!(
938 ContentCoding::from_wire("x-gzip"),
939 Some(ContentCoding::Gzip)
940 );
941 assert_eq!(
942 ContentCoding::from_wire("deflate"),
943 Some(ContentCoding::Deflate)
944 );
945 assert_eq!(ContentCoding::from_wire("br"), Some(ContentCoding::Brotli));
946 assert_eq!(ContentCoding::from_wire("zstd"), Some(ContentCoding::Zstd));
947 assert_eq!(ContentCoding::from_wire("identity"), None);
948 assert_eq!(ContentCoding::from_wire("unknown"), None);
949 assert_eq!(ContentCoding::from_wire("GZIP"), Some(ContentCoding::Gzip));
950 assert_eq!(
951 ContentCoding::from_wire(" Br "),
952 Some(ContentCoding::Brotli)
953 );
954 }
955
956 #[test]
957 fn content_coding_as_str() {
958 assert_eq!(ContentCoding::Gzip.as_str(), "gzip");
959 assert_eq!(ContentCoding::Deflate.as_str(), "deflate");
960 assert_eq!(ContentCoding::Brotli.as_str(), "br");
961 assert_eq!(ContentCoding::Zstd.as_str(), "zstd");
962 }
963
964 #[test]
965 fn parse_content_encodings_single() {
966 let encs = parse_content_encodings("gzip").unwrap();
967 assert_eq!(encs, vec![ContentCoding::Gzip]);
968 }
969
970 #[test]
971 fn parse_content_encodings_multiple() {
972 let encs = parse_content_encodings("gzip, br").unwrap();
973 assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
974 }
975
976 #[test]
977 fn parse_content_encodings_with_whitespace() {
978 let encs = parse_content_encodings(" gzip , br ").unwrap();
979 assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
980 }
981
982 #[test]
983 fn parse_content_encodings_empty() {
984 assert!(parse_content_encodings("").is_none());
985 assert!(parse_content_encodings(" ").is_none());
986 }
987
988 #[test]
989 fn parse_content_encodings_unknown_filtered() {
990 let encs = parse_content_encodings("gzip, identity, br").unwrap();
991 assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
992 }
993
994 #[test]
995 fn validate_content_encodings_supported() {
996 assert!(validate_content_encodings("gzip").is_ok());
997 assert!(validate_content_encodings("gzip, br").is_ok());
998 assert!(validate_content_encodings("").is_ok());
999 }
1000
1001 #[test]
1002 fn validate_content_encodings_accepts_identity() {
1003 assert!(validate_content_encodings("identity").is_ok());
1005 assert!(validate_content_encodings("IDENTITY").is_ok());
1006 assert!(validate_content_encodings("identity, gzip").is_ok());
1007 assert!(validate_content_encodings("gzip, identity").is_ok());
1008 }
1009
1010 #[test]
1011 fn parse_content_encodings_identity_only_is_none() {
1012 assert!(parse_content_encodings("identity").is_none());
1013 let encs = parse_content_encodings("gzip, identity, br").unwrap();
1014 assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1015 }
1016
1017 #[test]
1018 fn validate_content_encodings_unsupported() {
1019 let err = validate_content_encodings("gzip, weird").unwrap_err();
1020 assert_eq!(err.kind(), "unsupported_content_encoding");
1021 assert!(err.to_string().contains("weird"));
1022 }
1023
1024 #[test]
1025 fn accept_encoding_value_requires_compiled_features() {
1026 let _ = accept_encoding_value();
1027 }
1028
1029 #[cfg(not(any(
1030 feature = "compression-gzip",
1031 feature = "compression-deflate",
1032 feature = "compression-brotli",
1033 feature = "compression-zstd"
1034 )))]
1035 #[test]
1036 fn accept_encoding_value_none_without_compression_features() {
1037 assert!(
1038 accept_encoding_value().is_none(),
1039 "accept_encoding_value() should return None when no compression features are compiled"
1040 );
1041 }
1042
1043 #[cfg(all(
1044 feature = "compression-gzip",
1045 not(feature = "compression-brotli"),
1046 not(feature = "compression-zstd")
1047 ))]
1048 #[test]
1049 fn accept_encoding_value_gzip_only() {
1050 let val = accept_encoding_value().unwrap();
1051 assert!(val.contains("gzip"), "should contain gzip, got: {val}");
1052 assert!(
1053 !val.contains("br"),
1054 "should not contain br without brotli feature, got: {val}"
1055 );
1056 assert!(
1057 !val.contains("zstd"),
1058 "should not contain zstd without zstd feature, got: {val}"
1059 );
1060 }
1061
1062 #[test]
1063 fn accept_encoding_value_deterministic_order() {
1064 let val1 = accept_encoding_value();
1065 let val2 = accept_encoding_value();
1066 assert_eq!(
1067 val1, val2,
1068 "accept_encoding_value() should return the same value across calls"
1069 );
1070 }
1071
1072 #[test]
1073 fn accept_encoding_advertises_only_supported_decoders() {
1074 let Some(value) = accept_encoding_value() else {
1078 return;
1079 };
1080 for token in value.split(", ") {
1081 let coding =
1082 ContentCoding::from_wire(token).unwrap_or_else(|| panic!("invalid token {token}"));
1083 let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1084 let result = make_decoder(stream, coding);
1085 assert!(
1086 result.is_ok(),
1087 "advertised encoding '{token}' has no compiled-in decoder"
1088 );
1089 }
1090 }
1091
1092 #[test]
1093 fn decompress_stream_no_encoding_returns_original() {
1094 let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1095 let result = decompress_stream(stream, None, true, DecompressionLimit::new()).unwrap();
1096 drop(result);
1097 }
1098
1099 #[test]
1100 fn decompress_stream_disabled_returns_original() {
1101 let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1102 let result =
1103 decompress_stream(stream, Some("gzip"), false, DecompressionLimit::new()).unwrap();
1104 drop(result);
1105 }
1106
1107 #[test]
1108 fn decompress_stream_empty_header_returns_original() {
1109 let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1110 let result =
1111 decompress_stream(stream, Some(" "), true, DecompressionLimit::new()).unwrap();
1112 drop(result);
1113 }
1114
1115 #[test]
1116 fn decompress_buffered_identity_passthrough() {
1117 assert_eq!(
1120 &decompress_buffered(b"plain body", "identity", DecompressionLimit::new()).unwrap()[..],
1121 b"plain body"
1122 );
1123 let err = decompress_buffered(b"x", "weird", DecompressionLimit::new()).unwrap_err();
1124 assert_eq!(err.kind(), "unsupported_content_encoding");
1125 }
1126
1127 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1128 #[test]
1129 fn sync_decode_flate2_rejects_non_flate_encoding() {
1130 let err = sync_decode_flate2(&bytes::Bytes::new(), ContentCoding::Brotli, None, false)
1131 .unwrap_err();
1132 assert_eq!(err.kind(), "unsupported_content_encoding");
1133 assert!(err.to_string().contains("br"));
1134 }
1135
1136 #[test]
1137 fn decompression_limit_new_is_unlimited() {
1138 let limit = DecompressionLimit::new();
1139 assert!(limit.is_unlimited());
1140 assert!(limit.max_decoded_body_size.is_none());
1141 assert!(limit.max_decompression_ratio.is_none());
1142 }
1143
1144 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1145 #[test]
1146 fn decompression_limit_max_size_rejects() {
1147 let data = gzip_compress(b"hello world");
1148 let limit = DecompressionLimit {
1149 max_decoded_body_size: Some(5),
1150 max_decompression_ratio: None,
1151 };
1152 let err = decompress_buffered(&data, "gzip", limit).unwrap_err();
1153 assert_eq!(err.kind(), "decoded_body_too_large");
1154 }
1155
1156 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1157 #[test]
1158 fn decompression_limit_max_size_allows_within() {
1159 let data = gzip_compress(b"hi");
1160 let limit = DecompressionLimit {
1161 max_decoded_body_size: Some(1024),
1162 max_decompression_ratio: None,
1163 };
1164 let result = decompress_buffered(&data, "gzip", limit).unwrap();
1165 assert_eq!(&result[..], b"hi");
1166 }
1167
1168 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1169 #[test]
1170 fn decompression_limit_ratio_rejects() {
1171 let data = gzip_compress(b"hello world, this is a test of compression");
1172 let limit = DecompressionLimit {
1173 max_decoded_body_size: None,
1174 max_decompression_ratio: Some(0.5),
1175 };
1176 let err = decompress_buffered(&data, "gzip", limit).unwrap_err();
1177 assert_eq!(err.kind(), "decompression_ratio_exceeded");
1178 }
1179
1180 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1181 #[test]
1182 fn decompression_limit_ratio_allows_within() {
1183 let data = gzip_compress(b"hello world, this is a test of compression");
1184 let limit = DecompressionLimit {
1185 max_decoded_body_size: None,
1186 max_decompression_ratio: Some(100.0),
1187 };
1188 let result = decompress_buffered(&data, "gzip", limit).unwrap();
1189 assert_eq!(&result[..], b"hello world, this is a test of compression");
1190 }
1191
1192 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1193 #[test]
1194 fn decompression_limit_unlimited_allows() {
1195 let data = gzip_compress(b"hello world");
1196 let limit = DecompressionLimit::new();
1197 let result = decompress_buffered(&data, "gzip", limit).unwrap();
1198 assert_eq!(&result[..], b"hello world");
1199 }
1200
1201 #[cfg(feature = "compression-gzip")]
1202 #[test]
1203 fn decompression_limit_ratio_enforced_per_layer() {
1204 let payload = vec![b'a'; 5000];
1210 let inner = gzip_compress(&payload);
1211 let outer = gzip_compress(&inner);
1212 #[allow(clippy::cast_precision_loss)]
1213 let (total_ratio, inner_step) = {
1214 (
1215 payload.len() as f64 / outer.len() as f64,
1216 payload.len() as f64 / inner.len() as f64,
1217 )
1218 };
1219 assert!(
1220 inner_step > total_ratio + 1.0,
1221 "expected the inner step to dominate: {inner_step} vs {total_ratio}"
1222 );
1223
1224 #[allow(clippy::cast_precision_loss)]
1225 let limit_value = ((total_ratio + inner_step) / 2.0).ceil();
1226 let limit = DecompressionLimit {
1227 max_decoded_body_size: None,
1228 max_decompression_ratio: Some(limit_value),
1229 };
1230 let err = decompress_buffered(&outer, "gzip, gzip", limit).unwrap_err();
1231 assert_eq!(err.kind(), "decompression_ratio_exceeded");
1232
1233 let ok = decompress_buffered(&outer, "gzip, gzip", DecompressionLimit::default()).unwrap();
1235 assert_eq!(&ok[..], &payload[..]);
1236 }
1237
1238 #[cfg(feature = "compression-gzip")]
1239 #[tokio::test]
1240 async fn decompression_stream_enforces_ratio_per_layer() {
1241 let payload = vec![b'a'; 5000];
1242 let inner = gzip_compress(&payload);
1243 let outer = gzip_compress(&inner);
1244 #[allow(clippy::cast_precision_loss)]
1245 let limit_value = ((payload.len() as f64 / outer.len() as f64)
1246 + (payload.len() as f64 / inner.len() as f64))
1247 / 2.0;
1248 let limit = DecompressionLimit {
1249 max_decoded_body_size: None,
1250 max_decompression_ratio: Some(limit_value.ceil()),
1251 };
1252 let input: BoxBytesStream = Box::pin(futures_util::stream::once(async move {
1253 Ok(bytes::Bytes::from(outer))
1254 }));
1255 let decoded = decompress_stream(input, Some("gzip, gzip"), true, limit).unwrap();
1256 let err = decoded
1257 .collect::<Vec<_>>()
1258 .await
1259 .into_iter()
1260 .find_map(std::result::Result::err)
1261 .expect("streaming ratio limit must reject the inner layer");
1262 assert_eq!(err.kind(), "decompression_ratio_exceeded");
1263 }
1264
1265 #[cfg(feature = "compression-gzip")]
1266 #[tokio::test]
1267 async fn decompression_stream_enforces_ratio_against_wire_size() {
1268 let payload: Vec<u8> = (0usize..5000)
1272 .map(|i| u8::try_from((i * 73 + i / 17) % 251).expect("value is below 251"))
1273 .collect();
1274 let inner = gzip_compress(&payload);
1275 let outer = gzip_compress(&inner);
1276 #[allow(clippy::cast_precision_loss)]
1277 let (first_layer, second_layer, total) = (
1278 inner.len() as f64 / outer.len() as f64,
1279 payload.len() as f64 / inner.len() as f64,
1280 payload.len() as f64 / outer.len() as f64,
1281 );
1282 let per_layer_limit = first_layer.max(second_layer);
1283 assert!(total > per_layer_limit);
1284 let limit = DecompressionLimit {
1285 max_decoded_body_size: None,
1286 max_decompression_ratio: Some((per_layer_limit + total) / 2.0),
1287 };
1288 let input: BoxBytesStream = Box::pin(futures_util::stream::once(async move {
1289 Ok(bytes::Bytes::from(outer))
1290 }));
1291 let decoded = decompress_stream(input, Some("gzip, gzip"), true, limit).unwrap();
1292 let err = decoded
1293 .collect::<Vec<_>>()
1294 .await
1295 .into_iter()
1296 .find_map(std::result::Result::err)
1297 .expect("streaming ratio limit must include the wire size");
1298 assert_eq!(err.kind(), "decompression_ratio_exceeded");
1299 }
1300
1301 #[test]
1302 fn limiting_stream_enforces_max_decoded_body_size() {
1303 let limit = DecompressionLimit {
1304 max_decoded_body_size: Some(3),
1305 max_decompression_ratio: None,
1306 };
1307 let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![
1308 Ok(bytes::Bytes::from("ab")),
1309 Ok(bytes::Bytes::from("cd")),
1310 ]));
1311 let counter = Arc::new(AtomicUsize::new(0));
1312 let mut limited = LimitingStream::new(stream, limit, counter);
1313
1314 let chunk1 = futures_util::StreamExt::next(&mut limited)
1315 .now_or_never()
1316 .unwrap()
1317 .unwrap()
1318 .unwrap();
1319 assert_eq!(chunk1, "ab");
1320
1321 let err = futures_util::StreamExt::next(&mut limited)
1322 .now_or_never()
1323 .unwrap()
1324 .unwrap()
1325 .unwrap_err();
1326 assert_eq!(err.kind(), "decoded_body_too_large");
1327 }
1328
1329 #[test]
1330 fn limiting_stream_allows_within_limit() {
1331 let limit = DecompressionLimit {
1332 max_decoded_body_size: Some(1024),
1333 max_decompression_ratio: None,
1334 };
1335 let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![
1336 Ok(bytes::Bytes::from("a")),
1337 Ok(bytes::Bytes::from("b")),
1338 ]));
1339 let counter = Arc::new(AtomicUsize::new(0));
1340 let mut limited = LimitingStream::new(stream, limit, counter);
1341
1342 let chunk1 = futures_util::StreamExt::next(&mut limited)
1343 .now_or_never()
1344 .unwrap()
1345 .unwrap()
1346 .unwrap();
1347 assert_eq!(chunk1, "a");
1348 let chunk2 = futures_util::StreamExt::next(&mut limited)
1349 .now_or_never()
1350 .unwrap()
1351 .unwrap()
1352 .unwrap();
1353 assert_eq!(chunk2, "b");
1354 }
1355
1356 #[test]
1357 fn saturating_fetch_add_saturates_instead_of_wrapping() {
1358 let counter = AtomicUsize::new(usize::MAX - 2);
1359 saturating_fetch_add(&counter, 1);
1360 assert_eq!(counter.load(Ordering::Relaxed), usize::MAX - 1);
1361 saturating_fetch_add(&counter, 5);
1362 assert_eq!(counter.load(Ordering::Relaxed), usize::MAX);
1363 saturating_fetch_add(&counter, 5);
1365 assert_eq!(counter.load(Ordering::Relaxed), usize::MAX);
1366 }
1367
1368 #[test]
1369 fn limiting_stream_saturated_counter_still_enforces_size_limit() {
1370 let limit = DecompressionLimit {
1371 max_decoded_body_size: Some(10),
1372 max_decompression_ratio: None,
1373 };
1374 let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![Ok(
1375 bytes::Bytes::from("x"),
1376 )]));
1377 let counter = Arc::new(AtomicUsize::new(0));
1378 let mut limited = LimitingStream::new(stream, limit, counter);
1379 limited.decoded_bytes = usize::MAX;
1383 let err = futures_util::StreamExt::next(&mut limited)
1384 .now_or_never()
1385 .unwrap()
1386 .unwrap()
1387 .unwrap_err();
1388 assert_eq!(err.kind(), "decoded_body_too_large");
1389 }
1390
1391 fn gzip_compress(data: &[u8]) -> Vec<u8> {
1392 use flate2::write::GzEncoder;
1393 use flate2::Compression;
1394 use std::io::Write;
1395
1396 let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
1397 encoder.write_all(data).unwrap();
1398 encoder.finish().unwrap()
1399 }
1400}