fix(http2): add Date header if not present for HTTP2 server responses

This commit is contained in:
Sean McArthur
2018-10-08 17:52:08 -07:00
parent 3e12bccac0
commit 37ec724fd6
5 changed files with 149 additions and 36 deletions

View File

@@ -2,6 +2,7 @@ use std::cell::RefCell;
use std::fmt::{self, Write};
use std::str;
use http::header::HeaderValue;
use time::{self, Duration};
// "Sun, 06 Nov 1994 08:49:37 GMT".len()
@@ -19,6 +20,15 @@ pub fn update() {
})
}
pub(crate) fn update_and_header_value() -> HeaderValue {
CACHED.with(|cache| {
let mut cache = cache.borrow_mut();
cache.check();
HeaderValue::from_bytes(cache.buffer())
.expect("Date format should be valid HeaderValue")
})
}
struct CachedDate {
bytes: [u8; DATE_VALUE_LENGTH],
pos: usize,

View File

@@ -11,7 +11,7 @@ pub use self::io::Cursor; //TODO: move out of h1::io
pub use self::io::MINIMUM_MAX_BUFFER_SIZE;
mod conn;
mod date;
pub(super) mod date;
mod decode;
pub(crate) mod dispatch;
mod encode;

View File

@@ -193,6 +193,15 @@ where
let (head, body) = res.into_parts();
let mut res = ::http::Response::from_parts(head, ());
super::strip_connection_headers(res.headers_mut(), false);
// set Date header if it isn't already set...
res
.headers_mut()
.entry(::http::header::DATE)
.expect("DATE is a valid HeaderName")
.or_insert_with(::proto::h1::date::update_and_header_value);
// automatically set Content-Length from body...
if let Some(len) = body.content_length() {
headers::set_content_length_if_missing(res.headers_mut(), len);
}