Merge pull request #440 from pyfisch/refactorheaders5
refactor(headers): Use header!() for CORS headers.
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
use std::fmt::{self};
|
||||
|
||||
use header;
|
||||
|
||||
/// The `Access-Control-Allow-Headers` response header,
|
||||
/// part of [CORS](http://www.w3.org/TR/cors/).
|
||||
///
|
||||
/// > The `Access-Control-Allow-Headers` header indicates, as part of the
|
||||
/// > response to a preflight request, which header field names can be used
|
||||
/// > during the actual request.
|
||||
///
|
||||
/// Spec: www.w3.org/TR/cors/#access-control-allow-headers-response-header
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct AccessControlAllowHeaders(pub Vec<String>);
|
||||
|
||||
impl header::Header for AccessControlAllowHeaders {
|
||||
#[inline]
|
||||
fn header_name() -> &'static str {
|
||||
"Access-Control-Allow-Headers"
|
||||
}
|
||||
|
||||
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowHeaders> {
|
||||
header::parsing::from_comma_delimited(raw).map(AccessControlAllowHeaders)
|
||||
}
|
||||
}
|
||||
|
||||
impl header::HeaderFormat for AccessControlAllowHeaders {
|
||||
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let AccessControlAllowHeaders(ref parts) = *self;
|
||||
header::parsing::fmt_comma_delimited(f, parts.as_ref())
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
use std::fmt::{self};
|
||||
|
||||
use header;
|
||||
use method;
|
||||
|
||||
/// The `Access-Control-Allow-Methods` response header,
|
||||
/// part of [CORS](http://www.w3.org/TR/cors/).
|
||||
///
|
||||
/// > The `Access-Control-Allow-Methods` header indicates, as part of the
|
||||
/// > response to a preflight request, which methods can be used during the
|
||||
/// > actual request.
|
||||
///
|
||||
/// Spec: www.w3.org/TR/cors/#access-control-allow-methods-response-header
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct AccessControlAllowMethods(pub Vec<method::Method>);
|
||||
|
||||
impl header::Header for AccessControlAllowMethods {
|
||||
#[inline]
|
||||
fn header_name() -> &'static str {
|
||||
"Access-Control-Allow-Methods"
|
||||
}
|
||||
|
||||
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowMethods> {
|
||||
header::parsing::from_comma_delimited(raw).map(AccessControlAllowMethods)
|
||||
}
|
||||
}
|
||||
|
||||
impl header::HeaderFormat for AccessControlAllowMethods {
|
||||
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let AccessControlAllowMethods(ref parts) = *self;
|
||||
header::parsing::fmt_comma_delimited(f, parts.as_ref())
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
use std::fmt;
|
||||
|
||||
use header;
|
||||
|
||||
/// The `Access-Control-Max-Age` response header,
|
||||
/// part of [CORS](http://www.w3.org/TR/cors/).
|
||||
///
|
||||
/// > The `Access-Control-Max-Age` header indicates how long the results of a
|
||||
/// > preflight request can be cached in a preflight result cache.
|
||||
///
|
||||
/// Spec: www.w3.org/TR/cors/#access-control-max-age-response-header
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
pub struct AccessControlMaxAge(pub u32);
|
||||
|
||||
impl header::Header for AccessControlMaxAge {
|
||||
#[inline]
|
||||
fn header_name() -> &'static str {
|
||||
"Access-Control-Max-Age"
|
||||
}
|
||||
|
||||
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlMaxAge> {
|
||||
header::parsing::from_one_raw_str(raw).map(AccessControlMaxAge)
|
||||
}
|
||||
}
|
||||
|
||||
impl header::HeaderFormat for AccessControlMaxAge {
|
||||
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let AccessControlMaxAge(ref num) = *self;
|
||||
write!(f, "{}", num)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
pub use self::allow_headers::AccessControlAllowHeaders;
|
||||
pub use self::allow_methods::AccessControlAllowMethods;
|
||||
pub use self::allow_origin::AccessControlAllowOrigin;
|
||||
pub use self::max_age::AccessControlMaxAge;
|
||||
pub use self::request_headers::AccessControlRequestHeaders;
|
||||
pub use self::request_method::AccessControlRequestMethod;
|
||||
|
||||
mod allow_headers;
|
||||
mod allow_methods;
|
||||
mod allow_origin;
|
||||
mod max_age;
|
||||
mod request_headers;
|
||||
mod request_method;
|
||||
@@ -1,31 +0,0 @@
|
||||
use std::fmt::{self};
|
||||
|
||||
use header;
|
||||
|
||||
/// The `Access-Control-Request-Headers` request header,
|
||||
/// part of [CORS](http://www.w3.org/TR/cors/).
|
||||
///
|
||||
/// > The `Access-Control-Request-Headers` header indicates which headers will
|
||||
/// > be used in the actual request as part of the preflight request.
|
||||
///
|
||||
/// Spec: www.w3.org/TR/cors/#access-control-request-headers-request-header
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct AccessControlRequestHeaders(pub Vec<String>);
|
||||
|
||||
impl header::Header for AccessControlRequestHeaders {
|
||||
#[inline]
|
||||
fn header_name() -> &'static str {
|
||||
"Access-Control-Request-Headers"
|
||||
}
|
||||
|
||||
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlRequestHeaders> {
|
||||
header::parsing::from_comma_delimited(raw).map(AccessControlRequestHeaders)
|
||||
}
|
||||
}
|
||||
|
||||
impl header::HeaderFormat for AccessControlRequestHeaders {
|
||||
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let AccessControlRequestHeaders(ref parts) = *self;
|
||||
header::parsing::fmt_comma_delimited(f, parts.as_ref())
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
use std::fmt;
|
||||
|
||||
use header;
|
||||
use method::Method;
|
||||
|
||||
/// The `Access-Control-Request-Method` request header,
|
||||
/// part of [CORS](http://www.w3.org/TR/cors/).
|
||||
///
|
||||
/// > The `Access-Control-Request-Method` header indicates which method will be
|
||||
/// > used in the actual request as part of the preflight request.
|
||||
///
|
||||
/// Spec: www.w3.org/TR/cors/#access-control-request-method-request-header
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct AccessControlRequestMethod(pub Method);
|
||||
|
||||
impl header::Header for AccessControlRequestMethod {
|
||||
#[inline]
|
||||
fn header_name() -> &'static str {
|
||||
"Access-Control-Request-Method"
|
||||
}
|
||||
|
||||
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlRequestMethod> {
|
||||
header::parsing::from_one_raw_str(raw).map(AccessControlRequestMethod)
|
||||
}
|
||||
}
|
||||
|
||||
impl header::HeaderFormat for AccessControlRequestMethod {
|
||||
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let AccessControlRequestMethod(ref method) = *self;
|
||||
write!(f, "{}", method)
|
||||
}
|
||||
}
|
||||
11
src/header/common/access_control_allow_headers.rs
Normal file
11
src/header/common/access_control_allow_headers.rs
Normal file
@@ -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
src/header/common/access_control_allow_methods.rs
Normal file
11
src/header/common/access_control_allow_methods.rs
Normal file
@@ -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)*
|
||||
}
|
||||
@@ -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,
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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,
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
8
src/header/common/access_control_max_age.rs
Normal file
8
src/header/common/access_control_max_age.rs
Normal file
@@ -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
src/header/common/access_control_request_headers.rs
Normal file
11
src/header/common/access_control_request_headers.rs
Normal file
@@ -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
src/header/common/access_control_request_method.rs
Normal file
10
src/header/common/access_control_request_method.rs
Normal file
@@ -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]
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user