Skip to content

Commit

Permalink
Expose chunked_threshold on Response. Fix #149
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Nov 1, 2018
1 parent c37807c commit 64ba67b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/response.rs
Expand Up @@ -53,6 +53,7 @@ pub struct Response<R> where R: Read {
status_code: StatusCode,
headers: Vec<Header>,
data_length: Option<usize>,
chunked_threshold: Option<usize>
}

/// A `Response` without a template parameter.
Expand Down Expand Up @@ -113,7 +114,8 @@ fn write_message_header<W>(mut writer: W, http_version: &HTTPVersion,
}

fn choose_transfer_encoding(request_headers: &[Header], http_version: &HTTPVersion,
entity_length: &Option<usize>, has_additional_headers: bool)
entity_length: &Option<usize>, has_additional_headers: bool,
chunked_threshold: usize)
-> TransferEncoding
{
use util;
Expand Down Expand Up @@ -165,8 +167,7 @@ fn choose_transfer_encoding(request_headers: &[Header], http_version: &HTTPVersi
}

// if we don't have a Content-Length, or if the Content-Length is too big, using chunks writer
let chunks_threshold = 32768;
if entity_length.as_ref().map_or(true, |val| *val >= chunks_threshold) {
if entity_length.as_ref().map_or(true, |val| *val >= chunked_threshold) {
return TransferEncoding::Chunked;
}

Expand All @@ -191,6 +192,7 @@ impl<R> Response<R> where R: Read {
status_code: status_code,
headers: Vec::with_capacity(16),
data_length: data_length,
chunked_threshold: None,
};

for h in headers {
Expand All @@ -207,6 +209,23 @@ impl<R> Response<R> where R: Read {
response
}

/// Set a threshold for `Content-Length` where we chose chunked
/// transfer. Notice that chunked transfer might happen regardless of
/// this threshold, for instance when the request headers indicate
/// it is wanted or when there is no `Content-Length`.
pub fn with_chunked_threshold(mut self, length: usize) -> Response<R>{
self.chunked_threshold = Some(length);
self
}

/// The current `Content-Length` threshold for switching over to
/// chunked transfer. The default is 32768 bytes. Notice that
/// chunked transfer is mutually exclusive with sending a
/// `Content-Length` header as per the HTTP spec.
pub fn chunked_threshold(&self) -> usize {
self.chunked_threshold.unwrap_or(32768)
}

/// Adds a header to the list.
/// Does all the checks.
pub fn add_header<H>(&mut self, header: H) where H: Into<Header> {
Expand Down Expand Up @@ -260,6 +279,7 @@ impl<R> Response<R> where R: Read {
headers: self.headers,
status_code: self.status_code,
data_length: data_length,
chunked_threshold: None,
}
}

Expand All @@ -278,7 +298,8 @@ impl<R> Response<R> where R: Read {
-> IoResult<()>
{
let mut transfer_encoding = Some(choose_transfer_encoding(request_headers,
&http_version, &self.data_length, false /* TODO */));
&http_version, &self.data_length, false /* TODO */,
self.chunked_threshold()));

// add `Date` if not in the headers
if self.headers.iter().find(|h| h.field.equiv(&"Date")).is_none() {
Expand Down Expand Up @@ -386,6 +407,7 @@ impl<R> Response<R> where R: Read + Send + 'static {
status_code: self.status_code,
headers: self.headers,
data_length: self.data_length,
chunked_threshold: None,
}
}
}
Expand Down Expand Up @@ -463,6 +485,7 @@ impl Clone for Response<io::Empty> {
status_code: self.status_code.clone(),
headers: self.headers.clone(),
data_length: self.data_length.clone(),
chunked_threshold: self.chunked_threshold.clone(),
}
}
}
8 changes: 8 additions & 0 deletions tests/network.rs
Expand Up @@ -173,3 +173,11 @@ fn connection_timeout() {
tx_stop.send(());
}
*/


#[test]
fn chunked_threshold() {
let resp = tiny_http::Response::from_string("test".to_string());
assert_eq!(resp.chunked_threshold(), 32768);
assert_eq!(resp.with_chunked_threshold(42).chunked_threshold(), 42);
}

0 comments on commit 64ba67b

Please sign in to comment.