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
58
59
60
61
62
63
64
65
66
67
68
69
extern crate cookie;
extern crate iron;

use iron::prelude::*;
use iron::middleware::{AroundMiddleware,Handler};
use iron::typemap;

pub mod backends;

pub trait Session {
    fn get(&self, key: &str) -> Option<&str>;
    fn set(&mut self, key: &str, value: String);
    fn write_cookies(&self, response: &mut Response);
}

pub trait SessionBackend: Send + Sync + Clone + 'static {
    type S: Session;
    fn from_request(&self, request: &mut Request) -> Self::S;
}


pub struct SessionStorage<B: SessionBackend> {
    backend: B
}

impl<B: SessionBackend> SessionStorage<B> {
    pub fn new(backend: B) -> Self {
        SessionStorage {
            backend: backend
        }
    }
}

type RequestSession = Box<Session>;

impl typemap::Key for RequestSession { type Value = RequestSession; }

impl<B: SessionBackend> AroundMiddleware for SessionStorage<B> {
    fn around(self, handler: Box<Handler>) -> Box<Handler> {
        Box::new(move |req: &mut Request| -> IronResult<Response> {
            let s = self.backend.from_request(req);
            req.extensions.insert::<RequestSession>(Box::new(s));
            let mut res = handler.handle(req);
            let s = req.extensions.remove::<RequestSession>().unwrap();
            match res {
                Ok(ref mut x) => s.write_cookies(x),
                Err(ref mut e) => s.write_cookies(&mut e.response)
            };
            res
        })
    }
}

pub trait RequestExt {
    fn session(&mut self) -> &mut RequestSession;
}

impl<'a, 'b> RequestExt for Request<'a, 'b> {
    fn session(&mut self) -> &mut RequestSession {
        self.extensions.get_mut::<RequestSession>().unwrap()
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
    }
}