use std::fmt;
use crate::{Error, Result};
use url::Url;
#[derive(Clone)]
pub struct Proxy {
source: Source,
no_proxy: Option<String>,
}
#[derive(Clone)]
enum Source {
Explicit { scope: Scope, url: String },
Environment,
}
#[derive(Clone, Copy)]
enum Scope {
Http,
Https,
All,
}
impl Proxy {
#[must_use]
pub fn from_environment() -> Self {
Self {
source: Source::Environment,
no_proxy: None,
}
}
pub fn http(url: impl AsRef<str>) -> Result<Self> {
Self::explicit(Scope::Http, url.as_ref())
}
pub fn https(url: impl AsRef<str>) -> Result<Self> {
Self::explicit(Scope::Https, url.as_ref())
}
pub fn all(url: impl AsRef<str>) -> Result<Self> {
Self::explicit(Scope::All, url.as_ref())
}
#[must_use]
pub fn no_proxy(mut self, rules: impl AsRef<str>) -> Self {
let rules = rules.as_ref().trim();
self.no_proxy = (!rules.is_empty()).then(|| rules.to_owned());
self
}
pub(crate) fn into_reqwest(self) -> Result<Vec<reqwest::Proxy>> {
self.into_reqwest_with(process_environment_value)
}
fn explicit(scope: Scope, url: &str) -> Result<Self> {
Ok(Self {
source: Source::Explicit {
scope,
url: validated_proxy_url(url)?,
},
no_proxy: None,
})
}
fn into_reqwest_with(
self,
env: impl Fn(&str) -> Option<String>,
) -> Result<Vec<reqwest::Proxy>> {
match self.source {
Source::Explicit { scope, url } => {
let proxy = match scope {
Scope::Http => reqwest::Proxy::http(url),
Scope::Https => reqwest::Proxy::https(url),
Scope::All => reqwest::Proxy::all(url),
}
.map_err(Error::proxy)?;
Ok(vec![with_no_proxy(proxy, self.no_proxy.as_deref())])
}
Source::Environment => environment_proxies(env, self.no_proxy.as_deref()),
}
}
}
fn process_environment_value(name: &str) -> Option<String> {
#[cfg(windows)]
{
std::env::vars_os().find_map(|(key, value)| {
if key.as_os_str() == std::ffi::OsStr::new(name) {
value.into_string().ok()
} else {
None
}
})
}
#[cfg(not(windows))]
{
std::env::var(name).ok()
}
}
impl fmt::Debug for Proxy {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let source = match &self.source {
Source::Explicit { .. } => "explicit",
Source::Environment => "environment",
};
formatter
.debug_struct("Proxy")
.field("source", &source)
.field("has_no_proxy", &self.no_proxy.is_some())
.finish()
}
}
fn environment_proxies(
env: impl Fn(&str) -> Option<String>,
no_proxy: Option<&str>,
) -> Result<Vec<reqwest::Proxy>> {
let values = environment_proxy_values(&env, no_proxy);
let mut proxies = Vec::new();
if let Some(url) = values.http {
let url = environment_proxy_url(&url)?;
proxies.push(with_no_proxy(
reqwest::Proxy::http(url).map_err(Error::proxy)?,
values.no_proxy.as_deref(),
));
}
if let Some(url) = values.https {
let url = environment_proxy_url(&url)?;
proxies.push(with_no_proxy(
reqwest::Proxy::https(url).map_err(Error::proxy)?,
values.no_proxy.as_deref(),
));
}
Ok(proxies)
}
struct EnvironmentProxyValues {
http: Option<String>,
https: Option<String>,
no_proxy: Option<String>,
}
fn environment_proxy_values(
env: &impl Fn(&str) -> Option<String>,
no_proxy: Option<&str>,
) -> EnvironmentProxyValues {
let is_cgi = env("REQUEST_METHOD").is_some();
let http_names = if is_cgi {
&["http_proxy"][..]
} else {
&["http_proxy", "HTTP_PROXY"][..]
};
EnvironmentProxyValues {
http: env_any(env, http_names),
https: env_any(env, &["https_proxy", "HTTPS_PROXY"]),
no_proxy: no_proxy
.map(str::trim)
.filter(|value| !value.is_empty())
.map(ToOwned::to_owned)
.or_else(|| env_any(env, &["no_proxy", "NO_PROXY"])),
}
}
fn env_any(env: &impl Fn(&str) -> Option<String>, names: &[&str]) -> Option<String> {
names
.iter()
.filter_map(|name| env(name))
.map(|value| value.trim().to_owned())
.find(|value| !value.is_empty())
}
fn environment_proxy_url(value: &str) -> Result<String> {
let value = value.trim();
match Url::parse(value) {
Ok(url) if url.host().is_some() => {
return validated_proxy_url(value);
}
Ok(_) | Err(url::ParseError::RelativeUrlWithoutBase) => {}
Err(_) => return Err(invalid_environment_proxy()),
}
if value.contains(['/', '?', '#', '@']) {
return Err(invalid_environment_proxy());
}
let url = Url::parse(&format!("http://{value}")).map_err(|_| invalid_environment_proxy())?;
if url.host().is_none()
|| !url.username().is_empty()
|| url.password().is_some()
|| url.query().is_some()
|| url.fragment().is_some()
{
return Err(invalid_environment_proxy());
}
Ok(url.to_string())
}
fn validated_proxy_url(value: &str) -> Result<String> {
let candidate = if value.contains("://") {
value.to_owned()
} else {
format!("http://{value}")
};
let url = Url::parse(&candidate).map_err(|_| invalid_proxy())?;
let scheme = url.scheme().to_ascii_lowercase();
let supported = matches!(scheme.as_str(), "http" | "https")
|| (cfg!(feature = "socks")
&& matches!(scheme.as_str(), "socks4" | "socks4a" | "socks5" | "socks5h"));
if !supported {
return Err(Error::new(
crate::ErrorKind::Proxy,
"proxy URL scheme is not enabled or supported",
));
}
if scheme == "https" && !cfg!(feature = "rustls") {
return Err(Error::new(
crate::ErrorKind::Proxy,
"HTTPS proxies require the rustls feature",
));
}
if url.host().is_none()
|| !matches!(url.path(), "" | "/")
|| url.query().is_some()
|| url.fragment().is_some()
{
return Err(invalid_proxy());
}
Ok(url.to_string())
}
fn invalid_environment_proxy() -> Error {
Error::new(
crate::ErrorKind::Proxy,
"environment proxy must be a URL or host and optional port",
)
}
fn invalid_proxy() -> Error {
Error::new(
crate::ErrorKind::Proxy,
"proxy URL must contain only a scheme, authority, and optional credentials",
)
}
fn with_no_proxy(proxy: reqwest::Proxy, no_proxy: Option<&str>) -> reqwest::Proxy {
match no_proxy.and_then(reqwest::NoProxy::from_string) {
Some(no_proxy) => proxy.no_proxy(Some(no_proxy)),
None => proxy,
}
}
#[cfg(test)]
mod describe_proxy {
use std::{
io::{Read, Write},
net::TcpListener,
process::Command,
thread,
time::Duration,
};
use super::*;
#[test]
fn it_lowercase_environment_variables_take_precedence() {
let values = environment_proxy_values(
&|name| match name {
"http_proxy" => Some("http://lower-http.local".to_owned()),
"HTTP_PROXY" => Some("http://upper-http.local".to_owned()),
"https_proxy" => Some("http://lower-https.local".to_owned()),
"HTTPS_PROXY" => Some("http://upper-https.local".to_owned()),
"no_proxy" => Some("lower-bypass.local".to_owned()),
"NO_PROXY" => Some("upper-bypass.local".to_owned()),
_ => None,
},
None,
);
assert_eq!(values.http.as_deref(), Some("http://lower-http.local"));
assert_eq!(values.https.as_deref(), Some("http://lower-https.local"));
assert_eq!(values.no_proxy.as_deref(), Some("lower-bypass.local"));
}
#[test]
fn it_empty_environment_variables_fall_through() {
let values = environment_proxy_values(
&|name| match name {
"http_proxy" => Some(" ".to_owned()),
"HTTP_PROXY" => Some("http://upper-http.local".to_owned()),
"https_proxy" => Some(String::new()),
"HTTPS_PROXY" => Some("http://upper-https.local".to_owned()),
"no_proxy" => Some("\t".to_owned()),
"NO_PROXY" => Some("upper-bypass.local".to_owned()),
_ => None,
},
None,
);
assert_eq!(values.http.as_deref(), Some("http://upper-http.local"));
assert_eq!(values.https.as_deref(), Some("http://upper-https.local"));
assert_eq!(values.no_proxy.as_deref(), Some("upper-bypass.local"));
}
#[test]
fn it_cgi_ignores_uppercase_http_proxy_only() {
let values = environment_proxy_values(
&|name| match name {
"REQUEST_METHOD" => Some("GET".to_owned()),
"HTTP_PROXY" => Some("http://untrusted.local".to_owned()),
"HTTPS_PROXY" => Some("http://trusted-https.local".to_owned()),
"NO_PROXY" => Some("bypass.local".to_owned()),
_ => None,
},
None,
);
assert!(values.http.is_none());
assert_eq!(values.https.as_deref(), Some("http://trusted-https.local"));
assert_eq!(values.no_proxy.as_deref(), Some("bypass.local"));
let values = environment_proxy_values(
&|name| match name {
"REQUEST_METHOD" => Some(String::new()),
"http_proxy" => Some("http://explicit-lower.local".to_owned()),
"HTTP_PROXY" => Some("http://untrusted.local".to_owned()),
_ => None,
},
None,
);
assert_eq!(values.http.as_deref(), Some("http://explicit-lower.local"));
}
#[test]
fn it_no_proxy_precedence_is_explicit_then_lowercase_then_uppercase() {
fn env(name: &str) -> Option<String> {
match name {
"no_proxy" => Some("lower.local".to_owned()),
"NO_PROXY" => Some("upper.local".to_owned()),
_ => None,
}
}
let explicit = environment_proxy_values(&env, Some(" explicit.local "));
let lowercase = environment_proxy_values(&env, None);
let uppercase = environment_proxy_values(
&|name| match name {
"no_proxy" => Some(" ".to_owned()),
"NO_PROXY" => Some("upper.local".to_owned()),
_ => None,
},
None,
);
assert_eq!(explicit.no_proxy.as_deref(), Some("explicit.local"));
assert_eq!(lowercase.no_proxy.as_deref(), Some("lower.local"));
assert_eq!(uppercase.no_proxy.as_deref(), Some("upper.local"));
}
#[test]
fn it_explicit_proxy_does_not_read_environment() {
let proxies = Proxy::all("proxy.local:8080")
.unwrap()
.into_reqwest_with(|_| panic!("explicit proxy read the environment"))
.unwrap();
assert_eq!(proxies.len(), 1);
}
#[test]
fn it_environment_proxy_reads_proxy_variables() {
let proxies = Proxy::from_environment()
.into_reqwest_with(|name| match name {
"HTTP_PROXY" => Some("proxy.local:8080".to_owned()),
"HTTPS_PROXY" => Some("http://secure-proxy.local".to_owned()),
"NO_PROXY" => Some("localhost,.example.com".to_owned()),
_ => None,
})
.unwrap();
assert_eq!(proxies.len(), 2);
}
#[test]
fn it_environment_proxy_reads_lowercase_proxy_variables() {
let proxies = Proxy::from_environment()
.into_reqwest_with(|name| match name {
"http_proxy" => Some("proxy.local:8080".to_owned()),
"https_proxy" => Some("http://secure-proxy.local".to_owned()),
"no_proxy" => Some("localhost,.example.com".to_owned()),
_ => None,
})
.unwrap();
assert_eq!(proxies.len(), 2);
}
#[test]
fn it_environment_proxy_ignores_empty_values() {
let proxies = Proxy::from_environment()
.into_reqwest_with(|name| match name {
"HTTP_PROXY" => Some(" ".to_owned()),
"HTTPS_PROXY" => Some(String::new()),
_ => None,
})
.unwrap();
assert!(proxies.is_empty());
}
#[test]
fn it_environment_proxy_accepts_urls_and_host_port_values() {
for value in ["http://proxy.local:8080", "proxy.local:8080", "proxy.local"] {
let proxies = Proxy::from_environment()
.into_reqwest_with(|name| match name {
"HTTP_PROXY" => Some(value.to_owned()),
_ => None,
})
.unwrap();
assert_eq!(proxies.len(), 1);
}
#[cfg(feature = "rustls")]
{
let proxies = Proxy::from_environment()
.into_reqwest_with(|name| match name {
"HTTP_PROXY" => Some("https://secure-proxy.local".to_owned()),
_ => None,
})
.unwrap();
assert_eq!(proxies.len(), 1);
}
}
#[cfg(not(feature = "rustls"))]
#[test]
fn it_https_proxy_urls_require_the_rustls_feature() {
let error = Proxy::from_environment()
.into_reqwest_with(|name| match name {
"HTTPS_PROXY" => Some("https://secure-proxy.local".to_owned()),
_ => None,
})
.unwrap_err();
assert_eq!(error.kind(), crate::ErrorKind::Proxy);
}
#[test]
fn it_environment_proxy_returns_invalid_proxy_errors() {
let error = Proxy::from_environment()
.into_reqwest_with(|name| match name {
"HTTP_PROXY" => Some("http://[invalid".to_owned()),
_ => None,
})
.unwrap_err();
assert_eq!(error.kind(), crate::ErrorKind::Proxy);
}
#[test]
fn it_environment_proxy_rejects_non_url_host_port_values() {
for value in ["proxy.local/path", "user:pass@proxy.local", "/proxy.local"] {
let error = Proxy::from_environment()
.into_reqwest_with(|name| match name {
"HTTP_PROXY" => Some(value.to_owned()),
_ => None,
})
.unwrap_err();
assert_eq!(error.kind(), crate::ErrorKind::Proxy);
assert_eq!(
error.to_string(),
"proxy error: environment proxy must be a URL or host and optional port"
);
}
}
#[test]
fn it_environment_proxy_allows_no_proxy_overrides() {
let proxies = Proxy::from_environment()
.no_proxy("override.local")
.into_reqwest_with(|name| match name {
"HTTP_PROXY" => Some("proxy.local:8080".to_owned()),
"NO_PROXY" => Some("env.local".to_owned()),
_ => None,
})
.unwrap();
assert_eq!(proxies.len(), 1);
}
#[test]
fn it_proxy_urls_may_omit_a_scheme() {
Proxy::all("proxy.local:8080").unwrap();
}
#[test]
fn it_proxy_urls_reject_resource_components() {
for value in [
"http://proxy.local/path",
"http://proxy.local/?query=value",
"http://proxy.local/#fragment",
] {
let error = Proxy::all(value).unwrap_err();
assert_eq!(error.kind(), crate::ErrorKind::Proxy, "accepted {value}");
}
}
#[cfg(not(feature = "socks"))]
#[test]
fn it_socks_proxy_schemes_require_the_socks_feature() {
let error = Proxy::all("socks5h://127.0.0.1:1080").unwrap_err();
assert_eq!(error.kind(), crate::ErrorKind::Proxy);
}
#[cfg(feature = "socks")]
#[test]
fn it_socks_proxy_schemes_are_accepted_when_enabled() {
assert!(Proxy::all("socks5h://127.0.0.1:1080").is_ok());
}
#[test]
fn it_debug_output_does_not_expose_proxy_values() {
let proxy = Proxy::all("http://user:secret@proxy.local:8080")
.unwrap()
.no_proxy("localhost");
let debug = format!("{proxy:?}");
assert!(debug.contains("explicit"));
assert!(debug.contains("has_no_proxy"));
assert!(!debug.contains("secret"));
assert!(!debug.contains("proxy.local"));
assert!(!debug.contains("localhost"));
}
#[tokio::test]
async fn it_proxy_credentials_use_proxy_authorization() {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
let server = capture_one_request(listener);
let client = crate::Client::builder()
.proxy(Proxy::http(format!("http://proxy-user:proxy-password@{address}")).unwrap())
.timeout(Duration::from_secs(5))
.build()
.unwrap();
let response = client.get("http://origin.invalid/resource").await.unwrap();
assert_eq!(response.status(), crate::StatusCode::NO_CONTENT);
let request = String::from_utf8(server.join().unwrap()).unwrap();
assert_eq!(
header_value(&request, "proxy-authorization"),
Some("Basic cHJveHktdXNlcjpwcm94eS1wYXNzd29yZA==")
);
assert!(header_value(&request, "authorization").is_none());
}
#[test]
fn it_environment_proxy_routes_and_honors_no_proxy() {
let proxy_listener = TcpListener::bind("127.0.0.1:0").unwrap();
let proxy_address = proxy_listener.local_addr().unwrap();
let direct_listener = TcpListener::bind("127.0.0.1:0").unwrap();
let direct_address = direct_listener.local_addr().unwrap();
let proxy_server = capture_one_request(proxy_listener);
let direct_server = capture_one_request(direct_listener);
let mut command = Command::new(std::env::current_exe().unwrap());
command
.arg("proxy::describe_proxy::it_routes_from_an_isolated_process")
.arg("--exact")
.arg("--ignored")
.env("TEZ_PROXY_TEST_CHILD", "1")
.env(
"TEZ_PROXY_TEST_DIRECT_URL",
format!("http://{direct_address}/direct"),
)
.env_remove("https_proxy")
.env_remove("HTTPS_PROXY")
.env_remove("REQUEST_METHOD");
#[cfg(not(windows))]
command
.env("http_proxy", format!("http://{proxy_address}"))
.env("no_proxy", "127.0.0.1")
.env_remove("HTTP_PROXY")
.env_remove("NO_PROXY");
#[cfg(windows)]
command
.env_remove("http_proxy")
.env("HTTP_PROXY", format!("http://{proxy_address}"))
.env_remove("no_proxy")
.env("NO_PROXY", "127.0.0.1");
let status = command.status().unwrap();
assert!(status.success(), "isolated proxy test process failed");
let proxy_request = first_request_line(proxy_server.join().unwrap());
let direct_request = first_request_line(direct_server.join().unwrap());
assert_eq!(
proxy_request,
"GET http://origin.invalid/via-environment HTTP/1.1"
);
assert_eq!(direct_request, "GET /direct HTTP/1.1");
}
#[test]
fn it_cgi_environment_never_routes_through_uppercase_http_proxy() {
let unused_proxy = TcpListener::bind("127.0.0.1:0").unwrap();
let proxy_address = unused_proxy.local_addr().unwrap();
let direct_listener = TcpListener::bind("127.0.0.1:0").unwrap();
let direct_address = direct_listener.local_addr().unwrap();
let direct_server = capture_one_request(direct_listener);
let mut command = Command::new(std::env::current_exe().unwrap());
command
.arg("proxy::describe_proxy::it_routes_from_an_isolated_process")
.arg("--exact")
.arg("--ignored")
.env("TEZ_PROXY_TEST_CGI_CHILD", "1")
.env(
"TEZ_PROXY_TEST_DIRECT_URL",
format!("http://{direct_address}/cgi-direct"),
)
.env_remove("http_proxy")
.env("HTTP_PROXY", format!("http://{proxy_address}"))
.env_remove("https_proxy")
.env_remove("HTTPS_PROXY")
.env_remove("no_proxy")
.env_remove("NO_PROXY")
.env("REQUEST_METHOD", "GET");
let status = command.status().unwrap();
assert!(status.success(), "isolated CGI proxy test process failed");
assert_eq!(
first_request_line(direct_server.join().unwrap()),
"GET /cgi-direct HTTP/1.1"
);
}
#[tokio::test]
#[ignore = "run by the isolated environment proxy specs"]
async fn it_routes_from_an_isolated_process() {
let routing_child = std::env::var_os("TEZ_PROXY_TEST_CHILD").is_some();
let cgi_child = std::env::var_os("TEZ_PROXY_TEST_CGI_CHILD").is_some();
if !routing_child && !cgi_child {
return;
}
let client = crate::Client::builder()
.proxy(Proxy::from_environment())
.timeout(Duration::from_secs(5))
.build()
.unwrap();
if cgi_child {
let direct = client
.get(std::env::var("TEZ_PROXY_TEST_DIRECT_URL").unwrap())
.await
.unwrap();
assert_eq!(direct.status(), crate::StatusCode::NO_CONTENT);
return;
}
let proxied = client
.get("http://origin.invalid/via-environment")
.await
.unwrap();
assert_eq!(proxied.status(), crate::StatusCode::NO_CONTENT);
let direct = client
.get(std::env::var("TEZ_PROXY_TEST_DIRECT_URL").unwrap())
.await
.unwrap();
assert_eq!(direct.status(), crate::StatusCode::NO_CONTENT);
}
fn capture_one_request(listener: TcpListener) -> thread::JoinHandle<Vec<u8>> {
thread::spawn(move || {
let (mut stream, _) = listener.accept().unwrap();
let mut request = Vec::new();
let mut buffer = [0_u8; 1024];
while !request.windows(4).any(|window| window == b"\r\n\r\n") {
let read = stream.read(&mut buffer).unwrap();
if read == 0 {
break;
}
request.extend_from_slice(&buffer[..read]);
}
stream
.write_all(
b"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
)
.unwrap();
request
})
}
fn first_request_line(request: Vec<u8>) -> String {
String::from_utf8(request)
.unwrap()
.lines()
.next()
.unwrap()
.trim_end_matches('\r')
.to_owned()
}
fn header_value<'a>(request: &'a str, name: &str) -> Option<&'a str> {
request.lines().find_map(|line| {
let (header, value) = line.split_once(':')?;
header.eq_ignore_ascii_case(name).then(|| value.trim())
})
}
#[test]
fn it_empty_no_proxy_rules_are_ignored() {
let proxy = Proxy::all("proxy.local:8080").unwrap().no_proxy(" ");
let debug = format!("{proxy:?}");
assert!(debug.contains("has_no_proxy: false"));
}
}