hyperx/header/common/
if_range.rs

1use std::fmt::{self, Display};
2use header::{self, Header, RawLike, EntityTag, HttpDate};
3
4/// `If-Range` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-3.2)
5///
6/// If a client has a partial copy of a representation and wishes to have
7/// an up-to-date copy of the entire representation, it could use the
8/// Range header field with a conditional GET (using either or both of
9/// If-Unmodified-Since and If-Match.)  However, if the precondition
10/// fails because the representation has been modified, the client would
11/// then have to make a second request to obtain the entire current
12/// representation.
13///
14/// The `If-Range` header field allows a client to \"short-circuit\" the
15/// second request.  Informally, its meaning is as follows: if the
16/// representation is unchanged, send me the part(s) that I am requesting
17/// in Range; otherwise, send me the entire representation.
18///
19/// # ABNF
20///
21/// ```text
22/// If-Range = entity-tag / HTTP-date
23/// ```
24///
25/// # Example values
26///
27/// * `Sat, 29 Oct 1994 19:43:31 GMT`
28/// * `\"xyzzy\"`
29///
30/// # Examples
31///
32/// ```
33/// # extern crate http;
34/// use hyperx::header::{IfRange, EntityTag, TypedHeaders};
35///
36/// let mut headers = http::HeaderMap::new();
37/// headers.encode(&IfRange::EntityTag(EntityTag::new(false, "xyzzy".to_owned())));
38/// ```
39///
40/// ```
41/// # extern crate http;
42/// use hyperx::header::{IfRange, TypedHeaders};
43/// use std::time::{SystemTime, Duration};
44///
45/// let mut headers = http::HeaderMap::new();
46/// let fetched = SystemTime::now() - Duration::from_secs(60 * 60 * 24);
47/// headers.encode(&IfRange::Date(fetched.into()));
48/// ```
49#[derive(Clone, Debug, PartialEq)]
50pub enum IfRange {
51    /// The entity-tag the client has of the resource
52    EntityTag(EntityTag),
53    /// The date when the client retrieved the resource
54    Date(HttpDate),
55}
56
57impl Header for IfRange {
58    fn header_name() -> &'static str {
59        static NAME: &'static str = "If-Range";
60        NAME
61    }
62    fn parse_header<'a, T>(raw: &'a T) -> ::Result<IfRange>
63    where T: RawLike<'a>
64    {
65        let etag: ::Result<EntityTag> = header::parsing::from_one_raw_str(raw);
66        if let Ok(etag) = etag {
67            return Ok(IfRange::EntityTag(etag));
68        }
69        let date: ::Result<HttpDate> = header::parsing::from_one_raw_str(raw);
70        if let Ok(date) = date {
71            return Ok(IfRange::Date(date));
72        }
73        Err(::Error::Header)
74    }
75
76    fn fmt_header(&self, f: &mut ::header::Formatter) -> ::std::fmt::Result {
77        f.fmt_line(self)
78    }
79}
80
81impl Display for IfRange {
82    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83        match *self {
84            IfRange::EntityTag(ref x) => Display::fmt(x, f),
85            IfRange::Date(ref x) => Display::fmt(x, f),
86        }
87    }
88}
89
90#[cfg(test)]
91mod test_if_range {
92    use std::str;
93    use header::*;
94    use super::IfRange as HeaderField;
95    test_header!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
96    test_header!(test2, vec![b"\"xyzzy\""]);
97    test_header!(test3, vec![b"this-is-invalid"], None::<IfRange>);
98}
99
100standard_header!(IfRange, IF_RANGE);