Skip to content

Commit

Permalink
Add our own HTTP date formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Dec 29, 2021
1 parent 248e7da commit 690d0ed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ uuid = { version = "0.8.2", features = ["v4"] }
chrono = { version = "0.4.19", features = ["serde"] }
chrono-tz = "0.6.1"
time = "0.2.27"
httpdate = "1.0"

# Job scheduler
job_scheduler = "1.2.1"
Expand Down
19 changes: 15 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use rocket::{
Data, Request, Response, Rocket,
};

use httpdate::HttpDate;
use std::thread::sleep;
use std::time::{Duration, SystemTime};
use std::time::Duration;

use crate::CONFIG;

Expand Down Expand Up @@ -143,12 +142,13 @@ impl<'r, R: Responder<'r>> Responder<'r> for Cached<R> {
format!("public, max-age={}", self.ttl)
};

let time_now = SystemTime::now();
let time_now = chrono::Local::now();

match self.response.respond_to(req) {
Ok(mut res) => {
res.set_raw_header("Cache-Control", cache_control_header);
res.set_raw_header("Expires", HttpDate::from(time_now + Duration::from_secs(self.ttl)).to_string());
let expiry_time = time_now + chrono::Duration::seconds(self.ttl.try_into().unwrap());
res.set_raw_header("Expires", format_datetime_http(&expiry_time));
Ok(res)
}
e @ Err(_) => e,
Expand Down Expand Up @@ -436,6 +436,17 @@ pub fn format_naive_datetime_local(dt: &NaiveDateTime, fmt: &str) -> String {
format_datetime_local(&Local.from_utc_datetime(dt), fmt)
}

/// Formats a `DateTime<Local>` as required for HTTP
///
/// https://httpwg.org/specs/rfc7231.html#http.date
pub fn format_datetime_http(dt: &DateTime<Local>) -> String {
let expiry_time: chrono::DateTime<chrono::Utc> = chrono::DateTime::from_utc(dt.naive_utc(), chrono::Utc);

// HACK: HTTP expects the date to always be GMT (UTC) rather than giving an
// offset (which would always be 0 in UTC anyway)
return expiry_time.to_rfc2822().replace("+0000", "GMT");
}

//
// Deployment environment methods
//
Expand Down

0 comments on commit 690d0ed

Please sign in to comment.