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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use header::{Header, Host, HeaderFormat};
use std::fmt;
use std::str::FromStr;
use header::parsing::from_one_raw_str;
#[derive(Clone, Debug)]
pub struct Origin {
pub scheme: String,
pub host: Host,
}
impl Origin {
pub fn new<S: Into<String>, H: Into<String>>(scheme: S, hostname: H, port: Option<u16>) -> Origin{
Origin {
scheme: scheme.into(),
host: Host {
hostname: hostname.into(),
port: port
}
}
}
}
impl Header for Origin {
fn header_name() -> &'static str {
static NAME: &'static str = "Origin";
NAME
}
fn parse_header(raw: &[Vec<u8>]) -> ::Result<Origin> {
from_one_raw_str(raw)
}
}
impl FromStr for Origin {
type Err = ::Error;
fn from_str(s: &str) -> ::Result<Origin> {
let idx = match s.find("://") {
Some(idx) => idx,
None => return Err(::Error::Header)
};
let (scheme, etc) = (&s[..idx], &s[idx + 3..]);
let host = try!(Host::from_str(etc));
Ok(Origin{
scheme: scheme.to_owned(),
host: host
})
}
}
impl HeaderFormat for Origin {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}://{}", self.scheme, self.host)
}
}
impl PartialEq for Origin {
fn eq(&self, other: &Origin) -> bool {
self.scheme == other.scheme && self.host == other.host
}
}
#[cfg(test)]
mod tests {
use super::Origin;
use header::Header;
#[test]
fn test_origin() {
let origin = Header::parse_header([b"http://foo.com".to_vec()].as_ref());
assert_eq!(origin.ok(), Some(Origin::new("http", "foo.com", None)));
let origin = Header::parse_header([b"https://foo.com:443".to_vec()].as_ref());
assert_eq!(origin.ok(), Some(Origin::new("https", "foo.com", Some(443))));
}
}
bench_header!(bench, Origin, { vec![b"https://foo.com".to_vec()] });