diff --git a/CHANGES.md b/CHANGES.md index c37cb51cf..573d287c1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,8 @@ * Removed `ServiceRequest::from_parts()` as it is unsafe to create from parts. +* `CookieIdentityPolicy::max_age()` accepts value in seconds + ### Fixed * Fixed `TestRequest::app_data()` diff --git a/Cargo.toml b/Cargo.toml index 8003956fb..cfc3d59fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,13 +90,12 @@ regex = "1.0" serde = { version = "1.0", features=["derive"] } serde_json = "1.0" serde_urlencoded = "0.5.3" -time = "0.1" +time = "0.1.42" url = { version="1.7", features=["query_encoding"] } # ssl support openssl = { version="0.10", optional = true } rustls = { version = "^0.15", optional = true } -chrono = "0.4.6" [dev-dependencies] actix-http = { version = "0.1.0", features=["ssl", "brotli", "flate2-zlib"] } diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 3f2ccd4eb..a82b864b3 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -1,5 +1,14 @@ # Changes +## [0.1.1] - 2019-04-19 + +### Changes + +* Cookie::max_age() accepts value in seconds + +* Cookie::max_age_time() accepts value in time::Duration + + ## [0.1.0] - 2019-04-16 ### Added diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index d5a65b7b5..b94e12d87 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -77,7 +77,7 @@ serde_json = "1.0" sha1 = "0.6" slab = "0.4" serde_urlencoded = "0.5.5" -time = "0.1" +time = "0.1.42" tokio-tcp = "0.1.3" tokio-timer = "0.2.8" tokio-current-thread = "0.1" diff --git a/actix-http/src/cookie/builder.rs b/actix-http/src/cookie/builder.rs index 71c9bd19a..efeddbb62 100644 --- a/actix-http/src/cookie/builder.rs +++ b/actix-http/src/cookie/builder.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; -use time::Tm; use chrono::Duration; +use time::Tm; use super::{Cookie, SameSite}; @@ -17,7 +17,6 @@ use super::{Cookie, SameSite}; /// /// ```rust /// use actix_http::cookie::Cookie; -/// use chrono::Duration; /// /// # fn main() { /// let cookie: Cookie = Cookie::build("name", "value") @@ -25,7 +24,7 @@ use super::{Cookie, SameSite}; /// .path("/") /// .secure(true) /// .http_only(true) -/// .max_age(Duration::days(1)) +/// .max_age(84600) /// .finish(); /// # } /// ``` @@ -80,6 +79,26 @@ impl CookieBuilder { self } + /// Sets the `max_age` field in seconds in the cookie being built. + /// + /// # Example + /// + /// ```rust + /// use actix_http::cookie::Cookie; + /// + /// # fn main() { + /// let c = Cookie::build("foo", "bar") + /// .max_age(1800) + /// .finish(); + /// + /// assert_eq!(c.max_age(), Some(time::Duration::seconds(30 * 60))); + /// # } + /// ``` + #[inline] + pub fn max_age(self, seconds: i64) -> CookieBuilder { + self.max_age_time(Duration::seconds(seconds)) + } + /// Sets the `max_age` field in the cookie being built. /// /// # Example @@ -89,14 +108,14 @@ impl CookieBuilder { /// /// # fn main() { /// let c = Cookie::build("foo", "bar") - /// .max_age(time::Duration::minutes(30)) + /// .max_age_time(time::Duration::minutes(30)) /// .finish(); /// /// assert_eq!(c.max_age(), Some(time::Duration::seconds(30 * 60))); /// # } /// ``` #[inline] - pub fn max_age(mut self, value: Duration) -> CookieBuilder { + pub fn max_age_time(mut self, value: Duration) -> CookieBuilder { self.cookie.set_max_age(value); self } diff --git a/actix-http/src/cookie/jar.rs b/actix-http/src/cookie/jar.rs index 8b67c37d8..cc67536c6 100644 --- a/actix-http/src/cookie/jar.rs +++ b/actix-http/src/cookie/jar.rs @@ -537,8 +537,8 @@ mod test { #[test] #[cfg(feature = "secure-cookies")] fn delta() { - use std::collections::HashMap; use chrono::Duration; + use std::collections::HashMap; let mut c = CookieJar::new(); diff --git a/actix-http/src/cookie/mod.rs b/actix-http/src/cookie/mod.rs index eef41a121..ddcb12bbf 100644 --- a/actix-http/src/cookie/mod.rs +++ b/actix-http/src/cookie/mod.rs @@ -65,9 +65,9 @@ use std::borrow::Cow; use std::fmt; use std::str::FromStr; +use chrono::Duration; use percent_encoding::{percent_encode, USERINFO_ENCODE_SET}; use time::Tm; -use chrono::Duration; pub use self::builder::CookieBuilder; pub use self::draft::*; @@ -979,7 +979,6 @@ impl<'a, 'b> PartialEq> for Cookie<'a> { mod tests { use super::{Cookie, SameSite}; use time::strptime; - use chrono::Duration; #[test] fn format() { @@ -989,9 +988,7 @@ mod tests { let cookie = Cookie::build("foo", "bar").http_only(true).finish(); assert_eq!(&cookie.to_string(), "foo=bar; HttpOnly"); - let cookie = Cookie::build("foo", "bar") - .max_age(Duration::seconds(10)) - .finish(); + let cookie = Cookie::build("foo", "bar").max_age(10).finish(); assert_eq!(&cookie.to_string(), "foo=bar; Max-Age=10"); let cookie = Cookie::build("foo", "bar").secure(true).finish(); diff --git a/actix-http/src/cookie/parse.rs b/actix-http/src/cookie/parse.rs index cc042733e..42a2c1fcf 100644 --- a/actix-http/src/cookie/parse.rs +++ b/actix-http/src/cookie/parse.rs @@ -5,8 +5,8 @@ use std::error::Error; use std::fmt; use std::str::Utf8Error; -use percent_encoding::percent_decode; use chrono::Duration; +use percent_encoding::percent_decode; use super::{Cookie, CookieStr, SameSite}; @@ -220,8 +220,8 @@ where #[cfg(test)] mod tests { use super::{Cookie, SameSite}; - use time::strptime; use chrono::Duration; + use time::strptime; macro_rules! assert_eq_parse { ($string:expr, $expected:expr) => { @@ -419,9 +419,7 @@ mod tests { #[test] fn do_not_panic_on_large_max_ages() { let max_seconds = Duration::max_value().num_seconds(); - let expected = Cookie::build("foo", "bar") - .max_age(Duration::seconds(max_seconds)) - .finish(); + let expected = Cookie::build("foo", "bar").max_age(max_seconds).finish(); assert_eq_parse!(format!(" foo=bar; Max-Age={:?}", max_seconds + 1), expected); } } diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index 6125ae1cb..fd51e54c7 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -860,7 +860,7 @@ mod tests { .domain("www.rust-lang.org") .path("/test") .http_only(true) - .max_age(time::Duration::days(1)) + .max_age_time(time::Duration::days(1)) .finish(), ) .del_cookie(&cookies[1]) diff --git a/actix-session/CHANGES.md b/actix-session/CHANGES.md index 54ea66d9e..dfa3033c9 100644 --- a/actix-session/CHANGES.md +++ b/actix-session/CHANGES.md @@ -1,5 +1,8 @@ # Changes + +* `CookieSession::max_age()` accepts value in seconds + ## [0.1.0-alpha.6] - 2019-04-14 * Update actix-web alpha.6 diff --git a/actix-session/Cargo.toml b/actix-session/Cargo.toml index 058fc7068..e21bb732f 100644 --- a/actix-session/Cargo.toml +++ b/actix-session/Cargo.toml @@ -32,7 +32,7 @@ futures = "0.1.25" hashbrown = "0.2.0" serde = "1.0" serde_json = "1.0" -time = "0.1" +time = "0.1.42" [dev-dependencies] actix-rt = "0.2.2" diff --git a/actix-session/src/cookie.rs b/actix-session/src/cookie.rs index 4f7614dc6..ac08d1146 100644 --- a/actix-session/src/cookie.rs +++ b/actix-session/src/cookie.rs @@ -17,7 +17,6 @@ use std::collections::HashMap; use std::rc::Rc; -use std::time::Duration; use actix_service::{Service, Transform}; use actix_web::cookie::{Cookie, CookieJar, Key, SameSite}; @@ -57,7 +56,7 @@ struct CookieSessionInner { domain: Option, secure: bool, http_only: bool, - max_age: Option, + max_age: Option, same_site: Option, } @@ -98,7 +97,7 @@ impl CookieSessionInner { } if let Some(max_age) = self.max_age { - cookie.set_max_age(time::Duration::from_std(max_age).unwrap()); + cookie.set_max_age(max_age); } if let Some(same_site) = self.same_site { @@ -250,7 +249,12 @@ impl CookieSession { } /// Sets the `max-age` field in the session cookie being built. - pub fn max_age(mut self, value: Duration) -> CookieSession { + pub fn max_age(self, seconds: i64) -> CookieSession { + self.max_age_time(time::Duration::seconds(seconds)) + } + + /// Sets the `max-age` field in the session cookie being built. + pub fn max_age_time(mut self, value: time::Duration) -> CookieSession { Rc::get_mut(&mut self.0).unwrap().max_age = Some(value); self } @@ -390,7 +394,7 @@ mod tests { .domain("localhost") .http_only(true) .same_site(SameSite::Lax) - .max_age(Duration::from_secs(100)), + .max_age(100), ) .service(web::resource("/").to(|ses: Session| { let _ = ses.set("counter", 100); diff --git a/src/middleware/identity.rs b/src/middleware/identity.rs index bf739636f..ba03366fa 100644 --- a/src/middleware/identity.rs +++ b/src/middleware/identity.rs @@ -53,7 +53,7 @@ use std::rc::Rc; use actix_service::{Service, Transform}; use futures::future::{ok, Either, FutureResult}; use futures::{Future, IntoFuture, Poll}; -use chrono::Duration; +use time::Duration; use crate::cookie::{Cookie, CookieJar, Key, SameSite}; use crate::error::{Error, Result}; @@ -427,16 +427,16 @@ impl CookieIdentityPolicy { self } + /// Sets the `max-age` field in the session cookie being built with given number of seconds. + pub fn max_age(self, seconds: i64) -> CookieIdentityPolicy { + self.max_age_time(Duration::seconds(seconds)) + } + /// Sets the `max-age` field in the session cookie being built with `chrono::Duration`. pub fn max_age_time(mut self, value: Duration) -> CookieIdentityPolicy { Rc::get_mut(&mut self.0).unwrap().max_age = Some(value); self } - /// Sets the `max-age` field in the session cookie being built with given number of seconds. - pub fn max_age(mut self, seconds: isize) -> CookieIdentityPolicy { - Rc::get_mut(&mut self.0).unwrap().max_age = Some(Duration::seconds(seconds as i64)); - self - } /// Sets the `same_site` field in the session cookie being built. pub fn same_site(mut self, same_site: SameSite) -> Self { @@ -547,7 +547,7 @@ mod tests { .service(web::resource("/login").to(|id: Identity| { id.remember("test".to_string()); HttpResponse::Ok() - })) + })), ); let resp = test::call_service(&mut srv, TestRequest::with_uri("/login").to_request()); @@ -559,7 +559,7 @@ mod tests { #[test] fn test_identity_max_age() { - let seconds = 60isize; + let seconds = 60; let mut srv = test::init_service( App::new() .wrap(IdentityService::new( @@ -573,7 +573,7 @@ mod tests { .service(web::resource("/login").to(|id: Identity| { id.remember("test".to_string()); HttpResponse::Ok() - })) + })), ); let resp = test::call_service(&mut srv, TestRequest::with_uri("/login").to_request()); diff --git a/src/middleware/normalize.rs b/src/middleware/normalize.rs index 4b0afe7a9..060331a6b 100644 --- a/src/middleware/normalize.rs +++ b/src/middleware/normalize.rs @@ -1,8 +1,8 @@ //! `Middleware` to normalize request's URI -use regex::Regex; use actix_service::{Service, Transform}; use futures::future::{self, FutureResult}; +use regex::Regex; use crate::service::{ServiceRequest, ServiceResponse}; @@ -28,7 +28,7 @@ where fn new_transform(&self, service: S) -> Self::Future { future::ok(NormalizePathNormalization { service, - merge_slash: Regex::new("//+").unwrap() + merge_slash: Regex::new("//+").unwrap(), }) } } @@ -72,7 +72,6 @@ mod tests { use super::*; use crate::dev::ServiceRequest; - use crate::http::header::CONTENT_TYPE; use crate::test::{block_on, TestRequest}; use crate::HttpResponse; diff --git a/src/test.rs b/src/test.rs index a8eed3881..d932adfd3 100644 --- a/src/test.rs +++ b/src/test.rs @@ -335,12 +335,12 @@ impl TestRequest { pub fn post() -> TestRequest { TestRequest::default().method(Method::POST) } - + /// Create TestRequest and set method to `Method::PUT` pub fn put() -> TestRequest { TestRequest::default().method(Method::PUT) } - + /// Create TestRequest and set method to `Method::PATCH` pub fn patch() -> TestRequest { TestRequest::default().method(Method::PATCH) @@ -521,7 +521,6 @@ mod tests { let result = read_response(&mut app, put_req); assert_eq!(result, Bytes::from_static(b"put!")); - let patch_req = TestRequest::patch() .uri("/index.html") .header(header::CONTENT_TYPE, "application/json")