1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pub trait CookieExt {
    fn cookie(&self, name: &str) -> Option<&str>;
}

impl CookieExt for ::http::Request<bytes::Bytes> {
    fn cookie(&self, name: &str) -> Option<&str> {
        self.headers()
            .get("cookie")
            .and_then(|v| v.to_str().ok())
            .and_then(|v| {
                v.split(';')
                    .find(|v| v.trim_start().starts_with(name))
                    .map(|v| v.trim_start().strip_prefix(name).unwrap().trim_start())
            })
    }
}