Debian Codegraph @

third_party/rust/headers/src/common/if_unmodified_since.rs

raw
1 use std::time::SystemTime;
2 use util::HttpDate;
3
4 /// `If-Unmodified-Since` header, defined in
5 /// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4)
6 ///
7 /// The `If-Unmodified-Since` header field makes the request method
8 /// conditional on the selected representation's last modification date
9 /// being earlier than or equal to the date provided in the field-value.
10 /// This field accomplishes the same purpose as If-Match for cases where
11 /// the user agent does not have an entity-tag for the representation.
12 ///
13 /// # ABNF
14 ///
15 /// ```text
16 /// If-Unmodified-Since = HTTP-date
17 /// ```
18 ///
19 /// # Example values
20 ///
21 /// * `Sat, 29 Oct 1994 19:43:31 GMT`
22 ///
23 /// # Example
24 ///
25 /// ```
26 /// # extern crate headers;
27 /// use headers::IfUnmodifiedSince;
28 /// use std::time::{SystemTime, Duration};
29 ///
30 /// let time = SystemTime::now() - Duration::from_secs(60 * 60 * 24);
31 /// let if_unmod = IfUnmodifiedSince::from(time);
32 /// ```
33 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
34 pub struct IfUnmodifiedSince(HttpDate);
35
36 derive_header! {
37 IfUnmodifiedSince(_),
38 name: IF_UNMODIFIED_SINCE
39 }
40
41 impl IfUnmodifiedSince {
42 /// Check if the supplied time passes the precondtion.
43 pub fn precondition_passes(&self, last_modified: SystemTime) -> bool {
44 self.0 >= last_modified.into()
45 }
46 }
47
48 impl From<SystemTime> for IfUnmodifiedSince {
49 fn from(time: SystemTime) -> IfUnmodifiedSince {
50 IfUnmodifiedSince(time.into())
51 }
52 }
53
54 impl From<IfUnmodifiedSince> for SystemTime {
55 fn from(date: IfUnmodifiedSince) -> SystemTime {
56 date.0.into()
57 }
58 }
59
60 #[cfg(test)]
61 mod tests {
62 use super::*;
63 use std::time::Duration;
64
65 #[test]
66 fn precondition_passes() {
67 let newer = SystemTime::now();
68 let exact = newer - Duration::from_secs(2);
69 let older = newer - Duration::from_secs(4);
70
71 let if_unmod = IfUnmodifiedSince::from(exact);
72 assert!(!if_unmod.precondition_passes(newer));
73 assert!(if_unmod.precondition_passes(exact));
74 assert!(if_unmod.precondition_passes(older));
75 }
76 }