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
use std::pin::Pin;
use cookie::{Cookie, CookieJar};
use hyper::header::{HeaderMap, HeaderValue, COOKIE};
use super::{Middleware, NewMiddleware};
use crate::handler::HandlerFuture;
use crate::state::{FromState, State};
#[derive(Copy, Clone)]
pub struct CookieParser;
impl CookieParser {
pub fn from_state(state: &State) -> CookieJar {
HeaderMap::borrow_from(state)
.get_all(COOKIE)
.iter()
.flat_map(HeaderValue::to_str)
.flat_map(|cs| cs.split("; "))
.flat_map(|cs| Cookie::parse(cs.to_owned()))
.fold(CookieJar::new(), |mut jar, cookie| {
jar.add_original(cookie);
jar
})
}
}
impl Middleware for CookieParser {
fn call<Chain>(self, mut state: State, chain: Chain) -> Pin<Box<HandlerFuture>>
where
Chain: FnOnce(State) -> Pin<Box<HandlerFuture>>,
{
let cookies = { CookieParser::from_state(&state) };
state.put(cookies);
chain(state)
}
}
impl NewMiddleware for CookieParser {
type Instance = Self;
fn new_middleware(&self) -> anyhow::Result<Self::Instance> {
Ok(*self)
}
}