Skip to content

Commit 7f93184

Browse files
committed
Merge pull request #188 from wenderen/allow-header
Allow header Closes #174
2 parents e4bf115 + 4bae6b7 commit 7f93184

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

src/header/common/allow.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use header::{Header, HeaderFormat};
2+
use method::Method;
3+
use std::fmt::{mod};
4+
use super::util::{from_comma_delimited, fmt_comma_delimited};
5+
6+
/// The `Allow` header.
7+
/// See also https://tools.ietf.org/html/rfc7231#section-7.4.1
8+
9+
#[deriving(Clone, PartialEq, Show)]
10+
pub struct Allow(pub Vec<Method>);
11+
12+
deref!(Allow -> Vec<Method>)
13+
14+
impl Header for Allow {
15+
fn header_name(_: Option<Allow>) -> &'static str {
16+
"Allow"
17+
}
18+
19+
fn parse_header(raw: &[Vec<u8>]) -> Option<Allow> {
20+
from_comma_delimited(raw).map(|vec| Allow(vec))
21+
}
22+
}
23+
24+
impl HeaderFormat for Allow {
25+
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
26+
fmt_comma_delimited(fmt, self[])
27+
}
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::Allow;
33+
use header::Header;
34+
use method::Method::{mod, Options, Get, Put, Post, Delete, Head, Trace, Connect, Patch, Extension};
35+
36+
#[test]
37+
fn test_allow() {
38+
let mut allow: Option<Allow>;
39+
40+
allow = Header::parse_header([b"OPTIONS,GET,PUT,POST,DELETE,HEAD,TRACE,CONNECT,PATCH,fOObAr".to_vec()].as_slice());
41+
assert_eq!(allow, Some(Allow(vec![Options, Get, Put, Post, Delete, Head, Trace, Connect, Patch, Extension("fOObAr".to_string())])));
42+
43+
allow = Header::parse_header([b"".to_vec()].as_slice());
44+
assert_eq!(allow, Some(Allow(Vec::<Method>::new())));
45+
}
46+
}
47+
48+
bench_header!(bench, Allow, { vec![b"OPTIONS,GET,PUT,POST,DELETE,HEAD,TRACE,CONNECT,PATCH,fOObAr".to_vec()] })

src/header/common/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! is used, such as `ContentType(pub Mime)`.
88
99
pub use self::accept::Accept;
10+
pub use self::allow::Allow;
1011
pub use self::authorization::Authorization;
1112
pub use self::cache_control::CacheControl;
1213
pub use self::cookie::Cookies;
@@ -74,6 +75,9 @@ macro_rules! deref(
7475
/// Exposes the Accept header.
7576
pub mod accept;
7677

78+
/// Exposes the Allow header.
79+
pub mod allow;
80+
7781
/// Exposes the Authorization header.
7882
pub mod authorization;
7983

src/method.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,22 @@ impl Method {
6969

7070
impl FromStr for Method {
7171
fn from_str(s: &str) -> Option<Method> {
72-
Some(match s {
73-
"OPTIONS" => Options,
74-
"GET" => Get,
75-
"POST" => Post,
76-
"PUT" => Put,
77-
"DELETE" => Delete,
78-
"HEAD" => Head,
79-
"TRACE" => Trace,
80-
"CONNECT" => Connect,
81-
"PATCH" => Patch,
82-
_ => Extension(s.to_string())
83-
})
72+
if s == "" {
73+
None
74+
} else {
75+
Some(match s {
76+
"OPTIONS" => Options,
77+
"GET" => Get,
78+
"POST" => Post,
79+
"PUT" => Put,
80+
"DELETE" => Delete,
81+
"HEAD" => Head,
82+
"TRACE" => Trace,
83+
"CONNECT" => Connect,
84+
"PATCH" => Patch,
85+
_ => Extension(s.to_string())
86+
})
87+
}
8488
}
8589
}
8690

0 commit comments

Comments
 (0)