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

View File

@@ -1,4 +1,6 @@
use std::any::Any;
use hyper::Uri;
use hyper::header::{Scheme};
use {IntoUrl};
use hyper_proxy::Intercept;
use hyper_proxy::Proxy as HyperProxy;
@@ -14,11 +16,13 @@ use hyper_proxy::Proxy as HyperProxy;
///
/// ```rust
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// use reqwest::header::Basic;
///
/// let mut proxy = reqwest::Proxy::http("https://secure.example")?;
/// // proxy.set_authorization(Basic {
/// // username: "John Doe".into(),
/// // password: Some("Agent1234".into()),
/// // });
/// proxy.set_authorization(Basic {
/// username: "John Doe".into(),
/// password: Some("Agent1234".into()),
/// });
/// # Ok(())
/// # }
/// ```
@@ -111,6 +115,11 @@ impl Proxy {
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 {

View File

@@ -1,5 +1,7 @@
extern crate reqwest;
use reqwest::header::Bearer;
#[macro_use]
mod support;
@@ -12,6 +14,7 @@ fn test_http_proxy() {
User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\
Accept-Encoding: gzip\r\n\
Authorization: Bearer MY_SECRET_TOKEN\r\n\
\r\n\
",
response: b"\
@@ -23,7 +26,8 @@ fn test_http_proxy() {
};
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 res = reqwest::Client::builder()