From 25a05894b97eca5b5e486cf2f4245e638464d318 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 28 Jul 2020 17:28:21 -0700 Subject: [PATCH] refactor(h1): use httpdate for server date header --- Cargo.toml | 2 +- src/proto/h1/date.rs | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 54bc2946..d9135dea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/proto/h1/date.rs b/src/proto/h1/date.rs index 8527c00f..b4d166b3 100644 --- a/src/proto/h1/date.rs +++ b/src/proto/h1/date.rs @@ -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; @@ -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 = RefCell::new(CachedDate::new())); @@ -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 } @@ -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); } } @@ -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;