Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add proxy support to the package download subcommand #4640

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/cli/src/commands/package/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions lib/wasix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
24 changes: 16 additions & 8 deletions lib/wasix/src/http/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -98,3 +96,13 @@ impl super::HttpClient for ReqwestHttpClient {
Box::pin(f)
}
}

maminrayej marked this conversation as resolved.
Show resolved Hide resolved
pub fn get_proxy() -> Result<Option<reqwest::Proxy>, 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)
}
}
Loading