Skip to content

Commit e255f88

Browse files
committed
feat(headers): add Expires header
1 parent b1761ad commit e255f88

File tree

4 files changed

+83
-39
lines changed

4 files changed

+83
-39
lines changed

src/header/common/date.rs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use header::{Header, HeaderFormat};
21
use std::fmt::{mod, Show};
3-
use super::util::from_one_raw_str;
42
use std::str::FromStr;
5-
use time::{Tm, strptime};
3+
use time::Tm;
4+
use header::{Header, HeaderFormat};
5+
use super::util::{from_one_raw_str, tm_from_str};
66

77
// Egh, replace as soon as something better than time::Tm exists.
88
/// The `Date` header field.
@@ -21,17 +21,10 @@ impl Header for Date {
2121
}
2222
}
2323

24+
2425
impl HeaderFormat for Date {
2526
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
26-
self.fmt(fmt)
27-
}
28-
}
29-
30-
impl fmt::Show for Date {
31-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
32-
let Date(ref tm) = *self;
33-
// bummer that tm.strftime allocates a string. It would nice if it
34-
// returned a Show instead, since I don't need the String here
27+
let tm = **self;
3528
match tm.tm_utcoff {
3629
0 => tm.rfc822().fmt(fmt),
3730
_ => tm.to_utc().rfc822().fmt(fmt)
@@ -40,34 +33,8 @@ impl fmt::Show for Date {
4033
}
4134

4235
impl FromStr for Date {
43-
// Prior to 1995, there were three different formats commonly used by
44-
// servers to communicate timestamps. For compatibility with old
45-
// implementations, all three are defined here. The preferred format is
46-
// a fixed-length and single-zone subset of the date and time
47-
// specification used by the Internet Message Format [RFC5322].
48-
//
49-
// HTTP-date = IMF-fixdate / obs-date
50-
//
51-
// An example of the preferred format is
52-
//
53-
// Sun, 06 Nov 1994 08:49:37 GMT ; IMF-fixdate
54-
//
55-
// Examples of the two obsolete formats are
56-
//
57-
// Sunday, 06-Nov-94 08:49:37 GMT ; obsolete RFC 850 format
58-
// Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
59-
//
60-
// A recipient that parses a timestamp value in an HTTP header field
61-
// MUST accept all three HTTP-date formats. When a sender generates a
62-
// header field that contains one or more timestamps defined as
63-
// HTTP-date, the sender MUST generate those timestamps in the
64-
// IMF-fixdate format.
6536
fn from_str(s: &str) -> Option<Date> {
66-
strptime(s, "%a, %d %b %Y %T %Z").or_else(|_| {
67-
strptime(s, "%A, %d-%b-%y %T %Z")
68-
}).or_else(|_| {
69-
strptime(s, "%c")
70-
}).ok().map(|tm| Date(tm))
37+
tm_from_str(s).map(Date)
7138
}
7239
}
7340

src/header/common/expires.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::fmt::{mod, Show};
2+
use std::str::FromStr;
3+
use time::Tm;
4+
use header::{Header, HeaderFormat};
5+
use super::util::{from_one_raw_str, tm_from_str};
6+
7+
/// The `Expires` header field.
8+
#[deriving(PartialEq, Clone)]
9+
pub struct Expires(pub Tm);
10+
11+
deref!(Expires -> Tm)
12+
13+
impl Header for Expires {
14+
fn header_name(_: Option<Expires>) -> &'static str {
15+
"Expires"
16+
}
17+
18+
fn parse_header(raw: &[Vec<u8>]) -> Option<Expires> {
19+
from_one_raw_str(raw)
20+
}
21+
}
22+
23+
24+
impl HeaderFormat for Expires {
25+
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
26+
let tm = **self;
27+
match tm.tm_utcoff {
28+
0 => tm.rfc822().fmt(fmt),
29+
_ => tm.to_utc().rfc822().fmt(fmt)
30+
}
31+
}
32+
}
33+
34+
impl FromStr for Expires {
35+
fn from_str(s: &str) -> Option<Expires> {
36+
tm_from_str(s).map(Expires)
37+
}
38+
}
39+
40+
bench_header!(imf_fixdate, Expires, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] })
41+
bench_header!(rfc_850, Expires, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] })
42+
bench_header!(asctime, Expires, { vec![b"Sun Nov 6 08:49:37 1994".to_vec()] })

src/header/common/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ pub mod content_type;
9696
/// Exposes the Date header.
9797
pub mod date;
9898

99+
/// Exposes the Expires header.
100+
pub mod expires;
101+
99102
/// Exposes the Host header.
100103
pub mod host;
101104

src/header/common/util.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::str::{FromStr, from_utf8};
44
use std::fmt::{mod, Show};
5+
use time::{Tm, strptime};
56

67
/// Reads a single raw string when parsing a header
78
pub fn from_one_raw_str<T: FromStr>(raw: &[Vec<u8>]) -> Option<T> {
@@ -43,3 +44,34 @@ pub fn fmt_comma_delimited<T: Show>(fmt: &mut fmt::Formatter, parts: &[T]) -> fm
4344
}
4445
Ok(())
4546
}
47+
48+
/// Get a Tm from HTTP date formats.
49+
// Prior to 1995, there were three different formats commonly used by
50+
// servers to communicate timestamps. For compatibility with old
51+
// implementations, all three are defined here. The preferred format is
52+
// a fixed-length and single-zone subset of the date and time
53+
// specification used by the Internet Message Format [RFC5322].
54+
//
55+
// HTTP-date = IMF-fixdate / obs-date
56+
//
57+
// An example of the preferred format is
58+
//
59+
// Sun, 06 Nov 1994 08:49:37 GMT ; IMF-fixdate
60+
//
61+
// Examples of the two obsolete formats are
62+
//
63+
// Sunday, 06-Nov-94 08:49:37 GMT ; obsolete RFC 850 format
64+
// Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
65+
//
66+
// A recipient that parses a timestamp value in an HTTP header field
67+
// MUST accept all three HTTP-date formats. When a sender generates a
68+
// header field that contains one or more timestamps defined as
69+
// HTTP-date, the sender MUST generate those timestamps in the
70+
// IMF-fixdate format.
71+
pub fn tm_from_str(s: &str) -> Option<Tm> {
72+
strptime(s, "%a, %d %b %Y %T %Z").or_else(|_| {
73+
strptime(s, "%A, %d-%b-%y %T %Z")
74+
}).or_else(|_| {
75+
strptime(s, "%c")
76+
}).ok()
77+
}

0 commit comments

Comments
 (0)