refactor(headers): Use header!() for CORS headers.

This is the last bunch of headers that should use the new macro. Moved them out of
their own folder so that the macro works. Changed them, so that they are more in
line with the other headers.

BREAKING CHANGE: `AccessControlAllowHeaders` and `AccessControlRequestHeaders` values
are case insensitive now. `AccessControlAllowOrigin` variants are now `Any` and
`Value` to match the other headers.
This commit is contained in:
Pyfisch
2015-04-07 11:54:01 +02:00
parent ed1ccc6ad3
commit 94f38950dd
13 changed files with 71 additions and 184 deletions

View File

@@ -0,0 +1,55 @@
use std::fmt::{self};
use std::str;
use url::Url;
use header;
/// The `Access-Control-Allow-Origin` response header,
/// part of [CORS](http://www.w3.org/TR/cors/).
///
/// > The `Access-Control-Allow-Origin` header indicates whether a resource
/// > can be shared based by returning the value of the Origin request header,
/// > "*", or "null" in the response.
///
/// Spec: www.w3.org/TR/cors/#access-control-allow-origin-response-header
#[derive(Clone, PartialEq, Debug)]
pub enum AccessControlAllowOrigin {
/// Allow all origins
Any,
/// Allow one particular origin
Value(Url),
}
impl header::Header for AccessControlAllowOrigin {
fn header_name() -> &'static str {
"Access-Control-Allow-Origin"
}
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowOrigin> {
if raw.len() == 1 {
match str::from_utf8(unsafe { &raw.get_unchecked(0)[..] }) {
Ok(s) => {
if s == "*" {
Some(AccessControlAllowOrigin::Any)
} else {
Url::parse(s).ok().map(
|url| AccessControlAllowOrigin::Value(url))
}
},
_ => return None,
}
} else {
return None;
}
}
}
impl header::HeaderFormat for AccessControlAllowOrigin {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
AccessControlAllowOrigin::Any => write!(f, "*"),
AccessControlAllowOrigin::Value(ref url) =>
write!(f, "{}", url)
}
}
}