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