rama_http_headers/common/
referer.rs

1use std::fmt;
2use std::str::FromStr;
3
4use crate::util::HeaderValueString;
5
6/// `Referer` header, defined in
7/// [RFC7231](https://datatracker.ietf.org/doc/html/rfc7231#section-5.5.2)
8///
9/// The `Referer` \[sic\] header field allows the user agent to specify a
10/// URI reference for the resource from which the target URI was obtained
11/// (i.e., the "referrer", though the field name is misspelled).  A user
12/// agent MUST NOT include the fragment and userinfo components of the
13/// URI reference, if any, when generating the Referer field value.
14///
15/// ## ABNF
16///
17/// ```text
18/// Referer = absolute-URI / partial-URI
19/// ```
20///
21/// ## Example values
22///
23/// * `http://www.example.org/hypertext/Overview.html`
24///
25/// # Examples
26///
27/// ```
28/// use rama_http_headers::Referer;
29///
30/// let r = Referer::from_static("/People.html#tim");
31/// ```
32#[derive(Debug, Clone, PartialEq)]
33pub struct Referer(HeaderValueString);
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(HeaderValueString::from_static(s))
48    }
49}
50
51rama_utils::macros::error::static_str_error! {
52    #[doc = "referer is not valid"]
53    pub struct InvalidReferer;
54}
55
56impl FromStr for Referer {
57    type Err = InvalidReferer;
58    fn from_str(src: &str) -> Result<Self, Self::Err> {
59        HeaderValueString::from_str(src)
60            .map(Referer)
61            .map_err(|_| InvalidReferer)
62    }
63}
64
65impl fmt::Display for Referer {
66    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67        fmt::Display::fmt(&self.0, f)
68    }
69}