diff --git a/lib/backend-api/src/client.rs b/lib/backend-api/src/client.rs index 6407ab0edd1..d3579939f72 100644 --- a/lib/backend-api/src/client.rs +++ b/lib/backend-api/src/client.rs @@ -75,10 +75,14 @@ impl WasmerClient { }) } + pub fn new(graphql_endpoint: Url, user_agent: &str) -> Result { + Self::new_with_proxy(graphql_endpoint, user_agent, None) + } + pub fn new_with_proxy( graphql_endpoint: Url, user_agent: &str, - proxy: reqwest::Proxy, + proxy: Option, ) -> Result { let builder = { #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] @@ -89,7 +93,11 @@ impl WasmerClient { .connect_timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(90)); - builder.proxy(proxy) + if let Some(proxy) = proxy { + builder.proxy(proxy) + } else { + builder + } }; let client = builder.build().context("failed to create reqwest client")?; @@ -97,22 +105,6 @@ impl WasmerClient { Self::new_with_client(client, graphql_endpoint, user_agent) } - pub fn new(graphql_endpoint: Url, user_agent: &str) -> Result { - #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] - let client = reqwest::Client::builder() - .build() - .context("could not construct http client")?; - - #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] - let client = reqwest::Client::builder() - .connect_timeout(Duration::from_secs(10)) - .timeout(Duration::from_secs(90)) - .build() - .context("could not construct http client")?; - - Self::new_with_client(client, graphql_endpoint, user_agent) - } - pub fn with_auth_token(mut self, auth_token: String) -> Self { self.auth_token = Some(auth_token); self diff --git a/lib/cli/src/commands/package/common/mod.rs b/lib/cli/src/commands/package/common/mod.rs index 5641f96bfde..76f3e2c0bf6 100644 --- a/lib/cli/src/commands/package/common/mod.rs +++ b/lib/cli/src/commands/package/common/mod.rs @@ -44,6 +44,7 @@ pub(super) async fn upload( timeout: humantime::Duration, package: &Package, pb: ProgressBar, + proxy: Option, ) -> anyhow::Result { let hash_str = hash.to_string(); let hash_str = hash_str.trim_start_matches("sha256:"); @@ -68,11 +69,19 @@ pub(super) async fn upload( tracing::info!("signed url is: {session_uri}"); - let client = reqwest::Client::builder() - .default_headers(reqwest::header::HeaderMap::default()) - .timeout(timeout.into()) - .build() - .unwrap(); + let client = { + let builder = reqwest::Client::builder() + .default_headers(reqwest::header::HeaderMap::default()) + .timeout(timeout.into()); + + let builder = if let Some(proxy) = proxy { + builder.proxy(proxy) + } else { + builder + }; + + builder.build().unwrap() + }; let res = client .post(&session_uri) diff --git a/lib/cli/src/commands/package/download.rs b/lib/cli/src/commands/package/download.rs index bc168fae95e..97cc9650a4a 100644 --- a/lib/cli/src/commands/package/download.rs +++ b/lib/cli/src/commands/package/download.rs @@ -5,7 +5,6 @@ use dialoguer::console::{style, Emoji}; use indicatif::{ProgressBar, ProgressStyle}; use tempfile::NamedTempFile; use wasmer_config::package::{PackageIdent, PackageSource}; -use wasmer_wasix::http::reqwest::get_proxy; use crate::config::WasmerEnv; @@ -28,10 +27,6 @@ pub struct PackageDownload { #[clap(long)] pub quiet: bool, - /// proxy to use for downloading - #[clap(long)] - pub proxy: Option, - /// The package to download. package: PackageSource, } @@ -101,13 +96,7 @@ impl PackageDownload { // caveat: client_unauthennticated will use a token if provided, it // just won't fail if none is present. So, _unauthenticated() can actually // produce an authenticated client. - let client = if let Some(proxy) = &self.proxy { - let proxy = reqwest::Proxy::all(proxy)?; - - self.env.client_unauthennticated_with_proxy(proxy)? - } else { - self.env.client_unauthennticated()? - }; + let client = self.env.client_unauthennticated()?; let version = id.version_or_default().to_string(); let version = if version == "*" { @@ -171,7 +160,7 @@ impl PackageDownload { let builder = { let mut builder = reqwest::blocking::ClientBuilder::new(); - if let Some(proxy) = get_proxy()? { + if let Some(proxy) = self.env.proxy()? { builder = builder.proxy(proxy); } builder @@ -309,7 +298,6 @@ mod tests { out_path: Some(out_path.clone()), package: "wasmer/hello@0.1.0".parse().unwrap(), quiet: true, - proxy: None, }; cmd.execute().unwrap(); diff --git a/lib/cli/src/commands/package/push.rs b/lib/cli/src/commands/package/push.rs index 03de8e9aad7..7c2628949ea 100644 --- a/lib/cli/src/commands/package/push.rs +++ b/lib/cli/src/commands/package/push.rs @@ -128,7 +128,15 @@ impl PackagePush { ) -> anyhow::Result<()> { let pb = make_spinner!(self.quiet, "Uploading the package.."); - let signed_url = upload(client, package_hash, self.timeout, package, pb.clone()).await?; + let signed_url = upload( + client, + package_hash, + self.timeout, + package, + pb.clone(), + self.env.proxy()?, + ) + .await?; spinner_ok!(pb, "Package correctly uploaded"); let pb = make_spinner!(self.quiet, "Waiting for package to become available..."); diff --git a/lib/cli/src/config/env.rs b/lib/cli/src/config/env.rs index 496962c14f7..a328f66d40c 100644 --- a/lib/cli/src/config/env.rs +++ b/lib/cli/src/config/env.rs @@ -91,6 +91,17 @@ impl WasmerEnv { }) } + /// Returns the proxy specified in wasmer config if present + pub fn proxy(&self) -> Result, Error> { + self.config()? + .proxy + .url + .as_ref() + .map(reqwest::Proxy::all) + .transpose() + .map_err(Into::into) + } + /// The directory all Wasmer artifacts are stored in. pub fn dir(&self) -> &Path { &self.wasmer_dir @@ -127,22 +138,9 @@ impl WasmerEnv { pub fn client_unauthennticated(&self) -> Result { let registry_url = self.registry_endpoint()?; - let client = wasmer_api::WasmerClient::new(registry_url, &DEFAULT_WASMER_CLI_USER_AGENT)?; - let client = if let Some(token) = self.token() { - client.with_auth_token(token) - } else { - client - }; + let proxy = self.proxy()?; - Ok(client) - } - - pub fn client_unauthennticated_with_proxy( - &self, - proxy: reqwest::Proxy, - ) -> Result { - let registry_url = self.registry_endpoint()?; let client = wasmer_api::WasmerClient::new_with_proxy( registry_url, &DEFAULT_WASMER_CLI_USER_AGENT,