-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
cookie.rs
159 lines (141 loc) · 5.15 KB
/
cookie.rs
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
#![feature(macro_rules)]
#![crate_type = "lib"]
extern crate url;
extern crate time;
use std::collections::TreeMap;
use std::fmt;
use std::from_str::FromStr;
pub use jar::CookieJar;
mod jar;
#[deriving(PartialEq, Clone)]
pub struct Cookie {
pub name: String,
pub value: String,
pub expires: Option<time::Tm>,
pub max_age: Option<u64>,
pub domain: Option<String>,
pub path: Option<String>,
pub secure: bool,
pub httponly: bool,
pub custom: TreeMap<String, String>,
}
impl Cookie {
pub fn new(name: String, value: String) -> Cookie {
Cookie {
name: name,
value: value,
expires: None,
max_age: None,
domain: None,
path: None,
secure: false,
httponly: false,
custom: TreeMap::new(),
}
}
pub fn parse(s: &str) -> Result<Cookie, ()> {
macro_rules! try_option( ($e:expr) => (
match $e { Some(s) => s, None => return Err(()) }
) )
let mut c = Cookie::new(String::new(), String::new());
let mut pairs = s.trim().split(';');
let keyval = try_option!(pairs.next());
let (name, value) = try!(split(keyval));
c.name = url::decode_component(name);
c.value = url::decode_component(value);
for attr in pairs {
match attr.trim() {
"Secure" => c.secure = true,
"HttpOnly" => c.httponly = true,
s => {
let (k, v) = try!(split(s));
match k {
"Max-Age" => c.max_age = Some(try_option!(from_str(v))),
"Domain" => c.domain = Some(v.to_string()),
"Path" => c.path = Some(v.to_string()),
"Expires" => {
let fmt = "%a, %d %b %Y %H:%M:%S %Z";
let tm = try_option!(time::strptime(v, fmt).ok());
c.expires = Some(tm);
}
_ => { c.custom.insert(k.to_string(), v.to_string()); }
}
}
}
}
return Ok(c);
fn split<'a>(s: &'a str) -> Result<(&'a str, &'a str), ()> {
let mut parts = s.trim().splitn('=', 1);
Ok((try_option!(parts.next()), try_option!(parts.next())))
}
}
}
impl fmt::Show for Cookie {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{}={}", self.name,
url::encode_component(self.value.as_slice())));
if self.httponly { try!(write!(f, "; HttpOnly")); }
if self.secure { try!(write!(f, "; Secure")); }
match self.path {
Some(ref s) => try!(write!(f, "; Path={}", s)),
None => {}
}
match self.domain {
Some(ref s) => try!(write!(f, "; Domain={}", s)),
None => {}
}
match self.max_age {
Some(n) => try!(write!(f, "; Max-Age={}", n)),
None => {}
}
match self.expires {
Some(ref t) => try!(write!(f, "; Expires={}", t.rfc822())),
None => {}
}
for (k, v) in self.custom.iter() {
try!(write!(f, "; {}={}", k, url::encode_component(v.as_slice())));
}
Ok(())
}
}
impl FromStr for Cookie {
fn from_str(s: &str) -> Option<Cookie> {
Cookie::parse(s).ok()
}
}
#[cfg(test)]
mod tests {
use super::Cookie;
#[test]
fn parse() {
let mut expected = Cookie::new("foo".to_string(), "bar".to_string());
assert_eq!(Cookie::parse("foo=bar").unwrap(), expected);
assert_eq!(Cookie::parse(" foo=bar ").unwrap(), expected);
expected.httponly = true;
assert_eq!(Cookie::parse(" foo=bar ;HttpOnly").unwrap(), expected);
expected.secure = true;
assert_eq!(Cookie::parse(" foo=bar ;HttpOnly; Secure").unwrap(), expected);
expected.max_age = Some(4);
assert_eq!(Cookie::parse(" foo=bar ;HttpOnly; Secure; \
Max-Age=4").unwrap(), expected);
expected.path = Some("/foo".to_string());
assert_eq!(Cookie::parse(" foo=bar ;HttpOnly; Secure; \
Max-Age=4; Path=/foo").unwrap(), expected);
expected.domain = Some("foo.com".to_string());
assert_eq!(Cookie::parse(" foo=bar ;HttpOnly; Secure; \
Max-Age=4; Path=/foo; \
Domain=foo.com").unwrap(), expected);
expected.custom.insert("wut".to_string(), "lol".to_string());
assert_eq!(Cookie::parse(" foo=bar ;HttpOnly; Secure; \
Max-Age=4; Path=/foo; \
Domain=foo.com; wut=lol").unwrap(), expected);
assert_eq!(expected.to_str().as_slice(),
"foo=bar; HttpOnly; Secure; Path=/foo; Domain=foo.com; \
Max-Age=4; wut=lol");
}
#[test]
fn odd_characters() {
let expected = Cookie::new("foo".to_string(), "b/r".to_string());
assert_eq!(Cookie::parse("foo=b%2Fr").unwrap(), expected);
}
}