Skip to content

Commit 9a85ea5

Browse files
committed
Merge pull request #595 from pyfisch/originstring
refactor(headers): use String in Access-Control-Allow-Origin header
2 parents 3a5c56c + ed45862 commit 9a85ea5

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Diff for: src/error.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::error::Error as StdError;
33
use std::fmt;
44
use std::io::Error as IoError;
55
use std::str::Utf8Error;
6+
use std::string::FromUtf8Error;
67

78
use httparse;
89
use url;
@@ -127,6 +128,12 @@ impl From<Utf8Error> for Error {
127128
}
128129
}
129130

131+
impl From<FromUtf8Error> for Error {
132+
fn from(err: FromUtf8Error) -> Error {
133+
Utf8(err.utf8_error())
134+
}
135+
}
136+
130137
impl From<httparse::Error> for Error {
131138
fn from(err: httparse::Error) -> Error {
132139
match err {

Diff for: src/header/common/access_control_allow_origin.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::fmt::{self, Display};
2-
use std::str;
32

4-
use url::Url;
53
use header::{Header, HeaderFormat};
64

75
/// The `Access-Control-Allow-Origin` response header,
@@ -40,11 +38,10 @@ use header::{Header, HeaderFormat};
4038
/// ```
4139
/// ```
4240
/// use hyper::header::{Headers, AccessControlAllowOrigin};
43-
/// use hyper::Url;
4441
///
4542
/// let mut headers = Headers::new();
4643
/// headers.set(
47-
/// AccessControlAllowOrigin::Value(Url::parse("http://hyper.rs").unwrap())
44+
/// AccessControlAllowOrigin::Value("http://hyper.rs".to_owned())
4845
/// );
4946
/// ```
5047
#[derive(Clone, PartialEq, Debug)]
@@ -54,7 +51,7 @@ pub enum AccessControlAllowOrigin {
5451
/// A hidden origin
5552
Null,
5653
/// Allow one particular origin
57-
Value(Url),
54+
Value(String),
5855
}
5956

6057
impl Header for AccessControlAllowOrigin {
@@ -63,13 +60,15 @@ impl Header for AccessControlAllowOrigin {
6360
}
6461

6562
fn parse_header(raw: &[Vec<u8>]) -> ::Result<AccessControlAllowOrigin> {
66-
if raw.len() == 1 {
67-
match unsafe { &raw.get_unchecked(0)[..] } {
68-
b"*" => Ok(AccessControlAllowOrigin::Any),
69-
b"null" => Ok(AccessControlAllowOrigin::Null),
70-
r => Ok(AccessControlAllowOrigin::Value(try!(Url::parse(try!(str::from_utf8(r))))))
71-
}
72-
} else { Err(::Error::Header) }
63+
if raw.len() != 1 {
64+
return Err(::Error::Header)
65+
}
66+
let value = unsafe { raw.get_unchecked(0) };
67+
Ok(match &value[..] {
68+
b"*" => AccessControlAllowOrigin::Any,
69+
b"null" => AccessControlAllowOrigin::Null,
70+
_ => AccessControlAllowOrigin::Value(try!(String::from_utf8(value.clone())))
71+
})
7372
}
7473
}
7574

0 commit comments

Comments
 (0)