Skip to content

Commit

Permalink
refactor(h1): use httpdate for server date header
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Jul 29, 2020
1 parent 48f04b6 commit 25a0589
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ futures-channel = "0.3"
futures-util = { version = "0.3", default-features = false }
http = "0.2"
http-body = "0.3.1"
httpdate = "0.3"
httparse = "1.0"
h2 = "0.2.2"
itoa = "0.4.1"
tracing = { version = "0.1", default-features = false, features = ["log", "std"] }
pin-project = "0.4.20"
time = "0.1"
tower-service = "0.3"
tokio = { version = "0.2.11", features = ["sync"] }
want = "0.3"
Expand Down
22 changes: 11 additions & 11 deletions src/proto/h1/date.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::cell::RefCell;
use std::fmt::{self, Write};
use std::str;
use std::time::{Duration, SystemTime};

use http::header::HeaderValue;
use time::{self, Duration};
use httpdate::HttpDate;

// "Sun, 06 Nov 1994 08:49:37 GMT".len()
pub const DATE_VALUE_LENGTH: usize = 29;
Expand Down Expand Up @@ -31,7 +32,7 @@ pub(crate) fn update_and_header_value() -> HeaderValue {
struct CachedDate {
bytes: [u8; DATE_VALUE_LENGTH],
pos: usize,
next_update: time::Timespec,
next_update: SystemTime,
}

thread_local!(static CACHED: RefCell<CachedDate> = RefCell::new(CachedDate::new()));
Expand All @@ -41,9 +42,9 @@ impl CachedDate {
let mut cache = CachedDate {
bytes: [0; DATE_VALUE_LENGTH],
pos: 0,
next_update: time::Timespec::new(0, 0),
next_update: SystemTime::now(),
};
cache.update(time::get_time());
cache.update(cache.next_update);
cache
}

Expand All @@ -52,21 +53,20 @@ impl CachedDate {
}

fn check(&mut self) {
let now = time::get_time();
let now = SystemTime::now();
if now > self.next_update {
self.update(now);
}
}

fn update(&mut self, now: time::Timespec) {
fn update(&mut self, now: SystemTime) {
self.render(now);
self.next_update = now + Duration::seconds(1);
self.next_update.nsec = 0;
self.next_update = now + Duration::new(1, 0);
}

fn render(&mut self, now: time::Timespec) {
fn render(&mut self, now: SystemTime) {
self.pos = 0;
let _ = write!(self, "{}", time::at_utc(now).rfc822());
let _ = write!(self, "{}", HttpDate::from(now));
debug_assert!(self.pos == DATE_VALUE_LENGTH);
}
}
Expand Down Expand Up @@ -108,7 +108,7 @@ mod tests {
#[bench]
fn bench_date_render(b: &mut Bencher) {
let mut date = CachedDate::new();
let now = time::get_time();
let now = SystemTime::now();
date.render(now);
b.bytes = date.buffer().len() as u64;

Expand Down

0 comments on commit 25a0589

Please sign in to comment.