Skip to content

Commit 6cf052b

Browse files
committed
refactor(header): Implement HttpDate, a wrapper for dates
Using `time::Tm` directly in HTTP header fields requires special handling to parse and format the header values., this stops us from using the header macros. By wrapping `time::Time` in a `HttpDate`, we can use the `FromStr` and `Display` traits of `HttpDate` like for most other values. BREAKING_CHANGE: All code using one of the `Date`, `Expires`, `If-Modified-Since`, `If-Unmodified-Since`, `Last-Modified` header fields needs to wrap `time::Tm` with `HttpDate`. Removed `FromStr` trait of `Date`, `If-Modified-Sice` and `If-Unmodified-Sice`, implementing the trait here is inconsistent with other headers.
1 parent 4c5a42b commit 6cf052b

File tree

10 files changed

+110
-262
lines changed

10 files changed

+110
-262
lines changed

src/header/common/date.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,10 @@
1-
use std::fmt;
2-
use std::str::FromStr;
3-
use time::Tm;
4-
use header::{Header, HeaderFormat};
5-
use header::parsing::from_one_raw_str;
6-
use header::parsing::tm_from_str;
1+
use header::HttpDate;
72

8-
// Egh, replace as soon as something better than time::Tm exists.
93
/// The `Date` header field.
104
#[derive(Copy, PartialEq, Clone, Debug)]
11-
pub struct Date(pub Tm);
5+
pub struct Date(pub HttpDate);
126

13-
deref!(Date => Tm);
14-
15-
impl Header for Date {
16-
fn header_name() -> &'static str {
17-
"Date"
18-
}
19-
20-
fn parse_header(raw: &[Vec<u8>]) -> Option<Date> {
21-
from_one_raw_str(raw)
22-
}
23-
}
24-
25-
26-
impl HeaderFormat for Date {
27-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
28-
let tm = self.0;
29-
let tm = match tm.tm_utcoff {
30-
0 => tm,
31-
_ => tm.to_utc(),
32-
};
33-
fmt::Display::fmt(&tm.rfc822(), fmt)
34-
}
35-
}
36-
37-
impl FromStr for Date {
38-
type Err = ();
39-
fn from_str(s: &str) -> Result<Date, ()> {
40-
tm_from_str(s).map(Date).ok_or(())
41-
}
42-
}
7+
impl_header!(Date, "Date", HttpDate);
438

449
bench_header!(imf_fixdate, Date, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });
4510
bench_header!(rfc_850, Date, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] });

src/header/common/expires.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,9 @@
1-
use std::fmt;
2-
use std::str::FromStr;
3-
use time::Tm;
4-
use header::{Header, HeaderFormat};
5-
use header::parsing::from_one_raw_str;
6-
use header::parsing::tm_from_str;
1+
use header::HttpDate;
72

83
/// The `Expires` header field.
94
#[derive(Copy, PartialEq, Clone, Debug)]
10-
pub struct Expires(pub Tm);
11-
12-
deref!(Expires => Tm);
13-
14-
impl Header for Expires {
15-
fn header_name() -> &'static str {
16-
"Expires"
17-
}
18-
19-
fn parse_header(raw: &[Vec<u8>]) -> Option<Expires> {
20-
from_one_raw_str(raw)
21-
}
22-
}
23-
24-
25-
impl HeaderFormat for Expires {
26-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
27-
let tm = self.0;
28-
let tm = match tm.tm_utcoff {
29-
0 => tm,
30-
_ => tm.to_utc(),
31-
};
32-
fmt::Display::fmt(&tm.rfc822(), fmt)
33-
}
34-
}
35-
36-
impl FromStr for Expires {
37-
type Err = ();
38-
fn from_str(s: &str) -> Result<Expires, ()> {
39-
tm_from_str(s).map(Expires).ok_or(())
40-
}
41-
}
5+
pub struct Expires(pub HttpDate);
6+
impl_header!(Expires, "Expires", HttpDate);
427

438
bench_header!(imf_fixdate, Expires, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });
449
bench_header!(rfc_850, Expires, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] });

src/header/common/if_modified_since.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,9 @@
1-
use std::fmt;
2-
use std::str::FromStr;
3-
use time::Tm;
4-
use header::{Header, HeaderFormat};
5-
use header::parsing::from_one_raw_str;
6-
use header::parsing::tm_from_str;
1+
use header::HttpDate;
72

