fix: None host_matcher applies the proxy to all hosts (#1054)

* fix: None `host_matcher` applies the proxy to all hosts
This commit is contained in:
lollerfirst
2025-09-09 11:40:38 +02:00
committed by GitHub
parent 62de0c9925
commit b1026fb9bb

View File

@@ -78,29 +78,27 @@ impl Transport for Async {
host_matcher: Option<&str>,
accept_invalid_certs: bool,
) -> Result<(), Error> {
let regex = host_matcher
.map(regex::Regex::new)
.transpose()
.map_err(|e| Error::Custom(e.to_string()))?;
self.inner = reqwest::Client::builder()
.proxy(reqwest::Proxy::custom(move |url| {
if let Some(matcher) = regex.as_ref() {
if let Some(host) = url.host_str() {
if matcher.is_match(host) {
return Some(proxy.clone());
}
}
}
None
}))
.danger_accept_invalid_certs(accept_invalid_certs) // Allow self-signed certs
let builder = reqwest::Client::builder().danger_accept_invalid_certs(accept_invalid_certs);
let builder = match host_matcher {
Some(pattern) => {
// When a matcher is provided, only apply the proxy to matched hosts
let regex = regex::Regex::new(pattern).map_err(|e| Error::Custom(e.to_string()))?;
builder.proxy(reqwest::Proxy::custom(move |url| {
url.host_str()
.filter(|host| regex.is_match(host))
.map(|_| proxy.clone())
}))
}
// Apply proxy to all requests when no matcher is provided
None => {
builder.proxy(reqwest::Proxy::all(proxy).map_err(|e| Error::Custom(e.to_string()))?)
}
};
self.inner = builder
.build()
.map_err(|e| {
Error::HttpError(
e.status().map(|status_code| status_code.as_u16()),
e.to_string(),
)
})?;
.map_err(|e| Error::HttpError(e.status().map(|s| s.as_u16()), e.to_string()))?;
Ok(())
}