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 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 const fn from_static(s: &'static str) -> Referer {
47 Referer(HeaderValueString::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 HeaderValueString::from_str(src)
57 .map(Referer)
58 .map_err(|_| InvalidReferer { _inner: () })
59 }
60}
61
62impl fmt::Display for Referer {
63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64 fmt::Display::fmt(&self.0, f)
65 }
66}