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

Prefer compression encodings: brotli > gzip > deflate #325

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 16 additions & 20 deletions tower-http/src/content_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ pub(crate) trait SupportedEncodings: Copy {
fn br(&self) -> bool;
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
// This enum's variants are ordered from least to most preferred.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub(crate) enum Encoding {
#[cfg(any(feature = "fs", feature = "compression-gzip"))]
Gzip,
#[allow(dead_code)]
Identity,
#[cfg(any(feature = "fs", feature = "compression-deflate"))]
Deflate,
#[cfg(any(feature = "fs", feature = "compression-gzip"))]
Gzip,
#[cfg(any(feature = "fs", feature = "compression-br"))]
Brotli,
#[allow(dead_code)]
Identity,
}

impl Encoding {
Expand Down Expand Up @@ -95,16 +96,11 @@ impl Encoding {
feature = "fs",
))]
pub(crate) fn preferred_encoding(accepted_encodings: &[(Encoding, QValue)]) -> Option<Self> {
let mut preferred_encoding = None;
let mut max_qval = 0;

for (encoding, qval) in accepted_encodings {
if qval.0 > max_qval {
preferred_encoding = Some(*encoding);
max_qval = qval.0;
}
}
preferred_encoding
accepted_encodings
.iter()
.filter(|(_, qvalue)| qvalue.0 > 0)
.max_by_key(|(encoding, qvalue)| (qvalue, encoding))
.map(|(encoding, _)| *encoding)
}
}

Expand Down Expand Up @@ -277,7 +273,7 @@ mod tests {
http::HeaderValue::from_static("gzip,br"),
);
let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default());
assert_eq!(Encoding::Gzip, encoding);
assert_eq!(Encoding::Brotli, encoding);
}

#[test]
Expand All @@ -288,7 +284,7 @@ mod tests {
http::HeaderValue::from_static("gzip,deflate,br"),
);
let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default());
assert_eq!(Encoding::Gzip, encoding);
assert_eq!(Encoding::Brotli, encoding);
}

#[test]
Expand All @@ -310,7 +306,7 @@ mod tests {
http::HeaderValue::from_static("gzip;q=0.5,deflate,br"),
);
let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default());
assert_eq!(Encoding::Deflate, encoding);
assert_eq!(Encoding::Brotli, encoding);
}

#[test]
Expand Down Expand Up @@ -340,7 +336,7 @@ mod tests {
http::HeaderValue::from_static("br"),
);
let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default());
assert_eq!(Encoding::Deflate, encoding);
assert_eq!(Encoding::Brotli, encoding);
}

#[test]
Expand All @@ -359,7 +355,7 @@ mod tests {
http::HeaderValue::from_static("br"),
);
let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default());
assert_eq!(Encoding::Deflate, encoding);
assert_eq!(Encoding::Brotli, encoding);
}

#[test]
Expand Down