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
use header::{Header, HeaderFormat};
use std::fmt;
use std::str::from_utf8;
use cookie::Cookie;
use cookie::CookieJar;
#[derive(Clone, PartialEq, Show)]
pub struct Cookies(pub Vec<Cookie>);
deref!(Cookies => Vec<Cookie>);
impl Header for Cookies {
fn header_name(_: Option<Cookies>) -> &'static str {
"Cookie"
}
fn parse_header(raw: &[Vec<u8>]) -> Option<Cookies> {
let mut cookies = Vec::with_capacity(raw.len());
for cookies_raw in raw.iter() {
match from_utf8(&cookies_raw[]) {
Ok(cookies_str) => {
for cookie_str in cookies_str.split(';') {
match cookie_str.trim().parse() {
Some(cookie) => cookies.push(cookie),
None => return None
}
}
},
Err(_) => return None
};
}
if !cookies.is_empty() {
Some(Cookies(cookies))
} else {
None
}
}
}
impl HeaderFormat for Cookies {
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let cookies = &self.0;
let last = cookies.len() - 1;
for (i, cookie) in cookies.iter().enumerate() {
try!(write!(fmt, "{}", cookie.pair()));
if i < last {
try!(fmt.write_str("; "));
}
}
Ok(())
}
}
impl Cookies {
pub fn to_cookie_jar(&self, key: &[u8]) -> CookieJar<'static> {
let mut jar = CookieJar::new(key);
for cookie in self.iter() {
jar.add_original(cookie.clone());
}
jar
}
pub fn from_cookie_jar(jar: &CookieJar) -> Cookies {
Cookies(jar.iter().collect())
}
}
#[test]
fn test_parse() {
let h = Header::parse_header(&[b"foo=bar; baz=quux".to_vec()][]);
let c1 = Cookie::new("foo".to_string(), "bar".to_string());
let c2 = Cookie::new("baz".to_string(), "quux".to_string());
assert_eq!(h, Some(Cookies(vec![c1, c2])));
}
#[test]
fn test_fmt() {
use header::Headers;
let mut cookie = Cookie::new("foo".to_string(), "bar".to_string());
cookie.httponly = true;
cookie.path = Some("/p".to_string());
let cookies = Cookies(vec![cookie, Cookie::new("baz".to_string(), "quux".to_string())]);
let mut headers = Headers::new();
headers.set(cookies);
assert_eq!(&headers.to_string()[], "Cookie: foo=bar; baz=quux\r\n");
}
#[test]
fn cookie_jar() {
let cookie = Cookie::new("foo".to_string(), "bar".to_string());
let cookies = Cookies(vec![cookie]);
let jar = cookies.to_cookie_jar(&[]);
let new_cookies = Cookies::from_cookie_jar(&jar);
assert_eq!(cookies, new_cookies);
}
bench_header!(bench, Cookies, { vec![b"foo=bar; baz=quux".to_vec()] });