Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy: add support for proxy authentication with user-specified header values #2053

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Changes from 1 commit
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
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 { ref mut auth, .. } => {
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
Loading