1 //! Cookie Filters
2 
3 use futures::future;
4 use headers::Cookie;
5 
6 use super::header;
7 use crate::filter::{Filter, One};
8 use crate::reject::Rejection;
9 use std::convert::Infallible;
10 
11 /// Creates a `Filter` that requires a cookie by name.
12 ///
13 /// If found, extracts the value of the cookie, otherwise rejects.
cookie(name: &'static str) -> impl Filter<Extract = One<String>, Error = Rejection> + Copy14 pub fn cookie(name: &'static str) -> impl Filter<Extract = One<String>, Error = Rejection> + Copy {
15     header::header2().and_then(move |cookie: Cookie| {
16         let cookie = cookie
17             .get(name)
18             .map(String::from)
19             .ok_or_else(|| crate::reject::missing_cookie(name));
20         future::ready(cookie)
21     })
22 }
23 
24 /// Creates a `Filter` that looks for an optional cookie by name.
25 ///
26 /// If found, extracts the value of the cookie, otherwise continues
27 /// the request, extracting `None`.
optional( name: &'static str, ) -> impl Filter<Extract = One<Option<String>>, Error = Infallible> + Copy28 pub fn optional(
29     name: &'static str,
30 ) -> impl Filter<Extract = One<Option<String>>, Error = Infallible> + Copy {
31     header::optional2()
32         .map(move |opt: Option<Cookie>| opt.and_then(|cookie| cookie.get(name).map(String::from)))
33 }
34