From c695358bcb7dac708888309b9b47af852e900cd3 Mon Sep 17 00:00:00 2001 From: cuebyte Date: Fri, 8 Feb 2019 22:33:00 +0100 Subject: [PATCH] Ignored the If-Modified-Since if If-None-Match is specified (#680) (#692) --- CHANGES.md | 4 ++++ src/fs.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 83803abb..1a18e092 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,10 @@ * Add `from_file` and `from_file_with_config` to `NamedFile` to allow sending files without a known path. #670 +### Fixed + +* Ignored the `If-Modified-Since` if `If-None-Match` is specified. #680 + ## [0.7.18] - 2019-01-10 ### Added diff --git a/src/fs.rs b/src/fs.rs index dcf6c539..604ac550 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -441,6 +441,8 @@ impl Responder for NamedFile { // check last modified let not_modified = if !none_match(etag.as_ref(), req) { true + } else if req.headers().contains_key(header::IF_NONE_MATCH) { + false } else if let (Some(ref m), Some(header::IfModifiedSince(ref since))) = (last_modified, req.get_header()) { @@ -944,6 +946,8 @@ impl HttpRange { #[cfg(test)] mod tests { use std::fs; + use std::time::Duration; + use std::ops::Add; use super::*; use application::App; @@ -963,6 +967,43 @@ mod tests { assert_eq!(m, mime::APPLICATION_OCTET_STREAM); } + #[test] + fn test_if_modified_since_without_if_none_match() { + let mut file = NamedFile::open("Cargo.toml") + .unwrap() + .set_cpu_pool(CpuPool::new(1)); + let since = header::HttpDate::from( + SystemTime::now().add(Duration::from_secs(60))); + + let req = TestRequest::default() + .header(header::IF_MODIFIED_SINCE, since) + .finish(); + let resp = file.respond_to(&req).unwrap(); + assert_eq!( + resp.status(), + StatusCode::NOT_MODIFIED + ); + } + + #[test] + fn test_if_modified_since_with_if_none_match() { + let mut file = NamedFile::open("Cargo.toml") + .unwrap() + .set_cpu_pool(CpuPool::new(1)); + let since = header::HttpDate::from( + SystemTime::now().add(Duration::from_secs(60))); + + let req = TestRequest::default() + .header(header::IF_NONE_MATCH, "miss_etag") + .header(header::IF_MODIFIED_SINCE, since) + .finish(); + let resp = file.respond_to(&req).unwrap(); + assert_ne!( + resp.status(), + StatusCode::NOT_MODIFIED + ); + } + #[test] fn test_named_file_text() { assert!(NamedFile::open("test--").is_err());