add proxy authorization

This commit is contained in:
Johann Tuffe
2017-12-19 15:44:06 +08:00
parent 2f403175dc
commit 871b8076e4
3 changed files with 26 additions and 12 deletions

View File

@@ -7,7 +7,7 @@ use futures::{Async, Future, Poll};
use hyper::client::{Connect, FutureResponse, HttpConnector}; use hyper::client::{Connect, FutureResponse, HttpConnector};
use hyper::header::{Headers, Location, Referer, UserAgent, Accept, Encoding, use hyper::header::{Headers, Location, Referer, UserAgent, Accept, Encoding,
AcceptEncoding, Range, qitem}; AcceptEncoding, Range, qitem};
use hyper_proxy::Intercept; use hyper_proxy::Proxy as HyperProxy;
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use native_tls::{TlsConnector, TlsConnectorBuilder}; use native_tls::{TlsConnector, TlsConnectorBuilder};
use tokio_core::reactor::Handle; use tokio_core::reactor::Handle;
@@ -129,8 +129,7 @@ impl ClientBuilder {
if !config.hostname_verification { if !config.hostname_verification {
https_connector.danger_disable_hostname_verification(true); https_connector.danger_disable_hostname_verification(true);
} }
let intercept = config.proxy.inner.intercept().clone(); let mut connector = config.proxy.inner.clone().with_connector(https_connector);
let mut connector = config.proxy.inner.with_connector(https_connector);
let tls = try_!(config.tls.build()); let tls = try_!(config.tls.build());
connector.set_tls(Some(tls)); connector.set_tls(Some(tls));
@@ -142,7 +141,7 @@ impl ClientBuilder {
inner: Arc::new(ClientRef { inner: Arc::new(ClientRef {
gzip: config.gzip, gzip: config.gzip,
hyper: hyper_client, hyper: hyper_client,
intercept: intercept, proxy: config.proxy.inner,
headers: config.headers, headers: config.headers,
redirect_policy: config.redirect_policy, redirect_policy: config.redirect_policy,
referer: config.referer, referer: config.referer,
@@ -415,8 +414,9 @@ impl Client {
reusable reusable
}); });
if self.inner.intercept.matches(&uri) && uri.scheme() == Some("http") { if self.inner.proxy.intercept().matches(&uri) && uri.scheme() == Some("http") {
req.set_proxy(true); req.set_proxy(true);
req.headers_mut().extend(self.inner.proxy.headers().iter());
} }
let in_flight = self.inner.hyper.request(req); let in_flight = self.inner.hyper.request(req);
@@ -457,7 +457,7 @@ impl fmt::Debug for ClientBuilder {
struct ClientRef { struct ClientRef {
gzip: bool, gzip: bool,
headers: Headers, headers: Headers,
intercept: Intercept, proxy: HyperProxy<()>,
hyper: HyperClient, hyper: HyperClient,
redirect_policy: RedirectPolicy, redirect_policy: RedirectPolicy,
referer: bool, referer: bool,
@@ -556,8 +556,9 @@ impl Future for PendingRequest {
if let Some(Some(ref body)) = self.body { if let Some(Some(ref body)) = self.body {
req.set_body(body.clone()); req.set_body(body.clone());
} }
if self.client.intercept.matches(&uri) && uri.scheme() == Some("http") { if self.client.proxy.intercept().matches(&uri) && uri.scheme() == Some("http") {
req.set_proxy(true); req.set_proxy(true);
req.headers_mut().extend(self.client.proxy.headers().iter());
} }
self.in_flight = self.client.hyper.request(req); self.in_flight = self.client.hyper.request(req);
continue; continue;

View File

@@ -1,4 +1,6 @@
use std::any::Any;
use hyper::Uri; use hyper::Uri;
use hyper::header::{Scheme};
use {IntoUrl}; use {IntoUrl};
use hyper_proxy::Intercept; use hyper_proxy::Intercept;
use hyper_proxy::Proxy as HyperProxy; use hyper_proxy::Proxy as HyperProxy;
@@ -14,11 +16,13 @@ use hyper_proxy::Proxy as HyperProxy;
/// ///
/// ```rust /// ```rust
/// # fn run() -> Result<(), Box<::std::error::Error>> { /// # fn run() -> Result<(), Box<::std::error::Error>> {
/// use reqwest::header::Basic;
///
/// let mut proxy = reqwest::Proxy::http("https://secure.example")?; /// let mut proxy = reqwest::Proxy::http("https://secure.example")?;
/// // proxy.set_authorization(Basic { /// proxy.set_authorization(Basic {
/// // username: "John Doe".into(), /// username: "John Doe".into(),
/// // password: Some("Agent1234".into()), /// password: Some("Agent1234".into()),
/// // }); /// });
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
@@ -111,6 +115,11 @@ impl Proxy {
Proxy::new(Intercept::Custom(fun.into()), url) Proxy::new(Intercept::Custom(fun.into()), url)
} }
/// Set proxy authorization
pub fn set_authorization<S: Scheme + Any>(&mut self, scheme: S) {
self.inner.set_authorization(scheme);
}
/* /*
pub fn unix<P: AsRef<Path>(path: P) -> Proxy { pub fn unix<P: AsRef<Path>(path: P) -> Proxy {

View File

@@ -1,5 +1,7 @@
extern crate reqwest; extern crate reqwest;
use reqwest::header::Bearer;
#[macro_use] #[macro_use]
mod support; mod support;
@@ -12,6 +14,7 @@ fn test_http_proxy() {
User-Agent: $USERAGENT\r\n\ User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\ Accept: */*\r\n\
Accept-Encoding: gzip\r\n\ Accept-Encoding: gzip\r\n\
Authorization: Bearer MY_SECRET_TOKEN\r\n\
\r\n\ \r\n\
", ",
response: b"\ response: b"\
@@ -23,7 +26,8 @@ fn test_http_proxy() {
}; };
let proxy_uri = format!("http://{}", server.addr()); let proxy_uri = format!("http://{}", server.addr());
let proxy = reqwest::Proxy::http(&proxy_uri).unwrap(); let mut proxy = reqwest::Proxy::http(&proxy_uri).unwrap();
proxy.set_authorization(Bearer { token: "MY_SECRET_TOKEN".to_string() });
let url = "http://hyper.rs/prox"; let url = "http://hyper.rs/prox";
let res = reqwest::Client::builder() let res = reqwest::Client::builder()