1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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<AccessControlAllowOrigin>) -> &'static str { "Access-Control-Allow-Origin" } fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowOrigin> { 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) } } }