diff --git a/src/header/common/access_control/allow_headers.rs b/src/header/common/access_control/allow_headers.rs new file mode 100644 index 00000000..26a741b0 --- /dev/null +++ b/src/header/common/access_control/allow_headers.rs @@ -0,0 +1,25 @@ +use std::fmt::{self}; + +use header; +use header::shared; + +#[derive(Clone)] +struct AccessControlAllowHeaders(pub Vec); + +impl header::Header for AccessControlAllowHeaders { + #[inline] + fn header_name(_: Option) -> &'static str { + "Access-Control-Allow-Headers" + } + + fn parse_header(raw: &[Vec]) -> Option { + shared::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; + shared::fmt_comma_delimited(f, parts.as_slice()) + } +} diff --git a/src/header/common/access_control/allow_methods.rs b/src/header/common/access_control/allow_methods.rs new file mode 100644 index 00000000..cdd71896 --- /dev/null +++ b/src/header/common/access_control/allow_methods.rs @@ -0,0 +1,26 @@ +use std::fmt::{self}; + +use header; +use header::shared; +use method; + +#[derive(Clone)] +struct AccessControlAllowMethods(pub Vec); + +impl header::Header for AccessControlAllowMethods { + #[inline] + fn header_name(_: Option) -> &'static str { + "Access-Control-Allow-Methods" + } + + fn parse_header(raw: &[Vec]) -> Option { + shared::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; + shared::fmt_comma_delimited(f, parts.as_slice()) + } +} diff --git a/src/header/common/access_control/allow_origin.rs b/src/header/common/access_control/allow_origin.rs new file mode 100644 index 00000000..dfb1d118 --- /dev/null +++ b/src/header/common/access_control/allow_origin.rs @@ -0,0 +1,47 @@ +extern crate url; + +use std::fmt::{self}; +use std::str; + +use header; + +#[derive(Clone)] +enum AccessControlAllowOrigin { + AllowStar, + AllowOrigin(url::Url), +} + +impl header::Header for AccessControlAllowOrigin { + #[inline] + fn header_name(_: Option) -> &'static str { + "Access-Control-Allow-Origin" + } + + fn parse_header(raw: &[Vec]) -> Option { + if raw.len() == 1 { + match str::from_utf8(unsafe { raw[].get_unchecked(0)[] }) { + Ok(s) => { + if s == "*" { + Some(AccessControlAllowOrigin::AllowStar) + } else { + url::Url::parse(s).ok().map( + |url| AccessControlAllowOrigin::AllowOrigin(url)) + } + }, + _ => return None, + } + } else { + return None; + } + } +} + +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) => + write!(f, "{}", url) + } + } +} diff --git a/src/header/common/access_control/max_age.rs b/src/header/common/access_control/max_age.rs new file mode 100644 index 00000000..98b52b47 --- /dev/null +++ b/src/header/common/access_control/max_age.rs @@ -0,0 +1,25 @@ +use std::fmt; + +use header; +use header::shared; + +#[derive(Clone)] +struct AccessControlMaxAge(pub u32); + +impl header::Header for AccessControlMaxAge { + #[inline] + fn header_name(_: Option) -> &'static str { + "Access-Control-Max-Age" + } + + fn parse_header(raw: &[Vec]) -> Option { + shared::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) + } +} diff --git a/src/header/common/access_control/mod.rs b/src/header/common/access_control/mod.rs new file mode 100644 index 00000000..d8e6e21f --- /dev/null +++ b/src/header/common/access_control/mod.rs @@ -0,0 +1,17 @@ +/// Exposes the AccessControlAllowHeaders header +pub mod allow_headers; + +/// Exposes the AccessControlAllowMethods header +pub mod allow_methods; + +/// Exposes the AccessControlAllowOrigin header +pub mod allow_origin; + +/// Exposes the AccessControlMaxAge header +pub mod max_age; + +/// Exposes the AccessControlRequestHeaders header +pub mod request_headers; + +/// Exposes the AccessControlRequestMethod header +pub mod request_method; diff --git a/src/header/common/access_control/request_headers.rs b/src/header/common/access_control/request_headers.rs new file mode 100644 index 00000000..1aafea98 --- /dev/null +++ b/src/header/common/access_control/request_headers.rs @@ -0,0 +1,25 @@ +use std::fmt::{self}; + +use header; +use header::shared; + +#[derive(Clone)] +struct AccessControlRequestHeaders(pub Vec); + +impl header::Header for AccessControlRequestHeaders { + #[inline] + fn header_name(_: Option) -> &'static str { + "Access-Control-Request-Headers" + } + + fn parse_header(raw: &[Vec]) -> Option { + shared::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; + shared::fmt_comma_delimited(f, parts.as_slice()) + } +} diff --git a/src/header/common/access_control/request_method.rs b/src/header/common/access_control/request_method.rs new file mode 100644 index 00000000..4b5a7746 --- /dev/null +++ b/src/header/common/access_control/request_method.rs @@ -0,0 +1,26 @@ +use std::fmt; + +use header; +use header::shared; +use method::Method; + +#[derive(Clone)] +struct AccessControlRequestMethod(pub Method); + +impl header::Header for AccessControlRequestMethod { + #[inline] + fn header_name(_: Option) -> &'static str { + "Access-Control-Request-Method" + } + + fn parse_header(raw: &[Vec]) -> Option { + shared::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) + } +} diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index 2b1f411e..dff6373e 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -76,6 +76,9 @@ macro_rules! deref( } ); +/// Exposes the AccessControl* family of headers. +pub mod access_control; + /// Exposes the Accept header. pub mod accept;