mirror of
https://github.com/actix/actix-web.git
synced 2024-12-30 12:00:38 +00:00
migrate actix-session
This commit is contained in:
parent
0de101bc4d
commit
60ada97b3d
4 changed files with 132 additions and 112 deletions
|
@ -34,7 +34,7 @@ members = [
|
|||
"actix-cors",
|
||||
"actix-files",
|
||||
"actix-framed",
|
||||
#"actix-session",
|
||||
"actix-session",
|
||||
"actix-identity",
|
||||
#"actix-multipart",
|
||||
"actix-web-actors",
|
||||
|
|
|
@ -24,15 +24,15 @@ default = ["cookie-session"]
|
|||
cookie-session = ["actix-web/secure-cookies"]
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0.9"
|
||||
actix-service = "0.4.2"
|
||||
actix-web = "2.0.0-alpha.1"
|
||||
actix-service = "1.0.0-alpha.1"
|
||||
bytes = "0.4"
|
||||
derive_more = "0.15.0"
|
||||
futures = "0.1.24"
|
||||
futures = "0.3.1"
|
||||
hashbrown = "0.6.3"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
time = "0.1.42"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "0.2.2"
|
||||
actix-rt = "1.0.0-alpha.1"
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use actix_service::{Service, Transform};
|
||||
use actix_web::cookie::{Cookie, CookieJar, Key, SameSite};
|
||||
|
@ -24,8 +25,7 @@ use actix_web::dev::{ServiceRequest, ServiceResponse};
|
|||
use actix_web::http::{header::SET_COOKIE, HeaderValue};
|
||||
use actix_web::{Error, HttpMessage, ResponseError};
|
||||
use derive_more::{Display, From};
|
||||
use futures::future::{ok, Future, FutureResult};
|
||||
use futures::Poll;
|
||||
use futures::future::{ok, FutureExt, LocalBoxFuture, Ready};
|
||||
use serde_json::error::Error as JsonError;
|
||||
|
||||
use crate::{Session, SessionStatus};
|
||||
|
@ -284,7 +284,7 @@ where
|
|||
type Error = S::Error;
|
||||
type InitError = ();
|
||||
type Transform = CookieSessionMiddleware<S>;
|
||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ok(CookieSessionMiddleware {
|
||||
|
@ -309,10 +309,10 @@ where
|
|||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = S::Error;
|
||||
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.service.poll_ready()
|
||||
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||
self.service.poll_ready(cx)
|
||||
}
|
||||
|
||||
/// On first request, a new session cookie is returned in response, regardless
|
||||
|
@ -325,7 +325,10 @@ where
|
|||
let (is_new, state) = self.inner.load(&req);
|
||||
Session::set_session(state.into_iter(), &mut req);
|
||||
|
||||
Box::new(self.service.call(req).map(move |mut res| {
|
||||
let fut = self.service.call(req);
|
||||
|
||||
async move {
|
||||
fut.await.map(|mut res| {
|
||||
match Session::get_changes(&mut res) {
|
||||
(SessionStatus::Changed, Some(state))
|
||||
| (SessionStatus::Renewed, Some(state)) => {
|
||||
|
@ -336,7 +339,9 @@ where
|
|||
{
|
||||
if is_new {
|
||||
let state: HashMap<String, String> = HashMap::new();
|
||||
res.checked_expr(|res| inner.set_cookie(res, state.into_iter()))
|
||||
res.checked_expr(|res| {
|
||||
inner.set_cookie(res, state.into_iter())
|
||||
})
|
||||
} else {
|
||||
res
|
||||
}
|
||||
|
@ -347,7 +352,9 @@ where
|
|||
}
|
||||
_ => res,
|
||||
}
|
||||
}))
|
||||
})
|
||||
}
|
||||
.boxed_local()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -359,6 +366,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn cookie_session() {
|
||||
test::block_on(async {
|
||||
let mut app = test::init_service(
|
||||
App::new()
|
||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||
|
@ -366,19 +374,22 @@ mod tests {
|
|||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
})),
|
||||
);
|
||||
)
|
||||
.await;
|
||||
|
||||
let request = test::TestRequest::get().to_request();
|
||||
let response = test::block_on(app.call(request)).unwrap();
|
||||
let response = app.call(request).await.unwrap();
|
||||
assert!(response
|
||||
.response()
|
||||
.cookies()
|
||||
.find(|c| c.name() == "actix-session")
|
||||
.is_some());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn private_cookie() {
|
||||
test::block_on(async {
|
||||
let mut app = test::init_service(
|
||||
App::new()
|
||||
.wrap(CookieSession::private(&[0; 32]).secure(false))
|
||||
|
@ -386,19 +397,22 @@ mod tests {
|
|||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
})),
|
||||
);
|
||||
)
|
||||
.await;
|
||||
|
||||
let request = test::TestRequest::get().to_request();
|
||||
let response = test::block_on(app.call(request)).unwrap();
|
||||
let response = app.call(request).await.unwrap();
|
||||
assert!(response
|
||||
.response()
|
||||
.cookies()
|
||||
.find(|c| c.name() == "actix-session")
|
||||
.is_some());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cookie_session_extractor() {
|
||||
test::block_on(async {
|
||||
let mut app = test::init_service(
|
||||
App::new()
|
||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||
|
@ -406,19 +420,22 @@ mod tests {
|
|||
let _ = ses.set("counter", 100);
|
||||
"test"
|
||||
})),
|
||||
);
|
||||
)
|
||||
.await;
|
||||
|
||||
let request = test::TestRequest::get().to_request();
|
||||
let response = test::block_on(app.call(request)).unwrap();
|
||||
let response = app.call(request).await.unwrap();
|
||||
assert!(response
|
||||
.response()
|
||||
.cookies()
|
||||
.find(|c| c.name() == "actix-session")
|
||||
.is_some());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basics() {
|
||||
test::block_on(async {
|
||||
let mut app = test::init_service(
|
||||
App::new()
|
||||
.wrap(
|
||||
|
@ -438,10 +455,11 @@ mod tests {
|
|||
let val: usize = ses.get("counter").unwrap().unwrap();
|
||||
format!("counter: {}", val)
|
||||
})),
|
||||
);
|
||||
)
|
||||
.await;
|
||||
|
||||
let request = test::TestRequest::get().to_request();
|
||||
let response = test::block_on(app.call(request)).unwrap();
|
||||
let response = app.call(request).await.unwrap();
|
||||
let cookie = response
|
||||
.response()
|
||||
.cookies()
|
||||
|
@ -453,7 +471,8 @@ mod tests {
|
|||
let request = test::TestRequest::with_uri("/test/")
|
||||
.cookie(cookie)
|
||||
.to_request();
|
||||
let body = test::read_response(&mut app, request);
|
||||
let body = test::read_response(&mut app, request).await;
|
||||
assert_eq!(body, Bytes::from_static(b"counter: 100"));
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@ use std::rc::Rc;
|
|||
|
||||
use actix_web::dev::{Extensions, Payload, ServiceRequest, ServiceResponse};
|
||||
use actix_web::{Error, FromRequest, HttpMessage, HttpRequest};
|
||||
use futures::future::{ok, Ready};
|
||||
use hashbrown::HashMap;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
@ -230,12 +231,12 @@ impl Session {
|
|||
/// ```
|
||||
impl FromRequest for Session {
|
||||
type Error = Error;
|
||||
type Future = Result<Session, Error>;
|
||||
type Future = Ready<Result<Session, Error>>;
|
||||
type Config = ();
|
||||
|
||||
#[inline]
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
Ok(Session::get_session(&mut *req.extensions_mut()))
|
||||
ok(Session::get_session(&mut *req.extensions_mut()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue