From 5e81d780bd76666046681544a3f98bed3afde976 Mon Sep 17 00:00:00 2001 From: lichuan6 <74223747+lichuan6@users.noreply.github.com> Date: Thu, 1 Feb 2024 11:57:09 +0800 Subject: [PATCH] Read HTTP proxy from env (#6765) This PR will use http proxy from env for downloading files. --- crates/util/src/http.rs | 2 ++ crates/util/src/util.rs | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/crates/util/src/http.rs b/crates/util/src/http.rs index 6218c59c2b..eedb123917 100644 --- a/crates/util/src/http.rs +++ b/crates/util/src/http.rs @@ -1,3 +1,4 @@ +use crate::http_proxy_from_env; pub use anyhow::{anyhow, Result}; use futures::future::BoxFuture; use isahc::config::{Configurable, RedirectPolicy}; @@ -95,6 +96,7 @@ pub fn client() -> Arc { isahc::HttpClient::builder() .connect_timeout(Duration::from_secs(5)) .low_speed_timeout(100, Duration::from_secs(5)) + .proxy(http_proxy_from_env()) .build() .unwrap(), ) diff --git a/crates/util/src/util.rs b/crates/util/src/util.rs index 30f2628482..8f7a6fffbd 100644 --- a/crates/util/src/util.rs +++ b/crates/util/src/util.rs @@ -44,6 +44,28 @@ pub fn truncate(s: &str, max_chars: usize) -> &str { } } +pub fn http_proxy_from_env() -> Option { + macro_rules! try_env { + ($($env:literal),+) => { + $( + if let Ok(env) = std::env::var($env) { + return env.parse::().ok(); + } + )+ + }; + } + + try_env!( + "ALL_PROXY", + "all_proxy", + "HTTPS_PROXY", + "https_proxy", + "HTTP_PROXY", + "http_proxy" + ); + None +} + /// Removes characters from the end of the string if its length is greater than `max_chars` and /// appends "..." to the string. Returns string unchanged if its length is smaller than max_chars. pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String {