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
use crate::{Extractor, Error, http::Request, additional::Additional};
use std::collections::HashMap;
use cookie::Cookie;
use std::sync::Arc;

/// To be implemented
pub struct Session {
    values: HashMap<String, String>
}

impl Session {
    fn new() -> Session {
        Session{
            values: HashMap::new()
        }
    }
}

impl Session {
    pub fn set(&mut self, key: String, value: String) {
        self.values.insert(key, value);
    }

    pub fn get<T: AsRef<str>>(&self, key: T) -> Option<&String> {
        self.values.get(key.as_ref())
    }
}

impl<T: Sync> Extractor<T> for Session {
    fn extract(req: &Request, _additional: Arc<Additional<T>>) -> Result<Self, Error> {
        if let Some(cookie_string) = req.headers.get("Cookie") {
            let _cookie = match Cookie::parse(cookie_string) {
                Ok(v) => v,
                Err(_e) => return Ok(Session::new())
            };
            Ok(Session {
                values: HashMap::new()
            })
        } else {
            return Ok(Session::new())
        }
    }
}