Skip to content

Commit

Permalink
provide get_proxy function
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej committed May 9, 2024
1 parent 8008355 commit eedf6c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
13 changes: 9 additions & 4 deletions lib/cli/src/commands/package/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +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::client_blocking_builder;
use wasmer_wasix::http::reqwest::get_proxy;

/// Download a package from the registry.
#[derive(clap::Parser, Debug)]
Expand Down Expand Up @@ -139,9 +139,14 @@ impl PackageDownload {
PackageSource::Url(url) => bail!("cannot download a package from a URL: '{}'", url),
};

let client = client_blocking_builder()?
.build()
.context("failed to create reqwest client")?;
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)
Expand Down
30 changes: 11 additions & 19 deletions lib/wasix/src/http/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ impl ReqwestHttpClient {
// TODO: use persistent client?
let builder = {
let _guard = Handle::try_current().map_err(|_| self.handle.enter());
client_builder()?.connect_timeout(self.connect_timeout)
let mut builder = reqwest::ClientBuilder::new().connect_timeout(self.connect_timeout);
if let Some(proxy) = get_proxy()? {
builder = builder.proxy(proxy);
}
builder
};
let client = builder.build().context("failed to create reqwest client")?;

Expand Down Expand Up @@ -93,24 +97,12 @@ impl super::HttpClient for ReqwestHttpClient {
}
}

pub fn client_builder() -> Result<reqwest::ClientBuilder, anyhow::Error> {
let mut builder = reqwest::Client::builder();
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)?;

let proxy = env::var("http_proxy").or_else(|_| env::var("HTTP_PROXY"));
if let Ok(scheme) = proxy {
builder = builder.proxy(reqwest::Proxy::all(scheme)?);
Ok(Some(proxy))
} else {
Ok(None)
}

Ok(builder)
}

pub fn client_blocking_builder() -> Result<reqwest::blocking::ClientBuilder, anyhow::Error> {
let mut builder = reqwest::blocking::Client::builder();

let proxy = env::var("http_proxy").or_else(|_| env::var("HTTP_PROXY"));
if let Ok(scheme) = proxy {
builder = builder.proxy(reqwest::Proxy::all(scheme)?);
}

Ok(builder)
}

0 comments on commit eedf6c4

Please sign in to comment.