]> Untitled Git - lemmy.git/blob - crates/utils/src/request.rs
Use correct encoding when fetching non-UTF-8 site metadata (#2015)
[lemmy.git] / crates / utils / src / request.rs
1 use crate::{settings::structs::Settings, version::VERSION, LemmyError};
2 use anyhow::anyhow;
3 use encoding::{all::encodings, DecoderTrap};
4 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
5 use reqwest_middleware::ClientWithMiddleware;
6 use serde::{Deserialize, Serialize};
7 use std::future::Future;
8 use thiserror::Error;
9 use tracing::{error, info};
10 use url::Url;
11 use webpage::HTML;
12
13 #[derive(Clone, Debug, Error)]
14 #[error("Error sending request, {0}")]
15 struct SendError(pub String);
16
17 #[derive(Clone, Debug, Error)]
18 #[error("Error receiving response, {0}")]
19 pub struct RecvError(pub String);
20
21 pub async fn retry<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error>
22 where
23   F: Fn() -> Fut,
24   Fut: Future<Output = Result<T, reqwest_middleware::Error>>,
25 {
26   retry_custom(|| async { Ok((f)().await) }).await
27 }
28
29 async fn retry_custom<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error>
30 where
31   F: Fn() -> Fut,
32   Fut: Future<Output = Result<Result<T, reqwest_middleware::Error>, reqwest_middleware::Error>>,
33 {
34   let mut response: Option<Result<T, reqwest_middleware::Error>> = None;
35
36   for _ in 0u8..3 {
37     match (f)().await? {
38       Ok(t) => return Ok(t),
39       Err(reqwest_middleware::Error::Reqwest(e)) => {
40         if e.is_timeout() {
41           response = Some(Err(reqwest_middleware::Error::Reqwest(e)));
42           continue;
43         }
44         return Err(reqwest_middleware::Error::Reqwest(e));
45       }
46       Err(otherwise) => {
47         return Err(otherwise);
48       }
49     }
50   }
51
52   response.expect("retry http request")
53 }
54
55 #[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
56 pub struct SiteMetadata {
57   pub title: Option<String>,
58   pub description: Option<String>,
59   image: Option<Url>,
60   pub html: Option<String>,
61 }
62
63 /// Fetches the post link html tags (like title, description, image, etc)
64 pub async fn fetch_site_metadata(
65   client: &ClientWithMiddleware,
66   url: &Url,
67 ) -> Result<SiteMetadata, LemmyError> {
68   info!("Fetching site metadata for url: {}", url);
69   let response = client.get(url.as_str()).send().await?;
70
71   // Can't use .text() here, because it only checks the content header, not the actual bytes
72   // https://github.com/LemmyNet/lemmy/issues/1964
73   let html_bytes = response
74     .bytes()
75     .await
76     .map_err(|e| RecvError(e.to_string()))?
77     .to_vec();
78
79   let tags = html_to_site_metadata(&html_bytes)?;
80
81   Ok(tags)
82 }
83
84 fn html_to_site_metadata(html_bytes: &[u8]) -> Result<SiteMetadata, LemmyError> {
85   let html = String::from_utf8_lossy(html_bytes);
86
87   // Make sure the first line is doctype html
88   let first_line = html
89     .trim_start()
90     .lines()
91     .into_iter()
92     .next()
93     .ok_or_else(|| LemmyError::from_message("No lines in html"))?
94     .to_lowercase();
95
96   if !first_line.starts_with("<!doctype html>") {
97     return Err(LemmyError::from_message(
98       "Site metadata page fetch is not DOCTYPE html",
99     ));
100   }
101
102   let mut page = HTML::from_string(html.to_string(), None)?;
103
104   // If the web page specifies that it isn't actually UTF-8, re-decode the received bytes with the
105   // proper encoding. If the specified encoding cannot be found, fall back to the original UTF-8
106   // version.
107   if let Some(charset) = page.meta.get("charset") {
108     if charset.to_lowercase() != "utf-8" {
109       if let Some(encoding_ref) = encodings().iter().find(|e| e.name() == charset) {
110         if let Ok(html_with_encoding) = encoding_ref.decode(html_bytes, DecoderTrap::Replace) {
111           page = HTML::from_string(html_with_encoding, None)?;
112         }
113       }
114     }
115   }
116
117   let page_title = page.title;
118   let page_description = page.description;
119
120   let og_description = page
121     .opengraph
122     .properties
123     .get("description")
124     .map(|t| t.to_string());
125   let og_title = page
126     .opengraph
127     .properties
128     .get("title")
129     .map(|t| t.to_string());
130   let og_image = page
131     .opengraph
132     .images
133     .get(0)
134     .map(|ogo| Url::parse(&ogo.url).ok())
135     .flatten();
136
137   let title = og_title.or(page_title);
138   let description = og_description.or(page_description);
139   let image = og_image;
140
141   Ok(SiteMetadata {
142     title,
143     description,
144     image,
145     html: None,
146   })
147 }
148
149 #[derive(Deserialize, Debug, Clone)]
150 pub(crate) struct PictrsResponse {
151   files: Vec<PictrsFile>,
152   msg: String,
153 }
154
155 #[derive(Deserialize, Debug, Clone)]
156 pub(crate) struct PictrsFile {
157   file: String,
158   #[allow(dead_code)]
159   delete_token: String,
160 }
161
162 pub(crate) async fn fetch_pictrs(
163   client: &ClientWithMiddleware,
164   settings: &Settings,
165   image_url: &Url,
166 ) -> Result<PictrsResponse, LemmyError> {
167   if let Some(pictrs_url) = settings.pictrs_url.to_owned() {
168     is_image_content_type(client, image_url).await?;
169
170     let fetch_url = format!(
171       "{}/image/download?url={}",
172       pictrs_url,
173       utf8_percent_encode(image_url.as_str(), NON_ALPHANUMERIC) // TODO this might not be needed
174     );
175
176     let response = client.get(&fetch_url).send().await?;
177
178     let response: PictrsResponse = response
179       .json()
180       .await
181       .map_err(|e| RecvError(e.to_string()))?;
182
183     if response.msg == "ok" {
184       Ok(response)
185     } else {
186       Err(anyhow!("{}", &response.msg).into())
187     }
188   } else {
189     Err(anyhow!("pictrs_url not set up in config").into())
190   }
191 }
192
193 /// Both are options, since the URL might be either an html page, or an image
194 /// Returns the SiteMetadata, and a Pictrs URL, if there is a picture associated
195 pub async fn fetch_site_data(
196   client: &ClientWithMiddleware,
197   settings: &Settings,
198   url: Option<&Url>,
199 ) -> (Option<SiteMetadata>, Option<Url>) {
200   match &url {
201     Some(url) => {
202       // Fetch metadata
203       // Ignore errors, since it may be an image, or not have the data.
204       // Warning, this may ignore SSL errors
205       let metadata_option = fetch_site_metadata(client, url).await.ok();
206
207       // Fetch pictrs thumbnail
208       let pictrs_hash = match &metadata_option {
209         Some(metadata_res) => match &metadata_res.image {
210           // Metadata, with image
211           // Try to generate a small thumbnail if there's a full sized one from post-links
212           Some(metadata_image) => fetch_pictrs(client, settings, metadata_image)
213             .await
214             .map(|r| r.files[0].file.to_owned()),
215           // Metadata, but no image
216           None => fetch_pictrs(client, settings, url)
217             .await
218             .map(|r| r.files[0].file.to_owned()),
219         },
220         // No metadata, try to fetch the URL as an image
221         None => fetch_pictrs(client, settings, url)
222           .await
223           .map(|r| r.files[0].file.to_owned()),
224       };
225
226       // The full urls are necessary for federation
227       let pictrs_thumbnail = pictrs_hash
228         .map(|p| {
229           Url::parse(&format!(
230             "{}/pictrs/image/{}",
231             settings.get_protocol_and_hostname(),
232             p
233           ))
234           .ok()
235         })
236         .ok()
237         .flatten();
238
239       (metadata_option, pictrs_thumbnail)
240     }
241     None => (None, None),
242   }
243 }
244
245 async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> Result<(), LemmyError> {
246   let response = client.get(url.as_str()).send().await?;
247   if response
248     .headers()
249     .get("Content-Type")
250     .ok_or_else(|| anyhow!("No Content-Type header"))?
251     .to_str()?
252     .starts_with("image/")
253   {
254     Ok(())
255   } else {
256     Err(anyhow!("Not an image type.").into())
257   }
258 }
259
260 pub fn build_user_agent(settings: &Settings) -> String {
261   format!(
262     "Lemmy/{}; +{}",
263     VERSION,
264     settings.get_protocol_and_hostname()
265   )
266 }
267
268 #[cfg(test)]
269 mod tests {
270   use crate::request::{build_user_agent, fetch_site_metadata};
271   use url::Url;
272
273   use super::SiteMetadata;
274   use crate::settings::structs::Settings;
275
276   // These helped with testing
277   #[actix_rt::test]
278   async fn test_site_metadata() {
279     let settings = Settings::init().unwrap();
280     let client = reqwest::Client::builder()
281       .user_agent(build_user_agent(&settings))
282       .build()
283       .unwrap()
284       .into();
285     let sample_url = Url::parse("https://www.redspark.nu/en/peoples-war/district-leader-of-chand-led-cpn-arrested-in-bhojpur/").unwrap();
286     let sample_res = fetch_site_metadata(&client, &sample_url).await.unwrap();
287     assert_eq!(
288       SiteMetadata {
289         title: Some("District Leader Of Chand Led CPN Arrested In Bhojpur - Redspark".to_string()),
290         description: Some("BHOJPUR: A district leader of the outlawed Netra Bikram Chand alias Biplav-led outfit has been arrested. According to District Police".to_string()),
291         image: Some(Url::parse("https://www.redspark.nu/wp-content/uploads/2020/03/netra-bikram-chand-attends-program-1272019033653-1000x0-845x653-1.jpg").unwrap()),
292         html: None,
293       }, sample_res);
294
295     let youtube_url = Url::parse("https://www.youtube.com/watch?v=IquO_TcMZIQ").unwrap();
296     let youtube_res = fetch_site_metadata(&client, &youtube_url).await.unwrap();
297     assert_eq!(
298       SiteMetadata {
299         title: Some("A Hard Look at Rent and Rent Seeking with Michael Hudson & Pepe Escobar".to_string()),
300         description: Some("An interactive discussion on wealth inequality and the “Great Game” on the control of natural resources.In this webinar organized jointly by the Henry George...".to_string()),
301         image: Some(Url::parse("https://i.ytimg.com/vi/IquO_TcMZIQ/maxresdefault.jpg").unwrap()),
302         html: None,
303       }, youtube_res);
304   }
305
306   // #[test]
307   // fn test_pictshare() {
308   //   let res = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpg");
309   //   assert!(res.is_ok());
310   //   let res_other = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpgaoeu");
311   //   assert!(res_other.is_err());
312   // }
313 }