diff --git a/benches/client.rs b/benches/client.rs index c7a8605e..95836168 100644 --- a/benches/client.rs +++ b/benches/client.rs @@ -39,8 +39,8 @@ fn bench_curl(b: &mut test::Bencher) { struct Foo; impl hyper::header::Header for Foo { - fn header_name(_: Option) -> SendStr { - Slice("x-foo") + fn header_name(_: Option) -> &'static str { + "x-foo" } fn parse_header(_: &[Vec]) -> Option { None diff --git a/src/header.rs b/src/header.rs index 69acb03a..1834929b 100644 --- a/src/header.rs +++ b/src/header.rs @@ -22,6 +22,7 @@ use std::string::raw; use std::collections::hashmap::{HashMap, Entries}; use mime::Mime; +use time::{Tm, strptime}; use rfc7230::read_header; use {HttpResult}; @@ -522,6 +523,66 @@ impl Header for Server { } } +// Egh, replace as soon as something better than time::Tm exists. +/// The `Date` header field. +#[deriving(PartialEq, Clone)] +pub struct Date(pub Tm); + +impl Header for Date { + fn header_name(_: Option) -> &'static str { + "date" + } + + fn parse_header(raw: &[Vec]) -> Option { + from_one_raw_str(raw) + } + + fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + self.fmt(fmt) + } +} + +impl fmt::Show for Date { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let Date(ref tm) = *self; + // bummer that tm.strftime allocates a string. It would nice if it + // returned a Show instead, since I don't need the String here + write!(fmt, "{}", tm.to_utc().rfc822()) + } +} + +impl FromStr for Date { + // Prior to 1995, there were three different formats commonly used by + // servers to communicate timestamps. For compatibility with old + // implementations, all three are defined here. The preferred format is + // a fixed-length and single-zone subset of the date and time + // specification used by the Internet Message Format [RFC5322]. + // + // HTTP-date = IMF-fixdate / obs-date + // + // An example of the preferred format is + // + // Sun, 06 Nov 1994 08:49:37 GMT ; IMF-fixdate + // + // Examples of the two obsolete formats are + // + // Sunday, 06-Nov-94 08:49:37 GMT ; obsolete RFC 850 format + // Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format + // + // A recipient that parses a timestamp value in an HTTP header field + // MUST accept all three HTTP-date formats. When a sender generates a + // header field that contains one or more timestamps defined as + // HTTP-date, the sender MUST generate those timestamps in the + // IMF-fixdate format. + fn from_str(s: &str) -> Option { + strptime(s, "%a, %d %b %Y %T %Z").or_else(|_| { + strptime(s, "%A, %d-%b-%y %T %Z") + }).or_else(|_| { + strptime(s, "%c") + }).ok().map(|tm| Date(tm)) + } +} + fn from_one_raw_str(raw: &[Vec]) -> Option { if raw.len() != 1 { return None; diff --git a/src/lib.rs b/src/lib.rs index 5e79811d..6f765746 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ #![deny(warnings)] #![experimental] +extern crate time; extern crate url; #[phase(plugin,link)] extern crate log; #[cfg(test)] extern crate test; diff --git a/src/server/response.rs b/src/server/response.rs index c18611d2..7c41f1e6 100644 --- a/src/server/response.rs +++ b/src/server/response.rs @@ -5,6 +5,8 @@ use std::io::{BufferedWriter, IoResult}; use std::io::net::tcp::TcpStream; +use time::now_utc; + use header; use status; use version; @@ -46,6 +48,10 @@ impl Response { debug!("writing head: {} {}", self.version, self.status); try!(write!(self.body, "{} {}{}{}", self.version, self.status, CR as char, LF as char)); + if !self.headers.has::() { + self.headers.set(header::Date(now_utc())); + } + for (name, header) in self.headers.iter() { debug!("headers {}: {}", name, header); try!(write!(self.body, "{}: {}", name, header));