http_types/content/
content_location.rs1use crate::headers::{HeaderName, HeaderValue, Headers, CONTENT_LOCATION};
2use crate::{bail_status as bail, Status, Url};
3
4use std::convert::TryInto;
5
6#[derive(Debug)]
34pub struct ContentLocation {
35 url: Url,
36}
37
38impl ContentLocation {
39 pub fn new(url: Url) -> Self {
41 Self { url }
42 }
43
44 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 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 pub fn apply(&self, mut headers: impl AsMut<Headers>) {
69 headers.as_mut().insert(self.name(), self.value());
70 }
71
72 pub fn name(&self) -> HeaderName {
74 CONTENT_LOCATION
75 }
76
77 pub fn value(&self) -> HeaderValue {
79 let output = self.url.to_string();
80
81 unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
83 }
84
85 pub fn location(&self) -> &Url {
87 &self.url
88 }
89
90 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}