add allow header

This commit is contained in:
Rohan Prinja
2014-12-10 05:36:25 +05:30
parent e4bf1155de
commit 4bae6b7e0e
3 changed files with 68 additions and 12 deletions

View File

@@ -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<Method>);
deref!(Allow -> Vec<Method>)
impl Header for Allow {
fn header_name(_: Option<Allow>) -> &'static str {
"Allow"
}
fn parse_header(raw: &[Vec<u8>]) -> Option<Allow> {
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>;
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::<Method>::new())));
}
}
bench_header!(bench, Allow, { vec![b"OPTIONS,GET,PUT,POST,DELETE,HEAD,TRACE,CONNECT,PATCH,fOObAr".to_vec()] })

View File

@@ -7,6 +7,7 @@
//! is used, such as `ContentType(pub Mime)`. //! is used, such as `ContentType(pub Mime)`.
pub use self::accept::Accept; pub use self::accept::Accept;
pub use self::allow::Allow;
pub use self::authorization::Authorization; pub use self::authorization::Authorization;
pub use self::cache_control::CacheControl; pub use self::cache_control::CacheControl;
pub use self::cookie::Cookies; pub use self::cookie::Cookies;
@@ -74,6 +75,9 @@ macro_rules! deref(
/// Exposes the Accept header. /// Exposes the Accept header.
pub mod accept; pub mod accept;
/// Exposes the Allow header.
pub mod allow;
/// Exposes the Authorization header. /// Exposes the Authorization header.
pub mod authorization; pub mod authorization;

View File

@@ -69,18 +69,22 @@ impl Method {
impl FromStr for Method { impl FromStr for Method {
fn from_str(s: &str) -> Option<Method> { fn from_str(s: &str) -> Option<Method> {
Some(match s { if s == "" {
"OPTIONS" => Options, None
"GET" => Get, } else {
"POST" => Post, Some(match s {
"PUT" => Put, "OPTIONS" => Options,
"DELETE" => Delete, "GET" => Get,
"HEAD" => Head, "POST" => Post,
"TRACE" => Trace, "PUT" => Put,
"CONNECT" => Connect, "DELETE" => Delete,
"PATCH" => Patch, "HEAD" => Head,
_ => Extension(s.to_string()) "TRACE" => Trace,
}) "CONNECT" => Connect,
"PATCH" => Patch,
_ => Extension(s.to_string())
})
}
} }
} }