Skip to content

Commit

Permalink
refactor(headers): Use header!() for CORS headers.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pyfisch committed Apr 7, 2015
1 parent ed1ccc6 commit 94f3895
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 184 deletions.
32 changes: 0 additions & 32 deletions src/header/common/access_control/allow_headers.rs

This file was deleted.

33 changes: 0 additions & 33 deletions src/header/common/access_control/allow_methods.rs

This file was deleted.

31 changes: 0 additions & 31 deletions src/header/common/access_control/max_age.rs

This file was deleted.

13 changes: 0 additions & 13 deletions src/header/common/access_control/mod.rs

This file was deleted.

31 changes: 0 additions & 31 deletions src/header/common/access_control/request_headers.rs

This file was deleted.

32 changes: 0 additions & 32 deletions src/header/common/access_control/request_method.rs

This file was deleted.

11 changes: 11 additions & 0 deletions src/header/common/access_control_allow_headers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use unicase::UniCase;

header! {
#[doc="`Access-Control-Allow-Headers` header, part of"]
#[doc="[CORS](www.w3.org/TR/cors/#access-control-allow-headers-response-header)"]
#[doc=""]
#[doc="The `Access-Control-Allow-Headers` header indicates, as part of the"]
#[doc="response to a preflight request, which header field names can be used"]
#[doc="during the actual request."]
(AccessControlAllowHeaders, "Access-Control-Allow-Headers") => (UniCase<String>)*
}
11 changes: 11 additions & 0 deletions src/header/common/access_control_allow_methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use method::Method;

header! {
#[doc="`Access-Control-Allow-Methods` header, part of"]
#[doc="[CORS](www.w3.org/TR/cors/#access-control-allow-methods-response-header)"]
#[doc=""]
#[doc="The `Access-Control-Allow-Methods` header indicates, as part of the"]
#[doc="response to a preflight request, which methods can be used during the"]
#[doc="actual request."]
(AccessControlAllowMethods, "Access-Control-Allow-Methods") => (Method)*
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
extern crate url;

use std::fmt::{self};
use std::str;

use url::Url;
use header;

/// The `Access-Control-Allow-Origin` response header,
Expand All @@ -16,13 +15,12 @@ use header;
#[derive(Clone, PartialEq, Debug)]
pub enum AccessControlAllowOrigin {
/// Allow all origins
AllowStar,
Any,
/// Allow one particular origin
AllowOrigin(url::Url),
Value(Url),
}

impl header::Header for AccessControlAllowOrigin {
#[inline]
fn header_name() -> &'static str {
"Access-Control-Allow-Origin"
}
Expand All @@ -32,10 +30,10 @@ impl header::Header for AccessControlAllowOrigin {
match str::from_utf8(unsafe { &raw.get_unchecked(0)[..] }) {
Ok(s) => {
if s == "*" {
Some(AccessControlAllowOrigin::AllowStar)
Some(AccessControlAllowOrigin::Any)
} else {
url::Url::parse(s).ok().map(
|url| AccessControlAllowOrigin::AllowOrigin(url))
Url::parse(s).ok().map(
|url| AccessControlAllowOrigin::Value(url))
}
},
_ => return None,
Expand All @@ -49,8 +47,8 @@ impl header::Header for AccessControlAllowOrigin {
impl header::HeaderFormat for AccessControlAllowOrigin {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
AccessControlAllowOrigin::AllowStar => write!(f, "*"),
AccessControlAllowOrigin::AllowOrigin(ref url) =>
AccessControlAllowOrigin::Any => write!(f, "*"),
AccessControlAllowOrigin::Value(ref url) =>
write!(f, "{}", url)
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/header/common/access_control_max_age.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
header! {
#[doc="`Access-Control-Max-Age` header, part of"]
#[doc="[CORS](www.w3.org/TR/cors/#access-control-max-age-response-header)"]
#[doc=""]
#[doc="The `Access-Control-Max-Age` header indicates how long the results of a"]
#[doc="preflight request can be cached in a preflight result cache."]
(AccessControlMaxAge, "Access-Control-Max-Age") => [u32]
}
11 changes: 11 additions & 0 deletions src/header/common/access_control_request_headers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use unicase::UniCase;

header! {
#[doc="`Access-Control-Request-Headers` header, part of"]
#[doc="[CORS](www.w3.org/TR/cors/#access-control-request-headers-request-header)"]
#[doc=""]
#[doc="The `Access-Control-Request-Headers` header indicates which headers will"]
#[doc="be used in the actual request as part of the preflight request."]
#[doc="during the actual request."]
(AccessControlRequestHeaders, "Access-Control-Request-Headers") => (UniCase<String>)*
}
10 changes: 10 additions & 0 deletions src/header/common/access_control_request_method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use method::Method;

header! {
#[doc="`Access-Control-Request-Method` header, part of"]
#[doc="[CORS](www.w3.org/TR/cors/#access-control-request-method-request-header)"]
#[doc=""]
#[doc="The `Access-Control-Request-Method` header indicates which method will be"]
#[doc="used in the actual request as part of the preflight request."]
(AccessControlRequestMethod, "Access-Control-Request-Method") => [Method]
}
14 changes: 12 additions & 2 deletions src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
//! strongly-typed theme, the [mime](http://seanmonstar.github.io/mime.rs) crate
//! is used, such as `ContentType(pub Mime)`.

pub use self::access_control::*;
pub use self::accept::Accept;
pub use self::access_control_allow_headers::AccessControlAllowHeaders;
pub use self::access_control_allow_methods::AccessControlAllowMethods;
pub use self::access_control_allow_origin::AccessControlAllowOrigin;
pub use self::access_control_max_age::AccessControlMaxAge;
pub use self::access_control_request_headers::AccessControlRequestHeaders;
pub use self::access_control_request_method::AccessControlRequestMethod;
pub use self::accept_charset::AcceptCharset;
pub use self::accept_encoding::AcceptEncoding;
pub use self::accept_language::AcceptLanguage;
Expand Down Expand Up @@ -216,8 +221,13 @@ macro_rules! header {
};
}

mod access_control;
mod accept;
mod access_control_allow_headers;
mod access_control_allow_methods;
mod access_control_allow_origin;
mod access_control_max_age;
mod access_control_request_headers;
mod access_control_request_method;
mod accept_charset;
mod accept_encoding;
mod accept_language;
Expand Down

0 comments on commit 94f3895

Please sign in to comment.