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
use http::header::HeaderValue;
use ::never::Never;
use ::filter::{Filter, One};
use ::reject::Rejection;
use super::header;
pub fn cookie(name: &'static str) -> impl Filter<Extract=One<String>, Error=Rejection> + Copy {
header::value(&::http::header::COOKIE, move |val| {
find_cookie(name, val)
})
}
pub fn optional(name: &'static str) -> impl Filter<Extract=One<Option<String>>, Error=Never> + Copy {
header::optional_value(&::http::header::COOKIE, move |val| {
find_cookie(name, val)
})
}
fn find_cookie(name: &str, value: &HeaderValue) -> Option<String> {
value
.to_str()
.ok()
.and_then(|value| {
for pair in value.split(';') {
let pair = pair.trim();
if pair.len() > name.len() + 1 {
if pair.starts_with(name) {
if pair.as_bytes()[name.len()] == b'=' {
return Some(pair[name.len() + 1..].to_string())
}
}
}
}
None
})
}