|
| 1 | +use header::{Header, Host}; |
| 2 | +use std::fmt; |
| 3 | +use std::str::FromStr; |
| 4 | +use header::parsing::from_one_raw_str; |
| 5 | + |
| 6 | +/// The `Origin` header. |
| 7 | +/// |
| 8 | +/// The `Origin` header is a version of the `Referer` header that is used for all HTTP fetches and `POST`s whose CORS flag is set. |
| 9 | +/// This header is often used to inform recipients of the security context of where the request was initiated. |
| 10 | +/// |
| 11 | +/// |
| 12 | +/// Following the spec, https://fetch.spec.whatwg.org/#origin-header, the value of this header is composed of |
| 13 | +/// a String (scheme), header::Host (host/port) |
| 14 | +/// |
| 15 | +/// # Examples |
| 16 | +/// ``` |
| 17 | +/// use hyper::header::{Headers, Origin}; |
| 18 | +/// |
| 19 | +/// let mut headers = Headers::new(); |
| 20 | +/// headers.set( |
| 21 | +/// Origin::new("http", "hyper.rs", None) |
| 22 | +/// ); |
| 23 | +/// ``` |
| 24 | +/// ``` |
| 25 | +/// use hyper::header::{Headers, Origin}; |
| 26 | +/// |
| 27 | +/// let mut headers = Headers::new(); |
| 28 | +/// headers.set( |
| 29 | +/// Origin::new("https", "wikipedia.org", Some(443)) |
| 30 | +/// ); |
| 31 | +/// ``` |
| 32 | +
|
| 33 | +#[derive(Clone, Debug)] |
| 34 | +pub struct Origin { |
| 35 | + /// The scheme, such as http or https |
| 36 | + pub scheme: String, |
| 37 | + /// The host, such as Host{hostname: "hyper.rs".to_owned(), port: None} |
| 38 | + pub host: Host, |
| 39 | +} |
| 40 | + |
| 41 | +impl Origin { |
| 42 | + /// Creates a new `Origin` header. |
| 43 | + pub fn new<S: Into<String>, H: Into<String>>(scheme: S, hostname: H, port: Option<u16>) -> Origin{ |
| 44 | + Origin { |
| 45 | + scheme: scheme.into(), |
| 46 | + host: Host { |
| 47 | + hostname: hostname.into(), |
| 48 | + port: port |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl Header for Origin { |
| 55 | + fn header_name() -> &'static str { |
| 56 | + static NAME: &'static str = "Origin"; |
| 57 | + NAME |
| 58 | + } |
| 59 | + |
| 60 | + fn parse_header(raw: &[Vec<u8>]) -> ::Result<Origin> { |
| 61 | + from_one_raw_str(raw) |
| 62 | + } |
| 63 | + |
| 64 | + fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 65 | + write!(f, "{}://{}", self.scheme, self.host) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl FromStr for Origin { |
| 70 | + type Err = ::Error; |
| 71 | + |
| 72 | + fn from_str(s: &str) -> ::Result<Origin> { |
| 73 | + let idx = match s.find("://") { |
| 74 | + Some(idx) => idx, |
| 75 | + None => return Err(::Error::Header) |
| 76 | + }; |
| 77 | + // idx + 3 because thats how long "://" is |
| 78 | + let (scheme, etc) = (&s[..idx], &s[idx + 3..]); |
| 79 | + let host = try!(Host::from_str(etc)); |
| 80 | + |
| 81 | + |
| 82 | + Ok(Origin{ |
| 83 | + scheme: scheme.to_owned(), |
| 84 | + host: host |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl PartialEq for Origin { |
| 90 | + fn eq(&self, other: &Origin) -> bool { |
| 91 | + self.scheme == other.scheme && self.host == other.host |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod tests { |
| 98 | + use super::Origin; |
| 99 | + use header::Header; |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_origin() { |
| 103 | + let origin = Header::parse_header([b"http://foo.com".to_vec()].as_ref()); |
| 104 | + assert_eq!(origin.ok(), Some(Origin::new("http", "foo.com", None))); |
| 105 | + |
| 106 | + let origin = Header::parse_header([b"https://foo.com:443".to_vec()].as_ref()); |
| 107 | + assert_eq!(origin.ok(), Some(Origin::new("https", "foo.com", Some(443)))); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +bench_header!(bench, Origin, { vec![b"https://foo.com".to_vec()] }); |
0 commit comments