use actix_web::{
Error,
body::MessageBody,
dev::{ServiceRequest, ServiceResponse},
http::{
StatusCode,
header::{self, HeaderMap, HeaderValue},
},
middleware::Next,
mime::{self, Mime},
};
pub async fn vary_accept_encoding(
req: ServiceRequest,
next: Next<impl MessageBody>,
) -> Result<ServiceResponse<impl MessageBody>, Error> {
let mut res = next.call(req).await?;
if compressible(&res) && !varies_on_encoding(res.headers()) {
res.headers_mut()
.append(header::VARY, HeaderValue::from_static("accept-encoding"));
}
Ok(res)
}
fn compressible<B>(res: &ServiceResponse<B>) -> bool {
if matches!(
res.status(),
StatusCode::SWITCHING_PROTOCOLS | StatusCode::NO_CONTENT
) || res.headers().contains_key(header::CONTENT_ENCODING)
{
return false;
}
let content_type = res
.headers()
.get(header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.parse::<Mime>().ok());
match content_type {
Some(mime) if mime.type_() == mime::IMAGE => mime.subtype() == mime::SVG,
Some(mime) => mime.type_() != mime::VIDEO,
None => true,
}
}
fn varies_on_encoding(headers: &HeaderMap) -> bool {
headers
.get_all(header::VARY)
.filter_map(|value| value.to_str().ok())
.flat_map(|value| value.split(','))
.map(str::trim)
.any(|name| name == "*" || name.eq_ignore_ascii_case("accept-encoding"))
}
#[cfg(test)]
mod tests {
use actix_web::{
App, HttpResponse,
http::{StatusCode, header},
middleware::{Compress, from_fn},
test, web,
};
use super::vary_accept_encoding;
async fn get(uri: &str, accept_encoding: Option<&str>) -> (Vec<String>, Option<String>) {
let respond = |status: StatusCode, content_type: &'static str| {
move || async move {
HttpResponse::build(status)
.content_type(content_type)
.body("<p>Some content</p>")
}
};
let app = test::init_service(
App::new()
.wrap(Compress::default())
.wrap(from_fn(vary_accept_encoding))
.route("/page", web::get().to(respond(StatusCode::OK, "text/html")))
.route(
"/page-revalidated",
web::get().to(|| async {
HttpResponse::NotModified()
.content_type("text/html")
.finish()
}),
)
.route(
"/page-range",
web::get().to(respond(StatusCode::PARTIAL_CONTENT, "text/html")),
)
.route(
"/logo.png",
web::get().to(respond(StatusCode::OK, "image/png")),
)
.route(
"/logo.svg",
web::get().to(respond(StatusCode::OK, "image/svg+xml")),
)
.route(
"/intro.mp4",
web::get().to(respond(StatusCode::OK, "video/mp4")),
)
.route(
"/proxied",
web::get().to(|| async {
HttpResponse::Ok()
.content_type("text/html")
.insert_header((header::CONTENT_ENCODING, "gzip"))
.body("gzipped upstream")
}),
)
.route(
"/per-origin",
web::get().to(|| async {
HttpResponse::Ok()
.content_type("application/json")
.insert_header((header::VARY, "Origin"))
.body("{}")
}),
)
.route(
"/nothing",
web::get().to(|| async { HttpResponse::NoContent().finish() }),
),
)
.await;
let mut req = test::TestRequest::get().uri(uri);
if let Some(accept_encoding) = accept_encoding {
req = req.insert_header((header::ACCEPT_ENCODING, accept_encoding));
}
let res = test::call_service(&app, req.to_request()).await;
let header = |name| {
res.headers()
.get_all(name)
.map(|value| String::from_utf8_lossy(value.as_bytes()).into_owned())
.collect::<Vec<_>>()
};
(header(header::VARY), header(header::CONTENT_ENCODING).pop())
}
#[actix_web::test]
async fn compressed_or_not_the_response_depends_on_accept_encoding() {
let vary = |values: &[&str]| values.iter().map(|value| value.to_string()).collect();
for accept_encoding in [None, Some("identity"), Some("gzip;q=0")] {
assert_eq!(
get("/page", accept_encoding).await,
(vary(&["accept-encoding"]), None),
"{accept_encoding:?}"
);
}
assert_eq!(
get("/page", Some("gzip")).await,
(vary(&["accept-encoding"]), Some("gzip".to_string()))
);
assert_eq!(
get("/page-revalidated", None).await,
(vary(&["accept-encoding"]), None)
);
assert_eq!(
get("/page-range", Some("gzip")).await,
(vary(&["accept-encoding"]), None)
);
assert_eq!(
get("/logo.svg", None).await,
(vary(&["accept-encoding"]), None)
);
assert_eq!(
get("/per-origin", None).await,
(vary(&["Origin", "accept-encoding"]), None)
);
assert_eq!(
get("/per-origin", Some("gzip")).await,
(
vary(&["Origin", "accept-encoding"]),
Some("gzip".to_string())
)
);
}
#[actix_web::test]
async fn never_compressed_is_left_as_it_is() {
for uri in ["/logo.png", "/intro.mp4", "/nothing"] {
for accept_encoding in [None, Some("gzip")] {
assert_eq!(
get(uri, accept_encoding).await,
(vec![], None),
"{uri} {accept_encoding:?}"
);
}
}
assert_eq!(
get("/proxied", Some("gzip")).await,
(vec![], Some("gzip".to_string()))
);
}
}