Worked on Access-Control-* header family.

This commit is contained in:
Pyfisch
2015-01-06 22:04:35 +01:00
parent 0f781efd7b
commit c228a5506a
8 changed files with 51 additions and 26 deletions

View File

@@ -1,9 +1,9 @@
use std::fmt::{mod}; use std::fmt::{self};
use header; use header;
use header::shared; use header::shared;
#[deriving(Clone)] #[derive(Clone)]
struct AccessControlAllowHeaders(pub Vec<String>); struct AccessControlAllowHeaders(pub Vec<String>);
impl header::Header for AccessControlAllowHeaders { impl header::Header for AccessControlAllowHeaders {

View File

@@ -1,10 +1,11 @@
use std::fmt::{mod}; use std::fmt::{self};
use header; use header;
use header::shared; use header::shared;
use method;
#[deriving(Clone)] #[derive(Clone)]
struct AccessControlAllowMethods(pub Vec<Method>); struct AccessControlAllowMethods(pub Vec<method::Method>);
impl header::Header for AccessControlAllowMethods { impl header::Header for AccessControlAllowMethods {
#[inline] #[inline]

View File

@@ -1,9 +1,14 @@
use header::shared; extern crate url;
#[deriving(Clone)] use std::fmt::{self};
use std::str;
use header;
#[derive(Clone)]
enum AccessControlAllowOrigin { enum AccessControlAllowOrigin {
AllowStar, AllowStar,
AllowOrigin(Url), AllowOrigin(url::Url),
} }
impl header::Header for AccessControlAllowOrigin { impl header::Header for AccessControlAllowOrigin {
@@ -14,15 +19,19 @@ impl header::Header for AccessControlAllowOrigin {
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowOrigin> { fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowOrigin> {
if raw.len() == 1 { if raw.len() == 1 {
from_utf8(raw[0].as_slice()).and_then(|s| { match str::from_utf8(unsafe { raw[].get_unchecked(0)[] }) {
if s == "*" { Ok(s) => {
Some(AllowStar) if s == "*" {
} else { Some(AccessControlAllowOrigin::AllowStar)
Url::parse(s).ok().map(|url| AllowOrigin(url)) } else {
} url::Url::parse(s).ok().map(
}) |url| AccessControlAllowOrigin::AllowOrigin(url))
}
},
_ => return None,
}
} else { } else {
None return None;
} }
} }
} }
@@ -30,8 +39,9 @@ impl header::Header for AccessControlAllowOrigin {
impl header::HeaderFormat for AccessControlAllowOrigin { impl header::HeaderFormat for AccessControlAllowOrigin {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
AllowStar => "*".fmt(f), AccessControlAllowOrigin::AllowStar => write!(f, "*"),
AllowOrigin(ref url) => url.fmt(f) AccessControlAllowOrigin::AllowOrigin(ref url) =>
write!(f, "{}", url)
} }
} }
} }

View File

@@ -1,7 +1,9 @@
use std::fmt;
use header; use header;
use header::shared; use header::shared;
#[deriving(Clone)] #[derive(Clone)]
struct AccessControlMaxAge(pub u32); struct AccessControlMaxAge(pub u32);
impl header::Header for AccessControlMaxAge { impl header::Header for AccessControlMaxAge {
@@ -18,6 +20,6 @@ impl header::Header for AccessControlMaxAge {
impl header::HeaderFormat for AccessControlMaxAge { impl header::HeaderFormat for AccessControlMaxAge {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
let AccessControlMaxAge(ref num) = *self; let AccessControlMaxAge(ref num) = *self;
num.fmt(f) write!(f, "{}", num)
} }
} }

View File

@@ -1,5 +1,17 @@
/// Exposes the AccessControlAllowHeaders header
pub mod allow_headers; pub mod allow_headers;
/// Exposes the AccessControlAllowMethods header
pub mod allow_methods; pub mod allow_methods;
/// Exposes the AccessControlAllowOrigin header
pub mod allow_origin; pub mod allow_origin;
/// Exposes the AccessControlMaxAge header
pub mod max_age;
/// Exposes the AccessControlRequestHeaders header
pub mod request_headers; pub mod request_headers;
/// Exposes the AccessControlRequestMethod header
pub mod request_method; pub mod request_method;

View File

@@ -1,9 +1,9 @@
use std::fmt::{mod}; use std::fmt::{self};
use header; use header;
use header::shared; use header::shared;
#[deriving(Clone)] #[derive(Clone)]
struct AccessControlRequestHeaders(pub Vec<String>); struct AccessControlRequestHeaders(pub Vec<String>);
impl header::Header for AccessControlRequestHeaders { impl header::Header for AccessControlRequestHeaders {

View File

@@ -1,10 +1,10 @@
use std::fmt::{mod}; use std::fmt;
use header; use header;
use header::shared; use header::shared;
use method::Method; use method::Method;
#[deriving(Clone)] #[derive(Clone)]
struct AccessControlRequestMethod(pub Method); struct AccessControlRequestMethod(pub Method);
impl header::Header for AccessControlRequestMethod { impl header::Header for AccessControlRequestMethod {
@@ -21,6 +21,6 @@ impl header::Header for AccessControlRequestMethod {
impl header::HeaderFormat for AccessControlRequestMethod { impl header::HeaderFormat for AccessControlRequestMethod {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
let AccessControlRequestMethod(ref method) = *self; let AccessControlRequestMethod(ref method) = *self;
method.fmt(f) write!(f, "{}", method)
} }
} }

View File

@@ -76,7 +76,7 @@ macro_rules! deref(
} }
); );
// Exposes the AccessControl* family of headers. /// Exposes the AccessControl* family of headers.
pub mod access_control; pub mod access_control;
/// Exposes the Accept header. /// Exposes the Accept header.