Skip to content

Commit

Permalink
refactor(header): change HttpDate to opaque over SystemTime
Browse files Browse the repository at this point in the history
This removes the need for someone to use the `time` crate to create a
date compatible with HTTP headers. It now works with the `SystemTime`
type from the standard library.

BREAKING CHANGE: `HttpDate` no longer has public fields. Convert between
  `HttpDate` and `SystemTime` as needed.
  • Loading branch information
seanmonstar committed Apr 25, 2017
1 parent 011f28c commit 316c6fa
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 129 deletions.
22 changes: 8 additions & 14 deletions src/header/common/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@ use header::HttpDate;

header! {
/// `Date` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2)
///
///
/// The `Date` header field represents the date and time at which the
/// message was originated.
///
///
/// # ABNF
/// ```plain
/// Date = HTTP-date
/// ```
///
///
/// # Example values
/// * `Tue, 15 Nov 1994 08:12:31 GMT`
///
///
/// # Example
/// ```
/// # extern crate time;
/// # extern crate hyper;
/// # fn main() {
/// // extern crate time;
///
/// use hyper::header::{Headers, Date, HttpDate};
/// use time;
///
/// use hyper::header::{Headers, Date};
/// use std::time::SystemTime;
///
/// let mut headers = Headers::new();
/// headers.set(Date(HttpDate(time::now())));
/// # }
/// headers.set(Date(SystemTime::now().into()));
/// ```
(Date, "Date") => [HttpDate]

Expand Down
25 changes: 10 additions & 15 deletions src/header/common/expires.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,30 @@ use header::HttpDate;

header! {
/// `Expires` header, defined in [RFC7234](http://tools.ietf.org/html/rfc7234#section-5.3)
///
///
/// The `Expires` header field gives the date/time after which the
/// response is considered stale.
///
///
/// The presence of an Expires field does not imply that the original
/// resource will change or cease to exist at, before, or after that
/// time.
///
///
/// # ABNF
/// ```plain
/// Expires = HTTP-date
/// ```
///
///
/// # Example values
/// * `Thu, 01 Dec 1994 16:00:00 GMT`
///
///
/// # Example
/// ```
/// # extern crate hyper;
/// # extern crate time;
/// # fn main() {
/// // extern crate time;
///
/// use hyper::header::{Headers, Expires, HttpDate};
/// use time::{self, Duration};
///
/// use hyper::header::{Headers, Expires};
/// use std::time::{SystemTime, Duration};
///
/// let mut headers = Headers::new();
/// headers.set(Expires(HttpDate(time::now() + Duration::days(1))));
/// # }
/// let expiration = SystemTime::now() + Duration::from_secs(60 * 60 * 24);
/// headers.set(Expires(expiration.into()));
/// ```
(Expires, "Expires") => [HttpDate]

Expand Down
23 changes: 9 additions & 14 deletions src/header/common/if_modified_since.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,29 @@ use header::HttpDate;
header! {
/// `If-Modified-Since` header, defined in
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3)
///
///
/// The `If-Modified-Since` header field makes a GET or HEAD request
/// method conditional on the selected representation's modification date
/// being more recent than the date provided in the field-value.
/// Transfer of the selected representation's data is avoided if that
/// data has not changed.
///
///
/// # ABNF
/// ```plain
/// If-Unmodified-Since = HTTP-date
/// ```
///
///
/// # Example values
/// * `Sat, 29 Oct 1994 19:43:31 GMT`
///
///
/// # Example
/// ```
/// # extern crate hyper;
/// # extern crate time;
/// # fn main() {
/// // extern crate time;
///
/// use hyper::header::{Headers, IfModifiedSince, HttpDate};
/// use time::{self, Duration};
///
/// use hyper::header::{Headers, IfModifiedSince};
/// use std::time::{SystemTime, Duration};
///
/// let mut headers = Headers::new();
/// headers.set(IfModifiedSince(HttpDate(time::now() - Duration::days(1))));
/// # }
/// let modified = SystemTime::now() - Duration::from_secs(60 * 60 * 24);
/// headers.set(IfModifiedSince(modified.into()));
/// ```
(IfModifiedSince, "If-Modified-Since") => [HttpDate]

Expand Down
13 changes: 4 additions & 9 deletions src/header/common/if_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,12 @@ use header::{self, Header, Raw, EntityTag, HttpDate};
/// headers.set(IfRange::EntityTag(EntityTag::new(false, "xyzzy".to_owned())));
/// ```
/// ```
/// # extern crate hyper;
/// # extern crate time;
/// # fn main() {
/// // extern crate time;
///
/// use hyper::header::{Headers, IfRange, HttpDate};
/// use time::{self, Duration};
/// use hyper::header::{Headers, IfRange};
/// use std::time::{SystemTime, Duration};
///
/// let mut headers = Headers::new();
/// headers.set(IfRange::Date(HttpDate(time::now() - Duration::days(1))));
/// # }
/// let fetched = SystemTime::now() - Duration::from_secs(60 * 60 * 24);
/// headers.set(IfRange::Date(fetched.into()));
/// ```
#[derive(Clone, Debug, PartialEq)]
pub enum IfRange {
Expand Down
23 changes: 9 additions & 14 deletions src/header/common/if_unmodified_since.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,29 @@ use header::HttpDate;
header! {
/// `If-Unmodified-Since` header, defined in
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4)
///
///
/// The `If-Unmodified-Since` header field makes the request method
/// conditional on the selected representation's last modification date
/// being earlier than or equal to the date provided in the field-value.
/// This field accomplishes the same purpose as If-Match for cases where
/// the user agent does not have an entity-tag for the representation.
///
///
/// # ABNF
/// ```plain
/// If-Unmodified-Since = HTTP-date
/// ```
///
///
/// # Example values
/// * `Sat, 29 Oct 1994 19:43:31 GMT`
///
///
/// # Example
/// ```
/// # extern crate hyper;
/// # extern crate time;
/// # fn main() {
/// // extern crate time;
///
/// use hyper::header::{Headers, IfUnmodifiedSince, HttpDate};
/// use time::{self, Duration};
///
/// use hyper::header::{Headers, IfUnmodifiedSince};
/// use std::time::{SystemTime, Duration};
///
/// let mut headers = Headers::new();
/// headers.set(IfUnmodifiedSince(HttpDate(time::now() - Duration::days(1))));
/// # }
/// let modified = SystemTime::now() - Duration::from_secs(60 * 60 * 24);
/// headers.set(IfUnmodifiedSince(modified.into()));
/// ```
(IfUnmodifiedSince, "If-Unmodified-Since") => [HttpDate]

Expand Down
23 changes: 9 additions & 14 deletions src/header/common/last_modified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,28 @@ use header::HttpDate;
header! {
/// `Last-Modified` header, defined in
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2)
///
///
/// The `Last-Modified` header field in a response provides a timestamp
/// indicating the date and time at which the origin server believes the
/// selected representation was last modified, as determined at the
/// conclusion of handling the request.
///
///
/// # ABNF
/// ```plain
/// Expires = HTTP-date
/// ```
///
///
/// # Example values
/// * `Sat, 29 Oct 1994 19:43:31 GMT`
///
///
/// # Example
/// ```
/// # extern crate hyper;
/// # extern crate time;
/// # fn main() {
/// // extern crate time;
///
/// use hyper::header::{Headers, LastModified, HttpDate};
/// use time::{self, Duration};
///
/// use hyper::header::{Headers, LastModified};
/// use std::time::{SystemTime, Duration};
///
/// let mut headers = Headers::new();
/// headers.set(LastModified(HttpDate(time::now() - Duration::days(1))));
/// # }
/// let modified = SystemTime::now() - Duration::from_secs(60 * 60 * 24);
/// headers.set(LastModified(modified.into()));
/// ```
(LastModified, "Last-Modified") => [HttpDate]

Expand Down
54 changes: 19 additions & 35 deletions src/header/common/retry_after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
// Version 2.0, January 2004
// http://www.apache.org/licenses/

use std::fmt;
use std::time::Duration;

use header::{Header, Raw};
use header::shared::HttpDate;
use time;
use time::{Duration, Tm};
use std::fmt;

/// The `Retry-After` header.
///
Expand All @@ -53,33 +53,23 @@ use std::fmt;
///
/// # Examples
/// ```
/// # extern crate hyper;
/// # extern crate time;
/// # fn main() {
/// // extern crate time;
/// use time::{Duration};
/// use std::time::Duration;
/// use hyper::header::{Headers, RetryAfter};
///
/// let mut headers = Headers::new();
/// headers.set(
/// RetryAfter::Delay(Duration::seconds(300))
/// RetryAfter::Delay(Duration::from_secs(300))
/// );
/// # }
/// ```
/// ```
/// # extern crate hyper;
/// # extern crate time;
/// # fn main() {
/// // extern crate time;
/// use time;
/// use time::{Duration};
/// use std::time::{SystemTime, Duration};
/// use hyper::header::{Headers, RetryAfter};
///
/// let mut headers = Headers::new();
/// let date = SystemTime::now() + Duration::from_secs(300);
/// headers.set(
/// RetryAfter::DateTime(time::now_utc() + Duration::seconds(300))
/// RetryAfter::DateTime(date.into())
/// );
/// # }
/// ```

/// Retry-After header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.3)
Expand All @@ -91,7 +81,7 @@ pub enum RetryAfter {
Delay(Duration),

/// Retry after the given DateTime
DateTime(Tm),
DateTime(HttpDate),
}

impl Header for RetryAfter {
Expand All @@ -108,11 +98,11 @@ impl Header for RetryAfter {
};

if let Ok(datetime) = utf8_str.parse::<HttpDate>() {
return Ok(RetryAfter::DateTime(datetime.0))
return Ok(RetryAfter::DateTime(datetime))
}

if let Ok(seconds) = utf8_str.parse::<i64>() {
return Ok(RetryAfter::Delay(Duration::seconds(seconds)));
if let Ok(seconds) = utf8_str.parse::<u64>() {
return Ok(RetryAfter::Delay(Duration::from_secs(seconds)));
}

Err(::Error::Header)
Expand All @@ -130,26 +120,20 @@ impl fmt::Display for RetryAfter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
RetryAfter::Delay(ref duration) => {
write!(f, "{}", duration.num_seconds())
write!(f, "{}", duration.as_secs())
},
RetryAfter::DateTime(ref datetime) => {
// According to RFC7231, the sender of an HTTP-date must use the RFC1123 format.
// http://tools.ietf.org/html/rfc7231#section-7.1.1.1
if let Ok(date_string) = time::strftime("%a, %d %b %Y %T GMT", datetime) {
write!(f, "{}", date_string)
} else {
Err(fmt::Error::default())
}
fmt::Display::fmt(datetime, f)
}
}
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;
use header::Header;
use header::shared::HttpDate;
use time::{Duration};

use super::RetryAfter;

Expand All @@ -162,7 +146,7 @@ mod tests {
fn parse_delay() {
let retry_after = RetryAfter::parse_header(&vec![b"1234".to_vec()].into()).unwrap();

assert_eq!(RetryAfter::Delay(Duration::seconds(1234)), retry_after);
assert_eq!(RetryAfter::Delay(Duration::from_secs(1234)), retry_after);
}

macro_rules! test_retry_after_datetime {
Expand All @@ -172,7 +156,7 @@ mod tests {
let dt = "Sun, 06 Nov 1994 08:49:37 GMT".parse::<HttpDate>().unwrap();
let retry_after = RetryAfter::parse_header(&vec![$bytes.to_vec()].into()).expect("parse_header ok");

assert_eq!(RetryAfter::DateTime(dt.0), retry_after);
assert_eq!(RetryAfter::DateTime(dt), retry_after);
}
}
}
Expand All @@ -184,14 +168,14 @@ mod tests {
#[test]
fn hyper_headers_from_raw_delay() {
let retry_after = RetryAfter::parse_header(&b"300".to_vec().into()).unwrap();
assert_eq!(retry_after, RetryAfter::Delay(Duration::seconds(300)));
assert_eq!(retry_after, RetryAfter::Delay(Duration::from_secs(300)));
}

#[test]
fn hyper_headers_from_raw_datetime() {
let retry_after = RetryAfter::parse_header(&b"Sun, 06 Nov 1994 08:49:37 GMT".to_vec().into()).unwrap();
let expected = "Sun, 06 Nov 1994 08:49:37 GMT".parse::<HttpDate>().unwrap();

assert_eq!(retry_after, RetryAfter::DateTime(expected.0));
assert_eq!(retry_after, RetryAfter::DateTime(expected));
}
}
Loading

0 comments on commit 316c6fa

Please sign in to comment.