Skip to content

Commit 94f3895

Browse files
committed
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.
1 parent ed1ccc6 commit 94f3895

13 files changed

+71
-184
lines changed

src/header/common/access_control/allow_headers.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/header/common/access_control/allow_methods.rs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/header/common/access_control/max_age.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/header/common/access_control/mod.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/header/common/access_control/request_headers.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/header/common/access_control/request_method.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use unicase::UniCase;
2+
3+
header! {
4+
#[doc="`Access-Control-Allow-Headers` header, part of"]
5+
#[doc="[CORS](www.w3.org/TR/cors/#access-control-allow-headers-response-header)"]
6+
#[doc=""]
7+
#[doc="The `Access-Control-Allow-Headers` header indicates, as part of the"]
8+
#[doc="response to a preflight request, which header field names can be used"]
9+
#[doc="during the actual request."]
10+
(AccessControlAllowHeaders, "Access-Control-Allow-Headers") => (UniCase<String>)*
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use method::Method;
2+
3+
header! {
4+
#[doc="`Access-Control-Allow-Methods` header, part of"]
5+
#[doc="[CORS](www.w3.org/TR/cors/#access-control-allow-methods-response-header)"]
6+
#[doc=""]
7+
#[doc="The `Access-Control-Allow-Methods` header indicates, as part of the"]
8+
#[doc="response to a preflight request, which methods can be used during the"]
9+
#[doc="actual request."]
10+
(AccessControlAllowMethods, "Access-Control-Allow-Methods") => (Method)*
11+
}

src/header/common/access_control/allow_origin.rs renamed to src/header/common/access_control_allow_origin.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
extern crate url;
2-
31
use std::fmt::{self};
42
use std::str;
53

4+
use url::Url;
65
use header;
76

87
/// The `Access-Control-Allow-Origin` response header,
@@ -16,13 +15,12 @@ use header;
1615
#[derive(Clone, PartialEq, Debug)]
1716
pub enum AccessControlAllowOrigin {
1817
/// Allow all origins
19-
AllowStar,
18+
Any,
2019
/// Allow one particular origin
21-
AllowOrigin(url::Url),
20+
Value(Url),
2221
}
2322

2423
impl header::Header for AccessControlAllowOrigin {
25-
#[inline]
2624
fn header_name() -> &'static str {
2725
"Access-Control-Allow-Origin"
2826
}
@@ -32,10 +30,10 @@ impl header::Header for AccessControlAllowOrigin {
3230
match str::from_utf8(unsafe { &raw.get_unchecked(0)[..] }) {
3331
Ok(s) => {
3432
if s == "*" {
35-
Some(AccessControlAllowOrigin::AllowStar)
33+
Some(AccessControlAllowOrigin::Any)
3634
} else {
37-
url::Url::parse(s).ok().map(
38-
|url| AccessControlAllowOrigin::AllowOrigin(url))
35+
Url::parse(s).ok().map(
36+
|url| AccessControlAllowOrigin::Value(url))
3937
}
4038
},
4139
_ => return None,
@@ -49,8 +47,8 @@ impl header::Header for AccessControlAllowOrigin {
4947
impl header::HeaderFormat for AccessControlAllowOrigin {
5048
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
5149
match *self {
52-
AccessControlAllowOrigin::AllowStar => write!(f, "*"),
53-
AccessControlAllowOrigin::AllowOrigin(ref url) =>
50+
AccessControlAllowOrigin::Any => write!(f, "*"),
51+
AccessControlAllowOrigin::Value(ref url) =>
5452
write!(f, "{}", url)
5553
}
5654
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
header! {
2+
#[doc="`Access-Control-Max-Age` header, part of"]
3+
#[doc="[CORS](www.w3.org/TR/cors/#access-control-max-age-response-header)"]
4+
#[doc=""]
5+
#[doc="The `Access-Control-Max-Age` header indicates how long the results of a"]
6+
#[doc="preflight request can be cached in a preflight result cache."]
7+
(AccessControlMaxAge, "Access-Control-Max-Age") => [u32]
8+
}

0 commit comments

Comments
 (0)