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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::collections::HashMap;
use std::sync::Mutex;
use rand;
use rand::Rng;
use Request;
use Response;
use input;
pub struct SessionsManager<T> where T: Clone {
sessions: Mutex<HashMap<String, T>>,
cookie_name: String,
timeout_s: u64,
}
impl<T> SessionsManager<T> where T: Clone {
pub fn new<S>(cookie_name: S, timeout_s: u64) -> SessionsManager<T> where S: Into<String> {
SessionsManager {
sessions: Mutex::new(HashMap::new()),
cookie_name: cookie_name.into(),
timeout_s: timeout_s,
}
}
pub fn start(&self, request: &Request) -> Session<T> {
let mut cookie = input::get_cookies(request).into_iter();
let cookie = cookie.find(|&(ref k, _)| k == &self.cookie_name);
let cookie = cookie.map(|(k, v)| v);
if let Some(cookie) = cookie {
Session {
manager: self,
key: cookie,
}
} else {
Session {
manager: self,
key: generate_session_id(),
}
}
}
}
pub struct Session<'a, T> where T: Clone + 'a {
manager: &'a SessionsManager<T>,
key: String,
}
impl<'a, T> Session<'a, T> where T: Clone {
pub fn get(&self) -> Option<T> {
let session = self.manager.sessions.lock().unwrap();
session.get(&self.key).map(|d| d.clone())
}
#[inline]
pub fn has_data(&self) -> bool {
let session = self.manager.sessions.lock().unwrap();
session.get(&self.key).is_some()
}
pub fn set(&self, value: T) {
let mut session = self.manager.sessions.lock().unwrap();
session.insert(self.key.clone(), value);
}
pub fn clear(&self) {
let mut session = self.manager.sessions.lock().unwrap();
session.remove(&self.key);
}
pub fn apply(&self, mut response: Response) -> Response {
if !self.has_data() {
return response;
}
let header_value = format!("{}={}; Max-Age={}", self.manager.cookie_name, self.key,
self.manager.timeout_s);
response.headers.push(("Set-Cookie".to_owned(), header_value));
response
}
}
pub fn generate_session_id() -> String {
rand::OsRng::new().unwrap()
.gen_ascii_chars()
.filter(|&c| (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'))
.take(64).collect::<String>()
}