Skip to main content

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    #[must_use]
47    pub const fn from_static(s: &'static str) -> Self {
48        Self(HeaderValueString::from_static(s))
49    }
50}
51
52rama_utils::macros::error::static_str_error! {
53    #[doc = "referer is not valid"]
54    pub struct InvalidReferer;
55}
56
57impl FromStr for Referer {
58    type Err = InvalidReferer;
59    fn from_str(src: &str) -> Result<Self, Self::Err> {
60        HeaderValueString::from_str(src)
61            .map(Referer)
62            .map_err(|_e| InvalidReferer)
63    }
64}
65
66impl fmt::Display for Referer {
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        fmt::Display::fmt(&self.0, f)
69    }
70}