hyperx/header/common/
access_control_allow_credentials.rs

1use std::fmt::{self, Display};
2use std::str;
3use unicase;
4use header::{Header, RawLike};
5
6/// `Access-Control-Allow-Credentials` header, part of
7/// [CORS](http://www.w3.org/TR/cors/#access-control-allow-headers-response-header)
8///
9/// > The Access-Control-Allow-Credentials HTTP response header indicates whether the
10/// > response to request can be exposed when the credentials flag is true. When part
11/// > of the response to an preflight request it indicates that the actual request can
12/// > be made with credentials. The Access-Control-Allow-Credentials HTTP header must
13/// > match the following ABNF:
14///
15/// # ABNF
16///
17/// ```text
18/// Access-Control-Allow-Credentials: "Access-Control-Allow-Credentials" ":" "true"
19/// ```
20///
21/// Since there is only one acceptable field value, the header struct does not accept
22/// any values at all. Setting an empty `AccessControlAllowCredentials` header is
23/// sufficient. See the examples below.
24///
25/// # Example values
26/// * "true"
27///
28/// # Examples
29///
30/// ```
31/// # extern crate http;
32/// # extern crate hyperx;
33/// # fn main() {
34///
35/// use hyperx::header::{AccessControlAllowCredentials, TypedHeaders};
36///
37/// let mut headers = http::HeaderMap::new();
38/// headers.encode(&AccessControlAllowCredentials);
39/// # }
40/// ```
41#[derive(Clone, PartialEq, Debug)]
42pub struct AccessControlAllowCredentials;
43
44const ACCESS_CONTROL_ALLOW_CREDENTIALS_TRUE: &'static str = "true";
45
46impl Header for AccessControlAllowCredentials {
47    fn header_name() -> &'static str {
48        static NAME: &'static str = "Access-Control-Allow-Credentials";
49        NAME
50    }
51
52    fn parse_header<'a, T>(raw: &'a T) -> ::Result<AccessControlAllowCredentials>
53    where T: RawLike<'a>
54    {
55        if let Some(line) = raw.one() {
56            let text = unsafe {
57                // safe because:
58                // 1. we don't actually care if it's utf8, we just want to
59                //    compare the bytes with the "case" normalized. If it's not
60                //    utf8, then the byte comparison will fail, and we'll return
61                //    None. No big deal.
62                str::from_utf8_unchecked(line)
63            };
64            if unicase::eq_ascii(text, ACCESS_CONTROL_ALLOW_CREDENTIALS_TRUE) {
65                return Ok(AccessControlAllowCredentials);
66            }
67        }
68        Err(::Error::Header)
69    }
70
71    fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
72        f.fmt_line(self)
73    }
74}
75
76impl Display for AccessControlAllowCredentials {
77    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
78        f.write_str("true")
79    }
80}
81
82#[cfg(test)]
83mod test_access_control_allow_credentials {
84    use std::str;
85    use header::*;
86    use super::AccessControlAllowCredentials as HeaderField;
87    test_header!(works,        vec![b"true"], Some(HeaderField));
88    test_header!(ignores_case, vec![b"True"]);
89    test_header!(not_bool,     vec![b"false"], None);
90    test_header!(only_single,  vec![b"true", b"true"], None);
91    test_header!(no_gibberish, vec!["\u{645}\u{631}\u{62d}\u{628}\u{627}".as_bytes()], None);
92}
93
94standard_header!(AccessControlAllowCredentials, ACCESS_CONTROL_ALLOW_CREDENTIALS);