From edd4df084c78955aa64756decde09c30c50fc42b Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Mon, 6 May 2024 23:27:31 +0330 Subject: [PATCH 1/4] add proxy support to the package download subcommand --- lib/cli/src/commands/package/download.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/cli/src/commands/package/download.rs b/lib/cli/src/commands/package/download.rs index c03b674d782..695fd27cdd2 100644 --- a/lib/cli/src/commands/package/download.rs +++ b/lib/cli/src/commands/package/download.rs @@ -138,7 +138,17 @@ impl PackageDownload { PackageSource::Url(url) => bail!("cannot download a package from a URL: '{}'", url), }; - let client = reqwest::blocking::Client::new(); + let client = { + let mut builder = reqwest::blocking::ClientBuilder::new(); + + let proxy = std::env::var("http_proxy").or_else(|_| std::env::var("HTTP_PROXY")); + if let Ok(scheme) = proxy { + builder = builder.proxy(reqwest::Proxy::all(scheme)?); + } + + builder.build().context("Could not create reqwest client")? + }; + let mut b = client .get(download_url) .header(http::header::ACCEPT, "application/webc"); From 9a12331f92342f1fcc4633bccbcca49a4cab3258 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Tue, 7 May 2024 14:24:04 +0330 Subject: [PATCH 2/4] create client builder functions --- lib/cli/src/commands/package/download.rs | 14 +++------- lib/wasix/src/http/reqwest.rs | 34 +++++++++++++++++------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/lib/cli/src/commands/package/download.rs b/lib/cli/src/commands/package/download.rs index 695fd27cdd2..054344f40fd 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::client_blocking_builder; /// Download a package from the registry. #[derive(clap::Parser, Debug)] @@ -138,16 +139,9 @@ impl PackageDownload { PackageSource::Url(url) => bail!("cannot download a package from a URL: '{}'", url), }; - let client = { - let mut builder = reqwest::blocking::ClientBuilder::new(); - - let proxy = std::env::var("http_proxy").or_else(|_| std::env::var("HTTP_PROXY")); - if let Ok(scheme) = proxy { - builder = builder.proxy(reqwest::Proxy::all(scheme)?); - } - - builder.build().context("Could not create reqwest client")? - }; + let client = client_blocking_builder()? + .build() + .context("failed to create reqwest client")?; let mut b = client .get(download_url) diff --git a/lib/wasix/src/http/reqwest.rs b/lib/wasix/src/http/reqwest.rs index cb84dc4267d..79f65d8db65 100644 --- a/lib/wasix/src/http/reqwest.rs +++ b/lib/wasix/src/http/reqwest.rs @@ -42,17 +42,11 @@ 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)?); - } - - builder.build().context("Could not create reqwest client")? + client_builder()?.connect_timeout(self.connect_timeout) }; + 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 +92,25 @@ impl super::HttpClient for ReqwestHttpClient { Box::pin(f) } } + +pub fn client_builder() -> Result { + let mut builder = reqwest::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) +} + +pub fn client_blocking_builder() -> Result { + 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) +} From 8008355d280fea4b88281c6c15fda3cf95bf1aa5 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Thu, 9 May 2024 02:09:34 +0330 Subject: [PATCH 3/4] add blocking feature --- lib/wasix/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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] From eedf6c425f60519fe0a9299b501b6306a23f9830 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Thu, 9 May 2024 17:43:35 +0330 Subject: [PATCH 4/4] provide get_proxy function --- lib/cli/src/commands/package/download.rs | 13 ++++++---- lib/wasix/src/http/reqwest.rs | 30 +++++++++--------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/cli/src/commands/package/download.rs b/lib/cli/src/commands/package/download.rs index 054344f40fd..547de2965ba 100644 --- a/lib/cli/src/commands/package/download.rs +++ b/lib/cli/src/commands/package/download.rs @@ -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)] @@ -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) diff --git a/lib/wasix/src/http/reqwest.rs b/lib/wasix/src/http/reqwest.rs index 79f65d8db65..c3827bdafca 100644 --- a/lib/wasix/src/http/reqwest.rs +++ b/lib/wasix/src/http/reqwest.rs @@ -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")?; @@ -93,24 +97,12 @@ impl super::HttpClient for ReqwestHttpClient { } } -pub fn client_builder() -> Result { - let mut builder = reqwest::Client::builder(); +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)?; - 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 { - 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) }