Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/content_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,19 @@ 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),
None => Vec::new(),
};
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"))]
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ impl Response {
pub struct ResponseBody {
data: Box<dyn Read + Send>,
data_length: Option<usize>,
chunked_threshold: Option<usize>,
}

impl ResponseBody {
Expand All @@ -742,6 +743,7 @@ impl ResponseBody {
ResponseBody {
data: Box::new(io::empty()),
data_length: Some(0),
chunked_threshold: None,
}
}

Expand All @@ -767,6 +769,7 @@ impl ResponseBody {
ResponseBody {
data: Box::new(data),
data_length: None,
chunked_threshold: None,
}
}

Expand All @@ -793,6 +796,7 @@ impl ResponseBody {
ResponseBody {
data: Box::new(data),
data_length: Some(size),
chunked_threshold: None,
}
}

Expand All @@ -815,6 +819,7 @@ impl ResponseBody {
ResponseBody {
data: Box::new(Cursor::new(data)),
data_length: Some(len),
chunked_threshold: None,
}
}

Expand All @@ -836,6 +841,7 @@ impl ResponseBody {
ResponseBody {
data: Box::new(file),
data_length: len,
chunked_threshold: None,
}
}

Expand Down Expand Up @@ -863,11 +869,25 @@ impl ResponseBody {
pub fn into_reader_and_size(self) -> (Box<dyn Read + Send>, Option<usize>) {
(self.data, self.data_length)
}

/// Getter for chunked_threshold.
#[inline]
pub fn chunked_threshold(&self) -> Option<usize> {
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() {
Expand Down Expand Up @@ -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));
}
}