1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

Always enable content encoding if encoding explicitly selected

This commit is contained in:
Nikolay Kim 2018-01-21 08:31:46 -08:00
parent 1cff4619e7
commit 1914a6a0d8
3 changed files with 10 additions and 6 deletions

View File

@ -6,6 +6,8 @@
* Log request processing errors * Log request processing errors
* Always enable content encoding if encoding explicitly selected
* Allow multiple Applications on a single server with different state #49 * Allow multiple Applications on a single server with different state #49
* CORS middleware: allowed_headers is defaulting to None #50 * CORS middleware: allowed_headers is defaulting to None #50

View File

@ -164,8 +164,8 @@ impl HttpResponse {
/// Content encoding /// Content encoding
#[inline] #[inline]
pub fn content_encoding(&self) -> &ContentEncoding { pub fn content_encoding(&self) -> ContentEncoding {
&self.get_ref().encoding self.get_ref().encoding
} }
/// Set content encoding /// Set content encoding
@ -812,11 +812,11 @@ mod tests {
#[test] #[test]
fn test_content_encoding() { fn test_content_encoding() {
let resp = HttpResponse::build(StatusCode::OK).finish().unwrap(); 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) let resp = HttpResponse::build(StatusCode::OK)
.content_encoding(ContentEncoding::Br).finish().unwrap(); .content_encoding(ContentEncoding::Br).finish().unwrap();
assert_eq!(*resp.content_encoding(), ContentEncoding::Br); assert_eq!(resp.content_encoding(), ContentEncoding::Br);
} }
#[test] #[test]

View File

@ -346,15 +346,17 @@ impl PayloadEncoder {
pub fn new(buf: SharedBytes, req: &HttpMessage, resp: &mut HttpResponse) -> PayloadEncoder { pub fn new(buf: SharedBytes, req: &HttpMessage, resp: &mut HttpResponse) -> PayloadEncoder {
let version = resp.version().unwrap_or_else(|| req.version); let version = resp.version().unwrap_or_else(|| req.version);
let mut body = resp.replace_body(Body::Empty); let mut body = resp.replace_body(Body::Empty);
let response_encoding = resp.content_encoding();
let has_body = match body { let has_body = match body {
Body::Empty => false, Body::Empty => false,
Body::Binary(ref bin) => bin.len() >= 96, Body::Binary(ref bin) =>
!(response_encoding == ContentEncoding::Auto && bin.len() < 96),
_ => true, _ => true,
}; };
// Enable content encoding only if response does not contain Content-Encoding header // Enable content encoding only if response does not contain Content-Encoding header
let mut encoding = if has_body { let mut encoding = if has_body {
let encoding = match *resp.content_encoding() { let encoding = match response_encoding {
ContentEncoding::Auto => { ContentEncoding::Auto => {
// negotiate content-encoding // negotiate content-encoding
if let Some(val) = req.headers.get(ACCEPT_ENCODING) { if let Some(val) = req.headers.get(ACCEPT_ENCODING) {