Skip to content

Commit 1426a4c

Browse files
committed
Merge pull request #491 from pyfisch/ifrangeheader
feat(headers): Add If-Range header
2 parents 92ee51a + a39735f commit 1426a4c

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/header/common/if_range.rs

Lines changed: 73 additions & 0 deletions
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

Lines changed: 3 additions & 1 deletion
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;
@@ -316,10 +317,11 @@ mod expect;
316317
mod expires;
317318
mod host;
318319
mod if_match;
319-
mod last_modified;
320320
mod if_modified_since;
321321
mod if_none_match;
322+
mod if_range;
322323
mod if_unmodified_since;
324+
mod last_modified;
323325
mod location;
324326
mod pragma;
325327
mod referer;

0 commit comments

Comments
 (0)