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

Optimize encoding selection for compression middleware #471

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
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
22 changes: 11 additions & 11 deletions tower-http/src/content_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Encoding {
headers: &http::HeaderMap,
supported_encoding: impl SupportedEncodings,
) -> Self {
Encoding::preferred_encoding(&encodings(headers, supported_encoding))
Encoding::preferred_encoding(encodings(headers, supported_encoding))
.unwrap_or(Encoding::Identity)
}

Expand All @@ -111,12 +111,13 @@ impl Encoding {
feature = "compression-deflate",
feature = "fs",
))]
pub(crate) fn preferred_encoding(accepted_encodings: &[(Encoding, QValue)]) -> Option<Self> {
pub(crate) fn preferred_encoding(
accepted_encodings: impl Iterator<Item = (Encoding, QValue)>,
) -> Option<Self> {
accepted_encodings
.iter()
.filter(|(_, qvalue)| qvalue.0 > 0)
.max_by_key(|(encoding, qvalue)| (qvalue, encoding))
.map(|(encoding, _)| *encoding)
.max_by_key(|&(encoding, qvalue)| (qvalue, encoding))
.map(|(encoding, _)| encoding)
}
}

Expand Down Expand Up @@ -212,16 +213,16 @@ impl QValue {
feature = "fs",
))]
// based on https://github.com/http-rs/accept-encoding
pub(crate) fn encodings(
headers: &http::HeaderMap,
supported_encoding: impl SupportedEncodings,
) -> Vec<(Encoding, QValue)> {
pub(crate) fn encodings<'a>(
headers: &'a http::HeaderMap,
supported_encoding: impl SupportedEncodings + 'a,
) -> impl Iterator<Item = (Encoding, QValue)> + 'a {
headers
.get_all(http::header::ACCEPT_ENCODING)
.iter()
.filter_map(|hval| hval.to_str().ok())
.flat_map(|s| s.split(','))
.filter_map(|v| {
.filter_map(move |v| {
let mut v = v.splitn(2, ';');

let encoding = match Encoding::parse(v.next().unwrap().trim(), supported_encoding) {
Expand All @@ -237,7 +238,6 @@ pub(crate) fn encodings(

Some((encoding, qval))
})
.collect::<Vec<(Encoding, QValue)>>()
}

#[cfg(all(
Expand Down
5 changes: 3 additions & 2 deletions tower-http/src/services/fs/serve_dir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,11 @@ impl<F> ServeDir<F> {
.and_then(|value| value.to_str().ok())
.map(|s| s.to_owned());

let negotiated_encodings = encodings(
let negotiated_encodings: Vec<_> = encodings(
req.headers(),
self.precompressed_variants.unwrap_or_default(),
);
)
.collect();

let variant = self.variant.clone();

Expand Down
2 changes: 1 addition & 1 deletion tower-http/src/services/fs/serve_dir/open_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ fn preferred_encoding(
path: &mut PathBuf,
negotiated_encoding: &[(Encoding, QValue)],
) -> Option<Encoding> {
let preferred_encoding = Encoding::preferred_encoding(negotiated_encoding);
let preferred_encoding = Encoding::preferred_encoding(negotiated_encoding.iter().copied());

if let Some(file_extension) =
preferred_encoding.and_then(|encoding| encoding.to_file_extension())
Expand Down
Loading