hyper_sync/header/common/
access_control_allow_credentials.rs

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