Skip to content

Commit

Permalink
Add support for HTTP proxy in network commands (#10401)
Browse files Browse the repository at this point in the history
Closes #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.
  • Loading branch information
fnuttens committed Sep 27, 2023
1 parent 6c02624 commit 8f4ea69
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions crates/nu-command/src/network/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -639,3 +646,11 @@ pub fn request_handle_response_headers(
},
}
}

fn retrieve_http_proxy_from_env() -> Option<String> {
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)
}

0 comments on commit 8f4ea69

Please sign in to comment.