Skip to content

Commit c8e4f9c

Browse files
committed
feat(conn): support HTTP proxy basic authentication
1 parent d574011 commit c8e4f9c

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/client/legacy/client.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::time::Duration;
1414
use futures_util::future::{self, Either, FutureExt, TryFutureExt};
1515
use http::uri::Scheme;
1616
use hyper::client::conn::TrySendError as ConnTrySendError;
17-
use hyper::header::{HeaderValue, HOST};
17+
use hyper::header::{HeaderValue, HOST, PROXY_AUTHORIZATION};
1818
use hyper::rt::Timer;
1919
use hyper::{body::Body, Method, Request, Response, Uri, Version};
2020
use tracing::{debug, trace, warn};
@@ -315,6 +315,11 @@ where
315315
authority_form(req.uri_mut());
316316
} else if pooled.conn_info.is_proxied {
317317
absolute_form(req.uri_mut());
318+
if let Some(proxy_auth) = &pooled.conn_info.proxy_basic_auth {
319+
req.headers_mut()
320+
.entry(PROXY_AUTHORIZATION)
321+
.or_insert_with(|| proxy_auth.clone());
322+
}
318323
} else {
319324
origin_form(req.uri_mut());
320325
}

src/client/legacy/connect/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use std::{
7070
},
7171
};
7272

73-
use ::http::Extensions;
73+
use ::http::{Extensions, HeaderValue};
7474

7575
#[cfg(feature = "tokio")]
7676
pub use self::http::{HttpConnector, HttpInfo};
@@ -101,6 +101,7 @@ pub trait Connection {
101101
pub struct Connected {
102102
pub(super) alpn: Alpn,
103103
pub(super) is_proxied: bool,
104+
pub(crate) proxy_basic_auth: Option<HeaderValue>,
104105
pub(super) extra: Option<Extra>,
105106
pub(super) poisoned: PoisonPill,
106107
}
@@ -151,6 +152,7 @@ impl Connected {
151152
Connected {
152153
alpn: Alpn::None,
153154
is_proxied: false,
155+
proxy_basic_auth: None,
154156
extra: None,
155157
poisoned: PoisonPill::healthy(),
156158
}
@@ -184,6 +186,17 @@ impl Connected {
184186
self.is_proxied
185187
}
186188

189+
/// Set the Proxy-Authorization header value for HTTP Proxy authentication.
190+
pub fn proxy_basic_auth(mut self, auth: HeaderValue) -> Connected {
191+
self.proxy_basic_auth = Some(auth);
192+
self
193+
}
194+
195+
/// Get the Proxy-Authorization header value for HTTP Proxy authentication.
196+
pub fn get_proxy_basic_auth(&self) -> Option<&HeaderValue> {
197+
self.proxy_basic_auth.as_ref()
198+
}
199+
187200
/// Set extra connection information to be set in the extensions of every `Response`.
188201
pub fn extra<T: Clone + Send + Sync + 'static>(mut self, extra: T) -> Connected {
189202
if let Some(prev) = self.extra {
@@ -228,6 +241,7 @@ impl Connected {
228241
Connected {
229242
alpn: self.alpn,
230243
is_proxied: self.is_proxied,
244+
proxy_basic_auth: self.proxy_basic_auth.clone(),
231245
extra: self.extra.clone(),
232246
poisoned: self.poisoned.clone(),
233247
}

0 commit comments

Comments
 (0)