headers_ext/common/if_range.rs
1use std::time::SystemTime;
2
3use ::HeaderValue;
4use util::{EntityTag, HttpDate};
5use super::{LastModified, ETag};
6
7/// `If-Range` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-3.2)
8///
9/// If a client has a partial copy of a representation and wishes to have
10/// an up-to-date copy of the entire representation, it could use the
11/// Range header field with a conditional GET (using either or both of
12/// If-Unmodified-Since and If-Match.) However, if the precondition
13/// fails because the representation has been modified, the client would
14/// then have to make a second request to obtain the entire current
15/// representation.
16///
17/// The `If-Range` header field allows a client to \"short-circuit\" the
18/// second request. Informally, its meaning is as follows: if the
19/// representation is unchanged, send me the part(s) that I am requesting
20/// in Range; otherwise, send me the entire representation.
21///
22/// # ABNF
23///
24/// ```text
25/// If-Range = entity-tag / HTTP-date
26/// ```
27///
28/// # Example values
29///
30/// * `Sat, 29 Oct 1994 19:43:31 GMT`
31/// * `\"xyzzy\"`
32///
33/// # Examples
34///
35/// ```
36/// # extern crate headers_ext as headers;
37/// use headers::IfRange;
38/// use std::time::{SystemTime, Duration};
39///
40/// let fetched = SystemTime::now() - Duration::from_secs(60 * 60 * 24);
41/// let if_range = IfRange::date(fetched);
42/// ```
43#[derive(Clone, Debug, PartialEq, Header)]
44pub struct IfRange(IfRange_);
45
46impl IfRange {
47 /// Create an `IfRange` header with an entity tag.
48 pub fn etag(tag: ETag) -> IfRange {
49 IfRange(IfRange_::EntityTag(tag.0))
50 }
51
52 /// Create an `IfRange` header with a date value.
53 pub fn date(time: SystemTime) -> IfRange {
54 IfRange(IfRange_::Date(time.into()))
55 }
56
57 /// Checks if the resource has been modified, or if the range request
58 /// can be served.
59 pub fn is_modified(&self, _etag: Option<&ETag>, last_modified: Option<&LastModified>) -> bool {
60 match self.0 {
61 IfRange_::Date(since) => last_modified.map(|time| since < time.0).unwrap_or(true),
62 IfRange_::EntityTag(_) => true,
63 }
64 }
65}
66
67#[derive(Clone, Debug, PartialEq)]
68enum IfRange_ {
69 /// The entity-tag the client has of the resource
70 EntityTag(EntityTag),
71 /// The date when the client retrieved the resource
72 Date(HttpDate),
73}
74
75impl ::util::TryFromValues for IfRange_ {
76 fn try_from_values<'i, I>(values: &mut I) -> Result<Self, ::Error>
77 where
78 I: Iterator<Item = &'i HeaderValue>,
79 {
80 values
81 .next()
82 .and_then(|val| {
83 if let Some(tag) = EntityTag::from_val(val) {
84 return Some(IfRange_::EntityTag(tag));
85 }
86
87 let date = HttpDate::from_val(val)?;
88 Some(IfRange_::Date(date))
89 })
90 .ok_or_else(::Error::invalid)
91 }
92}
93
94impl<'a> From<&'a IfRange_> for HeaderValue {
95 fn from(if_range: &'a IfRange_) -> HeaderValue {
96 match *if_range {
97 IfRange_::EntityTag(ref tag) => tag.into(),
98 IfRange_::Date(ref date) => date.into(),
99 }
100 }
101}
102
103
104
105/*
106#[cfg(test)]
107mod tests {
108 use std::str;
109 use *;
110 use super::IfRange as HeaderField;
111 test_header!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
112 test_header!(test2, vec![b"\"xyzzy\""]);
113 test_header!(test3, vec![b"this-is-invalid"], None::<IfRange>);
114}
115*/