1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

use once_cell::sync::Lazy;
use std::fmt::Debug;
use std::sync::RwLock;
use url::Url;

use crate::entry::Entry;
use crate::error::source::HttpError;
use crate::sink::Message;

const USER_AGENT: &str =
	"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0";

// option because tls init could've failed and we took out the Err()
static CLIENT: Lazy<RwLock<Option<Result<reqwest::Client, HttpError>>>> = Lazy::new(|| {
	RwLock::new(Some(
		reqwest::ClientBuilder::new()
			.timeout(std::time::Duration::from_secs(30))
			.build()
			.map_err(HttpError::TlsInitFailed),
	))
});

pub struct Http {
	pub(crate) url: Url,
	client: reqwest::Client,
}

impl Http {
	pub fn new(url: Url) -> Result<Self, HttpError> {
		// take out the error out of the option if there was an error, otherwise just clone the Client
		let client = if let Ok(client) = CLIENT // if there was no error building the client
			.read()
			.expect("RwLock has been poisoned")
			.as_ref()
			// there was an error building the client and the error has already been extracted
			.ok_or(HttpError::ClientNotInitialized)?
		{
			client.clone()
		} else {
			return Err(CLIENT
				.write()
				.expect("RwLock has been poisoned")
				.take()
				.unwrap()
				.unwrap_err()); // should always be Some and Err because we .ok_or?'ed it up above
			    // TODO: remove these unwraps
		};

		Ok(Self { url, client })
	}

	#[tracing::instrument(skip_all)]
	pub async fn get(&self) -> Result<Vec<Entry>, HttpError> {
		tracing::debug!("Fetching HTTP source");

		tracing::trace!("Making a request to {:?}", self.url.as_str());
		let request = self
			.client
			.get(self.url.as_str())
			.header(reqwest::header::USER_AGENT, USER_AGENT)
			.send()
			.await
			.map_err(|e| HttpError::Get(e, self.url.to_string()))?;

		tracing::trace!("Getting text body of the responce");
		let page = request
			.text()
			.await
			.map_err(|e| HttpError::Get(e, self.url.to_string()))?;
		tracing::trace!("Done");

		Ok(vec![Entry {
			id: None,
			msg: Message {
				title: None,
				body: page,
				link: Some(self.url.clone()),
				media: None,
			},
		}])
	}
}

impl Debug for Http {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("Http")
			.field("url", &self.url.as_str())
			.finish_non_exhaustive()
	}
}