Skip to content

Commit

Permalink
Upgrade to time 0.2
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
jtgeibel committed May 3, 2020
1 parent 1d4e6c7 commit 6805f64
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ the response is fresh.
[dependencies]
conduit = "0.9.0-alpha.2"
conduit-middleware = "0.9.0-alpha.2"
time = "0.1.0"
time = { version = "0.2", default-features = false, features = ["std"] }

[dev-dependencies]
conduit-test = "0.9.0-alpha.2"
46 changes: 21 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate time;
use conduit::{header, Body, HeaderMap, Method, RequestExt, Response, StatusCode};
use conduit_middleware::{AfterResult, Middleware};
use std::borrow::Cow;
use time::{ParseError, Tm};
use time::{OffsetDateTime, ParseError, PrimitiveDateTime};

#[allow(missing_copy_implementations)]
pub struct ConditionalGet;
Expand Down Expand Up @@ -66,14 +66,14 @@ fn etag_matches(none_match: &[u8], res: &Response<Body>) -> bool {
value == none_match
}

fn is_modified_since(modified_since: Tm, res: &Response<Body>) -> bool {
fn is_modified_since(modified_since: OffsetDateTime, res: &Response<Body>) -> bool {
let last_modified = get_and_concat_header(res.headers(), header::LAST_MODIFIED);

match std::str::from_utf8(&last_modified) {
Err(_) => false,
Ok(last_modified) => match parse_http_date(last_modified) {
Err(_) => false,
Ok(last_modified) => modified_since.to_timespec() >= last_modified.to_timespec(),
Ok(last_modified) => modified_since.timestamp() >= last_modified.timestamp(),
},
}
}
Expand All @@ -90,23 +90,24 @@ fn get_and_concat_header(headers: &HeaderMap, name: header::HeaderName) -> Cow<'
}
}

fn parse_http_date(string: &str) -> Result<Tm, ()> {
fn parse_http_date(string: &str) -> Result<OffsetDateTime, ()> {
parse_rfc1123(string)
.or_else(|_| parse_rfc850(string))
.or_else(|_| parse_asctime(string))
.map_err(|_| ())
}

fn parse_rfc1123(string: &str) -> Result<Tm, ParseError> {
time::strptime(string, "%a, %d %b %Y %T GMT")
fn parse_rfc1123(string: &str) -> Result<OffsetDateTime, ParseError> {
Ok(PrimitiveDateTime::parse(string, "%a, %d %b %Y %T GMT")?.assume_utc())
}

fn parse_rfc850(string: &str) -> Result<Tm, ParseError> {
time::strptime(string, "%a, %d-%m-%y %T GMT")
fn parse_rfc850(string: &str) -> Result<OffsetDateTime, ParseError> {
Ok(PrimitiveDateTime::parse(string, "%a, %d-%m-%y %T GMT")?.assume_utc())
}

fn parse_asctime(string: &str) -> Result<Tm, ParseError> {
time::strptime(string, "%a %m%t%d %T %Y")
fn parse_asctime(string: &str) -> Result<OffsetDateTime, ParseError> {
// TODO: should this be "%a %b %d %T %Y"?
Ok(PrimitiveDateTime::parse(string, "%a %m\t%d %T %Y")?.assume_utc())
}

#[cfg(test)]
Expand All @@ -119,8 +120,7 @@ mod tests {
StatusCode,
};
use conduit_middleware::MiddlewareBuilder;
use time;
use time::Tm;
use time::{Duration, OffsetDateTime};

use super::ConditionalGet;

Expand Down Expand Up @@ -149,17 +149,17 @@ mod tests {

#[test]
fn test_sends_304() {
let handler = returning!(header::LAST_MODIFIED => httpdate(time::now()));
let handler = returning!(header::LAST_MODIFIED => httpdate(OffsetDateTime::now_utc()));
expect_304(handler.call(&mut request!(
header::IF_MODIFIED_SINCE => httpdate(time::now())
header::IF_MODIFIED_SINCE => httpdate(OffsetDateTime::now_utc())
)));
}

#[test]
fn test_sends_304_if_older_than_now() {
let handler = returning!(header::LAST_MODIFIED => before_now());
expect_304(handler.call(&mut request!(
header::IF_MODIFIED_SINCE => httpdate(time::now())
header::IF_MODIFIED_SINCE => httpdate(OffsetDateTime::now_utc())
)));
}

Expand Down Expand Up @@ -228,10 +228,7 @@ mod tests {

#[test]
fn test_does_not_affect_malformed_timestamp() {
let bad_stamp = time::now()
.strftime("%Y-%m-%d %H:%M:%S %z")
.unwrap()
.to_string();
let bad_stamp = OffsetDateTime::now_utc().format("%Y-%m-%d %H:%M:%S %z");
let handler = returning!(header::LAST_MODIFIED => before_now());
expect_200(handler.call(&mut request!(
header::IF_MODIFIED_SINCE => bad_stamp
Expand Down Expand Up @@ -281,16 +278,15 @@ mod tests {
}

fn before_now() -> String {
let mut now = time::now();
now.tm_year -= 1;
httpdate(now)
let now = OffsetDateTime::now_utc();
httpdate(now - Duration::weeks(52))
}

fn now() -> String {
httpdate(time::now())
httpdate(OffsetDateTime::now_utc())
}

fn httpdate(time: Tm) -> String {
time.strftime("%a, %d-%m-%y %T GMT").unwrap().to_string()
fn httpdate(time: OffsetDateTime) -> String {
time.format("%a, %d-%m-%y %T GMT")
}
}

0 comments on commit 6805f64

Please sign in to comment.