1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-07-03 20:45:46 +00:00

prepare actix-session release

This commit is contained in:
Nikolay Kim 2019-07-08 23:25:51 +06:00
parent e1fcd203f8
commit f410f3330f
4 changed files with 22 additions and 18 deletions

View file

@ -1,12 +1,12 @@
# Changes # Changes
## [0.2.0] - 2019-07-03 ## [0.2.0] - 2019-07-08
* Enhanced ``actix-session`` to facilitate state changes. Use ``Session.renew()`` * Enhanced ``actix-session`` to facilitate state changes. Use ``Session.renew()``
at successful login to cycle a session (new key/cookie but keeps state). at successful login to cycle a session (new key/cookie but keeps state).
Use ``Session.purge()`` at logout to invalid a session cookie (and remove Use ``Session.purge()`` at logout to invalid a session cookie (and remove
from redis cache, if applicable). from redis cache, if applicable).
## [0.1.1] - 2019-06-03 ## [0.1.1] - 2019-06-03
* Fix optional cookie session support * Fix optional cookie session support

View file

@ -6,4 +6,4 @@
* [API Documentation](https://docs.rs/actix-session/) * [API Documentation](https://docs.rs/actix-session/)
* [Chat on gitter](https://gitter.im/actix/actix) * [Chat on gitter](https://gitter.im/actix/actix)
* Cargo package: [actix-session](https://crates.io/crates/actix-session) * Cargo package: [actix-session](https://crates.io/crates/actix-session)
* Minimum supported Rust version: 1.33 or later * Minimum supported Rust version: 1.34 or later

View file

@ -120,8 +120,7 @@ impl CookieSessionInner {
} }
/// invalidates session cookie /// invalidates session cookie
fn remove_cookie<B>(&self, res: &mut ServiceResponse<B>) fn remove_cookie<B>(&self, res: &mut ServiceResponse<B>) -> Result<(), Error> {
-> Result<(), Error> {
let mut cookie = Cookie::named(self.name.clone()); let mut cookie = Cookie::named(self.name.clone());
cookie.set_value(""); cookie.set_value("");
cookie.set_max_age(time::Duration::seconds(0)); cookie.set_max_age(time::Duration::seconds(0));
@ -317,7 +316,7 @@ where
} }
/// On first request, a new session cookie is returned in response, regardless /// On first request, a new session cookie is returned in response, regardless
/// of whether any session state is set. With subsequent requests, if the /// of whether any session state is set. With subsequent requests, if the
/// session state changes, then set-cookie is returned in response. As /// session state changes, then set-cookie is returned in response. As
/// a user logs out, call session.purge() to set SessionStatus accordingly /// a user logs out, call session.purge() to set SessionStatus accordingly
/// and this will trigger removal of the session cookie in the response. /// and this will trigger removal of the session cookie in the response.
@ -329,21 +328,24 @@ where
Box::new(self.service.call(req).map(move |mut res| { Box::new(self.service.call(req).map(move |mut res| {
match Session::get_changes(&mut res) { match Session::get_changes(&mut res) {
(SessionStatus::Changed, Some(state)) (SessionStatus::Changed, Some(state))
| (SessionStatus::Renewed, Some(state)) => | (SessionStatus::Renewed, Some(state)) => {
res.checked_expr(|res| inner.set_cookie(res, state)), res.checked_expr(|res| inner.set_cookie(res, state))
}
(SessionStatus::Unchanged, _) => (SessionStatus::Unchanged, _) =>
// set a new session cookie upon first request (new client) // set a new session cookie upon first request (new client)
{
if is_new { if is_new {
let state: HashMap<String, String> = HashMap::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 { } else {
res res
}, }
}
(SessionStatus::Purged, _) => { (SessionStatus::Purged, _) => {
inner.remove_cookie(&mut res); inner.remove_cookie(&mut res);
res res
}, }
_ => res _ => res,
} }
})) }))
} }

View file

@ -103,7 +103,7 @@ pub enum SessionStatus {
Changed, Changed,
Purged, Purged,
Renewed, Renewed,
Unchanged Unchanged,
} }
impl Default for SessionStatus { impl Default for SessionStatus {
fn default() -> SessionStatus { fn default() -> SessionStatus {
@ -183,7 +183,10 @@ impl Session {
pub fn get_changes<B>( pub fn get_changes<B>(
res: &mut ServiceResponse<B>, res: &mut ServiceResponse<B>,
) -> (SessionStatus, Option<impl Iterator<Item = (String, String)>>) { ) -> (
SessionStatus,
Option<impl Iterator<Item = (String, String)>>,
) {
if let Some(s_impl) = res if let Some(s_impl) = res
.request() .request()
.extensions() .extensions()
@ -286,7 +289,6 @@ mod tests {
assert_eq!(session.0.borrow().status, SessionStatus::Purged); assert_eq!(session.0.borrow().status, SessionStatus::Purged);
} }
#[test] #[test]
fn renew_session() { fn renew_session() {
let mut req = test::TestRequest::default().to_srv_request(); let mut req = test::TestRequest::default().to_srv_request();