Skip to content

Allow header #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/header/common/allow.rs
Original file line number Diff line number Diff line change
@@ -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()] })
4 changes: 4 additions & 0 deletions src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
28 changes: 16 additions & 12 deletions src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,22 @@ impl Method {

impl FromStr for Method {
fn from_str(s: &str) -> Option<Method> {
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())
})
}
}
}

Expand Down