Skip to content

Commit 2dbe3f9

Browse files
committed
feat(headers): Add Accept-Ranges header field
1 parent 1426a4c commit 2dbe3f9

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/header/common/accept_ranges.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::fmt::{self, Display};
2+
use std::str::FromStr;
3+
4+
header! {
5+
#[doc="`Accept-Ranges` header, defined in"]
6+
#[doc="[RFC7233](http://tools.ietf.org/html/rfc7233#section-2.3)"]
7+
#[doc=""]
8+
#[doc="The `Accept-Ranges` header field allows a server to indicate that it"]
9+
#[doc="supports range requests for the target resource."]
10+
#[doc=""]
11+
#[doc="# ABNF"]
12+
#[doc="```plain"]
13+
#[doc="Accept-Ranges = acceptable-ranges"]
14+
#[doc="acceptable-ranges = 1#range-unit / \"none\""]
15+
#[doc=""]
16+
#[doc="# Example values"]
17+
#[doc="* `bytes`"]
18+
#[doc="* `none`"]
19+
#[doc="```"]
20+
(AcceptRanges, "Accept-Ranges") => (RangeUnit)+
21+
22+
test_acccept_ranges {
23+
test_header!(test1, vec![b"bytes"]);
24+
test_header!(test2, vec![b"none"]);
25+
}
26+
}
27+
28+
/// Range Units, described in [RFC7233](http://tools.ietf.org/html/rfc7233#section-2)
29+
///
30+
/// A representation can be partitioned into subranges according to
31+
/// various structural units, depending on the structure inherent in the
32+
/// representation's media type.
33+
///
34+
/// # ABNF
35+
/// ```plain
36+
/// range-unit = bytes-unit / other-range-unit
37+
/// bytes-unit = "bytes"
38+
/// other-range-unit = token
39+
/// ```
40+
#[derive(Clone, Debug, Eq, PartialEq)]
41+
pub enum RangeUnit {
42+
/// Indicating byte-range requests are supported.
43+
Bytes,
44+
/// Reserved as keyword, indicating no ranges are supported.
45+
None,
46+
/// The given range unit is not registered at IANA.
47+
Unregistered(String),
48+
}
49+
50+
51+
impl FromStr for RangeUnit {
52+
type Err = ();
53+
fn from_str(s: &str) -> Result<Self, ()> {
54+
match s {
55+
"bytes" => Ok(RangeUnit::Bytes),
56+
"none" => Ok(RangeUnit::None),
57+
// FIXME: Check if s is really a Token
58+
_ => Ok(RangeUnit::Unregistered(s.to_string())),
59+
}
60+
}
61+
}
62+
63+
impl Display for RangeUnit {
64+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65+
match *self {
66+
RangeUnit::Bytes => f.write_str("bytes"),
67+
RangeUnit::None => f.write_str("none"),
68+
RangeUnit::Unregistered(ref x) => f.write_str(&x),
69+
}
70+
}
71+
}

src/header/common/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub use self::access_control_request_method::AccessControlRequestMethod;
1616
pub use self::accept_charset::AcceptCharset;
1717
pub use self::accept_encoding::AcceptEncoding;
1818
pub use self::accept_language::AcceptLanguage;
19+
pub use self::accept_ranges::AcceptRanges;
1920
pub use self::allow::Allow;
2021
pub use self::authorization::{Authorization, Scheme, Basic};
2122
pub use self::cache_control::{CacheControl, CacheDirective};
@@ -302,6 +303,7 @@ mod access_control_request_method;
302303
mod accept_charset;
303304
mod accept_encoding;
304305
mod accept_language;
306+
mod accept_ranges;
305307
mod allow;
306308
mod authorization;
307309
mod cache_control;

0 commit comments

Comments
 (0)