headers_ext/common/access_control_allow_credentials.rs
1use ::{Header, HeaderName, HeaderValue};
2
3/// `Access-Control-Allow-Credentials` header, part of
4/// [CORS](http://www.w3.org/TR/cors/#access-control-allow-headers-response-header)
5///
6/// > The Access-Control-Allow-Credentials HTTP response header indicates whether the
7/// > response to request can be exposed when the credentials flag is true. When part
8/// > of the response to an preflight request it indicates that the actual request can
9/// > be made with credentials. The Access-Control-Allow-Credentials HTTP header must
10/// > match the following ABNF:
11///
12/// # ABNF
13///
14/// ```text
15/// Access-Control-Allow-Credentials: "Access-Control-Allow-Credentials" ":" "true"
16/// ```
17///
18/// Since there is only one acceptable field value, the header struct does not accept
19/// any values at all. Setting an empty `AccessControlAllowCredentials` header is
20/// sufficient. See the examples below.
21///
22/// # Example values
23/// * "true"
24///
25/// # Examples
26///
27/// ```
28/// # extern crate headers_ext as headers;
29/// use headers::AccessControlAllowCredentials;
30///
31/// let allow_creds = AccessControlAllowCredentials;
32/// ```
33#[derive(Clone, PartialEq, Eq, Debug)]
34pub struct AccessControlAllowCredentials;
35
36impl Header for AccessControlAllowCredentials {
37 const NAME: &'static HeaderName = &::http::header::ACCESS_CONTROL_ALLOW_CREDENTIALS;
38
39 fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, ::Error> {
40 values
41 .next()
42 .and_then(|value| {
43 if value.as_bytes().eq_ignore_ascii_case(b"true") {
44 Some(AccessControlAllowCredentials)
45 } else {
46 None
47 }
48 })
49 .ok_or_else(::Error::invalid)
50 }
51
52 fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) {
53 values.extend(::std::iter::once(HeaderValue::from_static("true")));
54 }
55}
56