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::shared;
#[deriving(Clone)]
#[derive(Clone)]
struct AccessControlAllowHeaders(pub Vec<String>);
impl header::Header for AccessControlAllowHeaders {

View File

@@ -1,10 +1,11 @@
use std::fmt::{mod};
use std::fmt::{self};
use header;
use header::shared;
use method;
#[deriving(Clone)]
struct AccessControlAllowMethods(pub Vec<Method>);
#[derive(Clone)]
struct AccessControlAllowMethods(pub Vec<method::Method>);
impl header::Header for AccessControlAllowMethods {
#[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 {
AllowStar,
AllowOrigin(Url),
AllowOrigin(url::Url),
}
impl header::Header for AccessControlAllowOrigin {
@@ -14,15 +19,19 @@ impl header::Header for AccessControlAllowOrigin {
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowOrigin> {
if raw.len() == 1 {
from_utf8(raw[0].as_slice()).and_then(|s| {
if s == "*" {
Some(AllowStar)
} else {
Url::parse(s).ok().map(|url| AllowOrigin(url))
}
})
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 {
None
return None;
}
}
}
@@ -30,8 +39,9 @@ impl header::Header for AccessControlAllowOrigin {
impl header::HeaderFormat for AccessControlAllowOrigin {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
AllowStar => "*".fmt(f),
AllowOrigin(ref url) => url.fmt(f)
AccessControlAllowOrigin::AllowStar => write!(f, "*"),
AccessControlAllowOrigin::AllowOrigin(ref url) =>
write!(f, "{}", url)
}
}
}

View File

@@ -1,7 +1,9 @@
use std::fmt;
use header;
use header::shared;
#[deriving(Clone)]
#[derive(Clone)]
struct AccessControlMaxAge(pub u32);
impl header::Header for AccessControlMaxAge {
@@ -18,6 +20,6 @@ impl header::Header for AccessControlMaxAge {
impl header::HeaderFormat for AccessControlMaxAge {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
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;
/// 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;

View File

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

View File

@@ -1,10 +1,10 @@
use std::fmt::{mod};
use std::fmt;
use header;
use header::shared;
use method::Method;
#[deriving(Clone)]
#[derive(Clone)]
struct AccessControlRequestMethod(pub Method);
impl header::Header for AccessControlRequestMethod {
@@ -21,6 +21,6 @@ impl header::Header for AccessControlRequestMethod {
impl header::HeaderFormat for AccessControlRequestMethod {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
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;
/// Exposes the Accept header.