use std::collections::VecDeque;
use std::convert::Infallible;
use std::io::Read;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::task::Waker;
use bytes::Bytes;
use http::Request;
use http::header::{ACCEPT_RANGES, CACHE_CONTROL, CONTENT_LENGTH, CONTENT_RANGE, ETAG};
use http_body::{Body as HttpBody, Frame, SizeHint};
use http_body_util::BodyExt;
use tower::{ServiceBuilder, ServiceExt, service_fn};
use super::*;
use crate::body::Body;
fn long(text: &str) -> String {
text.repeat(64)
}
fn request(accept: &[&str]) -> Request<Body> {
let mut builder = Request::builder().uri("/");
for value in accept {
builder = builder.header(ACCEPT_ENCODING, *value);
}
builder.body(Body::empty()).unwrap()
}
async fn send(accept: &[&str], response: Response<Body>) -> Response<Body> {
let response = std::sync::Mutex::new(Some(response));
let inner = service_fn(move |_: Request<Body>| {
let response = response.lock().unwrap().take().expect("one call");
async move { Ok::<_, Infallible>(response) }
});
ServiceBuilder::new()
.layer(CompressionLayer::new())
.service(inner)
.oneshot(request(accept))
.await
.unwrap()
}
fn text(body: impl Into<Body>) -> Response<Body> {
Response::builder()
.header(CONTENT_TYPE, "text/plain")
.body(body.into())
.unwrap()
}
fn gzipped(response: &Response<Body>) -> bool {
response
.headers()
.get(CONTENT_ENCODING)
.map(HeaderValue::as_bytes)
== Some(b"gzip")
}
fn gunzip(bytes: &[u8]) -> Vec<u8> {
let mut text = Vec::new();
flate2::read::GzDecoder::new(bytes)
.read_to_end(&mut text)
.expect("a whole gzip stream");
text
}
async fn collect(response: Response<Body>) -> (Vec<Bytes>, Option<HeaderMap>) {
let mut body = response.into_body();
let mut data = Vec::new();
let mut trailers = None;
while let Some(frame) = body.frame().await {
let frame = frame.unwrap();
match frame.into_data() {
Ok(chunk) => data.push(chunk),
Err(frame) => trailers = frame.into_trailers().ok(),
}
}
(data, trailers)
}
#[test]
fn the_client_chooses_gzip_by_its_accept_encoding() {
let cases: &[(&[&str], bool)] = &[
(&[], false),
(&["gzip"], true),
(&["GZip"], true),
(&["x-gzip"], true),
(&[" gzip "], true),
(&["br, deflate, gzip"], true),
(&["br", "gzip"], true),
(&["br"], false),
(&["*"], false),
(&["identity"], false),
(&["gzip;q=0"], false),
(&["gzip;q=0.000"], false),
(&["gzip; q=0.001"], true),
(&["gzip;Q=1"], true),
(&["gzip;q=1.000"], true),
(&["gzip;q=1.001"], false),
(&["gzip;q=0.5, identity"], false),
(&["gzip;q=0.5, identity;q=0.5"], true),
(&["gzip;q=0.4, gzip;q=0.6, identity;q=0.5"], true),
(&["identity;q=0, gzip;q=0.1"], true),
(&["gzip;q=0.1234"], false),
(&["gzip;q=.5"], false),
(&["gzip;q=0."], false),
(&["gzip;q=1."], true),
(&["gzip;level=1"], false),
(&["gzip;q=0.5;level=1"], false),
(&["gzip ;q=0.5"], true),
(&["gzip;q = 0.5"], false),
(&["gzip;q=abc", "gzip;q=0.2"], true),
(&["gzip,"], true),
(&[",,gzip"], true),
(&["gzipx"], false),
(&["deflate"], false),
];
for (accept, chosen) in cases {
let mut headers = HeaderMap::new();
for value in *accept {
headers.append(ACCEPT_ENCODING, HeaderValue::from_static(value));
}
assert_eq!(accepts_gzip(&headers), *chosen, "{accept:?}");
}
let mut headers = HeaderMap::new();
headers.append(
ACCEPT_ENCODING,
HeaderValue::from_bytes(b"gzip\xff").unwrap(),
);
assert!(!accepts_gzip(&headers));
headers.append(ACCEPT_ENCODING, HeaderValue::from_static("gzip"));
assert!(accepts_gzip(&headers));
}
#[tokio::test]
async fn a_response_is_compressed_for_a_client_that_accepts_gzip() {
let body = long("compressible text ");
let response = send(
&["gzip"],
Response::builder()
.header(CONTENT_TYPE, "text/plain")
.header(CONTENT_LENGTH, body.len())
.header(ACCEPT_RANGES, "bytes")
.body(Body::from(body.clone()))
.unwrap(),
)
.await;
assert!(gzipped(&response));
assert_eq!(response.headers()[VARY], "accept-encoding");
assert!(!response.headers().contains_key(CONTENT_LENGTH));
assert!(!response.headers().contains_key(ACCEPT_RANGES));
assert_eq!(response.body().size_hint().exact(), None);
assert!(!response.body().is_end_stream());
let (data, _) = collect(response).await;
assert_eq!(gunzip(&data.concat()), body.as_bytes());
}
#[tokio::test]
async fn a_client_that_does_not_accept_gzip_still_gets_the_vary() {
let body = long("text ");
let response = send(&["br"], text(body.clone())).await;
assert!(!gzipped(&response));
assert_eq!(response.headers()[VARY], "accept-encoding");
assert_eq!(
response.body().size_hint().exact(),
Some(body.len() as u64),
"an uncompressed body is passed on as it is"
);
let (data, _) = collect(response).await;
assert_eq!(data.concat(), body.as_bytes());
}
#[tokio::test]
async fn some_responses_are_never_compressed() {
let big = long("x");
let cases: Vec<(&str, Response<Body>)> = vec![
("31 bytes", text("x".repeat(31))),
(
"a content-length under 32",
Response::builder()
.header(CONTENT_LENGTH, "31")
.body(Body::from_stream(futures_util::stream::iter([Ok::<
_,
Infallible,
>(
"x".repeat(31),
)])))
.unwrap(),
),
(
"already encoded",
Response::builder()
.header(CONTENT_ENCODING, "br")
.body(Body::from(big.clone()))
.unwrap(),
),
(
"a range",
Response::builder()
.status(StatusCode::PARTIAL_CONTENT)
.header(CONTENT_RANGE, "bytes 0-99/200")
.body(Body::from(big.clone()))
.unwrap(),
),
("an image", typed("image/png", &big)),
("gRPC", typed("application/grpc", &big)),
("gRPC with protobuf", typed("application/grpc+proto", &big)),
("events", typed("text/event-stream", &big)),
("empty", text("")),
];
for (name, response) in cases {
let response = send(&["gzip"], response).await;
assert!(!gzipped(&response), "{name}");
assert!(!response.headers().contains_key(VARY), "{name}");
}
}
fn typed(content_type: &str, body: &str) -> Response<Body> {
Response::builder()
.header(CONTENT_TYPE, content_type)
.body(Body::from(body.to_owned()))
.unwrap()
}
#[tokio::test]
async fn svg_grpc_web_and_unknown_sizes_are_compressed() {
let big = long("x");
let cases: Vec<(&str, Response<Body>)> = vec![
("32 bytes", text("x".repeat(32))),
("svg", typed("image/svg+xml", &big)),
("gRPC-Web", typed("application/grpc-web+proto", &big)),
("no content type", Response::new(Body::from(big.clone()))),
(
"a stream of unknown size",
Response::new(Body::from_stream(futures_util::stream::iter([Ok::<
_,
Infallible,
>(
"x"
)]))),
),
(
"a content-length that is not a number",
Response::builder()
.header(CONTENT_LENGTH, "ten")
.body(Body::from_stream(futures_util::stream::iter([Ok::<
_,
Infallible,
>(
"x"
)])))
.unwrap(),
),
];
for (name, response) in cases {
let response = send(&["gzip"], response).await;
assert!(gzipped(&response), "{name}");
}
}
#[tokio::test]
async fn an_existing_vary_on_accept_encoding_is_not_repeated() {
for vary in [
"Accept-Encoding",
"origin, accept-encoding",
"x-accept-encoding-v2",
] {
let response = send(
&["gzip"],
Response::builder()
.header(VARY, vary)
.body(Body::from(long("x")))
.unwrap(),
)
.await;
let values: Vec<_> = response.headers().get_all(VARY).iter().collect();
assert_eq!(values, [vary], "{vary}");
}
let response = send(
&["gzip"],
Response::builder()
.header(VARY, "origin")
.body(Body::from(long("x")))
.unwrap(),
)
.await;
let values: Vec<_> = response.headers().get_all(VARY).iter().collect();
assert_eq!(values, ["origin", "accept-encoding"]);
}
struct Scripted {
steps: VecDeque<Step>,
dropped: Arc<AtomicBool>,
}
enum Step {
Data(&'static str),
Pending,
Trailers(HeaderMap),
Fail(&'static str),
}
impl Scripted {
fn new(steps: impl IntoIterator<Item = Step>) -> (Self, Arc<AtomicBool>) {
let dropped = Arc::new(AtomicBool::new(false));
let body = Scripted {
steps: steps.into_iter().collect(),
dropped: dropped.clone(),
};
(body, dropped)
}
}
impl Drop for Scripted {
fn drop(&mut self) {
self.dropped.store(true, Ordering::SeqCst);
}
}
impl HttpBody for Scripted {
type Data = Bytes;
type Error = BoxError;
fn poll_frame(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Bytes>, BoxError>>> {
match self.steps.pop_front() {
None => Poll::Ready(None),
Some(Step::Data(data)) => Poll::Ready(Some(Ok(Frame::data(Bytes::from(data))))),
Some(Step::Pending) => {
cx.waker().wake_by_ref();
Poll::Pending
}
Some(Step::Trailers(trailers)) => Poll::Ready(Some(Ok(Frame::trailers(trailers)))),
Some(Step::Fail(message)) => Poll::Ready(Some(Err(message.into()))),
}
}
fn size_hint(&self) -> SizeHint {
SizeHint::new()
}
}
type PolledFrame<B> = Poll<Option<Result<Frame<<B as HttpBody>::Data>, <B as HttpBody>::Error>>>;
fn poll_once<B: HttpBody + Unpin>(body: &mut B) -> PolledFrame<B> {
Pin::new(body).poll_frame(&mut Context::from_waker(Waker::noop()))
}
#[tokio::test]
async fn what_a_slow_body_sent_so_far_reaches_the_client_before_it_waits() {
let (body, _) = Scripted::new([
Step::Data("the first part of a slow response, "),
Step::Pending,
Step::Data("and the rest of it, a while later"),
]);
let response = send(&["gzip"], Response::new(Body::new(body))).await;
assert!(gzipped(&response));
let mut body = response.into_body();
let Poll::Ready(Some(Ok(first))) = poll_once(&mut body) else {
panic!("the first part goes out before the body waits");
};
let first = first.into_data().unwrap();
let mut decoder = flate2::read::GzDecoder::new(&first[..]);
let mut seen = Vec::new();
let _ = decoder.read_to_end(&mut seen);
assert_eq!(seen, b"the first part of a slow response, ");
let mut all = first.to_vec();
while let Some(frame) = body.frame().await {
all.extend_from_slice(&frame.unwrap().into_data().unwrap());
}
assert_eq!(
gunzip(&all),
b"the first part of a slow response, and the rest of it, a while later"
);
}
#[tokio::test]
async fn trailers_follow_the_compressed_data() {
let mut trailers = HeaderMap::new();
trailers.insert("x-checksum", HeaderValue::from_static("abc"));
let (body, _) = Scripted::new([
Step::Data("some data that is long enough to be compressed"),
Step::Trailers(trailers.clone()),
]);
let response = send(&["gzip"], Response::new(Body::new(body))).await;
let (data, seen) = collect(response).await;
assert_eq!(
gunzip(&data.concat()),
b"some data that is long enough to be compressed"
);
assert_eq!(seen, Some(trailers));
}
#[tokio::test]
async fn an_error_from_the_body_comes_out_as_it_is() {
let (body, _) = Scripted::new([
Step::Data("the start of a body that fails"),
Step::Pending,
Step::Fail("gone"),
]);
let response = send(&["gzip"], Response::new(Body::new(body))).await;
let mut body = response.into_body();
let first = body.frame().await.unwrap().unwrap().into_data().unwrap();
assert!(!first.is_empty());
let error = body.frame().await.unwrap().unwrap_err();
assert_eq!(error.to_string(), "gone");
}
#[tokio::test]
async fn an_empty_stream_is_an_empty_gzip_stream() {
let (body, _) = Scripted::new([]);
let response = send(&["gzip"], Response::new(Body::new(body))).await;
assert!(gzipped(&response));
let (data, _) = collect(response).await;
assert_eq!(gunzip(&data.concat()), b"");
}
#[tokio::test]
async fn a_client_that_goes_away_drops_the_body() {
let (body, dropped) = Scripted::new([
Step::Data("the start of a long download, "),
Step::Pending,
Step::Data("never read"),
]);
let response = send(&["gzip"], Response::new(Body::new(body))).await;
let mut body = response.into_body();
assert!(matches!(poll_once(&mut body), Poll::Ready(Some(Ok(_)))));
assert!(!dropped.load(Ordering::SeqCst));
drop(body);
assert!(dropped.load(Ordering::SeqCst));
}
#[tokio::test]
async fn a_request_cancelled_before_its_response_drops_the_handler() {
let dropped = Arc::new(AtomicBool::new(false));
let seen = dropped.clone();
let inner = service_fn(move |_: Request<Body>| {
let guard = DropFlag(seen.clone());
async move {
let _guard = guard;
std::future::pending::<()>().await;
Ok::<_, Infallible>(Response::new(Body::empty()))
}
});
let mut service = CompressionLayer::new().layer(inner);
let future = service.call(request(&["gzip"]));
let mut future = Box::pin(future);
assert!(
future
.as_mut()
.poll(&mut Context::from_waker(Waker::noop()))
.is_pending()
);
drop(future);
assert!(dropped.load(Ordering::SeqCst));
}
struct DropFlag(Arc<AtomicBool>);
impl Drop for DropFlag {
fn drop(&mut self) {
self.0.store(true, Ordering::SeqCst);
}
}
#[tokio::test]
async fn a_body_that_is_always_ready_comes_out_in_frames_of_at_most_4_kib() {
let mut state = 0x2545_f491_u32;
let input: Vec<u8> = (0..1 << 20)
.map(|_| {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
state as u8
})
.collect();
let response = send(&["gzip"], Response::new(Body::from(input.clone()))).await;
let (data, _) = collect(response).await;
assert!(data.len() > 200, "{} frames", data.len());
assert!(data.iter().all(|frame| frame.len() <= 4096));
assert_eq!(gunzip(&data.concat()), input);
}
fn noise(len: usize) -> Vec<u8> {
let mut state = 0x2545_f491_u32;
(0..len)
.map(|i| {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
if i % 3 == 0 {
b"navree "[i % 7]
} else {
state as u8
}
})
.collect()
}
fn frames(body: Body) -> Vec<Bytes> {
let mut gzip = GzipBody::new(body);
let mut frames = Vec::new();
loop {
match poll_once(&mut gzip) {
Poll::Pending => {}
Poll::Ready(None) => return frames,
Poll::Ready(Some(frame)) => frames.push(frame.unwrap().into_data().unwrap()),
}
}
}
fn fingerprint(frames: &[Bytes]) -> (Vec<usize>, String) {
use sha2::Digest;
let sizes = frames.iter().map(Bytes::len).collect();
(sizes, hex::encode(sha2::Sha256::digest(frames.concat())))
}
fn sizes(full: usize, frames: usize, rest: &[usize]) -> Vec<usize> {
let mut sizes = vec![full; frames];
sizes.extend_from_slice(rest);
sizes
}
#[test]
fn the_bytes_for_fixed_inputs_are_pinned_down() {
let text = frames(Body::from("Navree compresses this line. ".repeat(200)));
let expected = [
(
vec![80],
"ee66fbfe069f81f55c7bb86b28f30599bcea86b8c3f6b86fff83322d179a8439",
),
(
vec![105],
"ebe9575fa779f2e812b2da8669d33c64acda08067319328725a142aba2aa49a1",
),
];
check("one chunk of text", &text, &expected);
let (slow, _) = Scripted::new([
Step::Data("the first part of a slow response, "),
Step::Pending,
Step::Data("and the rest of it, a while later"),
]);
let slow = frames(Body::new(slow));
let expected = [
(
vec![55, 36],
"5816d56f9ab54101231f6925fd57978e873d75ce93c269d48f272dc921d27b40",
),
(
vec![51, 38],
"b657e4e469931ec24a2afb49f6ee745e9650da93a669b38dd059f6c630f2a166",
),
];
check("a slow body", &slow, &expected);
assert!(slow[0].ends_with(&[0, 0, 0xff, 0xff]));
let large = frames(Body::from(noise(256 * 1024)));
let expected = [
(
sizes(4096, 56, &[2111]),
"7868b86612e668ee30c8b7f8ef53fc073bcb42812ab8b619516e890af3a07c4d",
),
(
sizes(4096, 56, &[1980]),
"cb4ec14481eacd8be65d365e5508b5506a13a56b62c9f1a56b1a92201708f775",
),
];
check("256 KiB in one chunk", &large, &expected);
let data = noise(150_000);
let mut steps = Vec::new();
for (i, chunk) in data.chunks(3000).enumerate() {
steps.push(Bytes::copy_from_slice(chunk));
if i % 7 == 6 {
steps.push(Bytes::new());
}
}
let waits = Body::new(Waiting(steps.into()));
let chunked = frames(waits);
let mut zlib = sizes(4096, 22, &[3102, 994]);
zlib.extend(sizes(4096, 8, &[3510, 586, 2120]));
let expected = [
(
sizes(4096, 32, &[1566]),
"144fa250f60106b77959d549b91d2a07af6790481c1ec25fba28496e574dffa5",
),
(
zlib,
"f75bca2a33787d1671339d4058bbdf4a84217b724dbb66ddb1abddcc5824978b",
),
];
check(
"3000-byte chunks with a wait after every seventh",
&chunked,
&expected,
);
assert_eq!(gunzip(&chunked.concat()), data);
}
fn check(name: &str, frames: &[Bytes], expected: &[(Vec<usize>, &str)]) {
let (sizes, digest) = fingerprint(frames);
assert!(
expected
.iter()
.any(|(want, hash)| *want == sizes && *hash == digest),
"{name}: frames {sizes:?}, sha256 {digest}"
);
}
struct Waiting(VecDeque<Bytes>);
impl HttpBody for Waiting {
type Data = Bytes;
type Error = Infallible;
fn poll_frame(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Bytes>, Infallible>>> {
match self.0.pop_front() {
None => Poll::Ready(None),
Some(chunk) if chunk.is_empty() => {
cx.waker().wake_by_ref();
Poll::Pending
}
Some(chunk) => Poll::Ready(Some(Ok(Frame::data(chunk)))),
}
}
}
struct Strict {
frames: VecDeque<Frame<Bytes>>,
ended: bool,
}
impl HttpBody for Strict {
type Data = Bytes;
type Error = Infallible;
fn poll_frame(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Bytes>, Infallible>>> {
assert!(!self.ended, "polled after its end");
let next = self.frames.pop_front();
self.ended = next.is_none();
Poll::Ready(next.map(Ok))
}
}
#[tokio::test]
async fn a_body_that_has_ended_is_not_polled_again() {
let mut trailers = HeaderMap::new();
trailers.insert("x-checksum", HeaderValue::from_static("abc"));
for with_trailers in [false, true] {
let mut frames = VecDeque::from([Frame::data(Bytes::from(long("streamed ")))]);
if with_trailers {
frames.push_back(Frame::trailers(trailers.clone()));
}
let body = Strict {
frames,
ended: false,
};
let response = send(&["gzip"], Response::new(Body::new(body))).await;
let mut body = response.into_body();
let mut data = Vec::new();
while let Some(frame) = body.frame().await {
if let Ok(chunk) = frame.unwrap().into_data() {
data.extend_from_slice(&chunk);
}
}
assert!(body.frame().await.is_none());
assert_eq!(gunzip(&data), long("streamed ").as_bytes());
}
}
#[tokio::test]
async fn the_exclusions_are_compared_without_regard_to_case() {
for content_type in [
"image/png",
"Image/PNG",
"IMAGE/JPEG",
"Text/Event-Stream",
"TEXT/EVENT-STREAM; charset=utf-8",
"Application/GRPC",
"APPLICATION/grpc+proto",
] {
let response = Response::builder()
.header(CONTENT_TYPE, content_type)
.body(Body::from(long("x")))
.unwrap();
let response = send(&["gzip"], response).await;
assert!(!gzipped(&response), "{content_type}");
assert!(!response.headers().contains_key(VARY), "{content_type}");
}
for content_type in ["Image/SVG+XML", "Application/GRPC-Web", "TEXT/HTML"] {
let response = Response::builder()
.header(CONTENT_TYPE, content_type)
.body(Body::from(long("x")))
.unwrap();
assert!(gzipped(&send(&["gzip"], response).await), "{content_type}");
}
}
#[tokio::test]
async fn no_transform_is_honoured() {
for values in [
&["no-transform"][..],
&["No-Transform"],
&["public, max-age=60,no-transform"],
&["public", "NO-TRANSFORM , max-age=60"],
] {
let mut builder = Response::builder().header(CONTENT_TYPE, "text/plain");
for value in values {
builder = builder.header(CACHE_CONTROL, *value);
}
let response = send(&["gzip"], builder.body(Body::from(long("x"))).unwrap()).await;
assert!(!gzipped(&response), "{values:?}");
assert!(!response.headers().contains_key(VARY), "{values:?}");
}
for value in ["no-store", "no-transforms", "x=\"no-transform\""] {
let response = Response::builder()
.header(CONTENT_TYPE, "text/plain")
.header(CACHE_CONTROL, value)
.body(Body::from(long("x")))
.unwrap();
assert!(gzipped(&send(&["gzip"], response).await), "{value}");
}
}
#[tokio::test]
async fn a_route_can_opt_out() {
let route = |no_compression| {
Arc::new(crate::route::RouteMeta {
no_compression,
..Default::default()
})
};
let mut response = text(long("x"));
response.extensions_mut().insert(route(true));
let response = send(&["gzip"], response).await;
assert!(!gzipped(&response));
assert!(!response.headers().contains_key(VARY));
let mut response = text(long("x"));
response.extensions_mut().insert(route(false));
assert!(gzipped(&send(&["gzip"], response).await));
}
#[tokio::test]
async fn a_strong_etag_is_weakened_on_the_gzip_variant() {
let tagged = |etag: &'static str| {
Response::builder()
.header(CONTENT_TYPE, "text/plain")
.header(ETAG, etag)
.body(Body::from(long("x")))
.unwrap()
};
let response = send(&["gzip"], tagged("\"v1\"")).await;
assert!(gzipped(&response));
assert_eq!(response.headers()[ETAG], "W/\"v1\"");
let response = send(&["gzip"], tagged("W/\"v1\"")).await;
assert_eq!(response.headers()[ETAG], "W/\"v1\"");
let response = send(&["identity"], tagged("\"v1\"")).await;
assert!(!gzipped(&response));
assert_eq!(response.headers()[ETAG], "\"v1\"");
}
#[tokio::test]
async fn a_status_without_a_body_is_not_compressed() {
for status in [
StatusCode::NO_CONTENT,
StatusCode::RESET_CONTENT,
StatusCode::NOT_MODIFIED,
] {
let stream = futures_util::stream::empty::<Result<Bytes, Infallible>>();
let response = Response::builder()
.status(status)
.header(CONTENT_TYPE, "text/plain")
.body(Body::from_stream(stream))
.unwrap();
let response = send(&["gzip"], response).await;
assert!(!gzipped(&response), "{status}");
let (frames, _) = collect(response).await;
assert!(frames.is_empty(), "{status}: {frames:?}");
}
}
#[tokio::test]
async fn the_length_of_an_opted_out_response_says_nothing_of_a_guess() {
let page = |guess: &str| {
format!(
"<form><input type=hidden name=_csrf value=\"q7zk2mvb4xjt3nrc\"></form>\
<input name=q value=\"{guess}\"><p>Results for your search</p>"
)
};
let length = |body: String, no_transform: bool| async move {
let mut builder = Response::builder().header(CONTENT_TYPE, "text/html");
if no_transform {
builder = builder.header(CACHE_CONTROL, "no-transform");
}
let response = send(&["gzip"], builder.body(Body::from(body)).unwrap()).await;
let (frames, _) = collect(response).await;
frames.iter().map(Bytes::len).sum::<usize>()
};
let (right, wrong) = (
page("_csrf value=\"q7zk2mvb"),
page("_csrf value=\"zzzzzzzz"),
);
assert_eq!(right.len(), wrong.len());
assert!(length(right.clone(), false).await < length(wrong.clone(), false).await);
assert_eq!(length(right, true).await, length(wrong, true).await);
}
async fn send_as(
method: http::Method,
accept: &[&str],
response: Response<Body>,
) -> Response<Body> {
let response = std::sync::Mutex::new(Some(response));
let inner = service_fn(move |_: Request<Body>| {
let response = response.lock().unwrap().take().expect("one call");
async move { Ok::<_, Infallible>(response) }
});
let mut request = request(accept);
*request.method_mut() = method;
ServiceBuilder::new()
.layer(CompressionLayer::new())
.service(inner)
.oneshot(request)
.await
.unwrap()
}
fn head_of(get: Response<Body>) -> Response<Body> {
let (mut parts, body) = get.into_parts();
if let Some(length) = body.size_hint().exact() {
parts.headers.insert(CONTENT_LENGTH, length.into());
}
Response::from_parts(parts, Body::empty())
}
fn encoding_headers(response: &Response<Body>) -> Vec<(&'static str, Option<String>)> {
["content-encoding", "etag", "vary", "accept-ranges"]
.into_iter()
.map(|name| {
let value = response.headers().get(name);
(name, value.map(|v| v.to_str().unwrap().to_owned()))
})
.collect()
}
#[tokio::test]
async fn a_partial_content_response_is_never_compressed() {
for content_type in ["multipart/byteranges; boundary=x", "text/plain"] {
let response = Response::builder()
.status(StatusCode::PARTIAL_CONTENT)
.header(CONTENT_TYPE, content_type)
.body(Body::from(long("x")))
.unwrap();
let response = send(&["gzip"], response).await;
assert!(!gzipped(&response), "{content_type}");
assert!(!response.headers().contains_key(VARY), "{content_type}");
let (frames, _) = collect(response).await;
assert_eq!(frames.concat(), long("x").as_bytes(), "{content_type}");
}
}
#[tokio::test]
async fn a_head_response_is_encoded_as_the_get_response_is() {
let get = || {
Response::builder()
.header(CONTENT_TYPE, "text/plain")
.header(ACCEPT_RANGES, "bytes")
.header(ETAG, "\"v1\"")
.body(Body::from(long("x")))
.unwrap()
};
for accept in [&["gzip"][..], &["identity"], &["br"], &[]] {
let wanted = send_as(http::Method::GET, accept, get()).await;
let head = send_as(http::Method::HEAD, accept, head_of(get())).await;
assert_eq!(
encoding_headers(&head),
encoding_headers(&wanted),
"{accept:?}"
);
assert_eq!(gzipped(&head), gzipped(&wanted), "{accept:?}");
assert_eq!(
head.headers().get(CONTENT_LENGTH).is_some(),
!gzipped(&head),
"{accept:?}: the length of a body that is sent as it is"
);
let (frames, _) = collect(head).await;
assert!(frames.is_empty(), "{accept:?}: a HEAD response has no body");
}
let head = send_as(http::Method::HEAD, &["gzip"], head_of(get())).await;
assert!(gzipped(&head));
assert_eq!(head.headers()[ETAG], "W/\"v1\"");
assert_eq!(head.headers()[VARY], "accept-encoding");
assert!(!head.headers().contains_key(CONTENT_LENGTH));
assert!(!head.headers().contains_key(ACCEPT_RANGES));
}
#[tokio::test]
async fn a_head_response_is_left_alone_when_the_get_response_would_be() {
let small = || text("x".repeat(31));
let wanted = send_as(http::Method::GET, &["gzip"], small()).await;
let head = send_as(http::Method::HEAD, &["gzip"], head_of(small())).await;
assert_eq!(encoding_headers(&head), encoding_headers(&wanted));
assert!(!gzipped(&head));
assert_eq!(head.headers()[CONTENT_LENGTH], "31");
let stream = || {
let chunks = futures_util::stream::iter([Ok::<_, Infallible>(long("x"))]);
Response::builder()
.header(CONTENT_TYPE, "text/plain")
.body(Body::from_stream(chunks))
.unwrap()
};
let wanted = send_as(http::Method::GET, &["gzip"], stream()).await;
let head = send_as(http::Method::HEAD, &["gzip"], head_of(stream())).await;
assert!(gzipped(&wanted));
assert_eq!(encoding_headers(&head), encoding_headers(&wanted));
for (name, response) in [
(
"no-transform",
Response::builder()
.header(CONTENT_TYPE, "text/plain")
.header(CACHE_CONTROL, "no-transform")
.body(Body::from(long("x")))
.unwrap(),
),
(
"an image",
Response::builder()
.header(CONTENT_TYPE, "image/png")
.body(Body::from(long("x")))
.unwrap(),
),
] {
let head = send_as(http::Method::HEAD, &["gzip"], head_of(response)).await;
assert!(!gzipped(&head), "{name}");
assert!(!head.headers().contains_key(VARY), "{name}");
assert!(head.headers().contains_key(CONTENT_LENGTH), "{name}");
}
}
#[tokio::test]
async fn a_not_modified_response_varies_like_the_response_it_stands_for() {
let not_modified = || {
Response::builder()
.status(StatusCode::NOT_MODIFIED)
.header(ETAG, "\"v1\"")
.body(Body::empty())
.unwrap()
};
for accept in [&["gzip"][..], &["identity"], &[]] {
let response = send(accept, not_modified()).await;
assert_eq!(response.headers()[VARY], "accept-encoding", "{accept:?}");
assert!(!gzipped(&response), "{accept:?}");
assert_eq!(response.headers()[ETAG], "\"v1\"");
}
let response = send_as(http::Method::HEAD, &["gzip"], not_modified()).await;
assert_eq!(response.headers()[VARY], "accept-encoding");
let mut opted_out = not_modified();
opted_out
.extensions_mut()
.insert(Arc::new(crate::route::RouteMeta {
no_compression: true,
..Default::default()
}));
assert!(
!send(&["gzip"], opted_out)
.await
.headers()
.contains_key(VARY)
);
let mut no_transform = not_modified();
no_transform
.headers_mut()
.insert(CACHE_CONTROL, HeaderValue::from_static("no-transform"));
let response = send(&["gzip"], no_transform).await;
assert!(!response.headers().contains_key(VARY));
let mut varied = not_modified();
varied
.headers_mut()
.insert(VARY, HeaderValue::from_static("Accept-Encoding"));
let response = send(&["gzip"], varied).await;
assert_eq!(response.headers().get_all(VARY).iter().count(), 1);
for status in [StatusCode::NO_CONTENT, StatusCode::RESET_CONTENT] {
let response = Response::builder()
.status(status)
.body(Body::empty())
.unwrap();
let response = send(&["gzip"], response).await;
assert!(!response.headers().contains_key(VARY), "{status}");
}
}