diff --git a/pingora-core/Cargo.toml b/pingora-core/Cargo.toml index a8af5b70a..1c34fe891 100644 --- a/pingora-core/Cargo.toml +++ b/pingora-core/Cargo.toml @@ -47,7 +47,6 @@ serde_yaml = "0.9" strum = "0.26.2" strum_macros = "0.26.2" libc = "0.2.70" -chrono = { version = "~0.4.31", features = ["alloc"], default-features = false } sentry = { version = "0.36", features = [ "backtrace", "contexts", diff --git a/pingora-core/src/protocols/http/date.rs b/pingora-core/src/protocols/http/date.rs index 610c9386c..b92b6d459 100644 --- a/pingora-core/src/protocols/http/date.rs +++ b/pingora-core/src/protocols/http/date.rs @@ -12,14 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -use chrono::DateTime; use http::header::HeaderValue; +use httpdate::fmt_http_date; use std::cell::RefCell; use std::time::{Duration, SystemTime}; -fn to_date_string(epoch_sec: i64) -> String { - let dt = DateTime::from_timestamp(epoch_sec, 0).unwrap(); - dt.format("%a, %d %b %Y %H:%M:%S GMT").to_string() +fn to_date_string(epoch_sec: u64) -> String { + // HTTP dates have four-digit years. Clamp to the last representable second + // so an out-of-range system clock cannot make httpdate panic. + let epoch_sec = epoch_sec.min(253_402_300_799); + fmt_http_date(SystemTime::UNIX_EPOCH + Duration::from_secs(epoch_sec)) } struct CacheableDate { @@ -33,7 +35,7 @@ impl CacheableDate { .duration_since(SystemTime::UNIX_EPOCH) .unwrap(); CacheableDate { - h1_date: HeaderValue::from_str(&to_date_string(d.as_secs() as i64)).unwrap(), + h1_date: HeaderValue::from_str(&to_date_string(d.as_secs())).unwrap(), epoch: d, } } @@ -41,7 +43,7 @@ impl CacheableDate { pub fn update(&mut self, d_now: Duration) { if d_now.as_secs() != self.epoch.as_secs() { self.epoch = d_now; - self.h1_date = HeaderValue::from_str(&to_date_string(d_now.as_secs() as i64)).unwrap(); + self.h1_date = HeaderValue::from_str(&to_date_string(d_now.as_secs())).unwrap(); } } @@ -67,24 +69,58 @@ pub fn get_cached_date() -> HeaderValue { mod test { use super::*; - fn now_date_header() -> HeaderValue { - HeaderValue::from_str(&to_date_string( - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap() - .as_secs() as i64, - )) - .unwrap() + #[test] + fn test_date_string() { + for (seconds, expected) in [ + (0, "Thu, 01 Jan 1970 00:00:00 GMT"), + (1, "Thu, 01 Jan 1970 00:00:01 GMT"), + (784_111_777, "Sun, 06 Nov 1994 08:49:37 GMT"), + (951_782_399, "Mon, 28 Feb 2000 23:59:59 GMT"), + (951_782_400, "Tue, 29 Feb 2000 00:00:00 GMT"), + (951_868_800, "Wed, 01 Mar 2000 00:00:00 GMT"), + (4_107_542_400, "Mon, 01 Mar 2100 00:00:00 GMT"), + (253_402_300_799, "Fri, 31 Dec 9999 23:59:59 GMT"), + ] { + let actual = to_date_string(seconds); + assert_eq!(actual, expected); + assert_eq!(actual.len(), 29); + assert_eq!( + httpdate::parse_http_date(&actual).unwrap(), + SystemTime::UNIX_EPOCH + Duration::from_secs(seconds) + ); + } } #[test] - fn test_date_string() { - let date_str = to_date_string(1); - assert_eq!("Thu, 01 Jan 1970 00:00:01 GMT", date_str); + fn test_date_string_out_of_range() { + for seconds in [253_402_300_800, i64::MAX as u64, u64::MAX] { + assert_eq!(to_date_string(seconds), "Fri, 31 Dec 9999 23:59:59 GMT"); + } } #[test] fn test_date_cached() { - assert_eq!(get_cached_date(), now_date_header()); + let mut cache = CacheableDate { + h1_date: HeaderValue::from_str(&to_date_string(1)).unwrap(), + epoch: Duration::from_secs(1), + }; + let original = cache.h1_date.clone(); + cache.update(Duration::new(1, 999_999_999)); + assert_eq!(cache.h1_date, original); + // The cached allocation is reused until the second changes. + assert_eq!( + cache.h1_date.as_bytes().as_ptr(), + original.as_bytes().as_ptr() + ); + assert_eq!(cache.epoch, Duration::from_secs(1)); + + cache.update(Duration::from_secs(2)); + assert_eq!(cache.h1_date, "Thu, 01 Jan 1970 00:00:02 GMT"); + assert_eq!(cache.epoch, Duration::from_secs(2)); + + // A clock adjustment backwards also refreshes the header. + cache.update(Duration::from_secs(0)); + assert_eq!(cache.h1_date, "Thu, 01 Jan 1970 00:00:00 GMT"); + assert_eq!(cache.epoch, Duration::from_secs(0)); } }