|
|
use std::{hash::Hash, sync::LazyLock}; |
|
|
|
|
|
use anyhow::Result; |
|
|
use quick_cache::sync::Cache; |
|
|
use serde::{Deserialize, Serialize}; |
|
|
use turbo_rcstr::RcStr; |
|
|
use turbo_tasks::{ |
|
|
NonLocalValue, ReadRef, Vc, duration_span, mark_session_dependent, trace::TraceRawVcs, |
|
|
}; |
|
|
|
|
|
use crate::{FetchError, FetchResult, HttpResponse, HttpResponseBody}; |
|
|
|
|
|
const MAX_CLIENTS: usize = 16; |
|
|
static CLIENT_CACHE: LazyLock<Cache<ReadRef<FetchClient>, reqwest::Client>> = |
|
|
LazyLock::new(|| Cache::new(MAX_CLIENTS)); |
|
|
|
|
|
#[derive(Hash, PartialEq, Eq, Serialize, Deserialize, NonLocalValue, Debug, TraceRawVcs)] |
|
|
pub enum ProxyConfig { |
|
|
Http(RcStr), |
|
|
Https(RcStr), |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::value(shared)] |
|
|
#[derive(Hash)] |
|
|
pub struct FetchClient { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub tls_built_in_webpki_certs: bool, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub tls_built_in_native_certs: bool, |
|
|
} |
|
|
|
|
|
impl Default for FetchClient { |
|
|
fn default() -> Self { |
|
|
Self { |
|
|
tls_built_in_webpki_certs: true, |
|
|
tls_built_in_native_certs: false, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl FetchClient { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn try_get_cached_reqwest_client( |
|
|
self: ReadRef<FetchClient>, |
|
|
) -> reqwest::Result<reqwest::Client> { |
|
|
CLIENT_CACHE.get_or_insert_with(&self, { |
|
|
let this = ReadRef::clone(&self); |
|
|
move || this.try_build_uncached_reqwest_client() |
|
|
}) |
|
|
} |
|
|
|
|
|
fn try_build_uncached_reqwest_client(&self) -> reqwest::Result<reqwest::Client> { |
|
|
let client_builder = reqwest::Client::builder(); |
|
|
|
|
|
|
|
|
#[cfg(not(any( |
|
|
all(target_os = "windows", target_arch = "aarch64"), |
|
|
target_arch = "wasm32" |
|
|
)))] |
|
|
let client_builder = client_builder |
|
|
.tls_built_in_root_certs(false) |
|
|
.tls_built_in_webpki_certs(self.tls_built_in_webpki_certs) |
|
|
.tls_built_in_native_certs(self.tls_built_in_native_certs); |
|
|
|
|
|
client_builder.build() |
|
|
} |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl FetchClient { |
|
|
#[turbo_tasks::function(network)] |
|
|
pub async fn fetch( |
|
|
self: Vc<FetchClient>, |
|
|
url: RcStr, |
|
|
user_agent: Option<RcStr>, |
|
|
) -> Result<Vc<FetchResult>> { |
|
|
let url_ref = &*url; |
|
|
let this = self.await?; |
|
|
let response_result: reqwest::Result<HttpResponse> = async move { |
|
|
let reqwest_client = this.try_get_cached_reqwest_client()?; |
|
|
|
|
|
let mut builder = reqwest_client.get(url_ref); |
|
|
if let Some(user_agent) = user_agent { |
|
|
builder = builder.header("User-Agent", user_agent.as_str()); |
|
|
} |
|
|
|
|
|
let response = { |
|
|
let _span = duration_span!("fetch request", url = url_ref); |
|
|
builder.send().await |
|
|
} |
|
|
.and_then(|r| r.error_for_status())?; |
|
|
|
|
|
let status = response.status().as_u16(); |
|
|
|
|
|
let body = { |
|
|
let _span = duration_span!("fetch response", url = url_ref); |
|
|
response.bytes().await? |
|
|
} |
|
|
.to_vec(); |
|
|
|
|
|
Ok(HttpResponse { |
|
|
status, |
|
|
body: HttpResponseBody(body).resolved_cell(), |
|
|
}) |
|
|
} |
|
|
.await; |
|
|
|
|
|
match response_result { |
|
|
Ok(resp) => Ok(Vc::cell(Ok(resp.resolved_cell()))), |
|
|
Err(err) => { |
|
|
|
|
|
mark_session_dependent(); |
|
|
Ok(Vc::cell(Err( |
|
|
FetchError::from_reqwest_error(&err, &url).resolved_cell() |
|
|
))) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
#[doc(hidden)] |
|
|
pub fn __test_only_reqwest_client_cache_clear() { |
|
|
CLIENT_CACHE.clear() |
|
|
} |
|
|
|
|
|
#[doc(hidden)] |
|
|
pub fn __test_only_reqwest_client_cache_len() -> usize { |
|
|
CLIENT_CACHE.len() |
|
|
} |
|
|
|