1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

drop chrono and use i64 for max age

This commit is contained in:
Nikolay Kim 2019-04-19 17:23:17 -07:00
parent a3844c1bfd
commit 7292d0b696
15 changed files with 70 additions and 41 deletions

View File

@ -17,6 +17,8 @@
* Removed `ServiceRequest::from_parts()` as it is unsafe to create from parts. * Removed `ServiceRequest::from_parts()` as it is unsafe to create from parts.
* `CookieIdentityPolicy::max_age()` accepts value in seconds
### Fixed ### Fixed
* Fixed `TestRequest::app_data()` * Fixed `TestRequest::app_data()`

View File

@ -90,13 +90,12 @@ regex = "1.0"
serde = { version = "1.0", features=["derive"] } serde = { version = "1.0", features=["derive"] }
serde_json = "1.0" serde_json = "1.0"
serde_urlencoded = "0.5.3" serde_urlencoded = "0.5.3"
time = "0.1" time = "0.1.42"
url = { version="1.7", features=["query_encoding"] } url = { version="1.7", features=["query_encoding"] }
# ssl support # ssl support
openssl = { version="0.10", optional = true } openssl = { version="0.10", optional = true }
rustls = { version = "^0.15", optional = true } rustls = { version = "^0.15", optional = true }
chrono = "0.4.6"
[dev-dependencies] [dev-dependencies]
actix-http = { version = "0.1.0", features=["ssl", "brotli", "flate2-zlib"] } actix-http = { version = "0.1.0", features=["ssl", "brotli", "flate2-zlib"] }

View File

@ -1,5 +1,14 @@
# Changes # 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 ## [0.1.0] - 2019-04-16
### Added ### Added

View File

@ -77,7 +77,7 @@ serde_json = "1.0"
sha1 = "0.6" sha1 = "0.6"
slab = "0.4" slab = "0.4"
serde_urlencoded = "0.5.5" serde_urlencoded = "0.5.5"
time = "0.1" time = "0.1.42"
tokio-tcp = "0.1.3" tokio-tcp = "0.1.3"
tokio-timer = "0.2.8" tokio-timer = "0.2.8"
tokio-current-thread = "0.1" tokio-current-thread = "0.1"

View File

@ -1,7 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use time::Tm;
use chrono::Duration; use chrono::Duration;
use time::Tm;
use super::{Cookie, SameSite}; use super::{Cookie, SameSite};
@ -17,7 +17,6 @@ use super::{Cookie, SameSite};
/// ///
/// ```rust /// ```rust
/// use actix_http::cookie::Cookie; /// use actix_http::cookie::Cookie;
/// use chrono::Duration;
/// ///
/// # fn main() { /// # fn main() {
/// let cookie: Cookie = Cookie::build("name", "value") /// let cookie: Cookie = Cookie::build("name", "value")
@ -25,7 +24,7 @@ use super::{Cookie, SameSite};
/// .path("/") /// .path("/")
/// .secure(true) /// .secure(true)
/// .http_only(true) /// .http_only(true)
/// .max_age(Duration::days(1)) /// .max_age(84600)
/// .finish(); /// .finish();
/// # } /// # }
/// ``` /// ```
@ -80,6 +79,26 @@ impl CookieBuilder {
self 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. /// Sets the `max_age` field in the cookie being built.
/// ///
/// # Example /// # Example
@ -89,14 +108,14 @@ impl CookieBuilder {
/// ///
/// # fn main() { /// # fn main() {
/// let c = Cookie::build("foo", "bar") /// let c = Cookie::build("foo", "bar")
/// .max_age(time::Duration::minutes(30)) /// .max_age_time(time::Duration::minutes(30))
/// .finish(); /// .finish();
/// ///
/// assert_eq!(c.max_age(), Some(time::Duration::seconds(30 * 60))); /// assert_eq!(c.max_age(), Some(time::Duration::seconds(30 * 60)));
/// # } /// # }
/// ``` /// ```
#[inline] #[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.cookie.set_max_age(value);
self self
} }

View File

@ -537,8 +537,8 @@ mod test {
#[test] #[test]
#[cfg(feature = "secure-cookies")] #[cfg(feature = "secure-cookies")]
fn delta() { fn delta() {
use std::collections::HashMap;
use chrono::Duration; use chrono::Duration;
use std::collections::HashMap;
let mut c = CookieJar::new(); let mut c = CookieJar::new();

View File

@ -65,9 +65,9 @@ use std::borrow::Cow;
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
use chrono::Duration;
use percent_encoding::{percent_encode, USERINFO_ENCODE_SET}; use percent_encoding::{percent_encode, USERINFO_ENCODE_SET};
use time::Tm; use time::Tm;
use chrono::Duration;
pub use self::builder::CookieBuilder; pub use self::builder::CookieBuilder;
pub use self::draft::*; pub use self::draft::*;
@ -979,7 +979,6 @@ impl<'a, 'b> PartialEq<Cookie<'b>> for Cookie<'a> {
mod tests { mod tests {
use super::{Cookie, SameSite}; use super::{Cookie, SameSite};
use time::strptime; use time::strptime;
use chrono::Duration;
#[test] #[test]
fn format() { fn format() {
@ -989,9 +988,7 @@ mod tests {
let cookie = Cookie::build("foo", "bar").http_only(true).finish(); let cookie = Cookie::build("foo", "bar").http_only(true).finish();
assert_eq!(&cookie.to_string(), "foo=bar; HttpOnly"); assert_eq!(&cookie.to_string(), "foo=bar; HttpOnly");
let cookie = Cookie::build("foo", "bar") let cookie = Cookie::build("foo", "bar").max_age(10).finish();
.max_age(Duration::seconds(10))
.finish();
assert_eq!(&cookie.to_string(), "foo=bar; Max-Age=10"); assert_eq!(&cookie.to_string(), "foo=bar; Max-Age=10");
let cookie = Cookie::build("foo", "bar").secure(true).finish(); let cookie = Cookie::build("foo", "bar").secure(true).finish();

View File

@ -5,8 +5,8 @@ use std::error::Error;
use std::fmt; use std::fmt;
use std::str::Utf8Error; use std::str::Utf8Error;
use percent_encoding::percent_decode;
use chrono::Duration; use chrono::Duration;
use percent_encoding::percent_decode;
use super::{Cookie, CookieStr, SameSite}; use super::{Cookie, CookieStr, SameSite};
@ -220,8 +220,8 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{Cookie, SameSite}; use super::{Cookie, SameSite};
use time::strptime;
use chrono::Duration; use chrono::Duration;
use time::strptime;
macro_rules! assert_eq_parse { macro_rules! assert_eq_parse {
($string:expr, $expected:expr) => { ($string:expr, $expected:expr) => {
@ -419,9 +419,7 @@ mod tests {
#[test] #[test]
fn do_not_panic_on_large_max_ages() { fn do_not_panic_on_large_max_ages() {
let max_seconds = Duration::max_value().num_seconds(); let max_seconds = Duration::max_value().num_seconds();
let expected = Cookie::build("foo", "bar") let expected = Cookie::build("foo", "bar").max_age(max_seconds).finish();
.max_age(Duration::seconds(max_seconds))
.finish();
assert_eq_parse!(format!(" foo=bar; Max-Age={:?}", max_seconds + 1), expected); assert_eq_parse!(format!(" foo=bar; Max-Age={:?}", max_seconds + 1), expected);
} }
} }

View File

@ -860,7 +860,7 @@ mod tests {
.domain("www.rust-lang.org") .domain("www.rust-lang.org")
.path("/test") .path("/test")
.http_only(true) .http_only(true)
.max_age(time::Duration::days(1)) .max_age_time(time::Duration::days(1))
.finish(), .finish(),
) )
.del_cookie(&cookies[1]) .del_cookie(&cookies[1])

View File

@ -1,5 +1,8 @@
# Changes # Changes
* `CookieSession::max_age()` accepts value in seconds
## [0.1.0-alpha.6] - 2019-04-14 ## [0.1.0-alpha.6] - 2019-04-14
* Update actix-web alpha.6 * Update actix-web alpha.6

View File

@ -32,7 +32,7 @@ futures = "0.1.25"
hashbrown = "0.2.0" hashbrown = "0.2.0"
serde = "1.0" serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
time = "0.1" time = "0.1.42"
[dev-dependencies] [dev-dependencies]
actix-rt = "0.2.2" actix-rt = "0.2.2"

View File

@ -17,7 +17,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use std::time::Duration;
use actix_service::{Service, Transform}; use actix_service::{Service, Transform};
use actix_web::cookie::{Cookie, CookieJar, Key, SameSite}; use actix_web::cookie::{Cookie, CookieJar, Key, SameSite};
@ -57,7 +56,7 @@ struct CookieSessionInner {
domain: Option<String>, domain: Option<String>,
secure: bool, secure: bool,
http_only: bool, http_only: bool,
max_age: Option<Duration>, max_age: Option<time::Duration>,
same_site: Option<SameSite>, same_site: Option<SameSite>,
} }
@ -98,7 +97,7 @@ impl CookieSessionInner {
} }
if let Some(max_age) = self.max_age { 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 { 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. /// 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); Rc::get_mut(&mut self.0).unwrap().max_age = Some(value);
self self
} }
@ -390,7 +394,7 @@ mod tests {
.domain("localhost") .domain("localhost")
.http_only(true) .http_only(true)
.same_site(SameSite::Lax) .same_site(SameSite::Lax)
.max_age(Duration::from_secs(100)), .max_age(100),
) )
.service(web::resource("/").to(|ses: Session| { .service(web::resource("/").to(|ses: Session| {
let _ = ses.set("counter", 100); let _ = ses.set("counter", 100);

View File

@ -53,7 +53,7 @@ use std::rc::Rc;
use actix_service::{Service, Transform}; use actix_service::{Service, Transform};
use futures::future::{ok, Either, FutureResult}; use futures::future::{ok, Either, FutureResult};
use futures::{Future, IntoFuture, Poll}; use futures::{Future, IntoFuture, Poll};
use chrono::Duration; use time::Duration;
use crate::cookie::{Cookie, CookieJar, Key, SameSite}; use crate::cookie::{Cookie, CookieJar, Key, SameSite};
use crate::error::{Error, Result}; use crate::error::{Error, Result};
@ -427,16 +427,16 @@ impl CookieIdentityPolicy {
self 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`. /// Sets the `max-age` field in the session cookie being built with `chrono::Duration`.
pub fn max_age_time(mut self, value: Duration) -> CookieIdentityPolicy { pub fn max_age_time(mut self, value: Duration) -> CookieIdentityPolicy {
Rc::get_mut(&mut self.0).unwrap().max_age = Some(value); Rc::get_mut(&mut self.0).unwrap().max_age = Some(value);
self 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. /// Sets the `same_site` field in the session cookie being built.
pub fn same_site(mut self, same_site: SameSite) -> Self { pub fn same_site(mut self, same_site: SameSite) -> Self {
@ -547,7 +547,7 @@ mod tests {
.service(web::resource("/login").to(|id: Identity| { .service(web::resource("/login").to(|id: Identity| {
id.remember("test".to_string()); id.remember("test".to_string());
HttpResponse::Ok() HttpResponse::Ok()
})) })),
); );
let resp = let resp =
test::call_service(&mut srv, TestRequest::with_uri("/login").to_request()); test::call_service(&mut srv, TestRequest::with_uri("/login").to_request());
@ -559,7 +559,7 @@ mod tests {
#[test] #[test]
fn test_identity_max_age() { fn test_identity_max_age() {
let seconds = 60isize; let seconds = 60;
let mut srv = test::init_service( let mut srv = test::init_service(
App::new() App::new()
.wrap(IdentityService::new( .wrap(IdentityService::new(
@ -573,7 +573,7 @@ mod tests {
.service(web::resource("/login").to(|id: Identity| { .service(web::resource("/login").to(|id: Identity| {
id.remember("test".to_string()); id.remember("test".to_string());
HttpResponse::Ok() HttpResponse::Ok()
})) })),
); );
let resp = let resp =
test::call_service(&mut srv, TestRequest::with_uri("/login").to_request()); test::call_service(&mut srv, TestRequest::with_uri("/login").to_request());

View File

@ -1,8 +1,8 @@
//! `Middleware` to normalize request's URI //! `Middleware` to normalize request's URI
use regex::Regex;
use actix_service::{Service, Transform}; use actix_service::{Service, Transform};
use futures::future::{self, FutureResult}; use futures::future::{self, FutureResult};
use regex::Regex;
use crate::service::{ServiceRequest, ServiceResponse}; use crate::service::{ServiceRequest, ServiceResponse};
@ -28,7 +28,7 @@ where
fn new_transform(&self, service: S) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
future::ok(NormalizePathNormalization { future::ok(NormalizePathNormalization {
service, service,
merge_slash: Regex::new("//+").unwrap() merge_slash: Regex::new("//+").unwrap(),
}) })
} }
} }
@ -72,7 +72,6 @@ mod tests {
use super::*; use super::*;
use crate::dev::ServiceRequest; use crate::dev::ServiceRequest;
use crate::http::header::CONTENT_TYPE;
use crate::test::{block_on, TestRequest}; use crate::test::{block_on, TestRequest};
use crate::HttpResponse; use crate::HttpResponse;

View File

@ -521,7 +521,6 @@ mod tests {
let result = read_response(&mut app, put_req); let result = read_response(&mut app, put_req);
assert_eq!(result, Bytes::from_static(b"put!")); assert_eq!(result, Bytes::from_static(b"put!"));
let patch_req = TestRequest::patch() let patch_req = TestRequest::patch()
.uri("/index.html") .uri("/index.html")
.header(header::CONTENT_TYPE, "application/json") .header(header::CONTENT_TYPE, "application/json")