Skip to content

Commit a39735f

Browse files
committed
feat(headers): Add If-Range header
Closes #388
1 parent 29c8dd1 commit a39735f

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

src/header/common/if_range.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

src/header/common/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub use self::if_match::IfMatch;
3434
pub use self::if_modified_since::IfModifiedSince;
3535
pub use self::if_none_match::IfNoneMatch;
3636
pub use self::if_unmodified_since::IfUnmodifiedSince;
37+
pub use self::if_range::IfRange;
3738
pub use self::last_modified::LastModified;
3839
pub use self::location::Location;
3940
pub use self::pragma::Pragma;
@@ -118,8 +119,10 @@ macro_rules! test_header {
118119
// Test parsing
119120
assert_eq!(val, $typed);
120121
// Test formatting
121-
let res: &str = str::from_utf8($raw[0]).unwrap();
122-
assert_eq!(format!("{}", $typed.unwrap()), res);
122+
if $typed != None {
123+
let res: &str = str::from_utf8($raw[0]).unwrap();
124+
assert_eq!(format!("{}", $typed.unwrap()), res);
125+
}
123126
}
124127
}
125128
}
@@ -313,10 +316,11 @@ mod expect;
313316
mod expires;
314317
mod host;
315318
mod if_match;
316-
mod last_modified;
317319
mod if_modified_since;
318320
mod if_none_match;
321+
mod if_range;
319322
mod if_unmodified_since;
323+
mod last_modified;
320324
mod location;
321325
mod pragma;
322326
mod referer;

0 commit comments

Comments
 (0)