implement proxy handling using environment variables
@@ -42,21 +42,42 @@ where
Ok(())
}
+fn get_proxy(url: &str) -> Option<Result<ureq::Proxy>> {
+ let proxy_url = env_proxy::for_url_str(url).to_string();
+
+ // Option<Result<...>> is returned to handle both cases:
+ // 1. No proxy URL is specified => None
+ // 2. An invalid proxy URL is specified => Err(...)
+ proxy_url.map(|url| {
+ ureq::Proxy::new(&url)
+ .with_context(|| format!("Could not create proxy from proxy url: {url}"))
+ })
+}
+
#[cfg(not(any(windows, macos)))]
-pub fn get_ureq_agent() -> Result<ureq::Agent> {
- let agent = ureq::AgentBuilder::new().build();
+pub fn get_ureq_agent(url: &str) -> Result<ureq::Agent> {
+ let agent = match get_proxy(url) {
+ Some(proxy) => ureq::AgentBuilder::new().proxy(proxy?).build(),
+ None => ureq::AgentBuilder::new().build(),
+ };
Ok(agent)
}
#[cfg(any(windows, macos))]
-pub fn get_ureq_agent() -> Result<ureq::Agent> {
- use std::sync::Arc;
+pub fn get_ureq_agent(url: &str) -> Result<ureq::Agent> {
use native_tls::TlsConnector;
+ use std::sync::Arc;
- let agent = ureq::AgentBuilder::new()
+ let agent = match get_proxy(url) {
+ Some(proxy) => ureq::AgentBuilder::new()
.tls_connector(Arc::new(TlsConnector::new()?))
- .build();
+ .proxy(proxy?)
+ .build(),
+ None => ureq::AgentBuilder::new()
+ .tls_connector(Arc::new(TlsConnector::new()?))
+ .build(),
+ };
Ok(agent)
}
