use anyhow::{Context, Result, anyhow, bail};
use std::{
io::{Read, Write},
time::Duration,
};
const COPY_CHUNK_SIZE: usize = 1 << 20;
fn request_error(error: ureq::Error) -> anyhow::Error {
match error {
ureq::Error::StatusCode(status) => {
anyhow!("HTTP download failed: server returned status {status}")
}
ureq::Error::Timeout(_) => anyhow!("HTTP download timed out"),
ureq::Error::HostNotFound => anyhow!("HTTP download failed: host not found"),
ureq::Error::TooManyRedirects => anyhow!("HTTP download failed: too many redirects"),
ureq::Error::RequireHttpsOnly(_) => anyhow!("HTTP download refused an insecure redirect"),
ureq::Error::BadUri(_) | ureq::Error::Http(_) => {
anyhow!("HTTP download failed: invalid URL or request")
}
_ => anyhow!("HTTP download failed: network or protocol error"),
}
}
pub fn download(url: &str, max_bytes: u64) -> Result<tempfile::TempPath> {
let https = url.starts_with("https://");
anyhow::ensure!(
https || url.starts_with("http://"),
"URL must use HTTP or HTTPS"
);
let config = ureq::Agent::config_builder()
.https_only(https)
.timeout_resolve(Some(Duration::from_secs(15)))
.timeout_connect(Some(Duration::from_secs(30)))
.timeout_per_read(Some(Duration::from_secs(60)))
.timeout_per_write(Some(Duration::from_secs(30)))
.build();
let agent = ureq::Agent::new_with_config(config);
let mut response = agent.get(url).call().map_err(request_error)?;
if response.status().as_u16() != 200 {
bail!(
"HTTP download failed: unexpected status {}",
response.status().as_u16()
);
}
let mut file = tempfile::NamedTempFile::new().context("cannot create temporary file")?;
let mut body = response.body_mut().as_reader();
let mut copied: u64 = 0;
let mut chunk = vec![0u8; COPY_CHUNK_SIZE];
loop {
let read = body.read(&mut chunk).map_err(|_| {
anyhow!("HTTP download failed while reading body or writing temporary file")
})?;
if read == 0 {
break;
}
if copied.saturating_add(read as u64) > max_bytes {
bail!("download exceeded the maximum allowed size");
}
file.write_all(&chunk[..read]).map_err(|_| {
anyhow!("HTTP download failed while reading body or writing temporary file")
})?;
copied += read as u64;
}
file.flush().context("cannot flush temporary download")?;
Ok(file.into_temp_path())
}