http_types/content/
content_location.rs

1use crate::headers::{HeaderName, HeaderValue, Headers, CONTENT_LOCATION};
2use crate::{bail_status as bail, Status, Url};
3
4use std::convert::TryInto;
5
6/// Indicates an alternate location for the returned data.
7///
8/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Location)
9///
10/// # Specifications
11///
12/// - [RFC 7231, section 3.1.4.2: Content-Location](https://tools.ietf.org/html/rfc7231#section-3.1.4.2)
13///
14/// # Examples
15///
16/// ```
17/// # fn main() -> http_types::Result<()> {
18/// #
19/// use http_types::{Response, Url};
20/// use http_types::content::ContentLocation;
21///
22/// let content_location = ContentLocation::new(Url::parse("https://example.net/")?);
23///
24/// let mut res = Response::new(200);
25/// content_location.apply(&mut res);
26///
27/// let url = Url::parse("https://example.net/")?;
28/// let content_location = ContentLocation::from_headers(url, res)?.unwrap();
29/// assert_eq!(content_location.location(), &Url::parse("https://example.net/")?);
30/// #
31/// # Ok(()) }
32/// ```
33#[derive(Debug)]
34pub struct ContentLocation {
35    url: Url,
36}
37
38impl ContentLocation {
39    /// Create a new instance of `Content-Location` header.
40    pub fn new(url: Url) -> Self {
41        Self { url }
42    }
43
44    /// Create a new instance from headers.
45    pub fn from_headers<U>(base_url: U, headers: impl AsRef<Headers>) -> crate::Result<Option<Self>>
46    where
47        U: TryInto<Url>,
48        U::Error: std::fmt::Debug,
49    {
50        let headers = match headers.as_ref().get(CONTENT_LOCATION) {
51            Some(headers) => headers,
52            None => return Ok(None),
53        };
54
55        // If we successfully parsed the header then there's always at least one
56        // entry. We want the last entry.
57        let value = headers.iter().last().unwrap();
58        let base = match base_url.try_into() {
59            Ok(b) => b,
60            Err(_) => bail!(400, "Invalid base url provided"),
61        };
62
63        let url = base.join(value.as_str().trim()).status(400)?;
64        Ok(Some(Self { url }))
65    }
66
67    /// Sets the header.
68    pub fn apply(&self, mut headers: impl AsMut<Headers>) {
69        headers.as_mut().insert(self.name(), self.value());
70    }
71
72    /// Get the `HeaderName`.
73    pub fn name(&self) -> HeaderName {
74        CONTENT_LOCATION
75    }
76
77    /// Get the `HeaderValue`.
78    pub fn value(&self) -> HeaderValue {
79        let output = self.url.to_string();
80
81        // SAFETY: the internal string is validated to be ASCII.
82        unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
83    }
84
85    /// Get the url.
86    pub fn location(&self) -> &Url {
87        &self.url
88    }
89
90    /// Set the url.
91    pub fn set_location<U>(&mut self, location: U)
92    where
93        U: TryInto<Url>,
94        U::Error: std::fmt::Debug,
95    {
96        self.url = location
97            .try_into()
98            .expect("Could not convert into valid URL")
99    }
100}
101
102#[cfg(test)]
103mod test {
104    use super::*;
105    use crate::headers::Headers;
106
107    #[test]
108    fn smoke() -> crate::Result<()> {
109        let content_location = ContentLocation::new(Url::parse("https://example.net/test.json")?);
110
111        let mut headers = Headers::new();
112        content_location.apply(&mut headers);
113
114        let content_location =
115            ContentLocation::from_headers(Url::parse("https://example.net/").unwrap(), headers)?
116                .unwrap();
117        assert_eq!(
118            content_location.location(),
119            &Url::parse("https://example.net/test.json")?
120        );
121        Ok(())
122    }
123
124    #[test]
125    fn bad_request_on_parse_error() {
126        let mut headers = Headers::new();
127        headers.insert(CONTENT_LOCATION, "htt://<nori ate the tag. yum.>");
128        let err =
129            ContentLocation::from_headers(Url::parse("https://example.net").unwrap(), headers)
130                .unwrap_err();
131        assert_eq!(err.status(), 400);
132    }
133}