From 871b8076e44fa047dfa68e7b37a166f06354f834 Mon Sep 17 00:00:00 2001 From: Johann Tuffe Date: Tue, 19 Dec 2017 15:44:06 +0800 Subject: [PATCH] add proxy authorization --- src/async_impl/client.rs | 15 ++++++++------- src/proxy.rs | 17 +++++++++++++---- tests/proxy.rs | 6 +++++- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/async_impl/client.rs b/src/async_impl/client.rs index b99b169..7c55802 100644 --- a/src/async_impl/client.rs +++ b/src/async_impl/client.rs @@ -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; diff --git a/src/proxy.rs b/src/proxy.rs index 4eee7f2..c83b7c0 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -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(&mut self, scheme: S) { + self.inner.set_authorization(scheme); + } + /* pub fn unix(path: P) -> Proxy { diff --git a/tests/proxy.rs b/tests/proxy.rs index 49c8e5c..2d515c0 100644 --- a/tests/proxy.rs +++ b/tests/proxy.rs @@ -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()