| 1 |
use std::time::SystemTime; |
| 2 |
use 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; |
| 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)] |
| 33 |
pub struct IfModifiedSince(HttpDate); |
| 34 |
|
| 35 |
derive_header! { |
| 36 |
IfModifiedSince(_), |
| 37 |
name: IF_MODIFIED_SINCE |
| 38 |
} |
| 39 |
|
| 40 |
impl IfModifiedSince { |
| 41 |
/// Check if the supplied time means the resource has been modified. |
| 42 |
pub fn is_modified(&self, last_modified: SystemTime) -> bool { |
| 43 |
self.0 < last_modified.into() |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl From<SystemTime> for IfModifiedSince { |
| 48 |
fn from(time: SystemTime) -> IfModifiedSince { |
| 49 |
IfModifiedSince(time.into()) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
impl From<IfModifiedSince> for SystemTime { |
| 54 |
fn from(date: IfModifiedSince) -> SystemTime { |
| 55 |
date.0.into() |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
#[cfg(test)] |
| 60 |
mod tests { |
| 61 |
use super::*; |
| 62 |
use std::time::Duration; |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn is_modified() { |
| 66 |
let newer = SystemTime::now(); |
| 67 |
let exact = newer - Duration::from_secs(2); |
| 68 |
let older = newer - Duration::from_secs(4); |
| 69 |
|
| 70 |
let if_mod = IfModifiedSince::from(exact); |
| 71 |
assert!(if_mod.is_modified(newer)); |
| 72 |
assert!(!if_mod.is_modified(exact)); |
| 73 |
assert!(!if_mod.is_modified(older)); |
| 74 |
} |
| 75 |
} |