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)]
35pub struct DecompressionLimit {
36 pub(crate) max_decoded_body_size: Option<usize>,
38 pub(crate) max_decompression_ratio: Option<f64>,
41}
42
43impl DecompressionLimit {
44 #[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 #[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 #[must_use]
61 pub fn max_decoded_body_size(&self) -> Option<usize> {
62 self.max_decoded_body_size
63 }
64
65 #[must_use]
67 pub fn max_decompression_ratio(&self) -> Option<f64> {
68 self.max_decompression_ratio
69 }
70
71 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 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
113const MAX_NESTING_DEPTH: usize = 4;
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub enum ContentCoding {
121 Gzip,
123 Deflate,
126 Brotli,
128 Zstd,
130}
131
132impl ContentCoding {
133 #[must_use]
135 pub fn as_str(self) -> &'static str {
136 match self {
137 Self::Gzip => "gzip",
138 Self::Deflate => "deflate",
139 Self::Brotli => "br",
140 Self::Zstd => "zstd",
141 }
142 }
143
144 fn from_wire(s: &str) -> Option<Self> {
146 match s.trim().to_ascii_lowercase().as_str() {
147 "gzip" | "x-gzip" => Some(Self::Gzip),
148 "deflate" => Some(Self::Deflate),
149 "br" => Some(Self::Brotli),
150 "zstd" => Some(Self::Zstd),
151 _ => None,
152 }
153 }
154
155 fn is_identity(token: &str) -> bool {
158 token.trim().eq_ignore_ascii_case("identity")
159 }
160}
161
162#[must_use]
165pub fn accept_encoding_value() -> Option<&'static str> {
166 static VALUE: OnceLock<Option<Box<str>>> = OnceLock::new();
167
168 VALUE
169 .get_or_init(|| {
170 #[allow(clippy::vec_init_then_push)]
178 {
179 #[allow(unused_mut)]
180 let mut parts: Vec<&str> = Vec::new();
181
182 #[cfg(feature = "compression-gzip")]
183 parts.push("gzip");
184 #[cfg(feature = "compression-deflate")]
185 parts.push("deflate");
186 #[cfg(feature = "compression-brotli")]
187 parts.push("br");
188 #[cfg(feature = "compression-zstd")]
189 parts.push("zstd");
190
191 (!parts.is_empty()).then(|| parts.join(", ").into_boxed_str())
192 }
193 })
194 .as_deref()
195}
196
197pub(crate) fn parse_content_encodings(header: &str) -> Option<Vec<ContentCoding>> {
212 let encodings: Vec<ContentCoding> = header
213 .split(',')
214 .map(str::trim)
215 .filter(|s| !s.is_empty() && !ContentCoding::is_identity(s))
216 .filter_map(ContentCoding::from_wire)
217 .collect();
218
219 if encodings.is_empty() {
220 None
221 } else {
222 Some(encodings)
223 }
224}
225
226pub fn validate_content_encodings(header_value: &str) -> Result<()> {
239 for token in header_value.split(',') {
240 let token = token.trim();
241 if token.is_empty() || ContentCoding::is_identity(token) {
242 continue;
243 }
244 if ContentCoding::from_wire(token).is_none() {
245 return Err(Error::UnsupportedContentEncoding(token.to_string()));
246 }
247 }
248 Ok(())
249}
250
251pub fn decompress_stream(
271 stream: BoxBytesStream,
272 content_encoding: Option<&str>,
273 decompression_enabled: bool,
274 limit: DecompressionLimit,
275) -> Result<BoxBytesStream> {
276 if !decompression_enabled {
277 return Ok(stream);
278 }
279
280 let header = match content_encoding {
281 Some(h) if !h.trim().is_empty() => h,
282 _ => return Ok(stream),
283 };
284
285 limit.validate()?;
289
290 validate_content_encodings(header)?;
294
295 let Some(encodings) = parse_content_encodings(header) else {
296 return Ok(stream);
297 };
298
299 if encodings.len() > MAX_NESTING_DEPTH {
300 return Err(Error::Decompression(format!(
301 "content encoding nesting depth {} exceeds maximum {}",
302 encodings.len(),
303 MAX_NESTING_DEPTH
304 )));
305 }
306
307 let mut current = stream;
322 let mut outer_counter = None;
323 for encoding in encodings.into_iter().rev() {
324 let counting = CountingStream::new(current);
325 let counter = counting.counter();
326 current = make_decoder(Box::pin(counting), encoding)?;
327 if outer_counter.is_none() {
330 outer_counter = Some(Arc::clone(&counter));
331 }
332
333 if let Some(max_ratio) = limit.max_decompression_ratio {
337 current = Box::pin(LimitingStream::new(
338 current,
339 DecompressionLimit {
340 max_decoded_body_size: None,
341 max_decompression_ratio: Some(max_ratio),
342 },
343 counter,
344 ));
345 }
346 }
347
348 if limit.is_unlimited() {
349 return Ok(current);
350 }
351
352 let Some(outer_counter) = outer_counter else {
353 return Ok(current);
354 };
355 Ok(Box::pin(LimitingStream::new(current, limit, outer_counter)))
356}
357
358#[allow(clippy::too_many_lines)]
371pub fn decompress_buffered(
372 data: &[u8],
373 content_encoding: &str,
374 limit: DecompressionLimit,
375) -> Result<bytes::Bytes> {
376 limit.validate()?;
380 validate_content_encodings(content_encoding)?;
382
383 let encodings = parse_content_encodings(content_encoding);
384 let Some(encodings) = encodings else {
385 return Ok(bytes::Bytes::copy_from_slice(data));
389 };
390
391 if encodings.len() > MAX_NESTING_DEPTH {
392 return Err(Error::Decompression(format!(
393 "content encoding nesting depth {} exceeds maximum {}",
394 encodings.len(),
395 MAX_NESTING_DEPTH
396 )));
397 }
398
399 if data.is_empty() {
402 return Ok(bytes::Bytes::new());
403 }
404
405 let compressed_len = data.len();
406 let ratio_limit = limit.max_decompression_ratio.and_then(|ratio| {
407 if ratio.is_finite() && ratio > 0.0 {
408 #[allow(clippy::cast_precision_loss)]
409 let limit = (compressed_len as f64 * ratio).ceil();
410 #[allow(
411 clippy::cast_possible_truncation,
412 clippy::cast_sign_loss,
413 clippy::cast_precision_loss,
414 reason = "the finite non-negative ratio is clamped to the usize allocation limit"
415 )]
416 Some(if limit >= usize::MAX as f64 {
420 usize::MAX
421 } else {
422 limit.min(usize::MAX as f64) as usize
423 })
424 } else {
425 None
426 }
427 });
428 let output_limit = match (limit.max_decoded_body_size, ratio_limit) {
429 (Some(size), Some(ratio)) => Some(size.min(ratio)),
430 (Some(size), None) | (None, Some(size)) => Some(size),
431 (None, None) => None,
432 };
433 let ratio_is_tighter = ratio_limit.is_some_and(|ratio| Some(ratio) == output_limit);
434
435 #[allow(unused_mut)]
436 let mut current = bytes::Bytes::copy_from_slice(data);
437 #[allow(clippy::never_loop)]
438 for encoding in encodings.into_iter().rev() {
439 match encoding {
440 ContentCoding::Gzip | ContentCoding::Deflate => {
441 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
442 {
443 let layer_input_len = current.len();
444 current =
445 sync_decode_flate2(¤t, encoding, output_limit, ratio_is_tighter)?;
446 if let Some(max_ratio) = limit.max_decompression_ratio {
447 if layer_input_len > 0 {
448 #[allow(clippy::cast_precision_loss)]
449 let layer_ratio = current.len() as f64 / layer_input_len as f64;
450 if layer_ratio > max_ratio {
451 return Err(Error::DecompressionRatioExceeded);
452 }
453 }
454 }
455 }
456 #[cfg(not(any(feature = "compression-gzip", feature = "compression-deflate")))]
457 {
458 return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
459 }
460 }
461 ContentCoding::Brotli => {
462 #[cfg(feature = "compression-brotli")]
463 {
464 let layer_input_len = current.len();
465 current = sync_decode_brotli(¤t, output_limit, ratio_is_tighter)?;
466 if let Some(max_ratio) = limit.max_decompression_ratio {
467 if layer_input_len > 0 {
468 #[allow(clippy::cast_precision_loss)]
469 let layer_ratio = current.len() as f64 / layer_input_len as f64;
470 if layer_ratio > max_ratio {
471 return Err(Error::DecompressionRatioExceeded);
472 }
473 }
474 }
475 }
476 #[cfg(not(feature = "compression-brotli"))]
477 {
478 return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
479 }
480 }
481 ContentCoding::Zstd => {
482 #[cfg(feature = "compression-zstd")]
483 {
484 let layer_input_len = current.len();
485 current = sync_decode_zstd(¤t, output_limit, ratio_is_tighter)?;
486 if let Some(max_ratio) = limit.max_decompression_ratio {
487 if layer_input_len > 0 {
488 #[allow(clippy::cast_precision_loss)]
489 let layer_ratio = current.len() as f64 / layer_input_len as f64;
490 if layer_ratio > max_ratio {
491 return Err(Error::DecompressionRatioExceeded);
492 }
493 }
494 }
495 }
496 #[cfg(not(feature = "compression-zstd"))]
497 {
498 return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
499 }
500 }
501 }
502 }
503
504 let size_violated = limit
511 .max_decoded_body_size
512 .is_some_and(|max| current.len() > max);
513 let ratio_violated = limit.max_decompression_ratio.is_some_and(|max_ratio| {
514 compressed_len > 0 && {
515 #[allow(clippy::cast_precision_loss)]
516 let ratio = current.len() as f64 / compressed_len as f64;
517 ratio > max_ratio
518 }
519 });
520 if size_violated && ratio_violated {
521 if ratio_is_tighter {
522 return Err(Error::DecompressionRatioExceeded);
523 }
524 return Err(Error::DecodedBodyTooLarge);
525 }
526 if size_violated {
527 return Err(Error::DecodedBodyTooLarge);
528 }
529 if ratio_violated {
530 return Err(Error::DecompressionRatioExceeded);
531 }
532
533 Ok(current)
534}
535
536#[cfg(any(
543 feature = "compression-gzip",
544 feature = "compression-deflate",
545 feature = "compression-brotli",
546 feature = "compression-zstd"
547))]
548fn sync_read_limited<R: std::io::Read>(
549 mut reader: R,
550 output_limit: Option<usize>,
551 ratio_is_tighter: bool,
552) -> Result<bytes::Bytes> {
553 let mut output = Vec::new();
554 let mut buf = [0u8; 8 * 1024];
555 loop {
556 let n = reader
557 .read(&mut buf)
558 .map_err(|e| Error::Decompression(e.to_string()))?;
559 if n == 0 {
560 break;
561 }
562 output.extend_from_slice(&buf[..n]);
563 if output_limit.is_some_and(|limit| output.len() > limit) {
564 return Err(if ratio_is_tighter {
565 Error::DecompressionRatioExceeded
566 } else {
567 Error::DecodedBodyTooLarge
568 });
569 }
570 }
571 Ok(bytes::Bytes::from(output))
572}
573
574#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
576fn sync_decode_flate2(
577 data: &bytes::Bytes,
578 encoding: ContentCoding,
579 output_limit: Option<usize>,
580 ratio_is_tighter: bool,
581) -> Result<bytes::Bytes> {
582 match encoding {
583 ContentCoding::Gzip => {
584 let decoder = flate2::read::GzDecoder::new(&data[..]);
585 sync_read_limited(decoder, output_limit, ratio_is_tighter)
586 }
587 ContentCoding::Deflate => {
588 let zlib = flate2::read::ZlibDecoder::new(&data[..]);
593 if let ok @ Ok(_) = sync_read_limited(zlib, output_limit, ratio_is_tighter) {
594 ok
595 } else {
596 let raw = flate2::read::DeflateDecoder::new(&data[..]);
597 sync_read_limited(raw, output_limit, ratio_is_tighter)
598 }
599 }
600 _ => Err(Error::UnsupportedContentEncoding(
601 encoding.as_str().to_owned(),
602 )),
603 }
604}
605
606#[cfg(feature = "compression-brotli")]
607fn sync_decode_brotli(
608 data: &bytes::Bytes,
609 output_limit: Option<usize>,
610 ratio_is_tighter: bool,
611) -> Result<bytes::Bytes> {
612 let decoder = brotli::Decompressor::new(&data[..], 4096);
613 sync_read_limited(decoder, output_limit, ratio_is_tighter)
614}
615
616#[cfg(feature = "compression-zstd")]
617fn sync_decode_zstd(
618 data: &bytes::Bytes,
619 output_limit: Option<usize>,
620 ratio_is_tighter: bool,
621) -> Result<bytes::Bytes> {
622 let decoder = zstd::stream::read::Decoder::new(&data[..])
623 .map_err(|e| Error::Decompression(e.to_string()))?;
624 sync_read_limited(decoder, output_limit, ratio_is_tighter)
625}
626
627#[cfg(feature = "compression-deflate")]
633fn is_zlib_header(prefix: &[u8]) -> bool {
634 let [cmf, flg, ..] = prefix else {
635 return false;
636 };
637 let cmf = u16::from(*cmf);
638 let flg = u16::from(*flg);
639 cmf & 0x0F == 8 && (cmf * 256 + flg) % 31 == 0
640}
641
642#[cfg(feature = "compression-deflate")]
650fn make_deflate_decoder(stream: BoxBytesStream) -> BoxBytesStream {
651 use async_compression::tokio::bufread::{DeflateDecoder, ZlibDecoder};
652 use futures_util::StreamExt;
653 use tokio::io::{AsyncBufReadExt, AsyncRead, BufReader};
654 use tokio_util::io::{ReaderStream, StreamReader};
655
656 enum DeflateState<R> {
657 Sniffing(R),
658 Decoding(ReaderStream<std::pin::Pin<Box<dyn AsyncRead + Send>>>),
659 Done,
660 }
661
662 let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
663 let reader = StreamReader::new(mapped);
664 let sniffed = futures_util::stream::unfold(
665 DeflateState::Sniffing(BufReader::new(reader)),
666 |mut state| async move {
667 loop {
668 match state {
669 DeflateState::Sniffing(mut reader) => {
670 let is_zlib = match reader.fill_buf().await {
671 Ok(buf) => {
672 if buf.is_empty() {
673 return None;
674 }
675 is_zlib_header(buf)
676 }
677 Err(e) => return Some((Err(e), DeflateState::Done)),
678 };
679 let decoder: std::pin::Pin<Box<dyn AsyncRead + Send>> = if is_zlib {
680 Box::pin(ZlibDecoder::new(reader))
681 } else {
682 Box::pin(DeflateDecoder::new(reader))
683 };
684 state = DeflateState::Decoding(ReaderStream::new(decoder));
685 }
686 DeflateState::Decoding(mut inner) => {
687 return inner
688 .next()
689 .await
690 .map(|item| (item, DeflateState::Decoding(inner)));
691 }
692 DeflateState::Done => return None,
693 }
694 }
695 },
696 );
697 let stream = sniffed.map(|r| r.map_err(|e| Error::Decompression(e.to_string())));
698 Box::pin(stream)
699}
700
701#[allow(
703 clippy::unnecessary_wraps,
704 clippy::needless_pass_by_value,
705 unused_variables
706)]
707fn make_decoder(stream: BoxBytesStream, encoding: ContentCoding) -> Result<BoxBytesStream> {
708 match encoding {
709 ContentCoding::Gzip => {
710 #[cfg(feature = "compression-gzip")]
711 {
712 use async_compression::tokio::bufread::GzipDecoder;
713 use futures_util::StreamExt;
714 use tokio::io::BufReader;
715 use tokio_util::io::{ReaderStream, StreamReader};
716
717 let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
720 let reader = StreamReader::new(mapped);
721 let decoder = GzipDecoder::new(BufReader::new(reader));
722 let stream = ReaderStream::new(decoder);
723 Ok(Box::pin(stream.map(|r| {
724 r.map_err(|e| Error::Decompression(e.to_string()))
725 })))
726 }
727 #[cfg(not(feature = "compression-gzip"))]
728 {
729 Err(Error::UnsupportedContentEncoding("gzip".to_string()))
730 }
731 }
732 ContentCoding::Deflate => {
733 #[cfg(feature = "compression-deflate")]
734 {
735 Ok(make_deflate_decoder(stream))
736 }
737 #[cfg(not(feature = "compression-deflate"))]
738 {
739 Err(Error::UnsupportedContentEncoding("deflate".to_string()))
740 }
741 }
742 ContentCoding::Brotli => {
743 #[cfg(feature = "compression-brotli")]
744 {
745 use async_compression::tokio::bufread::BrotliDecoder;
746 use futures_util::StreamExt;
747 use tokio::io::BufReader;
748 use tokio_util::io::{ReaderStream, StreamReader};
749
750 let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
751 let reader = StreamReader::new(mapped);
752 let decoder = BrotliDecoder::new(BufReader::new(reader));
753 let stream = ReaderStream::new(decoder);
754 Ok(Box::pin(stream.map(|r| {
755 r.map_err(|e| Error::Decompression(e.to_string()))
756 })))
757 }
758 #[cfg(not(feature = "compression-brotli"))]
759 {
760 Err(Error::UnsupportedContentEncoding("br".to_string()))
761 }
762 }
763 ContentCoding::Zstd => {
764 #[cfg(feature = "compression-zstd")]
765 {
766 use async_compression::tokio::bufread::ZstdDecoder;
767 use futures_util::StreamExt;
768 use tokio::io::BufReader;
769 use tokio_util::io::{ReaderStream, StreamReader};
770
771 let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
772 let reader = StreamReader::new(mapped);
773 let decoder = ZstdDecoder::new(BufReader::new(reader));
774 let stream = ReaderStream::new(decoder);
775 Ok(Box::pin(stream.map(|r| {
776 r.map_err(|e| Error::Decompression(e.to_string()))
777 })))
778 }
779 #[cfg(not(feature = "compression-zstd"))]
780 {
781 Err(Error::UnsupportedContentEncoding("zstd".to_string()))
782 }
783 }
784 }
785}
786
787struct CountingStream {
798 inner: BoxBytesStream,
799 count: Arc<AtomicUsize>,
800}
801
802impl CountingStream {
803 fn new(inner: BoxBytesStream) -> Self {
804 Self {
805 inner,
806 count: Arc::new(AtomicUsize::new(0)),
807 }
808 }
809
810 fn counter(&self) -> Arc<AtomicUsize> {
811 Arc::clone(&self.count)
812 }
813}
814
815fn saturating_fetch_add(counter: &AtomicUsize, amount: usize) {
820 let mut current = counter.load(Ordering::Acquire);
821 loop {
822 let Some(next) = current.checked_add(amount) else {
823 counter.store(usize::MAX, Ordering::Release);
824 return;
825 };
826 match counter.compare_exchange_weak(current, next, Ordering::AcqRel, Ordering::Acquire) {
827 Ok(_) => return,
828 Err(actual) => current = actual,
829 }
830 }
831}
832
833impl futures_core::Stream for CountingStream {
834 type Item = <BoxBytesStream as futures_core::Stream>::Item;
835
836 fn poll_next(
837 mut self: std::pin::Pin<&mut Self>,
838 cx: &mut std::task::Context<'_>,
839 ) -> std::task::Poll<Option<Self::Item>> {
840 use std::pin::Pin;
841
842 match Pin::new(&mut self.inner).poll_next(cx) {
843 std::task::Poll::Ready(Some(Ok(chunk))) => {
844 saturating_fetch_add(&self.count, chunk.len());
845 std::task::Poll::Ready(Some(Ok(chunk)))
846 }
847 other => other,
848 }
849 }
850}
851
852struct LimitingStream {
853 inner: BoxBytesStream,
854 limit: DecompressionLimit,
855 decoded_bytes: usize,
856 compressed_counter: Arc<AtomicUsize>,
857 limit_exceeded: Option<LimitExceededKind>,
858}
859
860#[derive(Debug, Clone, Copy, PartialEq, Eq)]
863enum LimitExceededKind {
864 Size,
865 Ratio,
866}
867
868impl LimitingStream {
869 fn new(
870 inner: BoxBytesStream,
871 limit: DecompressionLimit,
872 compressed_counter: Arc<AtomicUsize>,
873 ) -> Self {
874 Self {
875 inner,
876 limit,
877 decoded_bytes: 0,
878 compressed_counter,
879 limit_exceeded: None,
880 }
881 }
882
883 #[allow(clippy::cast_precision_loss)]
884 fn check_limit(&self) -> std::result::Result<(), LimitExceededKind> {
885 if let Some(max) = self.limit.max_decoded_body_size {
886 if self.decoded_bytes > max {
887 return Err(LimitExceededKind::Size);
888 }
889 }
890 if let Some(max_ratio) = self.limit.max_decompression_ratio {
891 if !max_ratio.is_finite() || max_ratio <= 0.0 {
892 return Err(LimitExceededKind::Ratio);
895 }
896 let compressed = self.compressed_counter.load(Ordering::Acquire);
897 if compressed == 0 {
898 if self.decoded_bytes > 0 {
902 return Err(LimitExceededKind::Ratio);
903 }
904 } else {
905 let ratio = self.decoded_bytes as f64 / compressed as f64;
906 if ratio > max_ratio {
907 return Err(LimitExceededKind::Ratio);
908 }
909 }
910 }
911 Ok(())
912 }
913
914 fn kind_to_error(kind: LimitExceededKind, limit: &DecompressionLimit) -> Error {
915 match kind {
916 LimitExceededKind::Size => Error::DecodedBodyTooLarge,
917 LimitExceededKind::Ratio => {
918 if limit
921 .max_decompression_ratio
922 .is_some_and(|r| !r.is_finite() || r <= 0.0)
923 {
924 Error::Decompression(
925 "invalid max_decompression_ratio: must be finite and positive".into(),
926 )
927 } else {
928 Error::DecompressionRatioExceeded
929 }
930 }
931 }
932 }
933}
934
935impl futures_core::Stream for LimitingStream {
936 type Item = Result<bytes::Bytes>;
937
938 fn poll_next(
939 mut self: std::pin::Pin<&mut Self>,
940 cx: &mut std::task::Context<'_>,
941 ) -> std::task::Poll<Option<Self::Item>> {
942 use std::pin::Pin;
943
944 if self.limit_exceeded.is_some() {
948 return std::task::Poll::Ready(None);
949 }
950
951 match Pin::new(&mut self.inner).poll_next(cx) {
952 std::task::Poll::Ready(Some(Ok(chunk))) => {
953 self.decoded_bytes = self.decoded_bytes.saturating_add(chunk.len());
957 if let Err(kind) = self.check_limit() {
958 self.limit_exceeded = Some(kind);
959 let err = Self::kind_to_error(kind, &self.limit);
960 return std::task::Poll::Ready(Some(Err(err)));
961 }
962 std::task::Poll::Ready(Some(Ok(chunk)))
963 }
964 std::task::Poll::Ready(Some(Err(e))) => std::task::Poll::Ready(Some(Err(e))),
965 std::task::Poll::Ready(None) => std::task::Poll::Ready(None),
966 std::task::Poll::Pending => std::task::Poll::Pending,
967 }
968 }
969}
970
971#[cfg(test)]
972mod tests {
973 use super::*;
974 use futures_util::FutureExt;
975 #[cfg(feature = "compression-gzip")]
976 use futures_util::StreamExt;
977
978 #[test]
979 fn content_coding_from_wire() {
980 assert_eq!(ContentCoding::from_wire("gzip"), Some(ContentCoding::Gzip));
981 assert_eq!(
982 ContentCoding::from_wire("x-gzip"),
983 Some(ContentCoding::Gzip)
984 );
985 assert_eq!(
986 ContentCoding::from_wire("deflate"),
987 Some(ContentCoding::Deflate)
988 );
989 assert_eq!(ContentCoding::from_wire("br"), Some(ContentCoding::Brotli));
990 assert_eq!(ContentCoding::from_wire("zstd"), Some(ContentCoding::Zstd));
991 assert_eq!(ContentCoding::from_wire("identity"), None);
992 assert_eq!(ContentCoding::from_wire("unknown"), None);
993 assert_eq!(ContentCoding::from_wire("GZIP"), Some(ContentCoding::Gzip));
994 assert_eq!(
995 ContentCoding::from_wire(" Br "),
996 Some(ContentCoding::Brotli)
997 );
998 }
999
1000 #[test]
1001 fn content_coding_as_str() {
1002 assert_eq!(ContentCoding::Gzip.as_str(), "gzip");
1003 assert_eq!(ContentCoding::Deflate.as_str(), "deflate");
1004 assert_eq!(ContentCoding::Brotli.as_str(), "br");
1005 assert_eq!(ContentCoding::Zstd.as_str(), "zstd");
1006 }
1007
1008 #[test]
1009 fn parse_content_encodings_single() {
1010 let encs = parse_content_encodings("gzip").unwrap();
1011 assert_eq!(encs, vec![ContentCoding::Gzip]);
1012 }
1013
1014 #[test]
1015 fn parse_content_encodings_multiple() {
1016 let encs = parse_content_encodings("gzip, br").unwrap();
1017 assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1018 }
1019
1020 #[test]
1021 fn parse_content_encodings_with_whitespace() {
1022 let encs = parse_content_encodings(" gzip , br ").unwrap();
1023 assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1024 }
1025
1026 #[test]
1027 fn parse_content_encodings_empty() {
1028 assert!(parse_content_encodings("").is_none());
1029 assert!(parse_content_encodings(" ").is_none());
1030 }
1031
1032 #[test]
1033 fn parse_content_encodings_unknown_filtered() {
1034 let encs = parse_content_encodings("gzip, identity, br").unwrap();
1035 assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1036 }
1037
1038 #[test]
1039 fn validate_content_encodings_supported() {
1040 assert!(validate_content_encodings("gzip").is_ok());
1041 assert!(validate_content_encodings("gzip, br").is_ok());
1042 assert!(validate_content_encodings("").is_ok());
1043 }
1044
1045 #[test]
1046 fn validate_content_encodings_accepts_identity() {
1047 assert!(validate_content_encodings("identity").is_ok());
1049 assert!(validate_content_encodings("IDENTITY").is_ok());
1050 assert!(validate_content_encodings("identity, gzip").is_ok());
1051 assert!(validate_content_encodings("gzip, identity").is_ok());
1052 }
1053
1054 #[test]
1055 fn parse_content_encodings_identity_only_is_none() {
1056 assert!(parse_content_encodings("identity").is_none());
1057 let encs = parse_content_encodings("gzip, identity, br").unwrap();
1058 assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1059 }
1060
1061 #[test]
1062 fn validate_content_encodings_unsupported() {
1063 let err = validate_content_encodings("gzip, weird").unwrap_err();
1064 assert_eq!(err.kind(), "unsupported_content_encoding");
1065 assert!(err.to_string().contains("weird"));
1066 }
1067
1068 #[test]
1069 fn accept_encoding_value_requires_compiled_features() {
1070 let _ = accept_encoding_value();
1071 }
1072
1073 #[cfg(not(any(
1074 feature = "compression-gzip",
1075 feature = "compression-deflate",
1076 feature = "compression-brotli",
1077 feature = "compression-zstd"
1078 )))]
1079 #[test]
1080 fn accept_encoding_value_none_without_compression_features() {
1081 assert!(
1082 accept_encoding_value().is_none(),
1083 "accept_encoding_value() should return None when no compression features are compiled"
1084 );
1085 }
1086
1087 #[cfg(all(
1088 feature = "compression-gzip",
1089 not(feature = "compression-brotli"),
1090 not(feature = "compression-zstd")
1091 ))]
1092 #[test]
1093 fn accept_encoding_value_gzip_only() {
1094 let val = accept_encoding_value().unwrap();
1095 assert!(val.contains("gzip"), "should contain gzip, got: {val}");
1096 assert!(
1097 !val.contains("br"),
1098 "should not contain br without brotli feature, got: {val}"
1099 );
1100 assert!(
1101 !val.contains("zstd"),
1102 "should not contain zstd without zstd feature, got: {val}"
1103 );
1104 }
1105
1106 #[test]
1107 fn accept_encoding_value_deterministic_order() {
1108 let val1 = accept_encoding_value();
1109 let val2 = accept_encoding_value();
1110 assert_eq!(
1111 val1, val2,
1112 "accept_encoding_value() should return the same value across calls"
1113 );
1114 }
1115
1116 #[test]
1117 fn accept_encoding_advertises_only_supported_decoders() {
1118 let Some(value) = accept_encoding_value() else {
1122 return;
1123 };
1124 for token in value.split(", ") {
1125 let coding =
1126 ContentCoding::from_wire(token).unwrap_or_else(|| panic!("invalid token {token}"));
1127 let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1128 let result = make_decoder(stream, coding);
1129 assert!(
1130 result.is_ok(),
1131 "advertised encoding '{token}' has no compiled-in decoder"
1132 );
1133 }
1134 }
1135
1136 #[test]
1137 fn decompress_stream_no_encoding_returns_original() {
1138 let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1139 let result = decompress_stream(stream, None, true, DecompressionLimit::new()).unwrap();
1140 drop(result);
1141 }
1142
1143 #[test]
1144 fn decompress_stream_disabled_returns_original() {
1145 let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1146 let result =
1147 decompress_stream(stream, Some("gzip"), false, DecompressionLimit::new()).unwrap();
1148 drop(result);
1149 }
1150
1151 #[test]
1152 fn decompress_stream_empty_header_returns_original() {
1153 let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1154 let result =
1155 decompress_stream(stream, Some(" "), true, DecompressionLimit::new()).unwrap();
1156 drop(result);
1157 }
1158
1159 #[test]
1160 fn decompress_buffered_identity_passthrough() {
1161 assert_eq!(
1164 &decompress_buffered(b"plain body", "identity", DecompressionLimit::new()).unwrap()[..],
1165 b"plain body"
1166 );
1167 let err = decompress_buffered(b"x", "weird", DecompressionLimit::new()).unwrap_err();
1168 assert_eq!(err.kind(), "unsupported_content_encoding");
1169 }
1170
1171 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1172 #[test]
1173 fn sync_decode_flate2_rejects_non_flate_encoding() {
1174 let err = sync_decode_flate2(&bytes::Bytes::new(), ContentCoding::Brotli, None, false)
1175 .unwrap_err();
1176 assert_eq!(err.kind(), "unsupported_content_encoding");
1177 assert!(err.to_string().contains("br"));
1178 }
1179
1180 #[test]
1181 fn decompression_limit_new_is_unlimited() {
1182 let limit = DecompressionLimit::new();
1183 assert!(limit.is_unlimited());
1184 assert!(limit.max_decoded_body_size.is_none());
1185 assert!(limit.max_decompression_ratio.is_none());
1186 }
1187
1188 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1189 #[test]
1190 fn decompression_limit_max_size_rejects() {
1191 let data = gzip_compress(b"hello world");
1192 let limit = DecompressionLimit {
1193 max_decoded_body_size: Some(5),
1194 max_decompression_ratio: None,
1195 };
1196 let err = decompress_buffered(&data, "gzip", limit).unwrap_err();
1197 assert_eq!(err.kind(), "decoded_body_too_large");
1198 }
1199
1200 #[cfg(feature = "compression-deflate")]
1201 #[test]
1202 fn deflate_buffered_decodes_zlib_wrapper_and_raw() {
1203 use std::io::Write;
1204 let plain = b"hello deflate world, hello again";
1205
1206 let mut zlib = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
1207 zlib.write_all(plain).unwrap();
1208 let zlib = zlib.finish().unwrap();
1209 assert!(is_zlib_header(&zlib));
1210 let result = decompress_buffered(&zlib, "deflate", DecompressionLimit::new()).unwrap();
1211 assert_eq!(&result[..], &plain[..]);
1212
1213 let mut raw =
1214 flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default());
1215 raw.write_all(plain).unwrap();
1216 let raw = raw.finish().unwrap();
1217 let result = decompress_buffered(&raw, "deflate", DecompressionLimit::new()).unwrap();
1218 assert_eq!(&result[..], &plain[..]);
1219 }
1220
1221 #[cfg(feature = "compression-deflate")]
1222 #[test]
1223 fn zlib_header_sniff_classifies_framing() {
1224 assert!(!is_zlib_header(&[]));
1225 assert!(!is_zlib_header(&[0x78]));
1226 assert!(!is_zlib_header(&[0x1f, 0x8b])); assert!(is_zlib_header(&[0x78, 0x9c, 0x03])); }
1229
1230 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1231 #[test]
1232 fn decompression_limit_max_size_allows_within() {
1233 let data = gzip_compress(b"hi");
1234 let limit = DecompressionLimit {
1235 max_decoded_body_size: Some(1024),
1236 max_decompression_ratio: None,
1237 };
1238 let result = decompress_buffered(&data, "gzip", limit).unwrap();
1239 assert_eq!(&result[..], b"hi");
1240 }
1241
1242 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1243 #[test]
1244 fn decompression_limit_ratio_rejects() {
1245 let data = gzip_compress(b"hello world, this is a test of compression");
1246 let limit = DecompressionLimit {
1247 max_decoded_body_size: None,
1248 max_decompression_ratio: Some(0.5),
1249 };
1250 let err = decompress_buffered(&data, "gzip", limit).unwrap_err();
1251 assert_eq!(err.kind(), "decompression_ratio_exceeded");
1252 }
1253
1254 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1255 #[test]
1256 fn decompression_limit_ratio_allows_within() {
1257 let data = gzip_compress(b"hello world, this is a test of compression");
1258 let limit = DecompressionLimit {
1259 max_decoded_body_size: None,
1260 max_decompression_ratio: Some(100.0),
1261 };
1262 let result = decompress_buffered(&data, "gzip", limit).unwrap();
1263 assert_eq!(&result[..], b"hello world, this is a test of compression");
1264 }
1265
1266 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1267 #[test]
1268 fn decompression_limit_unlimited_allows() {
1269 let data = gzip_compress(b"hello world");
1270 let limit = DecompressionLimit::new();
1271 let result = decompress_buffered(&data, "gzip", limit).unwrap();
1272 assert_eq!(&result[..], b"hello world");
1273 }
1274
1275 #[cfg(feature = "compression-gzip")]
1276 #[test]
1277 fn decompression_limit_ratio_enforced_per_layer() {
1278 let payload = vec![b'a'; 5000];
1284 let inner = gzip_compress(&payload);
1285 let outer = gzip_compress(&inner);
1286 #[allow(clippy::cast_precision_loss)]
1287 let (total_ratio, inner_step) = {
1288 (
1289 payload.len() as f64 / outer.len() as f64,
1290 payload.len() as f64 / inner.len() as f64,
1291 )
1292 };
1293 assert!(
1294 inner_step > total_ratio + 1.0,
1295 "expected the inner step to dominate: {inner_step} vs {total_ratio}"
1296 );
1297
1298 #[allow(clippy::cast_precision_loss)]
1299 let limit_value = total_ratio.midpoint(inner_step).ceil();
1300 let limit = DecompressionLimit {
1301 max_decoded_body_size: None,
1302 max_decompression_ratio: Some(limit_value),
1303 };
1304 let err = decompress_buffered(&outer, "gzip, gzip", limit).unwrap_err();
1305 assert_eq!(err.kind(), "decompression_ratio_exceeded");
1306
1307 let ok = decompress_buffered(&outer, "gzip, gzip", DecompressionLimit::default()).unwrap();
1309 assert_eq!(&ok[..], &payload[..]);
1310 }
1311
1312 #[cfg(feature = "compression-gzip")]
1313 #[tokio::test]
1314 async fn decompression_stream_enforces_ratio_per_layer() {
1315 let payload = vec![b'a'; 5000];
1316 let inner = gzip_compress(&payload);
1317 let outer = gzip_compress(&inner);
1318 #[allow(clippy::cast_precision_loss)]
1319 let limit_value = (payload.len() as f64 / outer.len() as f64)
1320 .midpoint(payload.len() as f64 / inner.len() as f64);
1321 let limit = DecompressionLimit {
1322 max_decoded_body_size: None,
1323 max_decompression_ratio: Some(limit_value.ceil()),
1324 };
1325 let input: BoxBytesStream = Box::pin(futures_util::stream::once(async move {
1326 Ok(bytes::Bytes::from(outer))
1327 }));
1328 let decoded = decompress_stream(input, Some("gzip, gzip"), true, limit).unwrap();
1329 let err = decoded
1330 .collect::<Vec<_>>()
1331 .await
1332 .into_iter()
1333 .find_map(std::result::Result::err)
1334 .expect("streaming ratio limit must reject the inner layer");
1335 assert_eq!(err.kind(), "decompression_ratio_exceeded");
1336 }
1337
1338 #[cfg(feature = "compression-gzip")]
1339 #[tokio::test]
1340 async fn decompression_stream_enforces_ratio_against_wire_size() {
1341 let payload: Vec<u8> = (0usize..5000)
1345 .map(|i| u8::try_from((i * 73 + i / 17) % 251).expect("value is below 251"))
1346 .collect();
1347 let inner = gzip_compress(&payload);
1348 let outer = gzip_compress(&inner);
1349 #[allow(clippy::cast_precision_loss)]
1350 let (first_layer, second_layer, total) = (
1351 inner.len() as f64 / outer.len() as f64,
1352 payload.len() as f64 / inner.len() as f64,
1353 payload.len() as f64 / outer.len() as f64,
1354 );
1355 let per_layer_limit = first_layer.max(second_layer);
1356 assert!(total > per_layer_limit);
1357 let limit = DecompressionLimit {
1358 max_decoded_body_size: None,
1359 max_decompression_ratio: Some(per_layer_limit.midpoint(total)),
1360 };
1361 let input: BoxBytesStream = Box::pin(futures_util::stream::once(async move {
1362 Ok(bytes::Bytes::from(outer))
1363 }));
1364 let decoded = decompress_stream(input, Some("gzip, gzip"), true, limit).unwrap();
1365 let err = decoded
1366 .collect::<Vec<_>>()
1367 .await
1368 .into_iter()
1369 .find_map(std::result::Result::err)
1370 .expect("streaming ratio limit must include the wire size");
1371 assert_eq!(err.kind(), "decompression_ratio_exceeded");
1372 }
1373
1374 #[test]
1375 fn limiting_stream_enforces_max_decoded_body_size() {
1376 let limit = DecompressionLimit {
1377 max_decoded_body_size: Some(3),
1378 max_decompression_ratio: None,
1379 };
1380 let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![
1381 Ok(bytes::Bytes::from("ab")),
1382 Ok(bytes::Bytes::from("cd")),
1383 ]));
1384 let counter = Arc::new(AtomicUsize::new(0));
1385 let mut limited = LimitingStream::new(stream, limit, counter);
1386
1387 let chunk1 = futures_util::StreamExt::next(&mut limited)
1388 .now_or_never()
1389 .unwrap()
1390 .unwrap()
1391 .unwrap();
1392 assert_eq!(chunk1, "ab");
1393
1394 let err = futures_util::StreamExt::next(&mut limited)
1395 .now_or_never()
1396 .unwrap()
1397 .unwrap()
1398 .unwrap_err();
1399 assert_eq!(err.kind(), "decoded_body_too_large");
1400 }
1401
1402 #[test]
1403 fn limiting_stream_allows_within_limit() {
1404 let limit = DecompressionLimit {
1405 max_decoded_body_size: Some(1024),
1406 max_decompression_ratio: None,
1407 };
1408 let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![
1409 Ok(bytes::Bytes::from("a")),
1410 Ok(bytes::Bytes::from("b")),
1411 ]));
1412 let counter = Arc::new(AtomicUsize::new(0));
1413 let mut limited = LimitingStream::new(stream, limit, counter);
1414
1415 let chunk1 = futures_util::StreamExt::next(&mut limited)
1416 .now_or_never()
1417 .unwrap()
1418 .unwrap()
1419 .unwrap();
1420 assert_eq!(chunk1, "a");
1421 let chunk2 = futures_util::StreamExt::next(&mut limited)
1422 .now_or_never()
1423 .unwrap()
1424 .unwrap()
1425 .unwrap();
1426 assert_eq!(chunk2, "b");
1427 }
1428
1429 #[test]
1430 fn saturating_fetch_add_saturates_instead_of_wrapping() {
1431 let counter = AtomicUsize::new(usize::MAX - 2);
1432 saturating_fetch_add(&counter, 1);
1433 assert_eq!(counter.load(Ordering::Relaxed), usize::MAX - 1);
1434 saturating_fetch_add(&counter, 5);
1435 assert_eq!(counter.load(Ordering::Relaxed), usize::MAX);
1436 saturating_fetch_add(&counter, 5);
1438 assert_eq!(counter.load(Ordering::Relaxed), usize::MAX);
1439 }
1440
1441 #[test]
1442 fn limiting_stream_saturated_counter_still_enforces_size_limit() {
1443 let limit = DecompressionLimit {
1444 max_decoded_body_size: Some(10),
1445 max_decompression_ratio: None,
1446 };
1447 let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![Ok(
1448 bytes::Bytes::from("x"),
1449 )]));
1450 let counter = Arc::new(AtomicUsize::new(0));
1451 let mut limited = LimitingStream::new(stream, limit, counter);
1452 limited.decoded_bytes = usize::MAX;
1456 let err = futures_util::StreamExt::next(&mut limited)
1457 .now_or_never()
1458 .unwrap()
1459 .unwrap()
1460 .unwrap_err();
1461 assert_eq!(err.kind(), "decoded_body_too_large");
1462 }
1463
1464 #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1465 fn gzip_compress(data: &[u8]) -> Vec<u8> {
1466 use flate2::write::GzEncoder;
1467 use flate2::Compression;
1468 use std::io::Write;
1469
1470 let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
1471 encoder.write_all(data).unwrap();
1472 encoder.finish().unwrap()
1473 }
1474}