Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 64 additions & 56 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,60 @@ impl Default for Accepts {
#[derive(Clone)]
struct HyperService {
hyper: HyperClient,
proxies: Arc<Vec<ProxyMatcher>>,
proxies_maybe_http_auth: bool,
proxies_maybe_http_custom_headers: bool,
}

impl HyperService {
fn proxy_auth(&self, dst: &Uri, headers: &mut HeaderMap) {
if !self.proxies_maybe_http_auth {
return;
}

// Only set the header here if the destination scheme is 'http',
// since otherwise, the header will be included in the CONNECT tunnel
// request instead.
if dst.scheme() != Some(&Scheme::HTTP) {
return;
}

if headers.contains_key(PROXY_AUTHORIZATION) {
return;
}

for proxy in self.proxies.iter() {
if let Some(proxy) = proxy.intercept(dst) {
if let Some(header) = proxy.http_non_tunnel_basic_auth() {
headers.insert(PROXY_AUTHORIZATION, header);
}
// Use only the first matching proxy, as the connector does.
break;
}
}
}

fn proxy_custom_headers(&self, dst: &Uri, headers: &mut HeaderMap) {
if !self.proxies_maybe_http_custom_headers {
return;
}

if dst.scheme() != Some(&Scheme::HTTP) {
return;
}

for proxy in self.proxies.iter() {
if let Some(proxy) = proxy.intercept(dst) {
if let Some(iter) = proxy.http_non_tunnel_custom_headers() {
iter.iter().for_each(|(key, value)| {
headers.insert(key, value.clone());
});
}
// Use only the first matching proxy, as the connector does.
break;
}
}
}
}

impl Service<hyper::Request<crate::async_impl::body::Body>> for HyperService {
Expand All @@ -151,6 +205,13 @@ impl Service<hyper::Request<crate::async_impl::body::Body>> for HyperService {
}

fn call(&mut self, req: hyper::Request<crate::async_impl::body::Body>) -> Self::Future {
// Applied below the redirect layer, so that each hop carries the
// headers of the proxy that intercepts its own URI.
let (mut parts, body) = req.into_parts();
self.proxy_auth(&parts.uri, &mut parts.headers);
self.proxy_custom_headers(&parts.uri, &mut parts.headers);
let req = hyper::Request::from_parts(parts, body);

let clone = self.hyper.clone();
let mut inner = std::mem::replace(&mut self.hyper, clone);
Box::pin(async move { inner.call(req).await.map_err(crate::error::request) })
Expand Down Expand Up @@ -1031,6 +1092,9 @@ impl ClientBuilder {
let hyper_client = builder.build(connector_builder.build(config.connector_layers));
let hyper_service = HyperService {
hyper: hyper_client,
proxies: proxies.clone(),
proxies_maybe_http_auth,
proxies_maybe_http_custom_headers,
};

let redirect_policy = {
Expand Down Expand Up @@ -1115,8 +1179,6 @@ impl ClientBuilder {
total_timeout: RequestConfig::new(config.timeout),
hyper,
proxies,
proxies_maybe_http_auth,
proxies_maybe_http_custom_headers,
https_only: config.https_only,
redirect_policy_desc,
}),
Expand Down Expand Up @@ -2681,9 +2743,6 @@ impl Client {

let body = body.unwrap_or_else(Body::empty);

self.proxy_auth(&uri, &mut headers);
self.proxy_custom_headers(&uri, &mut headers);

let builder = hyper::Request::builder()
.method(method.clone())
.uri(uri)
Expand Down Expand Up @@ -2733,55 +2792,6 @@ impl Client {
})),
}
}

fn proxy_auth(&self, dst: &Uri, headers: &mut HeaderMap) {
if !self.inner.proxies_maybe_http_auth {
return;
}

// Only set the header here if the destination scheme is 'http',
// since otherwise, the header will be included in the CONNECT tunnel
// request instead.
if dst.scheme() != Some(&Scheme::HTTP) {
return;
}

if headers.contains_key(PROXY_AUTHORIZATION) {
return;
}

for proxy in self.inner.proxies.iter() {
if let Some(proxy) = proxy.intercept(dst) {
if let Some(header) = proxy.http_non_tunnel_basic_auth() {
headers.insert(PROXY_AUTHORIZATION, header);
}
// Use only the first matching proxy, as the connector does.
break;
}
}
}

fn proxy_custom_headers(&self, dst: &Uri, headers: &mut HeaderMap) {
if !self.inner.proxies_maybe_http_custom_headers {
return;
}

if dst.scheme() != Some(&Scheme::HTTP) {
return;
}

for proxy in self.inner.proxies.iter() {
if let Some(proxy) = proxy.intercept(dst) {
if let Some(iter) = proxy.http_non_tunnel_custom_headers() {
iter.iter().for_each(|(key, value)| {
headers.insert(key, value.clone());
});
}
// Use only the first matching proxy, as the connector does.
break;
}
}
}
}

impl fmt::Debug for Client {
Expand Down Expand Up @@ -3009,8 +3019,6 @@ struct ClientRef {
total_timeout: RequestConfig<TotalTimeout>,
read_timeout: Option<Duration>,
proxies: Arc<Vec<ProxyMatcher>>,
proxies_maybe_http_auth: bool,
proxies_maybe_http_custom_headers: bool,
https_only: bool,
redirect_policy_desc: Option<String>,
}
Expand Down
38 changes: 38 additions & 0 deletions tests/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,44 @@ async fn http_proxy_basic_auth_parsed() {
assert_eq!(res.status(), reqwest::StatusCode::OK);
}

#[tokio::test]
async fn http_proxy_basic_auth_on_cross_host_redirect() {
let url = "http://hyper.rs.local/redirect";
let redirected = "http://other.rs.local/dst";
let server = server::http(move |req| {
let mut res = http::Response::default();
if req.headers().get("proxy-authorization")
!= Some(&http::HeaderValue::from_static(
"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
))
{
*res.status_mut() = http::StatusCode::PROXY_AUTHENTICATION_REQUIRED;
} else if req.uri() == url {
*res.status_mut() = http::StatusCode::FOUND;
res.headers_mut()
.insert("location", http::HeaderValue::from_static(redirected));
} else {
assert_eq!(req.uri(), redirected);
}

async { res }
});

let proxy = format!("http://Aladdin:open sesame@{}", server.addr());

let res = reqwest::Client::builder()
.proxy(reqwest::Proxy::http(&proxy).unwrap())
.build()
.unwrap()
.get(url)
.send()
.await
.unwrap();

assert_eq!(res.url().as_str(), redirected);
assert_eq!(res.status(), reqwest::StatusCode::OK);
}

#[tokio::test]
async fn system_http_proxy_basic_auth_parsed() {
let url = "http://hyper.rs.local/prox";
Expand Down
Loading