1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
extern crate chrono;
extern crate hyper;

use chrono::{UTC, TimeZone, DateTime};
use hyper::header::{Header, HeaderFormat};

/// Retry-After header, defined in [RFC7231][RFC7231-7.1.3]
///
/// [RFC7231-7.1.3]: http://tools.ietf.org/html/rfc7231#section-7.1.3
pub enum RetryAfter {
    Delay(chrono::Duration),
    HttpDate(chrono::DateTime<UTC>),
}

static RFC850_FMT: &'static str =  "%A, %d-%b-%y %T GMT";
static RFC1123_FMT: &'static str = "%a, %d %b %Y %T GMT";
static ASCTIME_FMT: &'static str = "%a %b %e %T %Y";

fn parse_http_date(raw: &str) -> Result<DateTime<UTC>, &'static str> {

    if let Ok(dt) = UTC.datetime_from_str(raw, RFC1123_FMT) {
        Ok(dt)
    } else if let Ok(dt) = UTC.datetime_from_str(raw, RFC850_FMT) {
        Ok(dt)
    } else if let Ok(dt) = UTC.datetime_from_str(raw, ASCTIME_FMT) {
        Ok(dt)
    } else {
        Err("Could not parse.")
    }
}

#[cfg(test)]
mod tests {
    use chrono::{UTC, TimeZone};
    use super::{RFC850_FMT, RFC1123_FMT, ASCTIME_FMT};

    macro_rules! test_parse_format {
        ($name:ident, $fmt:ident, $dt_str:expr) => {
            #[test]
            fn $name() {
                let dt = UTC.ymd(1994, 11, 6).and_hms(8, 49, 37);

                // Check that the format is what we expect
                assert_eq!(dt.format($fmt).to_string(), $dt_str);

                // Check that it parses correctly
                assert_eq!(Ok(dt), UTC.datetime_from_str($dt_str, $fmt));
            }
        }
    }

    test_parse_format!(parse_rfc850,  RFC850_FMT,  "Sunday, 06-Nov-94 08:49:37 GMT");
    test_parse_format!(parse_rfc1123, RFC1123_FMT, "Sun, 06 Nov 1994 08:49:37 GMT");
    test_parse_format!(parse_asctime, ASCTIME_FMT, "Sun Nov  6 08:49:37 1994");
}