Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): add an option to disable adding the DATE header #2751

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 11 additions & 0 deletions src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ where
h1_header_read_timeout_running: false,
preserve_header_case: false,
title_case_headers: false,
#[cfg(feature = "server")]
add_date: true,
h09_responses: false,
#[cfg(feature = "ffi")]
on_informational: None,
Expand Down Expand Up @@ -111,6 +113,11 @@ where
self.state.preserve_header_case = true;
}

#[cfg(feature = "server")]
pub(crate) fn disable_add_date(&mut self) {
self.state.add_date = false;
}

#[cfg(feature = "client")]
pub(crate) fn set_h09_responses(&mut self) {
self.state.h09_responses = true;
Expand Down Expand Up @@ -558,6 +565,8 @@ where
keep_alive: self.state.wants_keep_alive(),
req_method: &mut self.state.method,
title_case_headers: self.state.title_case_headers,
#[cfg(feature = "server")]
add_date: self.state.add_date,
},
buf,
) {
Expand Down Expand Up @@ -827,6 +836,8 @@ struct State {
h1_header_read_timeout_running: bool,
preserve_header_case: bool,
title_case_headers: bool,
#[cfg(feature = "server")]
add_date: bool,
h09_responses: bool,
/// If set, called with each 1xx informational response received for
/// the current request. MUST be unset after a non-1xx response is
Expand Down
2 changes: 2 additions & 0 deletions src/proto/h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub(crate) struct Encode<'a, T> {
keep_alive: bool,
req_method: &'a mut Option<Method>,
title_case_headers: bool,
#[cfg(feature = "server")]
add_date: bool,
}

/// Extra flags that a request "wants", like expect-continue or upgrades.
Expand Down
42 changes: 41 additions & 1 deletion src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ impl Server {
}

// cached date is much faster than formatting every request
if !wrote_date {
if !wrote_date && msg.add_date {
dst.reserve(date::DATE_VALUE_LENGTH + 8);
header_name_writer.write_header_name_with_colon(dst, "date: ", header::DATE);
date::extend(dst);
Expand Down Expand Up @@ -2290,6 +2290,7 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: true,
add_date: true,
},
&mut vec,
)
Expand Down Expand Up @@ -2321,6 +2322,7 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: false,
add_date: true,
},
&mut vec,
)
Expand Down Expand Up @@ -2355,6 +2357,7 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: true,
add_date: true,
},
&mut vec,
)
Expand All @@ -2379,6 +2382,7 @@ mod tests {
keep_alive: true,
req_method: &mut Some(Method::CONNECT),
title_case_headers: false,
add_date: true,
},
&mut vec,
)
Expand Down Expand Up @@ -2408,6 +2412,7 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: true,
add_date: true,
},
&mut vec,
)
Expand Down Expand Up @@ -2442,6 +2447,7 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: false,
add_date: true,
},
&mut vec,
)
Expand Down Expand Up @@ -2476,6 +2482,7 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: true,
add_date: true,
},
&mut vec,
)
Expand All @@ -2487,6 +2494,37 @@ mod tests {
assert_eq!(&vec[..expected_response.len()], &expected_response[..]);
}

#[test]
fn test_server_disable_add_date() {
use crate::proto::BodyLength;
use http::header::HeaderValue;

let mut head = MessageHead::default();
head.headers
.insert("content-length", HeaderValue::from_static("10"));
head.headers
.insert("content-type", HeaderValue::from_static("application/json"));

let mut vec = Vec::new();
Server::encode(
Encode {
head: &mut head,
body: Some(BodyLength::Known(10)),
keep_alive: true,
req_method: &mut None,
title_case_headers: false,
add_date: false,
},
&mut vec,
)
.unwrap();

let expected_response =
b"HTTP/1.1 200 OK\r\ncontent-length: 10\r\ncontent-type: application/json\r\n\r\n";

assert_eq!(vec, expected_response);
}

#[test]
fn parse_header_htabs() {
let mut bytes = BytesMut::from("HTTP/1.1 200 OK\r\nserver: hello\tworld\r\n\r\n");
Expand Down Expand Up @@ -2677,6 +2715,7 @@ mod tests {
keep_alive: true,
req_method: &mut Some(Method::GET),
title_case_headers: false,
add_date: true,
},
&mut vec,
)
Expand Down Expand Up @@ -2705,6 +2744,7 @@ mod tests {
keep_alive: true,
req_method: &mut Some(Method::GET),
title_case_headers: false,
add_date: true
},
&mut vec,
)
Expand Down
23 changes: 23 additions & 0 deletions src/server/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub struct Http<E = Exec> {
h1_keep_alive: bool,
h1_title_case_headers: bool,
h1_preserve_header_case: bool,
add_date: bool,
#[cfg(all(feature = "http1", feature = "runtime"))]
h1_header_read_timeout: Option<Duration>,
h1_writev: Option<bool>,
Expand Down Expand Up @@ -286,6 +287,7 @@ impl Http {
h1_keep_alive: true,
h1_title_case_headers: false,
h1_preserve_header_case: false,
add_date: true,
#[cfg(all(feature = "http1", feature = "runtime"))]
h1_header_read_timeout: None,
h1_writev: None,
Expand Down Expand Up @@ -375,6 +377,23 @@ impl<E> Http<E> {
self
}

/// Set whether to automatically add the `DATE` header to responses.
///
/// If true, and a request does not include a `DATE` header, one will be
/// added automatically.
///
/// It is a protocol violation not to include a `DATE` header unless the
/// server does not have a clock capable of providing a reasonable
/// approximation of the time.
///
/// Currently, this setting is unimplemented for HTTP/2.
///
/// Default is `true`.
pub fn add_date(&mut self, enabled: bool) -> &mut Self {
self.add_date = enabled;
self
}

/// Set a timeout for reading client request headers. If a client does not
/// transmit the entire header within this time, the connection is closed.
///
Expand Down Expand Up @@ -605,6 +624,7 @@ impl<E> Http<E> {
h1_keep_alive: self.h1_keep_alive,
h1_title_case_headers: self.h1_title_case_headers,
h1_preserve_header_case: self.h1_preserve_header_case,
add_date: self.add_date,
#[cfg(all(feature = "http1", feature = "runtime"))]
h1_header_read_timeout: self.h1_header_read_timeout,
h1_writev: self.h1_writev,
Expand Down Expand Up @@ -669,6 +689,9 @@ impl<E> Http<E> {
if self.h1_preserve_header_case {
conn.set_preserve_header_case();
}
if !self.add_date {
conn.disable_add_date();
}
#[cfg(all(feature = "http1", feature = "runtime"))]
if let Some(header_read_timeout) = self.h1_header_read_timeout {
conn.set_http1_header_read_timeout(header_read_timeout);
Expand Down
16 changes: 16 additions & 0 deletions src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@ impl<I, E> Builder<I, E> {
self
}

/// Set whether to automatically add the `DATE` header to responses.
///
/// If true, and a request does not include a `DATE` header, one will be
/// added automatically.
///
/// It is a protocol violation not to include a `DATE` header unless the
/// server does not have a clock capable of providing a reasonable
/// approximation of the time.
///
/// Currently, this setting is unimplemented for HTTP/2.
///
/// Default is `true`.
pub fn add_date(mut self, val: bool) -> Self {
self.protocol.add_date(val);
self
}
/// Set a timeout for reading client request headers. If a client does not
/// transmit the entire header within this time, the connection is closed.
///
Expand Down