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
use cookie::{Cookie, CookieJar, ParseError};
use futures::future;
use crate::{configuration::Store, response::IntoResponse, Extract, Request, Response, RouteMatch};
#[derive(Clone, Debug)]
pub struct Cookies {
content: CookieJar,
}
impl Cookies {
#[inline]
pub fn get(&self, name: &str) -> Option<&Cookie<'static>> {
self.content.get(name)
}
}
impl<S: 'static> Extract<S> for Cookies {
type Fut = future::Ready<Result<Self, Response>>;
fn extract(
data: &mut S,
req: &mut Request,
params: &Option<RouteMatch<'_>>,
store: &Store,
) -> Self::Fut {
let cookie_jar = match req.headers().get("Cookie") {
Some(raw_cookies) => parse_from_header(raw_cookies.to_str().unwrap()),
_ => Ok(CookieJar::new()),
};
let resp = cookie_jar
.map(|c| Cookies { content: c })
.map_err(|_e| http::status::StatusCode::BAD_REQUEST.into_response());
future::ready(resp)
}
}
fn parse_from_header(s: &str) -> Result<CookieJar, ParseError> {
let mut jar = CookieJar::new();
s.split(';').try_for_each(|s| -> Result<_, ParseError> {
jar.add(Cookie::parse(s.trim().to_owned())?);
Ok(())
})?;
Ok(jar)
}