Skip to content

Commit 0e0b597

Browse files
committed
refactor(download): factor out backend selection logic
1 parent 0a96a33 commit 0e0b597

1 file changed

Lines changed: 55 additions & 45 deletions

File tree

src/download/mod.rs

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -84,50 +84,7 @@ pub(crate) fn is_network_failure(err: &anyhow::Error) -> bool {
8484
}
8585
}
8686

87-
async fn download_file_(
88-
url: &Url,
89-
path: &Path,
90-
hasher: Option<&mut Sha256>,
91-
resume_from_partial: bool,
92-
status: Option<&DownloadStatus>,
93-
process: &Process,
94-
) -> anyhow::Result<()> {
95-
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
96-
use crate::download::{Backend, Event, TlsBackend};
97-
use sha2::Digest;
98-
use std::cell::RefCell;
99-
100-
debug!(url = %url, "downloading file");
101-
let hasher = RefCell::new(hasher);
102-
103-
// This callback will write the download to disk and optionally
104-
// hash the contents, then forward the notification up the stack
105-
let callback: &dyn Fn(Event<'_>) -> anyhow::Result<()> = &|msg| {
106-
if let Event::DownloadDataReceived(data) = msg
107-
&& let Some(h) = hasher.borrow_mut().as_mut()
108-
{
109-
h.update(data);
110-
}
111-
112-
match msg {
113-
Event::DownloadContentLengthReceived(len) => {
114-
if let Some(status) = status {
115-
status.received_length(len)
116-
}
117-
}
118-
Event::DownloadDataReceived(data) => {
119-
if let Some(status) = status {
120-
status.received_data(data.len())
121-
}
122-
}
123-
Event::ResumingPartialDownload => debug!("resuming partial download"),
124-
}
125-
126-
Ok(())
127-
};
128-
129-
// Download the file
130-
87+
fn select_backend(process: &Process) -> anyhow::Result<Backend> {
13188
// Keep the curl env var around for a bit
13289
let use_curl_backend = process.var_os("RUSTUP_USE_CURL").map(|it| it != "0");
13390
if use_curl_backend == Some(true) {
@@ -199,15 +156,68 @@ async fn download_file_(
199156
_ => Backend::Curl,
200157
};
201158

159+
Ok(backend)
160+
}
161+
162+
fn timeout(process: &Process) -> anyhow::Result<Duration> {
202163
let timeout = Duration::from_secs(match process.var("RUSTUP_DOWNLOAD_TIMEOUT") {
203-
Ok(s) => NonZero::from_str(&s)
164+
Ok(s) => NonZero::from_str(&s)
204165
.context(
205166
"invalid value in RUSTUP_DOWNLOAD_TIMEOUT -- must be a natural number greater than zero",
206167
)?
207168
.get(),
208169
Err(_) => 180,
209170
});
210171

172+
Ok(timeout)
173+
}
174+
175+
async fn download_file_(
176+
url: &Url,
177+
path: &Path,
178+
hasher: Option<&mut Sha256>,
179+
resume_from_partial: bool,
180+
status: Option<&DownloadStatus>,
181+
process: &Process,
182+
) -> anyhow::Result<()> {
183+
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
184+
use crate::download::{Backend, Event};
185+
use sha2::Digest;
186+
use std::cell::RefCell;
187+
188+
debug!(url = %url, "downloading file");
189+
let hasher = RefCell::new(hasher);
190+
191+
// This callback will write the download to disk and optionally
192+
// hash the contents, then forward the notification up the stack
193+
let callback: &dyn Fn(Event<'_>) -> anyhow::Result<()> = &|msg| {
194+
if let Event::DownloadDataReceived(data) = msg
195+
&& let Some(h) = hasher.borrow_mut().as_mut()
196+
{
197+
h.update(data);
198+
}
199+
200+
match msg {
201+
Event::DownloadContentLengthReceived(len) => {
202+
if let Some(status) = status {
203+
status.received_length(len)
204+
}
205+
}
206+
Event::DownloadDataReceived(data) => {
207+
if let Some(status) = status {
208+
status.received_data(data.len())
209+
}
210+
}
211+
Event::ResumingPartialDownload => debug!("resuming partial download"),
212+
}
213+
214+
Ok(())
215+
};
216+
217+
// Download the file
218+
let backend = select_backend(process)?;
219+
let timeout = timeout(process)?;
220+
211221
match backend {
212222
#[cfg(feature = "curl-backend")]
213223
Backend::Curl => debug!("downloading with curl"),

0 commit comments

Comments
 (0)