From 309c480782ab028f3e49d16817a1c91a1d45a059 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 4 Apr 2019 15:03:40 -0700 Subject: [PATCH] encoder sent uncompressed data before compressed --- actix-http/src/encoding/encoder.rs | 12 ++++++-- src/lib.rs | 1 - tests/test_server.rs | 45 +++++++++++++++--------------- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index 0e02bc895..50e9d068b 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -19,6 +19,7 @@ use super::Writer; const INPLACE: usize = 2049; pub struct Encoder { + eof: bool, body: EncoderBody, encoder: Option, fut: Option>, @@ -56,12 +57,14 @@ impl Encoder { head.no_chunking(false); ResponseBody::Body(Encoder { body, + eof: false, fut: None, encoder: ContentEncoder::encoder(encoding), }) } else { ResponseBody::Body(Encoder { body, + eof: false, fut: None, encoder: None, }) @@ -90,6 +93,10 @@ impl MessageBody for Encoder { fn poll_next(&mut self) -> Poll, Error> { loop { + if self.eof { + return Ok(Async::Ready(None)); + } + if let Some(ref mut fut) = self.fut { let mut encoder = futures::try_ready!(fut.poll()); let chunk = encoder.take(); @@ -127,10 +134,10 @@ impl MessageBody for Encoder { encoder.write(&chunk)?; Ok(encoder) })); - continue; } + } else { + return Ok(Async::Ready(Some(chunk))); } - return Ok(Async::Ready(Some(chunk))); } Async::Ready(None) => { if let Some(encoder) = self.encoder.take() { @@ -138,6 +145,7 @@ impl MessageBody for Encoder { if chunk.is_empty() { return Ok(Async::Ready(None)); } else { + self.eof = true; return Ok(Async::Ready(Some(chunk))); } } else { diff --git a/src/lib.rs b/src/lib.rs index ca4968833..d1efd39c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,7 +143,6 @@ pub mod dev { }; pub use crate::types::form::UrlEncoded; pub use crate::types::json::JsonBody; - pub use crate::types::payload::HttpMessageBody; pub use crate::types::readlines::Readlines; pub use actix_http::body::{Body, BodySize, MessageBody, ResponseBody}; diff --git a/tests/test_server.rs b/tests/test_server.rs index 3c5d09066..7c91d4fed 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -16,7 +16,7 @@ use flate2::Compression; use futures::stream::once; use rand::{distributions::Alphanumeric, Rng}; -use actix_web::{dev::HttpMessageBody, http, test, web, App, HttpResponse, HttpServer}; +use actix_web::{http, test, web, App, HttpResponse, HttpServer}; #[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))] use actix_web::middleware::encoding; @@ -58,7 +58,7 @@ fn test_body() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -77,7 +77,7 @@ fn test_body_gzip() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); // decode let mut e = GzDecoder::new(&bytes[..]); @@ -115,7 +115,7 @@ fn test_body_encoding_override() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); // decode let mut e = ZlibDecoder::new(Vec::new()); @@ -134,7 +134,7 @@ fn test_body_encoding_override() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); // decode let mut e = ZlibDecoder::new(Vec::new()); @@ -165,7 +165,7 @@ fn test_body_gzip_large() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); // decode let mut e = GzDecoder::new(&bytes[..]); @@ -199,7 +199,7 @@ fn test_body_gzip_large_random() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); // decode let mut e = GzDecoder::new(&bytes[..]); @@ -232,7 +232,7 @@ fn test_body_chunked_implicit() { ); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); // decode let mut e = GzDecoder::new(&bytes[..]); @@ -267,7 +267,7 @@ fn test_body_br_streaming() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); // decode br let mut e = BrotliDecoder::new(Vec::with_capacity(2048)); @@ -293,7 +293,7 @@ fn test_head_binary() { } // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert!(bytes.is_empty()); } @@ -315,7 +315,7 @@ fn test_no_chunking() { assert!(!response.headers().contains_key(TRANSFER_ENCODING)); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -337,9 +337,8 @@ fn test_body_deflate() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); - // decode deflate let mut e = ZlibDecoder::new(Vec::new()); e.write_all(bytes.as_ref()).unwrap(); let dec = e.finish().unwrap(); @@ -371,7 +370,7 @@ fn test_body_brotli() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); // decode brotli let mut e = BrotliDecoder::new(Vec::with_capacity(2048)); @@ -405,7 +404,7 @@ fn test_encoding() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -434,7 +433,7 @@ fn test_gzip_encoding() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -464,7 +463,7 @@ fn test_gzip_encoding_large() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes, Bytes::from(data)); } @@ -498,7 +497,7 @@ fn test_reading_gzip_encoding_large_random() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes.len(), data.len()); assert_eq!(bytes, Bytes::from(data)); } @@ -528,7 +527,7 @@ fn test_reading_deflate_encoding() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -558,7 +557,7 @@ fn test_reading_deflate_encoding_large() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes, Bytes::from(data)); } @@ -592,7 +591,7 @@ fn test_reading_deflate_encoding_large_random() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes.len(), data.len()); assert_eq!(bytes, Bytes::from(data)); } @@ -622,7 +621,7 @@ fn test_brotli_encoding() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -652,7 +651,7 @@ fn test_brotli_encoding_large() { assert!(response.status().is_success()); // read response - let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); + let bytes = srv.block_on(response.body()).unwrap(); assert_eq!(bytes, Bytes::from(data)); }