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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::fmt;
use std::ascii::AsciiExt;
use header::{Header, HeaderFormat, parsing};
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ReferrerPolicy {
NoReferrer,
NoReferrerWhenDowngrade,
SameOrigin,
Origin,
OriginWhenCrossOrigin,
UnsafeUrl,
}
impl Header for ReferrerPolicy {
fn header_name() -> &'static str {
static NAME: &'static str = "Referrer-Policy";
NAME
}
fn parse_header(raw: &[Vec<u8>]) -> ::Result<ReferrerPolicy> {
use self::ReferrerPolicy::*;
parsing::from_one_raw_str(raw).and_then(|s: String| {
let slice = &s.to_ascii_lowercase()[..];
match slice {
"no-referrer" | "never" => Ok(NoReferrer),
"no-referrer-when-downgrade" | "default" => Ok(NoReferrerWhenDowngrade),
"same-origin" => Ok(SameOrigin),
"origin" => Ok(Origin),
"origin-when-cross-origin" => Ok(OriginWhenCrossOrigin),
"unsafe-url" | "always" => Ok(UnsafeUrl),
_ => Err(::Error::Header),
}
})
}
}
impl HeaderFormat for ReferrerPolicy {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::ReferrerPolicy::*;
f.write_str(match *self {
NoReferrer => "no-referrer",
NoReferrerWhenDowngrade => "no-referrer-when-downgrade",
SameOrigin => "same-origin",
Origin => "origin",
OriginWhenCrossOrigin => "origin-when-cross-origin",
UnsafeUrl => "unsafe-url",
})
}
}
#[test]
fn test_parse_header() {
let a: ReferrerPolicy = Header::parse_header([b"origin".to_vec()].as_ref()).unwrap();
let b = ReferrerPolicy::Origin;
assert_eq!(a, b);
let e: ::Result<ReferrerPolicy> = Header::parse_header([b"foobar".to_vec()].as_ref());
assert!(e.is_err());
}