refactor(headers): use String in Access-Control-Allow-Origin header
Access-Control-Allow-Origin origins are URLs but they do not need to be valid, they should just be compared as strings. So to support invalid URLs hyper should use a string instead. closes #526 BREAKING CHANGE: Access-Control-Allow-Origin does no longer use Url
This commit is contained in:
		| @@ -3,6 +3,7 @@ use std::error::Error as StdError; | |||||||
| use std::fmt; | use std::fmt; | ||||||
| use std::io::Error as IoError; | use std::io::Error as IoError; | ||||||
| use std::str::Utf8Error; | use std::str::Utf8Error; | ||||||
|  | use std::string::FromUtf8Error; | ||||||
|  |  | ||||||
| use httparse; | use httparse; | ||||||
| use url; | use url; | ||||||
| @@ -127,6 +128,12 @@ impl From<Utf8Error> for Error { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | impl From<FromUtf8Error> for Error { | ||||||
|  |     fn from(err: FromUtf8Error) -> Error { | ||||||
|  |         Utf8(err.utf8_error()) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| impl From<httparse::Error> for Error { | impl From<httparse::Error> for Error { | ||||||
|     fn from(err: httparse::Error) -> Error { |     fn from(err: httparse::Error) -> Error { | ||||||
|         match err { |         match err { | ||||||
|   | |||||||
| @@ -1,7 +1,5 @@ | |||||||
| use std::fmt::{self, Display}; | use std::fmt::{self, Display}; | ||||||
| use std::str; |  | ||||||
|  |  | ||||||
| use url::Url; |  | ||||||
| use header::{Header, HeaderFormat}; | use header::{Header, HeaderFormat}; | ||||||
|  |  | ||||||
| /// The `Access-Control-Allow-Origin` response header, | /// The `Access-Control-Allow-Origin` response header, | ||||||
| @@ -20,7 +18,7 @@ use header::{Header, HeaderFormat}; | |||||||
| /// * `null` | /// * `null` | ||||||
| /// * `*` | /// * `*` | ||||||
| /// * `http://google.com/` | /// * `http://google.com/` | ||||||
| ///  | /// | ||||||
| /// # Examples | /// # Examples | ||||||
| /// ``` | /// ``` | ||||||
| /// use hyper::header::{Headers, AccessControlAllowOrigin}; | /// use hyper::header::{Headers, AccessControlAllowOrigin}; | ||||||
| @@ -40,11 +38,10 @@ use header::{Header, HeaderFormat}; | |||||||
| /// ``` | /// ``` | ||||||
| /// ``` | /// ``` | ||||||
| /// use hyper::header::{Headers, AccessControlAllowOrigin}; | /// use hyper::header::{Headers, AccessControlAllowOrigin}; | ||||||
| /// use hyper::Url; |  | ||||||
| /// | /// | ||||||
| /// let mut headers = Headers::new(); | /// let mut headers = Headers::new(); | ||||||
| /// headers.set( | /// headers.set( | ||||||
| ///     AccessControlAllowOrigin::Value(Url::parse("http://hyper.rs").unwrap()) | ///     AccessControlAllowOrigin::Value("http://hyper.rs".to_owned()) | ||||||
| /// ); | /// ); | ||||||
| /// ``` | /// ``` | ||||||
| #[derive(Clone, PartialEq, Debug)] | #[derive(Clone, PartialEq, Debug)] | ||||||
| @@ -54,7 +51,7 @@ pub enum AccessControlAllowOrigin { | |||||||
|     /// A hidden origin |     /// A hidden origin | ||||||
|     Null, |     Null, | ||||||
|     /// Allow one particular origin |     /// Allow one particular origin | ||||||
|     Value(Url), |     Value(String), | ||||||
| } | } | ||||||
|  |  | ||||||
| impl Header for AccessControlAllowOrigin { | impl Header for AccessControlAllowOrigin { | ||||||
| @@ -63,13 +60,15 @@ impl Header for AccessControlAllowOrigin { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn parse_header(raw: &[Vec<u8>]) -> ::Result<AccessControlAllowOrigin> { |     fn parse_header(raw: &[Vec<u8>]) -> ::Result<AccessControlAllowOrigin> { | ||||||
|         if raw.len() == 1 { |         if raw.len() != 1 { | ||||||
|             match unsafe { &raw.get_unchecked(0)[..] } { |             return Err(::Error::Header) | ||||||
|                 b"*" => Ok(AccessControlAllowOrigin::Any), |         } | ||||||
|                 b"null" => Ok(AccessControlAllowOrigin::Null), |         let value = unsafe { raw.get_unchecked(0) }; | ||||||
|                 r => Ok(AccessControlAllowOrigin::Value(try!(Url::parse(try!(str::from_utf8(r)))))) |         Ok(match &value[..] { | ||||||
|             } |             b"*" => AccessControlAllowOrigin::Any, | ||||||
|         } else { Err(::Error::Header) } |             b"null" => AccessControlAllowOrigin::Null, | ||||||
|  |             _ => AccessControlAllowOrigin::Value(try!(String::from_utf8(value.clone()))) | ||||||
|  |         }) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user