1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use rama_http_types::{HeaderName, HeaderValue};
use crate::{Error, HeaderDecode, HeaderEncode, TypedHeader};
/// `Access-Control-Request-Private-Network` header, as documented in
/// [this draft of WICG](https://wicg.github.io/private-network-access/).
///
/// Not an official standard but widely used.
/// This CORS header to allow a public origin make a cross site request
/// to a server hosted on a private network (e.g. behind a firewall).
///
/// # ABNF
///
/// ```text
/// Access-Control-Request-Private-Network: "Access-Control-Request-Private-Network" ":" "true"
/// ```
///
/// Since there is only one acceptable field value, the header struct does not accept
/// any values at all. Setting an empty `AccessControlRequestPrivateNetwork` header is
/// sufficient. See the examples below.
///
/// # Example values
/// * "true"
///
/// # Examples
///
/// ```
/// use rama_http_headers::AccessControlRequestPrivateNetwork;
///
/// let allow_creds = AccessControlRequestPrivateNetwork::default();
/// ```
#[derive(Default, Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct AccessControlRequestPrivateNetwork;
impl AccessControlRequestPrivateNetwork {
#[must_use]
pub fn new() -> Self {
Self
}
}
impl TypedHeader for AccessControlRequestPrivateNetwork {
fn name() -> &'static HeaderName {
&::rama_http_types::header::ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK
}
}
impl HeaderDecode for AccessControlRequestPrivateNetwork {
fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, Error> {
values
.next()
.and_then(|value| if value == "true" { Some(Self) } else { None })
.ok_or_else(Error::invalid)
}
}
impl HeaderEncode for AccessControlRequestPrivateNetwork {
fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
values.extend(::std::iter::once(HeaderValue::from_static("true")));
}
}
#[cfg(test)]
mod tests {
use super::super::test_decode;
use super::*;
#[test]
fn allow_private_network_is_case_sensitive() {
let allow_header = test_decode::<AccessControlRequestPrivateNetwork>(&["true"]);
assert!(allow_header.is_some());
let allow_header = test_decode::<AccessControlRequestPrivateNetwork>(&["True"]);
assert!(allow_header.is_none());
}
}