immortal_http/
context.rs

1
2use uuid::Uuid;
3
4use crate::request::Request;
5use crate::response::Response;
6use crate::session::SessionManager;
7
8use std::rc::Rc;
9use std::sync::Arc;
10use std::cell::{Ref, RefMut, RefCell};
11use std::marker::PhantomData;
12
13/// Context is a structure that is exposed to the programmer when 
14/// registering closures as request handlers.
15pub struct Context<'ptr,'req> {
16    request: Rc<RefCell<Request<'req>>>,
17    response: Rc<RefCell<Response<'req>>>,
18    pub session_id: Uuid,
19    session_manager: Arc<SessionManager>,
20    phantom: PhantomData<&'ptr ()>,
21}
22
23#[allow(dead_code)]
24impl<'ptr, 'req> Context<'ptr, 'req> {
25    pub fn new(request: Rc<RefCell<Request<'req>>>,
26               response: Rc<RefCell<Response<'req>>>, 
27               session_id: Uuid,
28               session_manager: Arc<SessionManager>) -> Self {
29        Self {
30            request,
31            response,
32            session_id,
33            session_manager,
34            phantom: PhantomData,
35        }
36    }
37
38    /// Borrow the reference to the request.
39    pub fn request(&self) -> Ref<Request<'req>> {
40        self.request.borrow()
41    }
42
43    /// Mutably borrow the reference to the request.
44    /// This may be needed for what appears to be a read operation because items like
45    /// headers and GET parameters are lazily parsed out of the request buffer.
46    pub fn request_mut(&mut self) -> RefMut<Request<'req>> {
47        self.request.borrow_mut()
48    }
49
50    /// Borrow the response that is to be sent back to the client.
51    pub fn response(&self) -> Ref<Response<'req>> {
52        self.response.borrow()
53    }
54
55    /// Mutably borrow the response that is to be sent back to the client.
56    pub fn response_mut(&mut self) -> RefMut<Response<'req>> {
57        self.response.borrow_mut()
58    }
59
60    /// Makes a write to a session with a key and value
61    /// Returns true if a write happened to a session, false if no session id exists
62    /// Writing an empty string to this will remove the item from the session storage
63    pub fn write_session(&self, session_id: Uuid, key: &str, value: &str) -> bool {
64        if session_id.is_nil() {
65            return false;
66        }
67        self.session_manager.write_session(session_id, key, value)
68    }
69
70    /// Reads from a session store, the value associated with the key
71    /// Returns None if the session or the key is nonexistent
72    pub fn read_session(&self, session_id: Uuid, key: &str) -> Option<String> {
73        if session_id.is_nil() {
74            return None;
75        }
76        self.session_manager.read_session(session_id, key)
77    }
78
79    /// Clears the session data of any session ID passed in Shrinks the session data hashmap 
80    /// accordingly
81    pub fn clear_session(&self, session_id: Uuid) {
82        if session_id.is_nil() {
83            return;
84        }
85        self.session_manager.clear_session(session_id);
86    }
87
88    /// Completely deletes the session storage related to the passed-in session_id value
89    /// Shrinks the session storage hashmap accordingly
90    pub fn delete_session(&self, session_id: Uuid) {
91        if session_id.is_nil() {
92            return;
93        }
94        self.session_manager.delete_session(session_id);
95    }
96
97    /// Creates a new session and returns the session id
98    /// zero uuid is the default invalid uuid
99    pub fn new_session(&self) -> Uuid {
100        self.session_manager.create_session()
101    }
102
103    /// Returns true or false if the session associated with session_id exists
104    pub fn session_exists(&self, session_id: Uuid) -> bool {
105        if session_id.is_nil() {
106            return false;
107        }
108        self.session_manager.session_exists(session_id)
109    }
110
111    /// Sets the response code and location header
112    pub fn redirect(&mut self, location: &str) {
113        self.response.borrow_mut().code = "302";
114        self.response.borrow_mut().headers.insert("Location", location.to_string());
115    }
116}
117