83
/// The `If-Modified-Since` header field.
94
#[derive(Copy, PartialEq, Clone, Debug)]
10-
pub struct IfModifiedSince(pub Tm);
11-
12-
deref!(IfModifiedSince => Tm);
13-
14-
impl Header for IfModifiedSince {
15-
fn header_name() -> &'static str {
16-
"If-Modified-Since"
17-
}
18-
19-
fn parse_header(raw: &[Vec<u8>]) -> Option<IfModifiedSince> {
20-
from_one_raw_str(raw)
21-
}
22-
}
23-
24-
25-
impl HeaderFormat for IfModifiedSince {
26-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
27-
let tm = self.0;
28-
let tm = match tm.tm_utcoff {
29-
0 => tm,
30-
_ => tm.to_utc(),
31-
};
32-
fmt::Display::fmt(&tm.rfc822(), fmt)
33-
}
34-
}
35-
36-
impl FromStr for IfModifiedSince {
37-
type Err = ();
38-
fn from_str(s: &str) -> Result<IfModifiedSince, ()> {
39-
tm_from_str(s).map(IfModifiedSince).ok_or(())
40-
}
41-
}
5+
pub struct IfModifiedSince(pub HttpDate);
6+
impl_header!(IfModifiedSince, "If-Modified-Since", HttpDate);
427

438
bench_header!(imf_fixdate, IfModifiedSince, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });
449
bench_header!(rfc_850, IfModifiedSince, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] });

src/header/common/if_unmodified_since.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
1-
use std::fmt;
2-
use std::str::FromStr;
3-
use time::Tm;
4-
use header::{Header, HeaderFormat};
5-
use header::parsing::from_one_raw_str;
6-
use header::parsing::tm_from_str;
1+
use header::HttpDate;
72

83
/// The `If-Unmodified-Since` header field.
94
#[derive(Copy, PartialEq, Clone, Debug)]
10-
pub struct IfUnmodifiedSince(pub Tm);
5+
pub struct IfUnmodifiedSince(pub HttpDate);
116

12-
deref!(IfUnmodifiedSince => Tm);
13-
14-
impl Header for IfUnmodifiedSince {
15-
fn header_name() -> &'static str {
16-
"If-Unmodified-Since"
17-
}
18-
19-
fn parse_header(raw: &[Vec<u8>]) -> Option<IfUnmodifiedSince> {
20-
from_one_raw_str(raw)
21-
}
22-
}
23-
24-
25-
impl HeaderFormat for IfUnmodifiedSince {
26-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
27-
let tm = self.0;
28-
let tm = match tm.tm_utcoff {
29-
0 => tm,
30-
_ => tm.to_utc(),
31-
};
32-
fmt::Display::fmt(&tm.rfc822(), fmt)
33-
}
34-
}
35-
36-
impl FromStr for IfUnmodifiedSince {
37-
type Err = ();
38-
fn from_str(s: &str) -> Result<IfUnmodifiedSince, ()> {
39-
tm_from_str(s).map(IfUnmodifiedSince).ok_or(())
40-
}
41-
}
7+
impl_header!(IfUnmodifiedSince, "If-Unmodified-Since", HttpDate);
428

439
bench_header!(imf_fixdate, IfUnmodifiedSince, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });
4410
bench_header!(rfc_850, IfUnmodifiedSince, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] });

src/header/common/last_modified.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
1-
use std::fmt;
2-
use std::str::FromStr;
3-
use time::Tm;
4-
use header::{Header, HeaderFormat};
5-
use header::parsing::from_one_raw_str;
6-
use header::parsing::tm_from_str;
1+
use header::HttpDate;
72

83
/// The `LastModified` header field.
94
#[derive(Copy, PartialEq, Clone, Debug)]
10-
pub struct LastModified(pub Tm);
5+
pub struct LastModified(pub HttpDate);
116

