Skip to content

Commit

Permalink
proxy: add support for proxy authentication with user-specified heade…
Browse files Browse the repository at this point in the history
…r values (#2053)
  • Loading branch information
Noah-Kennedy authored Dec 8, 2023
1 parent 4926d76 commit c09c5e6
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,25 @@ impl Proxy {
self
}

/// Set the `Proxy-Authorization` header to a specified value.
///
/// # Example
///
/// ```
/// # extern crate reqwest;
/// # use reqwest::header::*;
/// # fn run() -> Result<(), Box<std::error::Error>> {
/// let proxy = reqwest::Proxy::https("http://localhost:1234")?
/// .custom_http_auth(HeaderValue::from_static("justletmeinalreadyplease"));
/// # Ok(())
/// # }
/// # fn main() {}
/// ```
pub fn custom_http_auth(mut self, header_value: HeaderValue) -> Proxy {
self.intercept.set_custom_http_auth(header_value);
self
}

/// Adds a `No Proxy` exclusion list to this Proxy
///
/// # Example
Expand Down Expand Up @@ -619,6 +638,21 @@ impl ProxyScheme {
}
}

fn set_custom_http_auth(&mut self, header_value: HeaderValue) {
match *self {
ProxyScheme::Http { ref mut auth, .. } => {
*auth = Some(header_value);
}
ProxyScheme::Https { ref mut auth, .. } => {
*auth = Some(header_value);
}
#[cfg(feature = "socks")]
ProxyScheme::Socks5 { .. } => {
panic!("Socks is not supported for this method")
}
}
}

fn if_no_auth(mut self, update: &Option<HeaderValue>) -> Self {
match self {
ProxyScheme::Http { ref mut auth, .. } => {
Expand Down Expand Up @@ -742,6 +776,18 @@ impl Intercept {
}
}
}

fn set_custom_http_auth(&mut self, header_value: HeaderValue) {
match self {
Intercept::All(ref mut s)
| Intercept::Http(ref mut s)
| Intercept::Https(ref mut s) => s.set_custom_http_auth(header_value),
Intercept::System(_) => unimplemented!(),
Intercept::Custom(ref mut custom) => {
custom.auth = Some(header_value);
}
}
}
}

#[derive(Clone)]
Expand Down

0 comments on commit c09c5e6

Please sign in to comment.