mirror of
https://github.com/actix/actix-web.git
synced 2024-11-23 01:51:11 +00:00
implement extractor for Session
This commit is contained in:
parent
fe2b50a9ef
commit
b4252f8fd1
2 changed files with 48 additions and 14 deletions
|
@ -1,11 +1,13 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## 0.6.6 (2018-05-16)
|
## 0.6.6 (2018-05-17)
|
||||||
|
|
||||||
* Panic during middleware execution #226
|
* Panic during middleware execution #226
|
||||||
|
|
||||||
* Add support for listen_tls/listen_ssl #224
|
* Add support for listen_tls/listen_ssl #224
|
||||||
|
|
||||||
|
* Implement extractor for `Session`
|
||||||
|
|
||||||
|
|
||||||
## 0.6.5 (2018-05-15)
|
## 0.6.5 (2018-05-15)
|
||||||
|
|
||||||
|
|
|
@ -80,6 +80,7 @@ use serde_json::error::Error as JsonError;
|
||||||
use time::Duration;
|
use time::Duration;
|
||||||
|
|
||||||
use error::{Error, ResponseError, Result};
|
use error::{Error, ResponseError, Result};
|
||||||
|
use handler::FromRequest;
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
use middleware::{Middleware, Response, Started};
|
use middleware::{Middleware, Response, Started};
|
||||||
|
@ -190,6 +191,16 @@ impl Session {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<S> FromRequest<S> for Session {
|
||||||
|
type Config = ();
|
||||||
|
type Result = Session;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result {
|
||||||
|
req.session()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct SessionImplCell(RefCell<Box<SessionImpl>>);
|
struct SessionImplCell(RefCell<Box<SessionImpl>>);
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
@ -226,13 +237,10 @@ impl<S: 'static, T: SessionBackend<S>> Middleware<S> for SessionStorage<T, S> {
|
||||||
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
||||||
let mut req = req.clone();
|
let mut req = req.clone();
|
||||||
|
|
||||||
let fut = self.0
|
let fut = self.0.from_request(&mut req).then(move |res| match res {
|
||||||
.from_request(&mut req)
|
|
||||||
.then(move |res| match res {
|
|
||||||
Ok(sess) => {
|
Ok(sess) => {
|
||||||
req.extensions_mut().insert(Arc::new(SessionImplCell(
|
req.extensions_mut()
|
||||||
RefCell::new(Box::new(sess)),
|
.insert(Arc::new(SessionImplCell(RefCell::new(Box::new(sess)))));
|
||||||
)));
|
|
||||||
FutOk(None)
|
FutOk(None)
|
||||||
}
|
}
|
||||||
Err(err) => FutErr(err),
|
Err(err) => FutErr(err),
|
||||||
|
@ -241,7 +249,9 @@ impl<S: 'static, T: SessionBackend<S>> Middleware<S> for SessionStorage<T, S> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn response(
|
fn response(
|
||||||
&self, req: &mut HttpRequest<S>, resp: HttpResponse,
|
&self,
|
||||||
|
req: &mut HttpRequest<S>,
|
||||||
|
resp: HttpResponse,
|
||||||
) -> Result<Response> {
|
) -> Result<Response> {
|
||||||
if let Some(s_box) = req.extensions_mut().remove::<Arc<SessionImplCell>>() {
|
if let Some(s_box) = req.extensions_mut().remove::<Arc<SessionImplCell>>() {
|
||||||
s_box.0.borrow_mut().write(resp)
|
s_box.0.borrow_mut().write(resp)
|
||||||
|
@ -357,7 +367,9 @@ impl CookieSessionInner {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_cookie(
|
fn set_cookie(
|
||||||
&self, resp: &mut HttpResponse, state: &HashMap<String, String>,
|
&self,
|
||||||
|
resp: &mut HttpResponse,
|
||||||
|
state: &HashMap<String, String>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let value =
|
let value =
|
||||||
serde_json::to_string(&state).map_err(CookieSessionError::Serialize)?;
|
serde_json::to_string(&state).map_err(CookieSessionError::Serialize)?;
|
||||||
|
@ -551,4 +563,24 @@ mod tests {
|
||||||
let response = srv.execute(request.send()).unwrap();
|
let response = srv.execute(request.send()).unwrap();
|
||||||
assert!(response.cookie("actix-session").is_some());
|
assert!(response.cookie("actix-session").is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cookie_session_extractor() {
|
||||||
|
let mut srv = test::TestServer::with_factory(|| {
|
||||||
|
App::new()
|
||||||
|
.middleware(SessionStorage::new(
|
||||||
|
CookieSessionBackend::signed(&[0; 32]).secure(false),
|
||||||
|
))
|
||||||
|
.resource("/", |r| {
|
||||||
|
r.with(|ses: Session| {
|
||||||
|
let _ = ses.set("counter", 100);
|
||||||
|
"test"
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
let request = srv.get().uri(srv.url("/")).finish().unwrap();
|
||||||
|
let response = srv.execute(request.send()).unwrap();
|
||||||
|
assert!(response.cookie("actix-session").is_some());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue