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

Bug fix: [de]compression layers ignore configuration #132

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
113 changes: 106 additions & 7 deletions tower-http/src/compression/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ impl<S> Layer<S> for CompressionLayer {
type Service = Compression<S>;

fn layer(&self, inner: S) -> Self::Service {
Compression::new(inner)
let mut outer = Compression::new(inner);
outer.accept = self.accept;
outer
Madoshakalaka marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -31,48 +33,145 @@ impl CompressionLayer {
/// Sets whether to enable the gzip encoding.
#[cfg(feature = "compression-gzip")]
#[cfg_attr(docsrs, doc(cfg(feature = "compression-gzip")))]
pub fn gzip(self, enable: bool) -> Self {
pub fn gzip(mut self, enable: bool) -> Self {
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
self.accept.set_gzip(enable);
self
}

/// Sets whether to enable the Deflate encoding.
#[cfg(feature = "compression-deflate")]
#[cfg_attr(docsrs, doc(cfg(feature = "compression-deflate")))]
pub fn deflate(self, enable: bool) -> Self {
pub fn deflate(mut self, enable: bool) -> Self {
self.accept.set_deflate(enable);
self
}

/// Sets whether to enable the Brotli encoding.
#[cfg(feature = "compression-br")]
#[cfg_attr(docsrs, doc(cfg(feature = "compression-br")))]
pub fn br(self, enable: bool) -> Self {
pub fn br(mut self, enable: bool) -> Self {
self.accept.set_br(enable);
self
}

/// Disables the gzip encoding.
///
/// This method is available even if the `gzip` crate feature is disabled.
pub fn no_gzip(self) -> Self {
pub fn no_gzip(mut self) -> Self {
self.accept.set_gzip(false);
self
}

/// Disables the Deflate encoding.
///
/// This method is available even if the `deflate` crate feature is disabled.
pub fn no_deflate(self) -> Self {
pub fn no_deflate(mut self) -> Self {
self.accept.set_deflate(false);
self
}

/// Disables the Brotli encoding.
///
/// This method is available even if the `br` crate feature is disabled.
pub fn no_br(self) -> Self {
pub fn no_br(mut self) -> Self {
self.accept.set_br(false);
self
}
}

#[cfg(test)]
mod tests {
use super::*;
use tokio::fs::File;
use http::{Request, Response, header::ACCEPT_ENCODING};
use hyper::Body;
use http_body::Body as _;
// for Body::data
use std::convert::Infallible;
use tokio_util::io::ReaderStream;
use tower::{Service, ServiceExt, ServiceBuilder};
use bytes::{Bytes, BytesMut};

async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
// Open the file.
let file = File::open("Cargo.toml").await.expect("file missing");
// Convert the file into a `Stream`.
let stream = ReaderStream::new(file);
// Convert the `Stream` into a `Body`.
let body = Body::wrap_stream(stream);
// Create response.
Ok(Response::new(body))
}

#[tokio::test]
async fn accept_encoding_configuration_works() -> Result<(), Box<dyn std::error::Error>> {
let deflate_only_layer = CompressionLayer::new().no_br().no_gzip();

let mut service = ServiceBuilder::new()
// Compress responses based on the `Accept-Encoding` header.
.layer(deflate_only_layer)
.service_fn(handle);

// Call the service with the deflate only layer
let request = Request::builder()
.header(ACCEPT_ENCODING, "gzip, deflate, br")
.body(Body::empty())?;

let response = service
.ready()
.await?
.call(request)
.await?;

assert_eq!(response.headers()["content-encoding"], "deflate");

// Read the body
let mut body = response.into_body();
let mut bytes = BytesMut::new();
while let Some(chunk) = body.data().await {
let chunk = chunk?;
bytes.extend_from_slice(&chunk[..]);
}
let bytes: Bytes = bytes.freeze();

let deflate_bytes_len = bytes.len();


let br_only_layer = CompressionLayer::new().no_gzip().no_deflate();

let mut service = ServiceBuilder::new()
// Compress responses based on the `Accept-Encoding` header.
.layer(br_only_layer)
.service_fn(handle);

// Call the service with the br only layer
let request = Request::builder()
.header(ACCEPT_ENCODING, "gzip, deflate, br")
.body(Body::empty())?;

let response = service
.ready()
.await?
.call(request)
.await?;

assert_eq!(response.headers()["content-encoding"], "br");

// Read the body
let mut body = response.into_body();
let mut bytes = BytesMut::new();
while let Some(chunk) = body.data().await {
let chunk = chunk?;
bytes.extend_from_slice(&chunk[..]);
}
let bytes: Bytes = bytes.freeze();

let br_byte_length = bytes.len();

// check the corresponding algorithms are actually used
// br should compresses better than deflate
assert!(br_byte_length < deflate_bytes_len * 9 / 10);

Ok(())
}
}
7 changes: 5 additions & 2 deletions tower-http/src/compression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Encoding {

// based on https://github.com/http-rs/accept-encoding
fn encodings(headers: &HeaderMap, accept: AcceptEncoding) -> Vec<(Encoding, f32)> {
headers
let ret = headers
.get_all(header::ACCEPT_ENCODING)
.iter()
.filter_map(|hval| hval.to_str().ok())
Expand Down Expand Up @@ -166,7 +166,10 @@ fn encodings(headers: &HeaderMap, accept: AcceptEncoding) -> Vec<(Encoding, f32)

Some((encoding, qval))
})
.collect::<Vec<(Encoding, f32)>>()
.collect::<Vec<(Encoding, f32)>>();


ret
Madoshakalaka marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(test)]
Expand Down
12 changes: 6 additions & 6 deletions tower-http/src/compression/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,47 +38,47 @@ impl<S> Compression<S> {
/// Sets whether to enable the gzip encoding.
#[cfg(feature = "compression-gzip")]
#[cfg_attr(docsrs, doc(cfg(feature = "compression-gzip")))]
pub fn gzip(self, enable: bool) -> Self {
pub fn gzip(mut self, enable: bool) -> Self {
self.accept.set_gzip(enable);
self
}

/// Sets whether to enable the Deflate encoding.
#[cfg(feature = "compression-deflate")]
#[cfg_attr(docsrs, doc(cfg(feature = "compression-deflate")))]
pub fn deflate(self, enable: bool) -> Self {
pub fn deflate(mut self, enable: bool) -> Self {
self.accept.set_deflate(enable);
self
}

/// Sets whether to enable the Brotli encoding.
#[cfg(feature = "compression-br")]
#[cfg_attr(docsrs, doc(cfg(feature = "compression-br")))]
pub fn br(self, enable: bool) -> Self {
pub fn br(mut self, enable: bool) -> Self {
self.accept.set_br(enable);
self
}

/// Disables the gzip encoding.
///
/// This method is available even if the `gzip` crate feature is disabled.
pub fn no_gzip(self) -> Self {
pub fn no_gzip(mut self) -> Self {
self.accept.set_gzip(false);
self
}

/// Disables the Deflate encoding.
///
/// This method is available even if the `deflate` crate feature is disabled.
pub fn no_deflate(self) -> Self {
pub fn no_deflate(mut self) -> Self {
self.accept.set_deflate(false);
self
}

/// Disables the Brotli encoding.
///
/// This method is available even if the `br` crate feature is disabled.
pub fn no_br(self) -> Self {
pub fn no_br(mut self) -> Self {
self.accept.set_br(false);
self
}
Expand Down
9 changes: 3 additions & 6 deletions tower-http/src/compression_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,18 @@ impl AcceptEncoding {
}

#[allow(dead_code)]
pub(crate) fn set_gzip(mut self, enable: bool) -> Self {
pub(crate) fn set_gzip(& mut self, enable: bool) {
Madoshakalaka marked this conversation as resolved.
Show resolved Hide resolved
self.gzip = enable;
self
}

#[allow(dead_code)]
pub(crate) fn set_deflate(mut self, enable: bool) -> Self {
pub(crate) fn set_deflate(& mut self, enable: bool) {
self.deflate = enable;
self
}

#[allow(dead_code)]
pub(crate) fn set_br(mut self, enable: bool) -> Self {
pub(crate) fn set_br(& mut self, enable: bool) {
self.br = enable;
self
}
}

Expand Down
12 changes: 6 additions & 6 deletions tower-http/src/decompression/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,47 @@ impl DecompressionLayer {
/// Sets whether to request the gzip encoding.
#[cfg(feature = "decompression-gzip")]
#[cfg_attr(docsrs, doc(cfg(feature = "decompression-gzip")))]
pub fn gzip(self, enable: bool) -> Self {
pub fn gzip(mut self, enable: bool) -> Self {
self.accept.set_gzip(enable);
self
}

/// Sets whether to request the Deflate encoding.
#[cfg(feature = "decompression-deflate")]
#[cfg_attr(docsrs, doc(cfg(feature = "decompression-deflate")))]
pub fn deflate(self, enable: bool) -> Self {
pub fn deflate(mut self, enable: bool) -> Self {
self.accept.set_deflate(enable);
self
}

/// Sets whether to request the Brotli encoding.
#[cfg(feature = "decompression-br")]
#[cfg_attr(docsrs, doc(cfg(feature = "decompression-br")))]
pub fn br(self, enable: bool) -> Self {
pub fn br(mut self, enable: bool) -> Self {
self.accept.set_br(enable);
self
}

/// Disables the gzip encoding.
///
/// This method is available even if the `gzip` crate feature is disabled.
pub fn no_gzip(self) -> Self {
pub fn no_gzip(mut self) -> Self {
self.accept.set_gzip(false);
self
}

/// Disables the Deflate encoding.
///
/// This method is available even if the `deflate` crate feature is disabled.
pub fn no_deflate(self) -> Self {
pub fn no_deflate(mut self) -> Self {
self.accept.set_deflate(false);
self
}

/// Disables the Brotli encoding.
///
/// This method is available even if the `br` crate feature is disabled.
pub fn no_br(self) -> Self {
pub fn no_br(mut self) -> Self {
self.accept.set_br(false);
self
}
Expand Down
12 changes: 6 additions & 6 deletions tower-http/src/decompression/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,47 @@ impl<S> Decompression<S> {
/// Sets whether to request the gzip encoding.
#[cfg(feature = "decompression-gzip")]
#[cfg_attr(docsrs, doc(cfg(feature = "decompression-gzip")))]
pub fn gzip(self, enable: bool) -> Self {
pub fn gzip(mut self, enable: bool) -> Self {
self.accept.set_gzip(enable);
self
}

/// Sets whether to request the Deflate encoding.
#[cfg(feature = "decompression-deflate")]
#[cfg_attr(docsrs, doc(cfg(feature = "decompression-deflate")))]
pub fn deflate(self, enable: bool) -> Self {
pub fn deflate(mut self, enable: bool) -> Self {
self.accept.set_deflate(enable);
self
}

/// Sets whether to request the Brotli encoding.
#[cfg(feature = "decompression-br")]
#[cfg_attr(docsrs, doc(cfg(feature = "decompression-br")))]
pub fn br(self, enable: bool) -> Self {
pub fn br(mut self, enable: bool) -> Self {
self.accept.set_br(enable);
self
}

/// Disables the gzip encoding.
///
/// This method is available even if the `gzip` crate feature is disabled.
pub fn no_gzip(self) -> Self {
pub fn no_gzip(mut self) -> Self {
self.accept.set_gzip(false);
self
}

/// Disables the Deflate encoding.
///
/// This method is available even if the `deflate` crate feature is disabled.
pub fn no_deflate(self) -> Self {
pub fn no_deflate(mut self) -> Self {
self.accept.set_deflate(false);
self
}

/// Disables the Brotli encoding.
///
/// This method is available even if the `br` crate feature is disabled.
pub fn no_br(self) -> Self {
pub fn no_br(mut self) -> Self {
self.accept.set_br(false);
self
}
Expand Down