hyperx/header/common/
access_control_allow_origin.rs

1use std::fmt::{self, Display};
2use std::str;
3
4use header::{Header, RawLike};
5
6/// The `Access-Control-Allow-Origin` response header,
7/// part of [CORS](http://www.w3.org/TR/cors/#access-control-allow-origin-response-header)
8///
9/// The `Access-Control-Allow-Origin` header indicates whether a resource
10/// can be shared based by returning the value of the Origin request header,
11/// `*`, or `null` in the response.
12///
13/// # ABNF
14///
15/// ```text
16/// Access-Control-Allow-Origin = "Access-Control-Allow-Origin" ":" origin-list-or-null | "*"
17/// ```
18///
19/// # Example values
20/// * `null`
21/// * `*`
22/// * `http://google.com/`
23///
24/// # Examples
25/// ```
26/// # extern crate http;
27/// use hyperx::header::{AccessControlAllowOrigin, TypedHeaders};
28///
29/// let mut headers = http::HeaderMap::new();
30/// headers.encode(
31///     &AccessControlAllowOrigin::Any
32/// );
33/// ```
34/// ```
35/// # extern crate http;
36/// use hyperx::header::{AccessControlAllowOrigin, TypedHeaders};
37///
38/// let mut headers = http::HeaderMap::new();
39/// headers.encode(
40///     &AccessControlAllowOrigin::Null,
41/// );
42/// ```
43/// ```
44/// # extern crate http;
45/// use hyperx::header::{AccessControlAllowOrigin, TypedHeaders};
46///
47/// let mut headers = http::HeaderMap::new();
48/// headers.encode(
49///     &AccessControlAllowOrigin::Value("http://hyper.rs".to_owned())
50/// );
51/// ```
52#[derive(Clone, PartialEq, Debug)]
53pub enum AccessControlAllowOrigin {
54    /// Allow all origins
55    Any,
56    /// A hidden origin
57    Null,
58    /// Allow one particular origin
59    Value(String),
60}
61
62impl Header for AccessControlAllowOrigin {
63    fn header_name() -> &'static str {
64        "Access-Control-Allow-Origin"
65    }
66
67    fn parse_header<'a, T>(raw: &'a T) -> ::Result<AccessControlAllowOrigin>
68    where T: RawLike<'a>
69    {
70        if let Some(line) = raw.one() {
71            Ok(match line {
72                b"*" => AccessControlAllowOrigin::Any,
73                b"null" => AccessControlAllowOrigin::Null,
74                _ => AccessControlAllowOrigin::Value(str::from_utf8(line)?.into())
75            })
76        } else {
77            Err(::Error::Header)
78        }
79    }
80
81    fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
82        f.fmt_line(self)
83    }
84}
85
86impl Display for AccessControlAllowOrigin {
87    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
88        match *self {
89            AccessControlAllowOrigin::Any => f.write_str("*"),
90            AccessControlAllowOrigin::Null => f.write_str("null"),
91            AccessControlAllowOrigin::Value(ref url) => Display::fmt(url, f),
92        }
93    }
94}
95
96#[cfg(test)]
97mod test_access_control_allow_origin {
98    use header::*;
99    use super::AccessControlAllowOrigin as HeaderField;
100    test_header!(test1, vec![b"null"]);
101    test_header!(test2, vec![b"*"]);
102    test_header!(test3, vec![b"http://google.com/"]);
103}
104
105standard_header!(AccessControlAllowOrigin, ACCESS_CONTROL_ALLOW_ORIGIN);