Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Referrer-Policy Header #920

Merged
merged 1 commit into from
Oct 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/header/common/referrer_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,23 @@ impl Header for ReferrerPolicy {

fn parse_header(raw: &Raw) -> ::Result<ReferrerPolicy> {
use self::ReferrerPolicy::*;
parsing::from_one_raw_str(raw).and_then(|s: String| {
let slice = &s.to_ascii_lowercase()[..];
// See https://www.w3.org/TR/referrer-policy/#determine-policy-for-token
// See https://www.w3.org/TR/referrer-policy/#determine-policy-for-token
let headers: Vec<String> = try!(parsing::from_comma_delimited(raw));

for h in headers.iter().rev() {
let slice = &h.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),
"no-referrer" | "never" => return Ok(NoReferrer),
"no-referrer-when-downgrade" | "default" => return Ok(NoReferrerWhenDowngrade),
"same-origin" => return Ok(SameOrigin),
"origin" => return Ok(Origin),
"origin-when-cross-origin" => return Ok(OriginWhenCrossOrigin),
"unsafe-url" | "always" => return Ok(UnsafeUrl),
_ => continue,
}
})
}

Err(::Error::Header)
}

fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -90,3 +94,10 @@ fn test_parse_header() {
let e: ::Result<ReferrerPolicy> = Header::parse_header(&"foobar".into());
assert!(e.is_err());
}

#[test]
fn test_rightmost_header() {
let a: ReferrerPolicy = Header::parse_header(&"same-origin, origin, foobar".into()).unwrap();
let b = ReferrerPolicy::Origin;
assert_eq!(a, b);
}