Skip to content

Commit

Permalink
compression: Exclude SSE from compression by default
Browse files Browse the repository at this point in the history
  • Loading branch information
BenJeau authored Feb 12, 2024
1 parent 49ead0f commit ad25019
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions tower-http/src/compression/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ where
///
/// - They're gRPC, which has its own protocol specific compression scheme.
/// - It's an image as determined by the `content-type` starting with `image/`.
/// - They're Server-Sent Events (SSE) as determined by the `content-type` being `text/event-stream`.
/// - The response is less than 32 bytes.
///
/// # Configuring the defaults
Expand All @@ -113,14 +114,17 @@ where
/// [`Compression`]: super::Compression
/// [`CompressionLayer`]: super::CompressionLayer
#[derive(Clone)]
pub struct DefaultPredicate(And<And<SizeAbove, NotForContentType>, NotForContentType>);
pub struct DefaultPredicate(
And<And<And<SizeAbove, NotForContentType>, NotForContentType>, NotForContentType>,
);

impl DefaultPredicate {
/// Create a new `DefaultPredicate`.
pub fn new() -> Self {
let inner = SizeAbove::new(SizeAbove::DEFAULT_MIN_SIZE)
.and(NotForContentType::GRPC)
.and(NotForContentType::IMAGES);
.and(NotForContentType::IMAGES)
.and(NotForContentType::SSE);
Self(inner)
}
}
Expand Down Expand Up @@ -200,6 +204,9 @@ impl NotForContentType {
exception: Some(Str::Static("image/svg+xml")),
};

/// Predicate that wont compress Server-Sent Events (SSE) responses.
pub const SSE: Self = Self::const_new("text/event-stream");

/// Create a new `NotForContentType`.
pub fn new(content_type: &str) -> Self {
Self {
Expand Down

0 comments on commit ad25019

Please sign in to comment.