diff --git a/src/content_encoding.rs b/src/content_encoding.rs index 332f6c30..4b8a4146 100644 --- a/src/content_encoding.rs +++ b/src/content_encoding.rs @@ -116,6 +116,7 @@ fn gzip(response: &mut Response) { .headers .push(("Content-Encoding".into(), "gzip".into())); let previous_body = mem::replace(&mut response.data, ResponseBody::empty()); + let chunked_threshold = previous_body.chunked_threshold(); let (mut raw_data, size) = previous_body.into_reader_and_size(); let mut src = match size { Some(size) => Vec::with_capacity(size), @@ -123,7 +124,11 @@ fn gzip(response: &mut Response) { }; io::copy(&mut raw_data, &mut src).expect("Failed reading response body while gzipping"); let zipped = deflate_bytes_gzip(&src); - response.data = ResponseBody::from_data(zipped); + let mut data = ResponseBody::from_data(zipped); + if let Some(len) = chunked_threshold { + data = data.with_chunked_threshold(len); + } + response.data = data; } #[cfg(not(feature = "gzip"))] diff --git a/src/lib.rs b/src/lib.rs index 21e8040b..ba92eeae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -615,10 +615,15 @@ where }; // writing the response + let chunked_threshold = rouille_response.data.chunked_threshold(); let (res_data, res_len) = rouille_response.data.into_reader_and_size(); let mut response = tiny_http::Response::empty(rouille_response.status_code) .with_data(res_data, res_len); + if let Some(value) = chunked_threshold { + response = response.with_chunked_threshold(value); + } + let mut upgrade_header = "".into(); for (key, value) in rouille_response.headers { diff --git a/src/response.rs b/src/response.rs index 8f8f2ee4..1a014343 100644 --- a/src/response.rs +++ b/src/response.rs @@ -726,6 +726,7 @@ impl Response { pub struct ResponseBody { data: Box, data_length: Option, + chunked_threshold: Option, } impl ResponseBody { @@ -742,6 +743,7 @@ impl ResponseBody { ResponseBody { data: Box::new(io::empty()), data_length: Some(0), + chunked_threshold: None, } } @@ -767,6 +769,7 @@ impl ResponseBody { ResponseBody { data: Box::new(data), data_length: None, + chunked_threshold: None, } } @@ -793,6 +796,7 @@ impl ResponseBody { ResponseBody { data: Box::new(data), data_length: Some(size), + chunked_threshold: None, } } @@ -815,6 +819,7 @@ impl ResponseBody { ResponseBody { data: Box::new(Cursor::new(data)), data_length: Some(len), + chunked_threshold: None, } } @@ -836,6 +841,7 @@ impl ResponseBody { ResponseBody { data: Box::new(file), data_length: len, + chunked_threshold: None, } } @@ -863,11 +869,25 @@ impl ResponseBody { pub fn into_reader_and_size(self) -> (Box, Option) { (self.data, self.data_length) } + + /// Getter for chunked_threshold. + #[inline] + pub fn chunked_threshold(&self) -> Option { + self.chunked_threshold + } + + /// Defines the chunked threshold for the data in this request, overriding the default. + #[inline] + pub fn with_chunked_threshold(mut self, len: usize) -> ResponseBody { + self.chunked_threshold = Some(len); + self + } } #[cfg(test)] mod tests { use Response; + use ResponseBody; #[test] fn unique_header_adds() { @@ -912,4 +932,13 @@ mod tests { assert_eq!(r.headers.len(), 1); assert_eq!(r.headers[0], ("foo".into(), "Bar".into())); } + + #[test] + fn can_set_chunked_threshold() { + let r = ResponseBody::empty(); + + assert_eq!(r.chunked_threshold(), None); + assert_eq!(r.with_chunked_threshold(123).chunked_threshold(), Some(123)); + } } +