diff options
author | bors-servo <metajack+bors@gmail.com> | 2015-02-27 09:48:58 -0700 |
---|---|---|
committer | bors-servo <metajack+bors@gmail.com> | 2015-02-27 09:48:58 -0700 |
commit | 60ce6c3b0fa7bd4199976a2b7e18f14df99bc511 (patch) | |
tree | 1b070809c81ffc93081ab45d406b5e7ed5859608 | |
parent | e4d607e5377fe0e91919d0c4c63195d3681b7298 (diff) | |
parent | fda3f7497a96f4b62f05bf23c657e352853b086c (diff) | |
download | servo-60ce6c3b0fa7bd4199976a2b7e18f14df99bc511.tar.gz servo-60ce6c3b0fa7bd4199976a2b7e18f14df99bc511.zip |
auto merge of #5091 : dhneio/servo/5090, r=jdm
We no longer need these header implementations as they are included in hyper. Fixes #5090.
-rw-r--r-- | components/script/cors.rs | 152 |
1 files changed, 4 insertions, 148 deletions
diff --git a/components/script/cors.rs b/components/script/cors.rs index e326e891765..6930848bb7c 100644 --- a/components/script/cors.rs +++ b/components/script/cors.rs @@ -10,13 +10,13 @@ //! with CORSRequest being expanded into FetchRequest (etc) use std::ascii::AsciiExt; -use std::fmt::{self, Display}; -use std::str::from_utf8; use time; use time::{now, Timespec}; -use hyper::header::{Headers, Header, HeaderFormat, HeaderView}; -use hyper::header::parsing as header_parsing; +use hyper::header::{AccessControlRequestMethod, AccessControlAllowMethods}; +use hyper::header::{AccessControlMaxAge, AccessControlAllowOrigin}; +use hyper::header::{AccessControlRequestHeaders, AccessControlAllowHeaders}; +use hyper::header::{Headers, HeaderView}; use hyper::client::Request; use hyper::mime::{Mime, TopLevel, SubLevel}; use hyper::header::{ContentType, Host}; @@ -382,150 +382,6 @@ fn is_simple_method(m: &Method) -> bool { } } -//XXX(seanmonstar): worth uplifting to Hyper? -#[derive(Clone)] -struct AccessControlRequestMethod(pub Method); - -impl 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 HeaderFormat for AccessControlRequestMethod { - fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { - let AccessControlRequestMethod(ref method) = *self; - <_ as Display>::fmt(method, f) - } -} - -#[derive(Clone)] -struct AccessControlRequestHeaders(pub Vec<String>); - -impl 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 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_slice()) - } -} - -#[derive(Clone)] -struct AccessControlAllowMethods(pub Vec<Method>); - -impl 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 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_slice()) - } -} - -#[derive(Clone)] -struct AccessControlAllowHeaders(pub Vec<String>); - -impl 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 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_slice()) - } -} - -#[derive(Clone)] -enum AccessControlAllowOrigin { - AllowStar, - AllowOrigin(Url), -} - - -impl Header for AccessControlAllowOrigin { - #[inline] - fn header_name() -> &'static str { - "Access-Control-Allow-Origin" - } - - fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowOrigin> { - if raw.len() == 1 { - from_utf8(raw[0].as_slice()).ok().and_then(|s| { - if s == "*" { - Some(AccessControlAllowOrigin::AllowStar) - } else { - Url::parse(s).ok().map(|url| AccessControlAllowOrigin::AllowOrigin(url)) - } - }) - } else { - None - } - } -} - -impl HeaderFormat for AccessControlAllowOrigin { - fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - AccessControlAllowOrigin::AllowStar => <_ as Display>::fmt("*", f), - AccessControlAllowOrigin::AllowOrigin(ref url) => <_ as Display>::fmt(url, f) - } - } -} - -#[derive(Clone)] -struct AccessControlMaxAge(pub u32); - -impl 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 HeaderFormat for AccessControlMaxAge { - fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { - let AccessControlMaxAge(ref num) = *self; - <_ as Display>::fmt(num, f) - } -} - - /// Perform a CORS check on a header list and CORS request /// http://fetch.spec.whatwg.org/#cors-check pub fn allow_cross_origin_request(req: &CORSRequest, headers: &Headers) -> bool { |