1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-18 05:41:50 +01:00

encoder sent uncompressed data before compressed

This commit is contained in:
Nikolay Kim 2019-04-04 15:03:40 -07:00
parent 9c205f9f1d
commit 309c480782
3 changed files with 32 additions and 26 deletions

View File

@ -19,6 +19,7 @@ use super::Writer;
const INPLACE: usize = 2049; const INPLACE: usize = 2049;
pub struct Encoder<B> { pub struct Encoder<B> {
eof: bool,
body: EncoderBody<B>, body: EncoderBody<B>,
encoder: Option<ContentEncoder>, encoder: Option<ContentEncoder>,
fut: Option<CpuFuture<ContentEncoder, io::Error>>, fut: Option<CpuFuture<ContentEncoder, io::Error>>,
@ -56,12 +57,14 @@ impl<B: MessageBody> Encoder<B> {
head.no_chunking(false); head.no_chunking(false);
ResponseBody::Body(Encoder { ResponseBody::Body(Encoder {
body, body,
eof: false,
fut: None, fut: None,
encoder: ContentEncoder::encoder(encoding), encoder: ContentEncoder::encoder(encoding),
}) })
} else { } else {
ResponseBody::Body(Encoder { ResponseBody::Body(Encoder {
body, body,
eof: false,
fut: None, fut: None,
encoder: None, encoder: None,
}) })
@ -90,6 +93,10 @@ impl<B: MessageBody> MessageBody for Encoder<B> {
fn poll_next(&mut self) -> Poll<Option<Bytes>, Error> { fn poll_next(&mut self) -> Poll<Option<Bytes>, Error> {
loop { loop {
if self.eof {
return Ok(Async::Ready(None));
}
if let Some(ref mut fut) = self.fut { if let Some(ref mut fut) = self.fut {
let mut encoder = futures::try_ready!(fut.poll()); let mut encoder = futures::try_ready!(fut.poll());
let chunk = encoder.take(); let chunk = encoder.take();
@ -127,10 +134,10 @@ impl<B: MessageBody> MessageBody for Encoder<B> {
encoder.write(&chunk)?; encoder.write(&chunk)?;
Ok(encoder) Ok(encoder)
})); }));
continue;
} }
} else {
return Ok(Async::Ready(Some(chunk)));
} }
return Ok(Async::Ready(Some(chunk)));
} }
Async::Ready(None) => { Async::Ready(None) => {
if let Some(encoder) = self.encoder.take() { if let Some(encoder) = self.encoder.take() {
@ -138,6 +145,7 @@ impl<B: MessageBody> MessageBody for Encoder<B> {
if chunk.is_empty() { if chunk.is_empty() {
return Ok(Async::Ready(None)); return Ok(Async::Ready(None));
} else { } else {
self.eof = true;
return Ok(Async::Ready(Some(chunk))); return Ok(Async::Ready(Some(chunk)));
} }
} else { } else {

View File

@ -143,7 +143,6 @@ pub mod dev {
}; };
pub use crate::types::form::UrlEncoded; pub use crate::types::form::UrlEncoded;
pub use crate::types::json::JsonBody; pub use crate::types::json::JsonBody;
pub use crate::types::payload::HttpMessageBody;
pub use crate::types::readlines::Readlines; pub use crate::types::readlines::Readlines;
pub use actix_http::body::{Body, BodySize, MessageBody, ResponseBody}; pub use actix_http::body::{Body, BodySize, MessageBody, ResponseBody};

View File

@ -16,7 +16,7 @@ use flate2::Compression;
use futures::stream::once; use futures::stream::once;
use rand::{distributions::Alphanumeric, Rng}; 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"))] #[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))]
use actix_web::middleware::encoding; use actix_web::middleware::encoding;
@ -58,7 +58,7 @@ fn test_body() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
} }
@ -77,7 +77,7 @@ fn test_body_gzip() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // read response
let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); let bytes = srv.block_on(response.body()).unwrap();
// decode // decode
let mut e = GzDecoder::new(&bytes[..]); let mut e = GzDecoder::new(&bytes[..]);
@ -115,7 +115,7 @@ fn test_body_encoding_override() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // read response
let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); let bytes = srv.block_on(response.body()).unwrap();
// decode // decode
let mut e = ZlibDecoder::new(Vec::new()); let mut e = ZlibDecoder::new(Vec::new());
@ -134,7 +134,7 @@ fn test_body_encoding_override() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // read response
let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); let bytes = srv.block_on(response.body()).unwrap();
// decode // decode
let mut e = ZlibDecoder::new(Vec::new()); let mut e = ZlibDecoder::new(Vec::new());
@ -165,7 +165,7 @@ fn test_body_gzip_large() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // read response
let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); let bytes = srv.block_on(response.body()).unwrap();
// decode // decode
let mut e = GzDecoder::new(&bytes[..]); let mut e = GzDecoder::new(&bytes[..]);
@ -199,7 +199,7 @@ fn test_body_gzip_large_random() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // read response
let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); let bytes = srv.block_on(response.body()).unwrap();
// decode // decode
let mut e = GzDecoder::new(&bytes[..]); let mut e = GzDecoder::new(&bytes[..]);
@ -232,7 +232,7 @@ fn test_body_chunked_implicit() {
); );
// read response // read response
let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); let bytes = srv.block_on(response.body()).unwrap();
// decode // decode
let mut e = GzDecoder::new(&bytes[..]); let mut e = GzDecoder::new(&bytes[..]);
@ -267,7 +267,7 @@ fn test_body_br_streaming() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // read response
let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); let bytes = srv.block_on(response.body()).unwrap();
// decode br // decode br
let mut e = BrotliDecoder::new(Vec::with_capacity(2048)); let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
@ -293,7 +293,7 @@ fn test_head_binary() {
} }
// read response // read response
let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); let bytes = srv.block_on(response.body()).unwrap();
assert!(bytes.is_empty()); assert!(bytes.is_empty());
} }
@ -315,7 +315,7 @@ fn test_no_chunking() {
assert!(!response.headers().contains_key(TRANSFER_ENCODING)); assert!(!response.headers().contains_key(TRANSFER_ENCODING));
// read response // 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())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
} }
@ -337,9 +337,8 @@ fn test_body_deflate() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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()); let mut e = ZlibDecoder::new(Vec::new());
e.write_all(bytes.as_ref()).unwrap(); e.write_all(bytes.as_ref()).unwrap();
let dec = e.finish().unwrap(); let dec = e.finish().unwrap();
@ -371,7 +370,7 @@ fn test_body_brotli() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // read response
let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap(); let bytes = srv.block_on(response.body()).unwrap();
// decode brotli // decode brotli
let mut e = BrotliDecoder::new(Vec::with_capacity(2048)); let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
@ -405,7 +404,7 @@ fn test_encoding() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
} }
@ -434,7 +433,7 @@ fn test_gzip_encoding() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
} }
@ -464,7 +463,7 @@ fn test_gzip_encoding_large() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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)); assert_eq!(bytes, Bytes::from(data));
} }
@ -498,7 +497,7 @@ fn test_reading_gzip_encoding_large_random() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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.len(), data.len());
assert_eq!(bytes, Bytes::from(data)); assert_eq!(bytes, Bytes::from(data));
} }
@ -528,7 +527,7 @@ fn test_reading_deflate_encoding() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
} }
@ -558,7 +557,7 @@ fn test_reading_deflate_encoding_large() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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)); assert_eq!(bytes, Bytes::from(data));
} }
@ -592,7 +591,7 @@ fn test_reading_deflate_encoding_large_random() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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.len(), data.len());
assert_eq!(bytes, Bytes::from(data)); assert_eq!(bytes, Bytes::from(data));
} }
@ -622,7 +621,7 @@ fn test_brotli_encoding() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
} }
@ -652,7 +651,7 @@ fn test_brotli_encoding_large() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // 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)); assert_eq!(bytes, Bytes::from(data));
} }