Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SSE compression exclusion within default predicate #465

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading