1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-10 19:01:05 +00: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.
* `CookieIdentityPolicy::max_age()` accepts value in seconds
### Fixed
* Fixed `TestRequest::app_data()`

View file

@ -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"] }

View file

@ -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

View file

@ -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"

View file

@ -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
}

View file

@ -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();

View file

@ -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<Cookie<'b>> 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();

View file

@ -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);
}
}

View file

@ -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])

View file

@ -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

View file

@ -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"

View file

@ -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<String>,
secure: bool,
http_only: bool,
max_age: Option<Duration>,
max_age: Option<time::Duration>,
same_site: Option<SameSite>,
}
@ -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);

View file

@ -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());

View file

@ -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;

View file

@ -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")