From 8b1467243e6be201cd65fa1071e9cb093d3f994c Mon Sep 17 00:00:00 2001 From: Noah Kennedy Date: Fri, 1 Dec 2023 17:57:52 -0600 Subject: [PATCH] proxy: add support for proxy authentication with user-specified header values --- src/proxy.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/proxy.rs b/src/proxy.rs index 6e1bfcc73..53d97beeb 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -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> { + /// 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 @@ -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 { ref mut auth, .. } => { + panic!("Socks is not supported for this method") + } + } + } + fn if_no_auth(mut self, update: &Option) -> Self { match self { ProxyScheme::Http { ref mut auth, .. } => { @@ -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)]