-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcookie.rs
264 lines (213 loc) · 6.42 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
pub struct Cookie {
name: ~str,
value: ~str,
path: Option<~str>,
domain: Option<~str>,
max_age: Option<uint>,
secure: bool,
http_only: bool
}
pub fn Cookie(name: ~str, value: ~str) -> Cookie {
Cookie {
name: name,
value: value,
path: None,
domain: None,
max_age: None,
secure: false,
http_only: false
}
}
impl Cookie {
fn to_header() -> Result<~str, ~str> {
// FIXME: Move error checking to the constructor.
if !cookie_parser::is_name(self.name) {
return Err(~"invalid name");
}
let mut cookie = if self.value.is_empty() {
self.name + "="
} else {
if !cookie_parser::is_value(self.value) {
return Err(~"invalid value");
}
fmt!("%s=%s", self.name, self.value)
};
match self.domain {
None => { },
Some(domain) => {
if cookie_parser::is_domain(domain) {
cookie += fmt!("; domain=%s", domain);
} else {
return Err(~"invalid domain");
}
}
}
match self.path {
None => { }
Some(path) => {
if cookie_parser::is_path(path) {
cookie += fmt!("; path=%s", path);
} else {
return Err(~"invalid path");
}
}
}
match copy self.max_age {
None => { }
Some(max_age) => {
let tm = if max_age == 0u {
std::time::at({ sec: 0_i64, nsec: 0_i32 })
} else {
let t = std::time::get_time();
std::time::at_utc({ sec: t.sec + max_age as i64, nsec: 0_i32 })
};
// Not every browser supports max-age...
//vec::push(cookie, "max-age=" + uint::str(max_age));
cookie += fmt!("; expires=%s", tm.rfc822());
}
}
if self.secure { cookie += "; Secure"; }
if self.http_only { cookie += "; HttpOnly"; }
Ok(cookie)
}
}
pub fn parse_header(header: &str) -> Result<~[Cookie], ~str> {
let header = header.trim();
// Exit early if empty.
if header.is_empty() { return Err(~"empty cookie") }
let mut cookies = ~[];
for header.split_char(';').each |line| {
let parts = str::splitn_char(*line, '=', 1u);
let (name, value) = if parts.len() == 1u {
return Err(~"empty cookie value")
} else {
(parts[0u].trim(), parts[1u].trim())
};
if !cookie_parser::is_name(name) {
return Err(fmt!("invalid cookie name: %?", name));
}
if !cookie_parser::is_value(value) {
return Err(fmt!("invalid cookie value: %?", value));
}
cookies.push(Cookie(name, value));
}
Ok(cookies)
}
pub fn parse_headers(headers: &[~str]) -> Result<LinearMap<~str, Cookie>, ~str> {
let mut cookies = LinearMap();
for headers.each |header| {
match parse_header(*header) {
Ok(move cs) => {
do vec::consume(cs) |_i, cookie| {
cookies.insert(copy cookie.name, move cookie);
}
},
Err(move e) => return Err(e),
}
}
Ok(cookies)
}
#[doc = "
Helper functions for parsing cookies according to RFC 6265, Section 4.1.
"]
mod cookie_parser {
pub fn is_name(name: &str) -> bool {
http_parser::is_token(name)
}
pub fn is_cookie_octet(ch: char) -> bool {
if !char::is_ascii(ch) { return false; }
match ch {
'\x21' | '\x23' .. '\x2b' | '\x2D' .. '\x3A' | '\x3C' .. '\x5B'
| '\x5D' .. '\x7E' => true,
_ => false,
}
}
pub fn is_value(value: &str) -> bool {
let mut pos = 0u;
let len = value.len();
// Exit early if we have an empty string.
if len == 0u { return true; }
// Check if the value is surrounded by double quotes.
let {ch, next} = str::char_range_at(value, pos);
let quoted = if ch == '"' {
pos = next;
if pos == len { return false; }
true
} else {
false
};
while pos < len {
let {ch, next} = str::char_range_at(value, pos);
if quoted && ch == '"' {
return next == len;
}
if !is_cookie_octet(ch) { return false; }
pos = next;
}
!quoted && pos == len
}
pub fn is_domain(_domain: &str) -> bool {
// FIXME: Actually implement.
true
}
pub fn is_path(path: &str) -> bool {
for str::each_char(path) |ch| {
if !char::is_ascii(ch) || http_parser::is_ctl(ch) || ch == ';' {
return false;
}
}
true
}
}
#[doc = "
Helper functions for parsing http according to RFC 2616, Section 2.2.
"]
mod http_parser {
pub fn is_char(ch: char) -> bool {
char::is_ascii(ch)
}
pub fn is_ctl(ch: char) -> bool {
match ch {
'\x00' .. '\x1f' | '\x7f' => true,
_ => false,
}
}
pub fn is_separator(ch: char) -> bool {
match ch {
'(' | ')' | '<' | '>' | '@'
| ',' | ';' | ':' | '\\' | '"'
| '/' | '[' | ']' | '?' | '='
| '{' | '}' | ' ' | '\t' => true,
_ => false,
}
}
pub fn is_token(token: &str) -> bool {
if token.len() == 0u { return false; }
for str::each_char(token) |ch| {
if !is_char(ch) || is_ctl(ch) || is_separator(ch) {
return false;
}
}
true
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_is_cookie_octet() {
assert cookie_parser::is_cookie_octet('A');
assert !cookie_parser::is_cookie_octet('"');
}
#[test]
fn test_is_value() {
assert !cookie_parser::is_value("\"");
assert !cookie_parser::is_value("\"a");
assert !cookie_parser::is_value("foo bar");
assert !cookie_parser::is_value("foo\"");
assert !cookie_parser::is_value("\"foo");
assert cookie_parser::is_value("");
assert cookie_parser::is_value("\"\"");
assert cookie_parser::is_value("foo");;
assert cookie_parser::is_value("\"foo\"");;
}
}