Skip to content

Commit 3bfb0ff

Browse files
committed
Implement more complete backend selection
1 parent 0935568 commit 3bfb0ff

File tree

1 file changed

+65
-19
lines changed

1 file changed

+65
-19
lines changed

src/utils/mod.rs

Lines changed: 65 additions & 19 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(feature = "reqwest-backend")]
16+
use tracing::info;
1517
use url::Url;
1618

1719
use crate::errors::*;
@@ -211,7 +213,9 @@ async fn download_file_(
211213
process: &Process,
212214
) -> Result<()> {
213215
use download::download_to_path_with_backend;
214-
use download::{Backend, Event, TlsBackend};
216+
#[cfg(feature = "reqwest-backend")]
217+
use download::TlsBackend;
218+
use download::{Backend, Event};
215219
use sha2::Digest;
216220
use std::cell::RefCell;
217221

@@ -246,28 +250,70 @@ async fn download_file_(
246250
// Download the file
247251

248252
// 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 {
253+
let use_curl_backend = process.var_os("RUSTUP_USE_CURL").map(|it| it != "0");
254+
#[cfg(not(feature = "curl-backend"))]
255+
if use_curl_backend == Some(true) {
256+
return Err(anyhow!(
257+
"RUSTUP_USE_CURL is set, but this rustup distribution was not built with the curl-backend feature"
258+
));
259+
}
260+
261+
let use_rustls = process.var_os("RUSTUP_USE_RUSTLS").map(|it| it != "0");
262+
#[cfg(not(feature = "reqwest-rustls-tls"))]
263+
if use_rustls == Some(true) {
264+
return Err(anyhow!(
265+
"RUSTUP_USE_RUSTLS is set, but this rustup distribution was not built with the reqwest-rustls-tls feature"
266+
));
267+
}
268+
#[cfg(not(feature = "reqwest-native-tls"))]
269+
if use_rustls == Some(false) {
270+
return Err(anyhow!(
271+
"RUSTUP_USE_RUSTLS is set to false, but this rustup distribution was not built with the reqwest-native-tls feature"
272+
));
273+
}
274+
275+
let backend = match use_curl_backend {
276+
#[cfg(feature = "reqwest-backend")]
277+
None | Some(false) => Backend::Reqwest(match use_rustls {
278+
#[cfg(feature = "reqwest-rustls-tls")]
279+
Some(true) | None => {
280+
if use_curl_backend == Some(true) {
281+
info!(
282+
"both RUSTUP_USE_CURL and RUSTUP_USE_RUSTLS are set, using reqwest with rustls"
283+
);
284+
}
285+
TlsBackend::Rustls
286+
}
261287
#[cfg(feature = "reqwest-native-tls")]
262-
{
288+
Some(false) => {
289+
if use_curl_backend == Some(true) {
290+
info!("RUSTUP_USE_CURL is set and RUSTUP_USE_RUSTLS is set to off, using reqwest with native-tls");
291+
}
263292
TlsBackend::NativeTls
264293
}
294+
#[cfg(not(feature = "reqwest-rustls-tls"))]
295+
None => TlsBackend::NativeTls,
265296
#[cfg(not(feature = "reqwest-native-tls"))]
266-
{
267-
TlsBackend::Rustls
268-
}
269-
};
270-
Backend::Reqwest(tls_backend)
297+
Some(false) => return Err(anyhow!(
298+
"RUSTUP_USE_RUSTLS is set to false, but this rustup distribution was not built with the reqwest-native-tls feature"
299+
)),
300+
#[cfg(not(feature = "reqwest-rustls-tls"))]
301+
Some(true) => return Err(anyhow!(
302+
"RUSTUP_USE_RUSTLS is set, but this rustup distribution was not built with the reqwest-rustls-tls feature"
303+
)),
304+
}),
305+
#[cfg(feature = "curl-backend")]
306+
Some(true) => Backend::Curl,
307+
#[cfg(not(feature = "curl-backend"))]
308+
Some(true) => return Err(anyhow!(
309+
"RUSTUP_USE_CURL is set, but this rustup distribution was not built with the curl-backend feature"
310+
)),
311+
#[cfg(not(feature = "reqwest-backend"))]
312+
Some(false) => return Err(anyhow!(
313+
"RUSTUP_USE_CURL is set, but this rustup distribution was not built with the reqwest-backend feature"
314+
)),
315+
#[cfg(not(feature = "reqwest-backend"))]
316+
None => Backend::Curl,
271317
};
272318

273319
notify_handler(match backend {

0 commit comments

Comments
 (0)