12-
deref!(LastModified => Tm);
13-
14-
impl Header for LastModified {
15-
fn header_name() -> &'static str {
16-
"Last-Modified"
17-
}
18-
19-
fn parse_header(raw: &[Vec<u8>]) -> Option<LastModified> {
20-
from_one_raw_str(raw)
21-
}
22-
}
23-
24-
25-
impl HeaderFormat for LastModified {
26-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
27-
let tm = self.0;
28-
let tm = match tm.tm_utcoff {
29-
0 => tm,
30-
_ => tm.to_utc(),
31-
};
32-
fmt::Display::fmt(&tm.rfc822(), fmt)
33-
}
34-
}
35-
36-
impl FromStr for LastModified {
37-
type Err = ();
38-
fn from_str(s: &str) -> Result<LastModified, ()> {
39-
tm_from_str(s).map(LastModified).ok_or(())
40-
}
41-
}
7+
impl_header!(LastModified, "Last-Modified", HttpDate);
428

439
bench_header!(imf_fixdate, LastModified, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });
4410
bench_header!(rfc_850, LastModified, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] });

src/header/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use unicase::UniCase;
2121
use self::internals::Item;
2222
use error::HttpResult;
2323

24-
pub use self::shared::{Charset, Encoding, EntityTag, Quality, QualityItem, qitem, q};
24+
pub use self::shared::{Charset, Encoding, EntityTag, HttpDate, Quality, QualityItem, qitem, q};
2525
pub use self::common::*;
2626

2727
mod common;

src/header/parsing.rs

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::str;
44
use std::fmt;
5-
use time;
65

76
/// Reads a single raw string when parsing a header
87
pub fn from_one_raw_str<T: str::FromStr>(raw: &[Vec<u8>]) -> Option<T> {
@@ -51,74 +50,3 @@ pub fn fmt_comma_delimited<T: fmt::Display>(fmt: &mut fmt::Formatter, parts: &[T
5150
}
5251
Ok(())
5352
}
54-
55-
/// Get a Tm from HTTP date formats.
56-
// Prior to 1995, there were three different formats commonly used by
57-
// servers to communicate timestamps. For compatibility with old
58-
// implementations, all three are defined here. The preferred format is
59-
// a fixed-length and single-zone subset of the date and time
60-
// specification used by the Internet Message Format [RFC5322].
61-
//
62-
// HTTP-date = IMF-fixdate / obs-date
63-
//
64-
// An example of the preferred format is
65-
//
66-
// Sun, 06 Nov 1994 08:49:37 GMT ; IMF-fixdate
67-
//
68-
// Examples of the two obsolete formats are
69-
//
70-
// Sunday, 06-Nov-94 08:49:37 GMT ; obsolete RFC 850 format
71-
// Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
72-
//
73-
// A recipient that parses a timestamp value in an HTTP header field
74-
// MUST accept all three HTTP-date formats. When a sender generates a
75-
// header field that contains one or more timestamps defined as
76-
// HTTP-date, the sender MUST generate those timestamps in the
77-
// IMF-fixdate format.
78-
pub fn tm_from_str(s: &str) -> Option<time::Tm> {
79-
time::strptime(s, "%a, %d %b %Y %T %Z").or_else(|_| {
80-
time::strptime(s, "%A, %d-%b-%y %T %Z")
81-
}).or_else(|_| {
82-
time::strptime(s, "%c")
83-
}).ok()
84-
}
85-
86-
#[cfg(test)]
87-
mod tests {
88-
use time::Tm;
89-
use super::tm_from_str;
90-
91-
const NOV_07: Tm = Tm {
92-
tm_nsec: 0,
93-
tm_sec: 37,
94-
tm_min: 48,
95-
tm_hour: 8,
96-
tm_mday: 7,
97-
tm_mon: 10,
98-
tm_year: 94,
99-
tm_wday: 0,
100-
tm_isdst: 0,
101-
tm_yday: 0,
102-
tm_utcoff: 0,
103-
};
104-
105-
#[test]
106-
fn test_imf_fixdate() {
107-
assert_eq!(tm_from_str("Sun, 07 Nov 1994 08:48:37 GMT"),
108-
Some(NOV_07));
109-
}
110-
111-
#[test]
112-
fn test_rfc_850() {
113-
assert_eq!(tm_from_str("Sunday, 07-Nov-94 08:48:37 GMT"),
114-
Some(NOV_07));
115-
}
116-
117-
#[test]
118-
fn test_asctime() {
119-
assert_eq!(tm_from_str("Sun Nov 7 08:48:37 1994"),
120-
Some(NOV_07));
121-
}
122-
123-
124-
}

0 commit comments

Comments
 (0)