Skip to content

Commit 2f3a3a5

Browse files
committed
Implement more complete backend selection
1 parent 0935568 commit 2f3a3a5

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

src/utils/mod.rs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use anyhow::{anyhow, bail, Context, Result};
1212
use retry::delay::{jitter, Fibonacci};
1313
use retry::{retry, OperationResult};
1414
use sha2::Sha256;
15+
use tracing::info;
1516
use url::Url;
1617

1718
use crate::errors::*;
@@ -246,28 +247,47 @@ async fn download_file_(
246247
// Download the file
247248

248249
// 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
250+
let use_curl_backend = process.var_os("RUSTUP_USE_CURL").map(|it| it != "0");
251+
let use_rustls = process.var_os("RUSTUP_USE_RUSTLS").map(|it| it != "0");
252+
253+
let backend = match (use_curl_backend, use_rustls) {
254+
// If environment specifies a backend that's unavailable, error out
255+
#[cfg(not(feature = "reqwest-rustls-tls"))]
256+
(_, Some(true)) => {
257+
return Err(anyhow!(
258+
"RUSTUP_USE_RUSTLS is set, but this rustup distribution was not built with the reqwest-rustls-tls feature"
259+
));
260+
}
261+
#[cfg(not(feature = "reqwest-native-tls"))]
262+
(_, Some(false)) => {
263+
return Err(anyhow!(
264+
"RUSTUP_USE_RUSTLS is set to false, but this rustup distribution was not built with the reqwest-native-tls feature"
265+
));
266+
}
267+
#[cfg(not(feature = "curl-backend"))]
268+
(Some(true), _) => {
269+
return Err(anyhow!(
270+
"RUSTUP_USE_CURL is set, but this rustup distribution was not built with the curl-backend feature"
271+
));
272+
}
273+
#[cfg(feature = "curl-backend")]
274+
(Some(true), None) => Backend::Curl,
275+
#[cfg(feature = "reqwest-native-tls")]
276+
(_, Some(false)) => {
277+
if use_curl_backend == Some(true) {
278+
info!("RUSTUP_USE_CURL is set and RUSTUP_USE_RUSTLS is set to off, using reqwest with native-tls");
264279
}
265-
#[cfg(not(feature = "reqwest-native-tls"))]
266-
{
267-
TlsBackend::Rustls
280+
Backend::Reqwest(TlsBackend::NativeTls)
281+
}
282+
#[cfg(feature = "reqwest-rustls-tls")]
283+
(_, Some(true) | None) => {
284+
if use_curl_backend == Some(true) {
285+
info!(
286+
"both RUSTUP_USE_CURL and RUSTUP_USE_RUSTLS are set, using reqwest with rustls"
287+
);
268288
}
269-
};
270-
Backend::Reqwest(tls_backend)
289+
Backend::Reqwest(TlsBackend::Rustls)
290+
}
271291
};
272292

273293
notify_handler(match backend {

0 commit comments

Comments
 (0)