|
| 1 | +use header::{self, EntityTag, HttpDate}; |
| 2 | + |
| 3 | +/// `If-Range` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-3.2) |
| 4 | +/// |
| 5 | +/// If a client has a partial copy of a representation and wishes to have |
| 6 | +/// an up-to-date copy of the entire representation, it could use the |
| 7 | +/// Range header field with a conditional GET (using either or both of |
| 8 | +/// If-Unmodified-Since and If-Match.) However, if the precondition |
| 9 | +/// fails because the representation has been modified, the client would |
| 10 | +/// then have to make a second request to obtain the entire current |
| 11 | +/// representation. |
| 12 | +/// |
| 13 | +/// The `If-Range` header field allows a client to \"short-circuit\" the |
| 14 | +/// second request. Informally, its meaning is as follows: if the |
| 15 | +/// representation is unchanged, send me the part(s) that I am requesting |
| 16 | +/// in Range; otherwise, send me the entire representation. |
| 17 | +/// |
| 18 | +/// # ABNF |
| 19 | +/// ```plain |
| 20 | +/// If-Range = entity-tag / HTTP-date |
| 21 | +/// ``` |
| 22 | +/// |
| 23 | +/// # Example values |
| 24 | +/// * `Sat, 29 Oct 1994 19:43:31 GMT` |
| 25 | +/// * `\"xyzzy\"` |
| 26 | +#[derive(Clone, Debug, PartialEq)] |
| 27 | +pub enum IfRange { |
| 28 | + /// The entity-tag the client has of the resource |
| 29 | + EntityTag(EntityTag), |
| 30 | + /// The date when the client retrieved the resource |
| 31 | + Date(HttpDate), |
| 32 | +} |
| 33 | + |
| 34 | +impl header::Header for IfRange { |
| 35 | + fn header_name() -> &'static str { |
| 36 | + "If-Range" |
| 37 | + } |
| 38 | + fn parse_header(raw: &[Vec<u8>]) -> Option<IfRange> { |
| 39 | + let etag: Option<EntityTag> = header::parsing::from_one_raw_str(raw); |
| 40 | + if etag != None { |
| 41 | + return Some(IfRange::EntityTag(etag.unwrap())); |
| 42 | + } |
| 43 | + let date: Option<HttpDate> = header::parsing::from_one_raw_str(raw); |
| 44 | + if date != None { |
| 45 | + return Some(IfRange::Date(date.unwrap())); |
| 46 | + } |
| 47 | + None |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl header::HeaderFormat for IfRange { |
| 52 | + fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
| 53 | + match self { |
| 54 | + &IfRange::EntityTag(ref x) => write!(f, "{}", x), |
| 55 | + &IfRange::Date(ref x) => write!(f, "{}", x), |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl ::std::fmt::Display for IfRange { |
| 61 | + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
| 62 | + use header::HeaderFormat; |
| 63 | + self.fmt_header(f) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(test)] |
| 68 | +mod test_range { |
| 69 | + use header::*; |
| 70 | + use super::IfRange as HeaderField; |
| 71 | + test_header!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]); |
| 72 | + test_header!(test2, vec![b"\"xyzzy\""]); |
| 73 | +} |
0 commit comments