diff --git a/CHANGES.md b/CHANGES.md index 85bae558..b88cec9f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,8 @@ * Log request processing errors +* Always enable content encoding if encoding explicitly selected + * Allow multiple Applications on a single server with different state #49 * CORS middleware: allowed_headers is defaulting to None #50 diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 0293ee32..10f13687 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -164,8 +164,8 @@ impl HttpResponse { /// Content encoding #[inline] - pub fn content_encoding(&self) -> &ContentEncoding { - &self.get_ref().encoding + pub fn content_encoding(&self) -> ContentEncoding { + self.get_ref().encoding } /// Set content encoding @@ -812,11 +812,11 @@ mod tests { #[test] fn test_content_encoding() { let resp = HttpResponse::build(StatusCode::OK).finish().unwrap(); - assert_eq!(*resp.content_encoding(), ContentEncoding::Auto); + assert_eq!(resp.content_encoding(), ContentEncoding::Auto); let resp = HttpResponse::build(StatusCode::OK) .content_encoding(ContentEncoding::Br).finish().unwrap(); - assert_eq!(*resp.content_encoding(), ContentEncoding::Br); + assert_eq!(resp.content_encoding(), ContentEncoding::Br); } #[test] diff --git a/src/server/encoding.rs b/src/server/encoding.rs index 98ff0c33..b5213efd 100644 --- a/src/server/encoding.rs +++ b/src/server/encoding.rs @@ -346,15 +346,17 @@ impl PayloadEncoder { pub fn new(buf: SharedBytes, req: &HttpMessage, resp: &mut HttpResponse) -> PayloadEncoder { let version = resp.version().unwrap_or_else(|| req.version); let mut body = resp.replace_body(Body::Empty); + let response_encoding = resp.content_encoding(); let has_body = match body { Body::Empty => false, - Body::Binary(ref bin) => bin.len() >= 96, + Body::Binary(ref bin) => + !(response_encoding == ContentEncoding::Auto && bin.len() < 96), _ => true, }; // Enable content encoding only if response does not contain Content-Encoding header let mut encoding = if has_body { - let encoding = match *resp.content_encoding() { + let encoding = match response_encoding { ContentEncoding::Auto => { // negotiate content-encoding if let Some(val) = req.headers.get(ACCEPT_ENCODING) {