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

feat(decompression): support HTTP responses containing multiple ZSTD frames #548

Merged
merged 3 commits into from
Mar 12, 2025
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
4 changes: 3 additions & 1 deletion tower-http/src/decompression/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ where
type Output = ZstdDecoder<Self::Input>;

fn apply(input: Self::Input, _quality: CompressionLevel) -> Self::Output {
ZstdDecoder::new(input)
let mut decoder = ZstdDecoder::new(input);
decoder.multiple_members(true);
decoder
}

fn get_pin_mut(pinned: Pin<&mut Self::Output>) -> Pin<&mut Self::Input> {
Expand Down
34 changes: 34 additions & 0 deletions tower-http/src/decompression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ mod tests {
assert_eq!(decompressed_data, "Hello, World!");
}

#[tokio::test]
async fn decompress_multi_zstd() {
let mut client = Decompression::new(service_fn(handle_multi_zstd));

let req = Request::builder()
.header("accept-encoding", "zstd")
.body(Body::empty())
.unwrap();
let res = client.ready().await.unwrap().call(req).await.unwrap();

// read the body, it will be decompressed automatically
let body = res.into_body();
let decompressed_data =
String::from_utf8(body.collect().await.unwrap().to_bytes().to_vec()).unwrap();

assert_eq!(decompressed_data, "Hello, World!");
}

async fn handle_multi_gz(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
let mut buf = Vec::new();
let mut enc1 = GzEncoder::new(&mut buf, Default::default());
Expand All @@ -184,6 +202,22 @@ mod tests {
Ok(res)
}

async fn handle_multi_zstd(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
let mut buf = Vec::new();
let mut enc1 = zstd::Encoder::new(&mut buf, Default::default()).unwrap();
enc1.write_all(b"Hello, ").unwrap();
enc1.finish().unwrap();

let mut enc2 = zstd::Encoder::new(&mut buf, Default::default()).unwrap();
enc2.write_all(b"World!").unwrap();
enc2.finish().unwrap();

let mut res = Response::new(Body::from(buf));
res.headers_mut()
.insert("content-encoding", "zstd".parse().unwrap());
Ok(res)
}

#[allow(dead_code)]
async fn is_compatible_with_hyper() {
let client =
Expand Down