headers_ext/common/if_modified_since.rs
1use std::time::SystemTime;
2use util::HttpDate;
3
4/// `If-Modified-Since` header, defined in
5/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3)
6///
7/// The `If-Modified-Since` header field makes a GET or HEAD request
8/// method conditional on the selected representation's modification date
9/// being more recent than the date provided in the field-value.
10/// Transfer of the selected representation's data is avoided if that
11/// data has not changed.
12///
13/// # ABNF
14///
15/// ```text
16/// If-Modified-Since = HTTP-date
17/// ```
18///
19/// # Example values
20/// * `Sat, 29 Oct 1994 19:43:31 GMT`
21///
22/// # Example
23///
24/// ```
25/// # extern crate headers_ext as headers;
26/// use headers::IfModifiedSince;
27/// use std::time::{Duration, SystemTime};
28///
29/// let time = SystemTime::now() - Duration::from_secs(60 * 60 * 24);
30/// let if_mod = IfModifiedSince::from(time);
31/// ```
32#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Header)]
33pub struct IfModifiedSince(HttpDate);
34
35impl IfModifiedSince {
36 /// Check if the supplied time means the resource has been modified.
37 pub fn is_modified(&self, last_modified: SystemTime) -> bool {
38 self.0 < last_modified.into()
39 }
40}
41
42impl From<SystemTime> for IfModifiedSince {
43 fn from(time: SystemTime) -> IfModifiedSince {
44 IfModifiedSince(time.into())
45 }
46}
47
48impl From<IfModifiedSince> for SystemTime {
49 fn from(date: IfModifiedSince) -> SystemTime {
50 date.0.into()
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use std::time::Duration;
57 use super::*;
58
59 #[test]
60 fn is_modified() {
61 let newer = SystemTime::now();
62 let exact = newer - Duration::from_secs(2);
63 let older = newer - Duration::from_secs(4);
64
65 let if_mod = IfModifiedSince::from(exact);
66 assert!(if_mod.is_modified(newer));
67 assert!(!if_mod.is_modified(exact));
68 assert!(!if_mod.is_modified(older));
69 }
70}