#[cfg(feature = "compression")]
use std::convert::Infallible;
#[cfg(feature = "compression")]
use std::io::Write as _;
#[cfg(feature = "compression")]
use std::pin::Pin;
#[cfg(feature = "compression")]
use std::task::{Context, Poll};
#[cfg(feature = "compression")]
use http_body_util::BodyExt as _;
#[cfg(feature = "compression")]
use http_body_util::combinators::BoxBody;
#[cfg(feature = "compression")]
use hyper::StatusCode;
#[cfg(feature = "compression")]
use hyper::body::{Body, Bytes, Frame, SizeHint};
use hyper::header::HeaderValue;
#[cfg(feature = "compression")]
use hyper::header::{self};
use crate::config::CompressionConfig;
use crate::handler::HttpResponse;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Encoding {
Brotli,
Gzip,
}
impl Encoding {
pub fn token(self) -> &'static str {
match self {
Encoding::Brotli => "br",
Encoding::Gzip => "gzip",
}
}
pub(crate) fn extension(self) -> &'static str {
match self {
Encoding::Brotli => "br",
Encoding::Gzip => "gz",
}
}
pub fn from_token(token: &str) -> Option<Self> {
match token {
"br" => Some(Encoding::Brotli),
"gzip" => Some(Encoding::Gzip),
_ => None,
}
}
fn from_token_ignore_ascii_case(token: &str) -> Option<Self> {
if token.eq_ignore_ascii_case("br") {
Some(Encoding::Brotli)
} else if token.eq_ignore_ascii_case("gzip") {
Some(Encoding::Gzip)
} else {
None
}
}
}
#[derive(Debug)]
pub struct Compression {
algorithms: Vec<Encoding>,
#[cfg(feature = "compression")]
enabled: bool,
#[cfg(feature = "compression")]
min_size: u64,
#[cfg(feature = "compression")]
types: Vec<String>,
}
#[cfg(feature = "compression")]
const INCOMPRESSIBLE: &[&str] = &[
"image/",
"video/",
"audio/",
"font/woff",
"application/zip",
"application/gzip",
"application/x-brotli",
"application/wasm",
];
impl Compression {
pub(crate) fn new(cfg: &CompressionConfig) -> Self {
Self {
algorithms: cfg
.algorithms
.iter()
.filter_map(|name| Encoding::from_token(name))
.collect(),
#[cfg(feature = "compression")]
enabled: cfg.enabled,
#[cfg(feature = "compression")]
min_size: cfg.min_size,
#[cfg(feature = "compression")]
types: cfg.types.iter().map(|t| t.to_ascii_lowercase()).collect(),
}
}
#[doc(hidden)]
pub fn negotiator_for_fuzzing(algorithms: Vec<Encoding>) -> Self {
Self {
algorithms,
#[cfg(feature = "compression")]
enabled: false,
#[cfg(feature = "compression")]
min_size: 0,
#[cfg(feature = "compression")]
types: Vec::new(),
}
}
pub(crate) fn negotiates(&self) -> bool {
#[cfg(feature = "compression")]
{
self.enabled
}
#[cfg(not(feature = "compression"))]
{
false
}
}
pub fn negotiate(&self, accept_encoding: Option<&HeaderValue>) -> Option<Encoding> {
let value = accept_encoding?.to_str().ok()?;
self.algorithms
.iter()
.copied()
.find(|enc| accept_encoding_entries(value).any(|(token, q)| q > 0.0 && token == *enc))
}
#[cfg(not(feature = "compression"))]
pub(crate) fn apply(&self, resp: HttpResponse, _encoding: Option<Encoding>) -> HttpResponse {
resp
}
#[cfg(feature = "compression")]
pub(crate) fn apply(&self, mut resp: HttpResponse, encoding: Option<Encoding>) -> HttpResponse {
let Some(encoding) = encoding.filter(|_| self.enabled) else {
return resp;
};
if !self.should_compress(&resp) {
return resp;
}
let headers = resp.headers_mut();
headers.remove(header::CONTENT_LENGTH);
headers.remove(header::ACCEPT_RANGES);
headers.insert(
header::CONTENT_ENCODING,
HeaderValue::from_static(encoding.token()),
);
crate::cors::append_vary(headers, "accept-encoding");
if let Some(etag) = headers.get(header::ETAG).and_then(|v| v.to_str().ok()) {
let weakened = weaken_etag(etag, encoding.token());
if let Ok(value) = HeaderValue::from_str(&weakened) {
headers.insert(header::ETAG, value);
}
}
let (parts, body) = resp.into_parts();
let body = CompressedBody::new(body, encoding).boxed();
HttpResponse::from_parts(parts, body)
}
#[cfg(feature = "compression")]
fn should_compress(&self, resp: &HttpResponse) -> bool {
let status = resp.status();
if status == StatusCode::NO_CONTENT
|| status == StatusCode::NOT_MODIFIED
|| status == StatusCode::PARTIAL_CONTENT
|| status.is_informational()
{
return false;
}
let headers = resp.headers();
if headers.contains_key(header::CONTENT_ENCODING) {
return false;
}
if headers
.get_all(header::CACHE_CONTROL)
.iter()
.filter_map(|v| v.to_str().ok())
.flat_map(|v| v.split(','))
.any(|directive| directive.trim().eq_ignore_ascii_case("no-transform"))
{
return false;
}
let declared = headers
.get(header::CONTENT_LENGTH)
.and_then(|v| v.to_str().ok())
.and_then(|v| v.parse::<u64>().ok());
if let Some(len) = declared.or_else(|| resp.body().size_hint().exact())
&& len < self.min_size
{
return false;
}
let content_type = headers
.get(header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or_default()
.split(';')
.next()
.unwrap_or_default()
.trim();
if content_type.is_empty() {
return false;
}
if self
.types
.iter()
.any(|pattern| !pattern.ends_with('*') && pattern.eq_ignore_ascii_case(content_type))
{
return true;
}
if INCOMPRESSIBLE
.iter()
.any(|p| starts_with_ignore_ascii_case(content_type, p))
{
return false;
}
self.types.iter().any(|pattern| {
pattern
.strip_suffix('*')
.is_some_and(|prefix| starts_with_ignore_ascii_case(content_type, prefix))
})
}
}
#[cfg(feature = "compression")]
fn starts_with_ignore_ascii_case(haystack: &str, prefix: &str) -> bool {
haystack
.as_bytes()
.get(..prefix.len())
.is_some_and(|head| head.eq_ignore_ascii_case(prefix.as_bytes()))
}
#[cfg(feature = "compression")]
impl Compression {}
#[cfg(feature = "compression")]
pub(crate) fn weaken_etag(etag: &str, token: &str) -> String {
let strong = etag.trim_start_matches("W/");
match strong.strip_prefix('"').and_then(|s| s.strip_suffix('"')) {
Some(inner) => format!("W/\"{inner}-{token}\""),
None => format!("W/\"{}-{token}\"", strong.trim_matches('"')),
}
}
pub fn parse_accept_encoding(value: &str) -> Vec<(Encoding, f32)> {
accept_encoding_entries(value).collect()
}
fn accept_encoding_entries(value: &str) -> impl Iterator<Item = (Encoding, f32)> + '_ {
value.split(',').filter_map(|entry| {
let mut parts = entry.split(';');
let encoding = Encoding::from_token_ignore_ascii_case(parts.next()?.trim())?;
let q = parts
.find_map(|p| p.trim().strip_prefix("q=")?.parse::<f32>().ok())
.unwrap_or(1.0);
Some((encoding, q))
})
}
#[cfg(feature = "compression")]
mod encoder {
use super::*;
pub(super) struct CompressedBody {
inner: BoxBody<Bytes, Infallible>,
encoder: Option<Encoder>,
}
pub(super) enum Encoder {
Brotli(Box<brotli::CompressorWriter<Vec<u8>>>),
Gzip(Box<flate2::write::GzEncoder<Vec<u8>>>),
}
impl CompressedBody {
pub(super) fn new(inner: BoxBody<Bytes, Infallible>, encoding: Encoding) -> Self {
let encoder = match encoding {
Encoding::Brotli => Encoder::Brotli(Box::new(brotli::CompressorWriter::new(
Vec::new(),
32 * 1024,
4,
22,
))),
Encoding::Gzip => Encoder::Gzip(Box::new(flate2::write::GzEncoder::new(
Vec::new(),
flate2::Compression::fast(),
))),
};
Self {
inner,
encoder: Some(encoder),
}
}
fn write(&mut self, data: &[u8]) -> std::io::Result<Bytes> {
let Some(encoder) = self.encoder.as_mut() else {
return Ok(Bytes::new());
};
match encoder {
Encoder::Brotli(w) => {
w.write_all(data)?;
w.flush()?;
Ok(Bytes::from(std::mem::take(w.get_mut())))
}
Encoder::Gzip(w) => {
w.write_all(data)?;
w.flush()?;
Ok(Bytes::from(std::mem::take(w.get_mut())))
}
}
}
fn finish(&mut self) -> std::io::Result<Bytes> {
match self.encoder.take() {
None => Ok(Bytes::new()),
Some(Encoder::Brotli(w)) => Ok(Bytes::from(w.into_inner())),
Some(Encoder::Gzip(w)) => Ok(Bytes::from(w.finish()?)),
}
}
}
impl Body for CompressedBody {
type Data = Bytes;
type Error = Infallible;
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<std::result::Result<Frame<Bytes>, Infallible>>> {
let this = self.get_mut();
loop {
if this.encoder.is_none() {
return Poll::Ready(None);
}
match Pin::new(&mut this.inner).poll_frame(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Some(Ok(frame))) => {
let Some(data) = frame.data_ref() else {
return Poll::Ready(Some(Ok(frame)));
};
match this.write(data) {
Ok(out) if out.is_empty() => continue,
Ok(out) => return Poll::Ready(Some(Ok(Frame::data(out)))),
Err(err) => {
tracing::error!("response compression failed: {err}");
this.encoder = None;
return Poll::Ready(None);
}
}
}
Poll::Ready(Some(Err(never))) => match never {},
Poll::Ready(None) => {
return match this.finish() {
Ok(tail) if tail.is_empty() => Poll::Ready(None),
Ok(tail) => Poll::Ready(Some(Ok(Frame::data(tail)))),
Err(err) => {
tracing::error!("response compression failed to finish: {err}");
Poll::Ready(None)
}
};
}
}
}
}
fn size_hint(&self) -> SizeHint {
SizeHint::default()
}
}
}
#[cfg(feature = "compression")]
use encoder::CompressedBody;
#[cfg(all(test, feature = "compression"))]
mod tests {
use super::*;
use http_body_util::Full;
fn policy(enabled: bool) -> Compression {
Compression::new(&CompressionConfig {
enabled,
..Default::default()
})
}
fn response(status: StatusCode, content_type: &str, body: &'static [u8]) -> HttpResponse {
hyper::Response::builder()
.status(status)
.header(header::CONTENT_TYPE, content_type)
.header(header::CONTENT_LENGTH, body.len())
.body(Full::new(Bytes::from_static(body)).boxed())
.expect("response")
}
async fn collect(resp: HttpResponse) -> Vec<u8> {
resp.into_body()
.collect()
.await
.expect("body")
.to_bytes()
.to_vec()
}
const BIG_JSON: &[u8] = &[b'{'; 4096];
#[test]
fn negotiation_follows_server_preference() {
let p = policy(true);
let both = HeaderValue::from_static("gzip, br");
assert_eq!(p.negotiate(Some(&both)), Some(Encoding::Brotli));
let gzip_only = HeaderValue::from_static("gzip");
assert_eq!(p.negotiate(Some(&gzip_only)), Some(Encoding::Gzip));
let refused = HeaderValue::from_static("br;q=0, gzip;q=0");
assert_eq!(p.negotiate(Some(&refused)), None);
assert_eq!(p.negotiate(None), None);
let unknown = HeaderValue::from_static("zstd");
assert_eq!(p.negotiate(Some(&unknown)), None);
}
#[tokio::test]
async fn gzip_round_trips_and_sets_the_headers() {
use std::io::Read as _;
let p = policy(true);
let resp = p.apply(
response(StatusCode::OK, "application/json", BIG_JSON),
Some(Encoding::Gzip),
);
assert_eq!(resp.headers()[header::CONTENT_ENCODING], "gzip");
assert_eq!(resp.headers()[header::VARY], "accept-encoding");
assert!(!resp.headers().contains_key(header::CONTENT_LENGTH));
let compressed = collect(resp).await;
assert!(
compressed.len() < BIG_JSON.len(),
"compression must shrink it"
);
let mut out = Vec::new();
flate2::read::GzDecoder::new(&compressed[..])
.read_to_end(&mut out)
.expect("decode");
assert_eq!(out, BIG_JSON);
}
#[tokio::test]
async fn brotli_round_trips() {
let p = policy(true);
let resp = p.apply(
response(StatusCode::OK, "text/html", BIG_JSON),
Some(Encoding::Brotli),
);
assert_eq!(resp.headers()[header::CONTENT_ENCODING], "br");
let compressed = collect(resp).await;
let mut out = Vec::new();
brotli::BrotliDecompress(&mut &compressed[..], &mut out).expect("decode");
assert_eq!(out, BIG_JSON);
}
#[tokio::test]
async fn a_listed_type_wins_over_its_incompressible_family() {
let p = policy(true);
let resp = response(StatusCode::OK, "image/svg+xml", BIG_JSON);
assert!(p.should_compress(&resp), "svg is in the default list");
let resp = response(StatusCode::OK, "image/png", BIG_JSON);
assert!(!p.should_compress(&resp), "png is not");
}
#[tokio::test]
async fn the_untouched_cases_stay_untouched() {
let p = policy(true);
let cases: Vec<(&str, HttpResponse)> = vec![
(
"small body",
response(StatusCode::OK, "text/plain", b"tiny"),
),
(
"incompressible type",
response(StatusCode::OK, "image/png", BIG_JSON),
),
(
"unlisted type",
response(StatusCode::OK, "application/octet-stream", BIG_JSON),
),
(
"partial content",
response(StatusCode::PARTIAL_CONTENT, "text/plain", BIG_JSON),
),
(
"not modified",
response(StatusCode::NOT_MODIFIED, "text/plain", BIG_JSON),
),
];
for (what, resp) in cases {
let out = p.apply(resp, Some(Encoding::Gzip));
assert!(
!out.headers().contains_key(header::CONTENT_ENCODING),
"{what} must not be compressed"
);
}
let off = policy(false);
let out = off.apply(
response(StatusCode::OK, "application/json", BIG_JSON),
Some(Encoding::Gzip),
);
assert!(!out.headers().contains_key(header::CONTENT_ENCODING));
}
#[tokio::test]
async fn an_already_encoded_response_is_left_alone() {
let p = policy(true);
let mut resp = response(StatusCode::OK, "application/json", BIG_JSON);
resp.headers_mut()
.insert(header::CONTENT_ENCODING, HeaderValue::from_static("br"));
let out = p.apply(resp, Some(Encoding::Gzip));
assert_eq!(out.headers()[header::CONTENT_ENCODING], "br");
}
#[tokio::test]
async fn compressing_weakens_the_etag() {
let p = policy(true);
let mut resp = response(StatusCode::OK, "application/json", BIG_JSON);
resp.headers_mut()
.insert(header::ETAG, HeaderValue::from_static("\"abc\""));
let out = p.apply(resp, Some(Encoding::Gzip));
assert_eq!(out.headers()[header::ETAG], "W/\"abc-gzip\"");
}
}