Skip to content

Commit 45ab1fd

Browse files
committed
Implement more complete backend selection
1 parent 0935568 commit 45ab1fd

File tree

1 file changed

+56
-22
lines changed

1 file changed

+56
-22
lines changed

src/utils/mod.rs

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use anyhow::{anyhow, bail, Context, Result};
1212
use retry::delay::{jitter, Fibonacci};
1313
use retry::{retry, OperationResult};
1414
use sha2::Sha256;
15+
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
16+
use tracing::info;
1517
use url::Url;
1618

1719
use crate::errors::*;
@@ -210,8 +212,9 @@ async fn download_file_(
210212
notify_handler: &dyn Fn(Notification<'_>),
211213
process: &Process,
212214
) -> Result<()> {
213-
use download::download_to_path_with_backend;
214-
use download::{Backend, Event, TlsBackend};
215+
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
216+
use download::TlsBackend;
217+
use download::{download_to_path_with_backend, Backend, Event};
215218
use sha2::Digest;
216219
use std::cell::RefCell;
217220

@@ -246,28 +249,59 @@ async fn download_file_(
246249
// Download the file
247250

248251
// Keep the curl env var around for a bit
249-
let use_curl_backend = process
250-
.var_os("RUSTUP_USE_CURL")
251-
.is_some_and(|it| it != "0");
252-
let use_rustls = process
253-
.var_os("RUSTUP_USE_RUSTLS")
254-
.is_none_or(|it| it != "0");
255-
let backend = if use_curl_backend {
256-
Backend::Curl
257-
} else {
258-
let tls_backend = if use_rustls {
259-
TlsBackend::Rustls
260-
} else {
261-
#[cfg(feature = "reqwest-native-tls")]
262-
{
263-
TlsBackend::NativeTls
252+
let use_curl_backend = process.var_os("RUSTUP_USE_CURL").map(|it| it != "0");
253+
let use_rustls = process.var_os("RUSTUP_USE_RUSTLS").map(|it| it != "0");
254+
255+
let backend = match (use_curl_backend, use_rustls) {
256+
// If environment specifies a backend that's unavailable, error out
257+
#[cfg(not(feature = "reqwest-rustls-tls"))]
258+
(_, Some(true)) => {
259+
return Err(anyhow!(
260+
"RUSTUP_USE_RUSTLS is set, but this rustup distribution was not built with the reqwest-rustls-tls feature"
261+
));
262+
}
263+
#[cfg(not(feature = "reqwest-native-tls"))]
264+
(_, Some(false)) => {
265+
return Err(anyhow!(
266+
"RUSTUP_USE_RUSTLS is set to false, but this rustup distribution was not built with the reqwest-native-tls feature"
267+
));
268+
}
269+
#[cfg(not(feature = "curl-backend"))]
270+
(Some(true), _) => {
271+
return Err(anyhow!(
272+
"RUSTUP_USE_CURL is set, but this rustup distribution was not built with the curl-backend feature"
273+
));
274+
}
275+
276+
// Positive selections, from least preferred to most preferred
277+
#[cfg(feature = "curl-backend")]
278+
(Some(true), None) => Backend::Curl,
279+
#[cfg(feature = "reqwest-native-tls")]
280+
(_, Some(false)) => {
281+
if use_curl_backend == Some(true) {
282+
info!("RUSTUP_USE_CURL is set and RUSTUP_USE_RUSTLS is set to off, using reqwest with native-tls");
264283
}
265-
#[cfg(not(feature = "reqwest-native-tls"))]
266-
{
267-
TlsBackend::Rustls
284+
Backend::Reqwest(TlsBackend::NativeTls)
285+
}
286+
#[cfg(feature = "reqwest-rustls-tls")]
287+
_ => {
288+
if use_curl_backend == Some(true) {
289+
info!(
290+
"both RUSTUP_USE_CURL and RUSTUP_USE_RUSTLS are set, using reqwest with rustls"
291+
);
268292
}
269-
};
270-
Backend::Reqwest(tls_backend)
293+
Backend::Reqwest(TlsBackend::Rustls)
294+
}
295+
296+
// Falling back if only one backend is available
297+
#[cfg(all(not(feature = "reqwest-rustls-tls"), feature = "reqwest-native-tls"))]
298+
_ => Backend::Reqwest(TlsBackend::NativeTls),
299+
#[cfg(all(
300+
not(feature = "reqwest-rustls-tls"),
301+
not(feature = "reqwest-native-tls"),
302+
feature = "curl-backend"
303+
))]
304+
_ => Backend::Curl,
271305
};
272306

273307
notify_handler(match backend {

0 commit comments

Comments
 (0)