Skip to content

Commit 471f8ae

Browse files
fix(downloads): substitute the LazyLock for a OnceLock
This change is needed to allow us to pass an argument for the creation of the client, as it is being done in `rustls_client()`.
1 parent 7a9e227 commit 471f8ae

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/download/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl TlsBackend {
388388
#[cfg(feature = "reqwest-rustls-tls")]
389389
Self::Rustls => reqwest_be::rustls_client()?,
390390
#[cfg(feature = "reqwest-native-tls")]
391-
Self::NativeTls => &reqwest_be::CLIENT_NATIVE_TLS,
391+
Self::NativeTls => reqwest_be::native_tls_client()?,
392392
};
393393

394394
reqwest_be::download(url, resume_from, callback, client).await
@@ -526,9 +526,7 @@ mod curl {
526526
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
527527
mod reqwest_be {
528528
use std::io;
529-
#[cfg(feature = "reqwest-native-tls")]
530-
use std::sync::LazyLock;
531-
#[cfg(feature = "reqwest-rustls-tls")]
529+
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
532530
use std::sync::{Arc, OnceLock};
533531
use std::time::Duration;
534532

@@ -622,21 +620,23 @@ mod reqwest_be {
622620
static CLIENT_RUSTLS_TLS: OnceLock<Client> = OnceLock::new();
623621

624622
#[cfg(feature = "reqwest-native-tls")]
625-
pub(super) static CLIENT_NATIVE_TLS: LazyLock<Client> = LazyLock::new(|| {
626-
let catcher = || {
627-
client_generic()
628-
.user_agent(super::REQWEST_DEFAULT_TLS_USER_AGENT)
629-
.build()
630-
};
623+
pub(super) fn native_tls_client() -> Result<&'static Client, DownloadError> {
624+
if let Some(client) = CLIENT_NATIVE_TLS.get() {
625+
return Ok(client);
626+
}
631627

632-
// woah, an unwrap?!
633-
// It's OK. This is the same as what is happening in curl.
634-
//
635-
// The curl::Easy::new() internally assert!s that the initialized
636-
// Easy is not null. Inside reqwest, the errors here would be from
637-
// the TLS library returning a null pointer as well.
638-
catcher().unwrap()
639-
});
628+
let client = client_generic()
629+
.user_agent(super::REQWEST_DEFAULT_TLS_USER_AGENT)
630+
.build()
631+
.map_err(DownloadError::Reqwest)?;
632+
633+
let _ = CLIENT_NATIVE_TLS.set(client);
634+
635+
Ok(CLIENT_NATIVE_TLS.get().unwrap())
636+
}
637+
638+
#[cfg(feature = "reqwest-native-tls")]
639+
static CLIENT_NATIVE_TLS: OnceLock<Client> = OnceLock::new();
640640

641641
fn env_proxy(url: &Url) -> Option<Url> {
642642
env_proxy::for_url(url).to_url()

0 commit comments

Comments
 (0)