Skip to content

Commit

Permalink
Merge branch 'master' into header-rework
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede authored Jan 11, 2021
2 parents 0820efd + a919d2d commit f5f986a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions actix-files/CHANGES.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 3 additions & 2 deletions actix-files/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ impl Files {
/// be inaccessible. Register more specific handlers and services first.
///
/// `Files` uses a threadpool for blocking filesystem operations. By default, the pool uses a
/// number of threads equal to 5x the number of available logical CPUs. Pool size can be changed
/// by setting ACTIX_THREADPOOL environment variable.
/// max number of threads equal to `512 * HttpServer::worker`. Real time thread count are
/// adjusted with work load. More threads would spawn when need and threads goes idle for a
/// period of time would be de-spawned.
pub fn new<T: Into<PathBuf>>(mount_path: &str, serve_from: T) -> Files {
let orig_dir = serve_from.into();
let dir = match orig_dir.canonicalize() {
Expand Down
36 changes: 36 additions & 0 deletions actix-files/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions actix-files/src/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit f5f986a

Please sign in to comment.