From 2dbe3f9b9a3fc9f04346712e55f40dabaf72d9a8 Mon Sep 17 00:00:00 2001 From: Pyfisch Date: Sat, 2 May 2015 21:05:25 +0200 Subject: [PATCH] feat(headers): Add Accept-Ranges header field --- src/header/common/accept_ranges.rs | 71 ++++++++++++++++++++++++++++++ src/header/common/mod.rs | 2 + 2 files changed, 73 insertions(+) create mode 100644 src/header/common/accept_ranges.rs diff --git a/src/header/common/accept_ranges.rs b/src/header/common/accept_ranges.rs new file mode 100644 index 0000000000..568f4b47f7 --- /dev/null +++ b/src/header/common/accept_ranges.rs @@ -0,0 +1,71 @@ +use std::fmt::{self, Display}; +use std::str::FromStr; + +header! { + #[doc="`Accept-Ranges` header, defined in"] + #[doc="[RFC7233](http://tools.ietf.org/html/rfc7233#section-2.3)"] + #[doc=""] + #[doc="The `Accept-Ranges` header field allows a server to indicate that it"] + #[doc="supports range requests for the target resource."] + #[doc=""] + #[doc="# ABNF"] + #[doc="```plain"] + #[doc="Accept-Ranges = acceptable-ranges"] + #[doc="acceptable-ranges = 1#range-unit / \"none\""] + #[doc=""] + #[doc="# Example values"] + #[doc="* `bytes`"] + #[doc="* `none`"] + #[doc="```"] + (AcceptRanges, "Accept-Ranges") => (RangeUnit)+ + + test_acccept_ranges { + test_header!(test1, vec![b"bytes"]); + test_header!(test2, vec![b"none"]); + } +} + +/// Range Units, described in [RFC7233](http://tools.ietf.org/html/rfc7233#section-2) +/// +/// A representation can be partitioned into subranges according to +/// various structural units, depending on the structure inherent in the +/// representation's media type. +/// +/// # ABNF +/// ```plain +/// range-unit = bytes-unit / other-range-unit +/// bytes-unit = "bytes" +/// other-range-unit = token +/// ``` +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum RangeUnit { + /// Indicating byte-range requests are supported. + Bytes, + /// Reserved as keyword, indicating no ranges are supported. + None, + /// The given range unit is not registered at IANA. + Unregistered(String), +} + + +impl FromStr for RangeUnit { + type Err = (); + fn from_str(s: &str) -> Result { + match s { + "bytes" => Ok(RangeUnit::Bytes), + "none" => Ok(RangeUnit::None), + // FIXME: Check if s is really a Token + _ => Ok(RangeUnit::Unregistered(s.to_string())), + } + } +} + +impl Display for RangeUnit { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + RangeUnit::Bytes => f.write_str("bytes"), + RangeUnit::None => f.write_str("none"), + RangeUnit::Unregistered(ref x) => f.write_str(&x), + } + } +} diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index cf8ef5dbd4..cdee9b99b5 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -16,6 +16,7 @@ pub use self::access_control_request_method::AccessControlRequestMethod; pub use self::accept_charset::AcceptCharset; pub use self::accept_encoding::AcceptEncoding; pub use self::accept_language::AcceptLanguage; +pub use self::accept_ranges::AcceptRanges; pub use self::allow::Allow; pub use self::authorization::{Authorization, Scheme, Basic}; pub use self::cache_control::{CacheControl, CacheDirective}; @@ -302,6 +303,7 @@ mod access_control_request_method; mod accept_charset; mod accept_encoding; mod accept_language; +mod accept_ranges; mod allow; mod authorization; mod cache_control;