diff --git a/lib/cli/src/commands/package/download.rs b/lib/cli/src/commands/package/download.rs index c03b674d782..547de2965ba 100644 --- a/lib/cli/src/commands/package/download.rs +++ b/lib/cli/src/commands/package/download.rs @@ -6,6 +6,7 @@ use indicatif::{ProgressBar, ProgressStyle}; use tempfile::NamedTempFile; use wasmer_config::package::{PackageIdent, PackageSource}; use wasmer_registry::wasmer_env::WasmerEnv; +use wasmer_wasix::http::reqwest::get_proxy; /// Download a package from the registry. #[derive(clap::Parser, Debug)] @@ -138,7 +139,15 @@ impl PackageDownload { PackageSource::Url(url) => bail!("cannot download a package from a URL: '{}'", url), }; - let client = reqwest::blocking::Client::new(); + let builder = { + let mut builder = reqwest::blocking::ClientBuilder::new(); + if let Some(proxy) = get_proxy()? { + builder = builder.proxy(proxy); + } + builder + }; + let client = builder.build().context("failed to create reqwest client")?; + let mut b = client .get(download_url) .header(http::header::ACCEPT, "application/webc"); diff --git a/lib/wasix/Cargo.toml b/lib/wasix/Cargo.toml index d586e898588..163847189cd 100644 --- a/lib/wasix/Cargo.toml +++ b/lib/wasix/Cargo.toml @@ -104,13 +104,13 @@ web-sys = { version = "0.3.64", features = [ [target.'cfg(not(target_arch = "riscv64"))'.dependencies.reqwest] version = "0.11" default-features = false -features = ["rustls-tls", "json", "stream", "socks"] +features = ["rustls-tls", "json", "stream", "socks", "blocking"] optional = true [target.'cfg(target_arch = "riscv64")'.dependencies.reqwest] version = "0.11" default-features = false -features = ["native-tls", "json", "stream", "socks"] +features = ["native-tls", "json", "stream", "socks", "blocking"] optional = true [target.'cfg(unix)'.dependencies] diff --git a/lib/wasix/src/http/reqwest.rs b/lib/wasix/src/http/reqwest.rs index cb84dc4267d..c3827bdafca 100644 --- a/lib/wasix/src/http/reqwest.rs +++ b/lib/wasix/src/http/reqwest.rs @@ -42,17 +42,15 @@ impl ReqwestHttpClient { .with_context(|| format!("Invalid http method {}", request.method))?; // TODO: use persistent client? - let client = { + let builder = { let _guard = Handle::try_current().map_err(|_| self.handle.enter()); - let mut builder = reqwest::Client::builder().connect_timeout(self.connect_timeout); - - let proxy = env::var("http_proxy").or_else(|_| env::var("HTTP_PROXY")); - if let Ok(scheme) = proxy { - builder = builder.proxy(reqwest::Proxy::all(scheme)?); + let mut builder = reqwest::ClientBuilder::new().connect_timeout(self.connect_timeout); + if let Some(proxy) = get_proxy()? { + builder = builder.proxy(proxy); } - - builder.build().context("Could not create reqwest client")? + builder }; + let client = builder.build().context("failed to create reqwest client")?; let mut builder = client.request(method, request.url.as_str()); for (header, val) in &request.headers { @@ -98,3 +96,13 @@ impl super::HttpClient for ReqwestHttpClient { Box::pin(f) } } + +pub fn get_proxy() -> Result, anyhow::Error> { + if let Ok(scheme) = env::var("http_proxy").or_else(|_| env::var("HTTP_PROXY")) { + let proxy = reqwest::Proxy::all(scheme)?; + + Ok(Some(proxy)) + } else { + Ok(None) + } +}