Skip to content

Commit

Permalink
Prefer encoding: brotli > gzip > deflate
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Jan 26, 2023
1 parent e941dee commit cec4ed2
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions tower-http/src/content_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ pub(crate) trait SupportedEncodings: Copy {
fn br(&self) -> bool;
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[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 @@ -102,8 +102,15 @@ impl Encoding {
if qval.0 > max_qval {
preferred_encoding = Some(*encoding);
max_qval = qval.0;
} else if qval.0 == max_qval && max_qval > 0 {
preferred_encoding = Some(
preferred_encoding
.map(|old| old.max(*encoding))
.unwrap_or(*encoding),
);
}
}

preferred_encoding
}
}
Expand Down Expand Up @@ -277,7 +284,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 +295,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 +317,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 +347,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 +366,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

0 comments on commit cec4ed2

Please sign in to comment.