diff --git a/actix-files/CHANGES.md b/actix-files/CHANGES.md index ff8ccd64..9f606dfc 100644 --- a/actix-files/CHANGES.md +++ b/actix-files/CHANGES.md @@ -1,7 +1,9 @@ # Changes ## Unreleased - 2021-xx-xx +* Fix If-Modified-Since and If-Unmodified-Since to not compare using sub-second timestamps. [#1887] +[#1887]: https://github.com/actix/actix-web/pull/1887 ## 0.6.0-beta.1 - 2021-01-07 * `HttpRange::parse` now has its own error type. diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index b7225fbc..f4314f6b 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -107,6 +107,18 @@ mod tests { assert_eq!(resp.status(), StatusCode::NOT_MODIFIED); } + #[actix_rt::test] + async fn test_if_modified_since_without_if_none_match_same() { + let file = NamedFile::open("Cargo.toml").unwrap(); + let since = file.last_modified().unwrap(); + + let req = TestRequest::default() + .header(header::IF_MODIFIED_SINCE, since) + .to_http_request(); + let resp = file.respond_to(&req).await.unwrap(); + assert_eq!(resp.status(), StatusCode::NOT_MODIFIED); + } + #[actix_rt::test] async fn test_if_modified_since_with_if_none_match() { let file = NamedFile::open("Cargo.toml").unwrap(); @@ -121,6 +133,30 @@ mod tests { assert_ne!(resp.status(), StatusCode::NOT_MODIFIED); } + #[actix_rt::test] + async fn test_if_unmodified_since() { + let file = NamedFile::open("Cargo.toml").unwrap(); + let since = file.last_modified().unwrap(); + + let req = TestRequest::default() + .header(header::IF_UNMODIFIED_SINCE, since) + .to_http_request(); + let resp = file.respond_to(&req).await.unwrap(); + assert_eq!(resp.status(), StatusCode::OK); + } + + #[actix_rt::test] + async fn test_if_unmodified_since_failed() { + let file = NamedFile::open("Cargo.toml").unwrap(); + let since = header::HttpDate::from(SystemTime::UNIX_EPOCH); + + let req = TestRequest::default() + .header(header::IF_UNMODIFIED_SINCE, since) + .to_http_request(); + let resp = file.respond_to(&req).await.unwrap(); + assert_eq!(resp.status(), StatusCode::PRECONDITION_FAILED); + } + #[actix_rt::test] async fn test_named_file_text() { assert!(NamedFile::open("test--").is_err()); diff --git a/actix-files/src/named.rs b/actix-files/src/named.rs index b3c247b1..dc461e29 100644 --- a/actix-files/src/named.rs +++ b/actix-files/src/named.rs @@ -331,7 +331,7 @@ impl NamedFile { let t2: SystemTime = since.clone().into(); match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) { - (Ok(t1), Ok(t2)) => t1 > t2, + (Ok(t1), Ok(t2)) => t1.as_secs() > t2.as_secs(), _ => false, } } else { @@ -350,7 +350,7 @@ impl NamedFile { let t2: SystemTime = since.clone().into(); match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) { - (Ok(t1), Ok(t2)) => t1 <= t2, + (Ok(t1), Ok(t2)) => t1.as_secs() <= t2.as_secs(), _ => false, } } else {