From 8f4ea69c22dc9d2d1b86499330f095a76c8c569d Mon Sep 17 00:00:00 2001 From: fnuttens Date: Wed, 27 Sep 2023 09:43:34 +0200 Subject: [PATCH] Add support for HTTP proxy in network commands (#10401) Closes https://github.com/nushell/nushell/issues/8847 # Description If the `HTTP_PROXY` variable is found, use its value to setup ureq proxy. I haven't implemented `NO_PROXY` at the moment. # User-Facing Changes No breaking change for the user, the network commands simply use an environment variable. # Tests + Formatting The existing tests seem to run fine, although I can't think of a new test to add. --- crates/nu-command/src/network/http/client.rs | 21 +++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/network/http/client.rs b/crates/nu-command/src/network/http/client.rs index 6450a870fe..1d0d455744 100644 --- a/crates/nu-command/src/network/http/client.rs +++ b/crates/nu-command/src/network/http/client.rs @@ -34,10 +34,17 @@ pub fn http_client(allow_insecure: bool) -> ureq::Agent { .build() .expect("Failed to build network tls"); - ureq::builder() + let mut agent_builder = ureq::builder() .user_agent("nushell") - .tls_connector(std::sync::Arc::new(tls)) - .build() + .tls_connector(std::sync::Arc::new(tls)); + + if let Some(http_proxy) = retrieve_http_proxy_from_env() { + if let Ok(proxy) = ureq::Proxy::new(http_proxy) { + agent_builder = agent_builder.proxy(proxy); + } + }; + + agent_builder.build() } pub fn http_parse_url( @@ -639,3 +646,11 @@ pub fn request_handle_response_headers( }, } } + +fn retrieve_http_proxy_from_env() -> Option { + std::env::vars() + .find(|(key, _)| key == "http_proxy") + .or(std::env::vars().find(|(key, _)| key == "HTTP_PROXY")) + .or(std::env::vars().find(|(key, _)| key == "ALL_PROXY")) + .map(|(_, value)| value) +}