Skip to content

Commit

Permalink
feat(headers): Add Referrer-Policy header
Browse files Browse the repository at this point in the history
  • Loading branch information
aravind-pg committed Jul 2, 2016
1 parent a10edc6 commit b76a02c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub use self::prefer::{Prefer, Preference};
pub use self::preference_applied::PreferenceApplied;
pub use self::range::{Range, ByteRangeSpec};
pub use self::referer::Referer;
pub use self::referrer_policy::ReferrerPolicy;
pub use self::server::Server;
pub use self::set_cookie::SetCookie;
pub use self::strict_transport_security::StrictTransportSecurity;
Expand Down Expand Up @@ -410,6 +411,7 @@ mod prefer;
mod preference_applied;
mod range;
mod referer;
mod referrer_policy;
mod server;
mod set_cookie;
mod strict_transport_security;
Expand Down
94 changes: 94 additions & 0 deletions src/header/common/referrer_policy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::fmt;
use std::ascii::AsciiExt;

use header::{Header, HeaderFormat, parsing};

/// `Referrer-Policy` header, part of
/// [Referrer Policy](https://www.w3.org/TR/referrer-policy/#referrer-policy-header)
///
/// The `Referrer-Policy` HTTP header specifies the referrer
/// policy that the user agent applies when determining what
/// referrer information should be included with requests made,
/// and with browsing contexts created from the context of the
/// protected resource.
///
/// # ABNF
/// ```plain
/// Referrer-Policy: 1#policy-token
/// policy-token = "no-referrer" / "no-referrer-when-downgrade"
/// / "same-origin" / "origin"
/// / "origin-when-cross-origin" / "unsafe-url"
/// ```
///
/// # Example values
/// * `no-referrer`
///
/// # Example
/// ```
/// use hyper::header::{Headers, ReferrerPolicy};
///
/// let mut headers = Headers::new();
/// headers.set(ReferrerPolicy::NoReferrer);
/// ```
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ReferrerPolicy {
/// `no-referrer`
NoReferrer,
/// `no-referrer-when-downgrade`
NoReferrerWhenDowngrade,
/// `same-origin`
SameOrigin,
/// `origin`
Origin,
/// `origin-when-cross-origin`
OriginWhenCrossOrigin,
/// `unsafe-url`
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()[..];
// See https://www.w3.org/TR/referrer-policy/#determine-policy-for-token
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());
}

0 comments on commit b76a02c

Please sign in to comment.