diff --git a/src/header/common/allow.rs b/src/header/common/allow.rs new file mode 100644 index 0000000000..41489adbe3 --- /dev/null +++ b/src/header/common/allow.rs @@ -0,0 +1,48 @@ +use header::{Header, HeaderFormat}; +use method::Method; +use std::fmt::{mod}; +use super::util::{from_comma_delimited, fmt_comma_delimited}; + +/// The `Allow` header. +/// See also https://tools.ietf.org/html/rfc7231#section-7.4.1 + +#[deriving(Clone, PartialEq, Show)] +pub struct Allow(pub Vec); + +deref!(Allow -> Vec) + +impl Header for Allow { + fn header_name(_: Option) -> &'static str { + "Allow" + } + + fn parse_header(raw: &[Vec]) -> Option { + from_comma_delimited(raw).map(|vec| Allow(vec)) + } +} + +impl HeaderFormat for Allow { + fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt_comma_delimited(fmt, self[]) + } +} + +#[cfg(test)] +mod tests { + use super::Allow; + use header::Header; + use method::Method::{mod, Options, Get, Put, Post, Delete, Head, Trace, Connect, Patch, Extension}; + + #[test] + fn test_allow() { + let mut allow: Option; + + allow = Header::parse_header([b"OPTIONS,GET,PUT,POST,DELETE,HEAD,TRACE,CONNECT,PATCH,fOObAr".to_vec()].as_slice()); + assert_eq!(allow, Some(Allow(vec![Options, Get, Put, Post, Delete, Head, Trace, Connect, Patch, Extension("fOObAr".to_string())]))); + + allow = Header::parse_header([b"".to_vec()].as_slice()); + assert_eq!(allow, Some(Allow(Vec::::new()))); + } +} + +bench_header!(bench, Allow, { vec![b"OPTIONS,GET,PUT,POST,DELETE,HEAD,TRACE,CONNECT,PATCH,fOObAr".to_vec()] }) \ No newline at end of file diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index 3bbfa0564d..87b4f8630b 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -7,6 +7,7 @@ //! is used, such as `ContentType(pub Mime)`. pub use self::accept::Accept; +pub use self::allow::Allow; pub use self::authorization::Authorization; pub use self::cache_control::CacheControl; pub use self::cookie::Cookies; @@ -74,6 +75,9 @@ macro_rules! deref( /// Exposes the Accept header. pub mod accept; +/// Exposes the Allow header. +pub mod allow; + /// Exposes the Authorization header. pub mod authorization; diff --git a/src/method.rs b/src/method.rs index 0e0b89e125..41d53d2715 100644 --- a/src/method.rs +++ b/src/method.rs @@ -69,18 +69,22 @@ impl Method { impl FromStr for Method { fn from_str(s: &str) -> Option { - Some(match s { - "OPTIONS" => Options, - "GET" => Get, - "POST" => Post, - "PUT" => Put, - "DELETE" => Delete, - "HEAD" => Head, - "TRACE" => Trace, - "CONNECT" => Connect, - "PATCH" => Patch, - _ => Extension(s.to_string()) - }) + if s == "" { + None + } else { + Some(match s { + "OPTIONS" => Options, + "GET" => Get, + "POST" => Post, + "PUT" => Put, + "DELETE" => Delete, + "HEAD" => Head, + "TRACE" => Trace, + "CONNECT" => Connect, + "PATCH" => Patch, + _ => Extension(s.to_string()) + }) + } } }