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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
extern crate chrono;
extern crate hyper;
use std::fmt;
use chrono::{UTC, TimeZone, DateTime};
use hyper::header::{Header, HeaderFormat};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum RetryAfter {
Delay(chrono::Duration),
DateTime(chrono::DateTime<UTC>),
}
impl Header for RetryAfter {
fn header_name() -> &'static str {
"Retry-After"
}
fn parse_header(raw: &[Vec<u8>]) -> hyper::Result<RetryAfter> {
if raw.len() == 0 {
return Err(hyper::Error::Header);
}
let line = &raw[0];
let utf8_str = match ::std::str::from_utf8(line) {
Ok(utf8_str) => utf8_str,
Err(_) => return Err(hyper::Error::Header),
};
if let Ok(seconds) = utf8_str.parse::<i64>() {
return Ok(RetryAfter::Delay(chrono::Duration::seconds(seconds)));
}
if let Ok(datetime) = parse_http_date(utf8_str) {
return Ok(RetryAfter::DateTime(datetime));
}
Err(hyper::Error::Header)
}
}
impl HeaderFormat for RetryAfter {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
RetryAfter::Delay(ref duration) => {
write!(f, "{}", duration.num_seconds())
},
RetryAfter::DateTime(ref datetime) => {
write!(f, "{}", datetime.format(RFC1123_FMT).to_string())
}
}
}
}
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 {
extern crate httparse;
use hyper::header::{Header, Headers};
use chrono::{self, UTC, TimeZone, Duration};
use super::{RFC850_FMT, RFC1123_FMT, ASCTIME_FMT};
use super::RetryAfter;
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);
assert_eq!(dt.format($fmt).to_string(), $dt_str);
assert_eq!(Ok(dt), UTC.datetime_from_str($dt_str, $fmt));
}
}
}
test_parse_format!(parse_rfc1123, RFC1123_FMT, "Sun, 06 Nov 1994 08:49:37 GMT");
test_parse_format!(parse_rfc850, RFC850_FMT, "Sunday, 06-Nov-94 08:49:37 GMT");
test_parse_format!(parse_asctime, ASCTIME_FMT, "Sun Nov 6 08:49:37 1994");
#[test]
fn header_name_regression() {
assert_eq!(RetryAfter::header_name(), "Retry-After");
}
#[test]
fn parse_delay() {
let delay_raw = [b"1234".to_vec()];
let retry_after = RetryAfter::parse_header(&delay_raw).unwrap();
assert_eq!(RetryAfter::Delay(chrono::Duration::seconds(1234)), retry_after);
}
macro_rules! test_retry_after_datetime {
($name:ident, $bytes:expr) => {
#[test]
fn $name() {
let raw = [$bytes.to_vec()];
let dt = UTC.ymd(1994, 11, 6).and_hms(8, 49, 37);
let retry_after = RetryAfter::parse_header(&raw).expect("parse_header ok");
assert_eq!(RetryAfter::DateTime(dt), retry_after);
}
}
}
test_retry_after_datetime!(header_parse_rfc1123, b"Sun, 06 Nov 1994 08:49:37 GMT");
test_retry_after_datetime!(header_parse_rfc850, b"Sunday, 06-Nov-94 08:49:37 GMT");
test_retry_after_datetime!(header_parse_asctime, b"Sun Nov 6 08:49:37 1994");
#[test]
fn hyper_headers_from_raw_delay() {
let raw = [httparse::Header {
name: "Retry-After",
value: b"300",
}];
let headers = Headers::from_raw(&raw).unwrap();
let retry_after = headers.get::<RetryAfter>().unwrap();
assert_eq!(retry_after, &RetryAfter::Delay(Duration::seconds(300)));
}
#[test]
fn hyper_headers_from_raw_datetime() {
let raw = [httparse::Header {
name: "Retry-After",
value: b"Sun, 06 Nov 1994 08:49:37 GMT",
}];
let headers = Headers::from_raw(&raw).unwrap();
let retry_after = headers.get::<RetryAfter>().unwrap();
let expected = UTC.ymd(1994, 11, 6).and_hms(8, 49, 37);
assert_eq!(retry_after, &RetryAfter::DateTime(expected));
}
}