headers/common/referer.rs
1use std::str::FromStr;
2
3use http::header::HeaderValue;
4
5/// `Referer` header, defined in
6/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.5.2)
7///
8/// The `Referer` \[sic\] header field allows the user agent to specify a
9/// URI reference for the resource from which the target URI was obtained
10/// (i.e., the "referrer", though the field name is misspelled). A user
11/// agent MUST NOT include the fragment and userinfo components of the
12/// URI reference, if any, when generating the Referer field value.
13///
14/// ## ABNF
15///
16/// ```text
17/// Referer = absolute-URI / partial-URI
18/// ```
19///
20/// ## Example values
21///
22/// * `http://www.example.org/hypertext/Overview.html`
23///
24/// # Examples
25///
26/// ```
27/// # extern crate headers;
28/// use headers::Referer;
29///
30/// let r = Referer::from_static("/People.html#tim");
31/// ```
32#[derive(Debug, Clone, PartialEq)]
33pub struct Referer(HeaderValue);
34
35derive_header! {
36 Referer(_),
37 name: REFERER
38}
39
40impl Referer {
41 /// Create a `Referer` with a static string.
42 ///
43 /// # Panic
44 ///
45 /// Panics if the string is not a legal header value.
46 pub fn from_static(s: &'static str) -> Referer {
47 Referer(HeaderValue::from_static(s))
48 }
49}
50
51error_type!(InvalidReferer);
52
53impl FromStr for Referer {
54 type Err = InvalidReferer;
55 fn from_str(src: &str) -> Result<Self, Self::Err> {
56 HeaderValue::from_str(src)
57 .map(Referer)
58 .map_err(|_| InvalidReferer { _inner: () })
59 }
60}