Skip to content

Commit

Permalink
Merge pull request #235 from kstep/referer-header
Browse files Browse the repository at this point in the history
Add `Referer` header definition
  • Loading branch information
seanmonstar committed Jan 19, 2015
2 parents 2790f60 + df857aa commit 7a5813b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub use self::user_agent::UserAgent;
pub use self::vary::Vary;
pub use self::server::Server;
pub use self::set_cookie::SetCookie;
pub use self::referer::Referer;

macro_rules! bench_header(
($name:ident, $ty:ty, $value:expr) => {
Expand Down Expand Up @@ -202,3 +203,7 @@ pub mod user_agent;

/// Exposes the Vary header.
pub mod vary;

/// Exposes the Referer header
pub mod referer;

35 changes: 35 additions & 0 deletions src/header/common/referer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use header::{Header, HeaderFormat};
use std::fmt::{self, Show};
use header::shared::util::from_one_raw_str;

/// The `Referer` header.
///
/// The Referer header is used by user agents to inform server about
/// the page URL user has came from.
///
/// See alse [RFC 1945, section 10.13](http://tools.ietf.org/html/rfc1945#section-10.13).
///
/// Currently just a string, but maybe better replace it with url::Url or something like it.
#[derive(Clone, PartialEq, Show)]
pub struct Referer(pub String);

deref!(Referer => String);

impl Header for Referer {
fn header_name(_: Option<Referer>) -> &'static str {
"Referer"
}

fn parse_header(raw: &[Vec<u8>]) -> Option<Referer> {
from_one_raw_str(raw).map(|s| Referer(s))
}
}

impl HeaderFormat for Referer {
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let Referer(ref value) = *self;
value.fmt(fmt)
}
}

bench_header!(bench, Referer, { vec![b"http://foo.com/hello:3000".to_vec()] });

0 comments on commit 7a5813b

Please sign in to comment.