|
|
|
@ -1,18 +1,21 @@ |
|
|
|
|
use rocket::request::FromRequest; |
|
|
|
|
use rocket::{Outcome, Request, State, http::{Status, Cookies, Cookie}, Response, Data, Rocket}; |
|
|
|
|
use std::ops::{Deref, DerefMut}; |
|
|
|
|
use std::collections::HashMap; |
|
|
|
|
use json_dotpath::DotPaths; |
|
|
|
|
use parking_lot::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; |
|
|
|
|
use serde_json::{Value, Map}; |
|
|
|
|
use rocket::fairing::{self, Fairing, Info}; |
|
|
|
|
use rand::Rng; |
|
|
|
|
use std::borrow::Cow; |
|
|
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
|
use serde::de::DeserializeOwned; |
|
|
|
|
use json_dotpath::DotPaths; |
|
|
|
|
use rocket::fairing::{self, Fairing, Info}; |
|
|
|
|
use rocket::request::FromRequest; |
|
|
|
|
use rocket::response::ResponseBuilder; |
|
|
|
|
use rocket::{ |
|
|
|
|
http::{Cookie, Cookies, Status}, |
|
|
|
|
Data, Outcome, Request, Response, Rocket, State, |
|
|
|
|
}; |
|
|
|
|
use serde::de::DeserializeOwned; |
|
|
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
|
use serde_json::{Map, Value}; |
|
|
|
|
use std::borrow::Cow; |
|
|
|
|
use std::collections::HashMap; |
|
|
|
|
use std::ops::{Deref, DerefMut}; |
|
|
|
|
|
|
|
|
|
const SESSION_ID : &'static str = "SESSID"; |
|
|
|
|
const SESSION_ID: &'static str = "SESSID"; |
|
|
|
|
|
|
|
|
|
type SessionsMap = HashMap<String, SessionInstance>; |
|
|
|
|
|
|
|
|
@ -41,10 +44,12 @@ impl<'a, 'r> FromRequest<'a, 'r> for &'a SessionID { |
|
|
|
|
SessionID(cookie.value().to_string()) // FIXME avoid cloning
|
|
|
|
|
} else { |
|
|
|
|
println!("new id"); |
|
|
|
|
SessionID(rand::thread_rng() |
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric) |
|
|
|
|
.take(16) |
|
|
|
|
.collect()) |
|
|
|
|
SessionID( |
|
|
|
|
rand::thread_rng() |
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric) |
|
|
|
|
.take(16) |
|
|
|
|
.collect(), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
})) |
|
|
|
|
} |
|
|
|
@ -53,7 +58,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for &'a SessionID { |
|
|
|
|
#[derive(Debug)] |
|
|
|
|
pub struct Session<'a> { |
|
|
|
|
store: State<'a, SessionStore>, |
|
|
|
|
id : &'a SessionID, |
|
|
|
|
id: &'a SessionID, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for Session<'a> { |
|
|
|
@ -65,13 +70,15 @@ impl<'a, 'r> FromRequest<'a, 'r> for Session<'a> { |
|
|
|
|
if let Some(cookie) = request.cookies().get(SESSION_ID) { |
|
|
|
|
SessionID(cookie.value().to_string()) |
|
|
|
|
} else { |
|
|
|
|
SessionID(rand::thread_rng() |
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric) |
|
|
|
|
.take(16) |
|
|
|
|
.collect()) |
|
|
|
|
SessionID( |
|
|
|
|
rand::thread_rng() |
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric) |
|
|
|
|
.take(16) |
|
|
|
|
.collect(), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
}), |
|
|
|
|
store: request.guard().unwrap() |
|
|
|
|
store: request.guard().unwrap(), |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -81,7 +88,7 @@ impl<'a> Session<'a> { |
|
|
|
|
SessionFairing |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get<T : DeserializeOwned>(&self, path : &str) -> Option<T> { |
|
|
|
|
pub fn get<T: DeserializeOwned>(&self, path: &str) -> Option<T> { |
|
|
|
|
let rg = self.store.inner.read(); |
|
|
|
|
if let Some(ses) = rg.get(&self.id.0) { |
|
|
|
|
ses.data.dot_get(path) |
|
|
|
@ -90,19 +97,19 @@ impl<'a> Session<'a> { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get_or<T : DeserializeOwned>(&self, path : &str, def : T) -> T { |
|
|
|
|
pub fn get_or<T: DeserializeOwned>(&self, path: &str, def: T) -> T { |
|
|
|
|
self.get(path).unwrap_or(def) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get_or_else<T : DeserializeOwned, F : FnOnce() -> T>(&self, path : &str, def : F) -> T { |
|
|
|
|
pub fn get_or_else<T: DeserializeOwned, F: FnOnce() -> T>(&self, path: &str, def: F) -> T { |
|
|
|
|
self.get(path).unwrap_or_else(def) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get_or_default<T : DeserializeOwned + Default>(&self, path : &str) -> T { |
|
|
|
|
pub fn get_or_default<T: DeserializeOwned + Default>(&self, path: &str) -> T { |
|
|
|
|
self.get(path).unwrap_or_default() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn take<T : DeserializeOwned>(&self, path : &str) -> Option<T> { |
|
|
|
|
pub fn take<T: DeserializeOwned>(&self, path: &str) -> Option<T> { |
|
|
|
|
let mut wg = self.store.inner.write(); |
|
|
|
|
if let Some(ses) = wg.get_mut(&self.id.0) { |
|
|
|
|
ses.data.dot_take(path) |
|
|
|
@ -111,7 +118,7 @@ impl<'a> Session<'a> { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn replace<O: DeserializeOwned, N: Serialize>(&self, path : &str, new : N) -> Option<O> { |
|
|
|
|
pub fn replace<O: DeserializeOwned, N: Serialize>(&self, path: &str, new: N) -> Option<O> { |
|
|
|
|
let mut wg = self.store.inner.write(); |
|
|
|
|
if let Some(ses) = wg.get_mut(&self.id.0) { |
|
|
|
|
ses.data.dot_replace(path, new) |
|
|
|
@ -120,20 +127,18 @@ impl<'a> Session<'a> { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn set<T : Serialize>(&self, path : &str, value : T) { |
|
|
|
|
pub fn set<T: Serialize>(&self, path: &str, value: T) { |
|
|
|
|
let mut wg = self.store.inner.write(); |
|
|
|
|
if let Some(ses) = wg.get_mut(&self.id.0) { |
|
|
|
|
ses.data.dot_set(path, value); |
|
|
|
|
} else { |
|
|
|
|
let mut map = Map::new(); |
|
|
|
|
map.dot_set(path, value); |
|
|
|
|
wg.insert(self.id.0.clone(), SessionInstance { |
|
|
|
|
data : map, |
|
|
|
|
}); |
|
|
|
|
wg.insert(self.id.0.clone(), SessionInstance { data: map }); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn remove(&self, path : &str) { |
|
|
|
|
pub fn remove(&self, path: &str) { |
|
|
|
|
let mut wg = self.store.inner.write(); |
|
|
|
|
if let Some(ses) = wg.get_mut(&self.id.0) { |
|
|
|
|
ses.data.dot_remove(path); |
|
|
|
@ -148,7 +153,7 @@ impl Fairing for SessionFairing { |
|
|
|
|
fn info(&self) -> Info { |
|
|
|
|
Info { |
|
|
|
|
name: "Session Fairing", |
|
|
|
|
kind: fairing::Kind::Attach | fairing::Kind::Response |
|
|
|
|
kind: fairing::Kind::Attach | fairing::Kind::Response, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -157,9 +162,7 @@ impl Fairing for SessionFairing { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn on_response<'r>(&self, request: &'r Request, response: &mut Response) { |
|
|
|
|
let session = request.local_cache(|| { |
|
|
|
|
SessionID("".to_string()) |
|
|
|
|
}); |
|
|
|
|
let session = request.local_cache(|| SessionID("".to_string())); |
|
|
|
|
|
|
|
|
|
if !session.0.is_empty() { |
|
|
|
|
response.adjoin_header(Cookie::build(SESSION_ID, session.0.clone()).finish()); |
|
|
|
|