@@ -388,7 +388,7 @@ impl TlsBackend {
388
388
#[ cfg( feature = "reqwest-rustls-tls" ) ]
389
389
Self :: Rustls => reqwest_be:: rustls_client ( ) ?,
390
390
#[ cfg( feature = "reqwest-native-tls" ) ]
391
- Self :: NativeTls => & reqwest_be:: CLIENT_NATIVE_TLS ,
391
+ Self :: NativeTls => reqwest_be:: native_tls_client ( ) ? ,
392
392
} ;
393
393
394
394
reqwest_be:: download ( url, resume_from, callback, client) . await
@@ -526,9 +526,7 @@ mod curl {
526
526
#[ cfg( any( feature = "reqwest-rustls-tls" , feature = "reqwest-native-tls" ) ) ]
527
527
mod reqwest_be {
528
528
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" ) ) ]
532
530
use std:: sync:: { Arc , OnceLock } ;
533
531
use std:: time:: Duration ;
534
532
@@ -622,21 +620,23 @@ mod reqwest_be {
622
620
static CLIENT_RUSTLS_TLS : OnceLock < Client > = OnceLock :: new ( ) ;
623
621
624
622
#[ 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
+ }
631
627
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 ( ) ;
640
640
641
641
fn env_proxy ( url : & Url ) -> Option < Url > {
642
642
env_proxy:: for_url ( url) . to_url ( )
0 commit comments