Merge pull request #126 from tomprince/rustfmt-no-controversy
Apply some uncontoversial rustfmt changes.
This commit is contained in:
		| @@ -11,6 +11,7 @@ matrix: | |||||||
|         - rust: nightly |         - rust: nightly | ||||||
|  |  | ||||||
| sudo: false | sudo: false | ||||||
|  | dist: trusty | ||||||
|  |  | ||||||
| cache: | cache: | ||||||
|   apt: true |   apt: true | ||||||
|   | |||||||
| @@ -1,7 +1,9 @@ | |||||||
| //! `cargo run --example response_json` | //! `cargo run --example response_json` | ||||||
| extern crate reqwest; | extern crate reqwest; | ||||||
| #[macro_use] extern crate serde_derive; | #[macro_use] | ||||||
| #[macro_use] extern crate error_chain; | extern crate serde_derive; | ||||||
|  | #[macro_use] | ||||||
|  | extern crate error_chain; | ||||||
|  |  | ||||||
| error_chain! { | error_chain! { | ||||||
|     foreign_links { |     foreign_links { | ||||||
|   | |||||||
| @@ -1,7 +1,8 @@ | |||||||
| //! `cargo run --example simple` | //! `cargo run --example simple` | ||||||
| extern crate reqwest; | extern crate reqwest; | ||||||
| extern crate env_logger; | extern crate env_logger; | ||||||
| #[macro_use] extern crate error_chain; | #[macro_use] | ||||||
|  | extern crate error_chain; | ||||||
|  |  | ||||||
| error_chain! { | error_chain! { | ||||||
|     foreign_links { |     foreign_links { | ||||||
| @@ -11,8 +12,7 @@ error_chain! { | |||||||
| } | } | ||||||
|  |  | ||||||
| fn run() -> Result<()> { | fn run() -> Result<()> { | ||||||
|     env_logger::init() |     env_logger::init().expect("Failed to initialize logger"); | ||||||
|         .expect("Failed to initialize logger"); |  | ||||||
|  |  | ||||||
|     println!("GET https://www.rust-lang.org"); |     println!("GET https://www.rust-lang.org"); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -49,13 +49,10 @@ impl Body { | |||||||
| pub fn read_to_string(mut body: Body) -> ::std::io::Result<String> { | pub fn read_to_string(mut body: Body) -> ::std::io::Result<String> { | ||||||
|     let mut s = String::new(); |     let mut s = String::new(); | ||||||
|     match body.reader { |     match body.reader { | ||||||
|         Kind::Reader(ref mut reader, _) => { |             Kind::Reader(ref mut reader, _) => reader.read_to_string(&mut s), | ||||||
|             reader.read_to_string(&mut s) |             Kind::Bytes(ref mut bytes) => (&**bytes).read_to_string(&mut s), | ||||||
|         } |         } | ||||||
|         Kind::Bytes(ref mut bytes) => { |         .map(|_| s) | ||||||
|             (&**bytes).read_to_string(&mut s) |  | ||||||
|         } |  | ||||||
|     }.map(|_| s) |  | ||||||
| } | } | ||||||
|  |  | ||||||
| enum Kind { | enum Kind { | ||||||
|   | |||||||
| @@ -5,11 +5,11 @@ use std::time::Duration; | |||||||
|  |  | ||||||
| use hyper::client::IntoUrl; | use hyper::client::IntoUrl; | ||||||
| use hyper::header::{Headers, ContentType, Location, Referer, UserAgent, Accept, Encoding, | use hyper::header::{Headers, ContentType, Location, Referer, UserAgent, Accept, Encoding, | ||||||
|     AcceptEncoding, Range, qitem}; |                     AcceptEncoding, Range, qitem}; | ||||||
| use hyper::method::Method; | use hyper::method::Method; | ||||||
| use hyper::status::StatusCode; | use hyper::status::StatusCode; | ||||||
| use hyper::version::HttpVersion; | use hyper::version::HttpVersion; | ||||||
| use hyper::{Url}; | use hyper::Url; | ||||||
|  |  | ||||||
| use hyper_native_tls::{NativeTlsClient, native_tls}; | use hyper_native_tls::{NativeTlsClient, native_tls}; | ||||||
|  |  | ||||||
| @@ -17,11 +17,12 @@ use serde::Serialize; | |||||||
| use serde_json; | use serde_json; | ||||||
| use serde_urlencoded; | use serde_urlencoded; | ||||||
|  |  | ||||||
| use ::body::{self, Body}; | use body::{self, Body}; | ||||||
| use ::redirect::{self, RedirectPolicy, check_redirect, remove_sensitive_headers}; | use redirect::{self, RedirectPolicy, check_redirect, remove_sensitive_headers}; | ||||||
| use ::response::Response; | use response::Response; | ||||||
|  |  | ||||||
| static DEFAULT_USER_AGENT: &'static str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); | static DEFAULT_USER_AGENT: &'static str = | ||||||
|  |     concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); | ||||||
|  |  | ||||||
| /// A `Client` to make Requests with. | /// A `Client` to make Requests with. | ||||||
| /// | /// | ||||||
| @@ -76,7 +77,8 @@ impl Certificate { | |||||||
|     pub fn from_der(der: &[u8]) -> ::Result<Certificate> { |     pub fn from_der(der: &[u8]) -> ::Result<Certificate> { | ||||||
|         let inner = try_!( |         let inner = try_!( | ||||||
|             native_tls::Certificate::from_der(der) |             native_tls::Certificate::from_der(der) | ||||||
|                 .map_err(|e| ::hyper::Error::Ssl(Box::new(e)))); |                 .map_err(|e| ::hyper::Error::Ssl(Box::new(e))) | ||||||
|  |         ); | ||||||
|         Ok(Certificate(inner)) |         Ok(Certificate(inner)) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -121,7 +123,8 @@ impl ClientBuilder { | |||||||
|     pub fn new() -> ::Result<ClientBuilder> { |     pub fn new() -> ::Result<ClientBuilder> { | ||||||
|         let tls_connector_builder = try_!( |         let tls_connector_builder = try_!( | ||||||
|             native_tls::TlsConnector::builder() |             native_tls::TlsConnector::builder() | ||||||
|                 .map_err(|e| ::hyper::Error::Ssl(Box::new(e)))); |                 .map_err(|e| ::hyper::Error::Ssl(Box::new(e))) | ||||||
|  |         ); | ||||||
|         Ok(ClientBuilder { |         Ok(ClientBuilder { | ||||||
|             config: Some(Config { |             config: Some(Config { | ||||||
|                 hostname_verification: true, |                 hostname_verification: true, | ||||||
| @@ -140,7 +143,11 @@ impl ClientBuilder { | |||||||
|         let config = self.take_config(); |         let config = self.take_config(); | ||||||
|  |  | ||||||
|         let tls_connector = try_!( |         let tls_connector = try_!( | ||||||
|             config.tls.build().map_err(|e| ::hyper::Error::Ssl(Box::new(e)))); |             config | ||||||
|  |                 .tls | ||||||
|  |                 .build() | ||||||
|  |                 .map_err(|e| ::hyper::Error::Ssl(Box::new(e))) | ||||||
|  |         ); | ||||||
|         let mut tls_client = NativeTlsClient::from(tls_connector); |         let mut tls_client = NativeTlsClient::from(tls_connector); | ||||||
|         if !config.hostname_verification { |         if !config.hostname_verification { | ||||||
|             tls_client.danger_disable_hostname_verification(true); |             tls_client.danger_disable_hostname_verification(true); | ||||||
| @@ -149,7 +156,9 @@ impl ClientBuilder { | |||||||
|         let mut hyper_client = ::hyper::Client::with_connector( |         let mut hyper_client = ::hyper::Client::with_connector( | ||||||
|             ::hyper::client::Pool::with_connector( |             ::hyper::client::Pool::with_connector( | ||||||
|                 Default::default(), |                 Default::default(), | ||||||
|                 ::hyper::net::HttpsConnector::new(tls_client))); |                 ::hyper::net::HttpsConnector::new(tls_client), | ||||||
|  |             ) | ||||||
|  |         ); | ||||||
|  |  | ||||||
|         hyper_client.set_redirect_policy(::hyper::client::RedirectPolicy::FollowNone); |         hyper_client.set_redirect_policy(::hyper::client::RedirectPolicy::FollowNone); | ||||||
|  |  | ||||||
| @@ -168,8 +177,12 @@ impl ClientBuilder { | |||||||
|     /// This can be used to connect to a server that has a self-signed |     /// This can be used to connect to a server that has a self-signed | ||||||
|     /// certificate for example. |     /// certificate for example. | ||||||
|     pub fn add_root_certificate(&mut self, cert: Certificate) -> ::Result<&mut ClientBuilder> { |     pub fn add_root_certificate(&mut self, cert: Certificate) -> ::Result<&mut ClientBuilder> { | ||||||
|         try_!(self.config_mut().tls.add_root_certificate(cert.0) |         try_!( | ||||||
|                 .map_err(|e| ::hyper::Error::Ssl(Box::new(e)))); |             self.config_mut() | ||||||
|  |                 .tls | ||||||
|  |                 .add_root_certificate(cert.0) | ||||||
|  |                 .map_err(|e| ::hyper::Error::Ssl(Box::new(e))) | ||||||
|  |         ); | ||||||
|         Ok(self) |         Ok(self) | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -192,11 +205,15 @@ impl ClientBuilder { | |||||||
|  |  | ||||||
|     // private |     // private | ||||||
|     fn config_mut(&mut self) -> &mut Config { |     fn config_mut(&mut self) -> &mut Config { | ||||||
|         self.config.as_mut().expect("ClientBuilder cannot be reused after building a Client") |         self.config | ||||||
|  |             .as_mut() | ||||||
|  |             .expect("ClientBuilder cannot be reused after building a Client") | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn take_config(&mut self) -> Config { |     fn take_config(&mut self) -> Config { | ||||||
|         self.config.take().expect("ClientBuilder cannot be reused after building a Client") |         self.config | ||||||
|  |             .take() | ||||||
|  |             .expect("ClientBuilder cannot be reused after building a Client") | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -327,7 +344,10 @@ impl RequestBuilder { | |||||||
|     /// # Ok(()) |     /// # Ok(()) | ||||||
|     /// # } |     /// # } | ||||||
|     /// ``` |     /// ``` | ||||||
|     pub fn header<H: ::header::Header + ::header::HeaderFormat>(mut self, header: H) -> RequestBuilder { |     pub fn header<H>(mut self, header: H) -> RequestBuilder | ||||||
|  |     where | ||||||
|  |         H: ::header::Header + ::header::HeaderFormat, | ||||||
|  |     { | ||||||
|         self.headers.set(header); |         self.headers.set(header); | ||||||
|         self |         self | ||||||
|     } |     } | ||||||
| @@ -341,7 +361,9 @@ impl RequestBuilder { | |||||||
|  |  | ||||||
|     /// Enable HTTP basic authentication. |     /// Enable HTTP basic authentication. | ||||||
|     pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder |     pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder | ||||||
|             where U: Into<String>, P: Into<String> |     where | ||||||
|  |         U: Into<String>, | ||||||
|  |         P: Into<String>, | ||||||
|     { |     { | ||||||
|         self.header(::header::Authorization(::header::Basic{ |         self.header(::header::Authorization(::header::Basic{ | ||||||
|             username: username.into(), |             username: username.into(), | ||||||
| @@ -456,13 +478,13 @@ impl RequestBuilder { | |||||||
|                 StatusCode::SeeOther => { |                 StatusCode::SeeOther => { | ||||||
|                     body = None; |                     body = None; | ||||||
|                     match method { |                     match method { | ||||||
|                         Method::Get | Method::Head => {}, |                         Method::Get | Method::Head => {} | ||||||
|                         _ => { |                         _ => { | ||||||
|                             method = Method::Get; |                             method = Method::Get; | ||||||
|                         } |                         } | ||||||
|                     } |                     } | ||||||
|                     true |                     true | ||||||
|                 }, |                 } | ||||||
|                 StatusCode::TemporaryRedirect | |                 StatusCode::TemporaryRedirect | | ||||||
|                 StatusCode::PermanentRedirect => { |                 StatusCode::PermanentRedirect => { | ||||||
|                     if let Some(ref body) = body { |                     if let Some(ref body) = body { | ||||||
| @@ -470,7 +492,7 @@ impl RequestBuilder { | |||||||
|                     } else { |                     } else { | ||||||
|                         true |                         true | ||||||
|                     } |                     } | ||||||
|                 }, |                 } | ||||||
|                 _ => false, |                 _ => false, | ||||||
|             }; |             }; | ||||||
|  |  | ||||||
| @@ -492,33 +514,34 @@ impl RequestBuilder { | |||||||
|                             } |                             } | ||||||
|                         } |                         } | ||||||
|                         urls.push(url); |                         urls.push(url); | ||||||
|                         let action = check_redirect(&client.redirect_policy.lock().unwrap(), &loc, &urls); |                         let action = | ||||||
|  |                             check_redirect(&client.redirect_policy.lock().unwrap(), &loc, &urls); | ||||||
|  |  | ||||||
|                         match action { |                         match action { | ||||||
|                             redirect::Action::Follow => loc, |                             redirect::Action::Follow => loc, | ||||||
|                             redirect::Action::Stop => { |                             redirect::Action::Stop => { | ||||||
|                                 debug!("redirect_policy disallowed redirection to '{}'", loc); |                                 debug!("redirect_policy disallowed redirection to '{}'", loc); | ||||||
|                                 return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed))); |                                 return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed))); | ||||||
|                             }, |                             } | ||||||
|                             redirect::Action::LoopDetected => { |                             redirect::Action::LoopDetected => { | ||||||
|                                 return Err(::error::loop_detected(res.url.clone())); |                                 return Err(::error::loop_detected(res.url.clone())); | ||||||
|                             }, |                             } | ||||||
|                             redirect::Action::TooManyRedirects => { |                             redirect::Action::TooManyRedirects => { | ||||||
|                                 return Err(::error::too_many_redirects(res.url.clone())); |                                 return Err(::error::too_many_redirects(res.url.clone())); | ||||||
|                             } |                             } | ||||||
|                         } |                         } | ||||||
|                     }, |                     } | ||||||
|                     Err(e) => { |                     Err(e) => { | ||||||
|                         debug!("Location header had invalid URI: {:?}", e); |                         debug!("Location header had invalid URI: {:?}", e); | ||||||
|  |  | ||||||
|                         return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed))) |                         return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed))); | ||||||
|                     } |                     } | ||||||
|                 }; |                 }; | ||||||
|  |  | ||||||
|                 remove_sensitive_headers(&mut headers, &url, &urls); |                 remove_sensitive_headers(&mut headers, &url, &urls); | ||||||
|                 debug!("redirecting to {:?} '{}'", method, url); |                 debug!("redirecting to {:?} '{}'", method, url); | ||||||
|             } else { |             } else { | ||||||
|                 return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed))) |                 return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed))); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -549,7 +572,7 @@ fn make_referer(next: &Url, previous: &Url) -> Option<Referer> { | |||||||
| #[cfg(test)] | #[cfg(test)] | ||||||
| mod tests { | mod tests { | ||||||
|     use super::*; |     use super::*; | ||||||
|     use ::body; |     use body; | ||||||
|     use hyper::method::Method; |     use hyper::method::Method; | ||||||
|     use hyper::Url; |     use hyper::Url; | ||||||
|     use hyper::header::{Host, Headers, ContentType}; |     use hyper::header::{Host, Headers, ContentType}; | ||||||
| @@ -683,7 +706,8 @@ mod tests { | |||||||
|         r = r.form(&form_data); |         r = r.form(&form_data); | ||||||
|  |  | ||||||
|         // Make sure the content type was set |         // Make sure the content type was set | ||||||
|         assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::form_url_encoded())); |         assert_eq!(r.headers.get::<ContentType>(), | ||||||
|  |                    Some(&ContentType::form_url_encoded())); | ||||||
|  |  | ||||||
|         let buf = body::read_to_string(r.body.unwrap().unwrap()).unwrap(); |         let buf = body::read_to_string(r.body.unwrap().unwrap()).unwrap(); | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										20
									
								
								src/error.rs
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								src/error.rs
									
									
									
									
									
								
							| @@ -1,7 +1,7 @@ | |||||||
| use std::error::Error as StdError; | use std::error::Error as StdError; | ||||||
| use std::fmt; | use std::fmt; | ||||||
|  |  | ||||||
| use ::Url; | use Url; | ||||||
|  |  | ||||||
| /// The Errors that may occur when processing a `Request`. | /// The Errors that may occur when processing a `Request`. | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| @@ -153,10 +153,12 @@ impl From<InternalFrom<Error>> for Error { | |||||||
| } | } | ||||||
|  |  | ||||||
| impl<T> From<InternalFrom<T>> for Error | impl<T> From<InternalFrom<T>> for Error | ||||||
| where T: Into<Kind> { | where | ||||||
|  |     T: Into<Kind>, | ||||||
|  | { | ||||||
|     #[inline] |     #[inline] | ||||||
|     fn from(other: InternalFrom<T>) -> Error { |     fn from(other: InternalFrom<T>) -> Error { | ||||||
|          Error { |         Error { | ||||||
|             kind: other.0.into(), |             kind: other.0.into(), | ||||||
|             url: other.1, |             url: other.1, | ||||||
|         } |         } | ||||||
| @@ -165,7 +167,9 @@ where T: Into<Kind> { | |||||||
|  |  | ||||||
| #[inline] | #[inline] | ||||||
| pub fn from<T>(err: T) -> Error | pub fn from<T>(err: T) -> Error | ||||||
| where T: Into<Kind> { | where | ||||||
|  |     T: Into<Kind>, | ||||||
|  | { | ||||||
|     InternalFrom(err, None).into() |     InternalFrom(err, None).into() | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -188,11 +192,13 @@ pub fn too_many_redirects(url: Url) -> Error { | |||||||
| #[test] | #[test] | ||||||
| fn test_error_get_ref_downcasts() { | fn test_error_get_ref_downcasts() { | ||||||
|     let err: Error = from(::hyper::Error::Status); |     let err: Error = from(::hyper::Error::Status); | ||||||
|     let cause = err.get_ref().unwrap() |     let cause = err.get_ref() | ||||||
|         .downcast_ref::<::hyper::Error>().unwrap(); |         .unwrap() | ||||||
|  |         .downcast_ref::<::hyper::Error>() | ||||||
|  |         .unwrap(); | ||||||
|  |  | ||||||
|     match cause { |     match cause { | ||||||
|         &::hyper::Error::Status => (), |         &::hyper::Error::Status => (), | ||||||
|         _ => panic!("unexpected downcast: {:?}", cause) |         _ => panic!("unexpected downcast: {:?}", cause), | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -115,7 +115,8 @@ | |||||||
| //! [serde]: http://serde.rs | //! [serde]: http://serde.rs | ||||||
| extern crate hyper; | extern crate hyper; | ||||||
|  |  | ||||||
| #[macro_use] extern crate log; | #[macro_use] | ||||||
|  | extern crate log; | ||||||
| extern crate libflate; | extern crate libflate; | ||||||
| extern crate hyper_native_tls; | extern crate hyper_native_tls; | ||||||
| extern crate serde; | extern crate serde; | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ use std::fmt; | |||||||
|  |  | ||||||
| use hyper::header::{Headers, Authorization, Cookie}; | use hyper::header::{Headers, Authorization, Cookie}; | ||||||
|  |  | ||||||
| use ::Url; | use Url; | ||||||
|  |  | ||||||
| /// A type that controls the policy on how to handle the following of redirects. | /// A type that controls the policy on how to handle the following of redirects. | ||||||
| /// | /// | ||||||
| @@ -77,7 +77,9 @@ impl RedirectPolicy { | |||||||
|     /// # } |     /// # } | ||||||
|     /// ``` |     /// ``` | ||||||
|     pub fn custom<T>(policy: T) -> RedirectPolicy |     pub fn custom<T>(policy: T) -> RedirectPolicy | ||||||
|     where T: Fn(RedirectAttempt) -> RedirectAction + Send + Sync + 'static { |     where | ||||||
|  |         T: Fn(RedirectAttempt) -> RedirectAction + Send + Sync + 'static, | ||||||
|  |     { | ||||||
|         RedirectPolicy { |         RedirectPolicy { | ||||||
|             inner: Policy::Custom(Box::new(policy)), |             inner: Policy::Custom(Box::new(policy)), | ||||||
|         } |         } | ||||||
| @@ -94,7 +96,7 @@ impl RedirectPolicy { | |||||||
|                 } else { |                 } else { | ||||||
|                     attempt.follow() |                     attempt.follow() | ||||||
|                 } |                 } | ||||||
|             }, |             } | ||||||
|             Policy::None => attempt.stop(), |             Policy::None => attempt.stop(), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -187,8 +189,8 @@ pub fn check_redirect(policy: &RedirectPolicy, next: &Url, previous: &[Url]) -> | |||||||
|  |  | ||||||
| pub fn remove_sensitive_headers(headers: &mut Headers, next: &Url, previous: &[Url]) { | pub fn remove_sensitive_headers(headers: &mut Headers, next: &Url, previous: &[Url]) { | ||||||
|     if let Some(previous) = previous.last() { |     if let Some(previous) = previous.last() { | ||||||
|         let cross_host = next.host_str() != previous.host_str() |         let cross_host = next.host_str() != previous.host_str() || | ||||||
|             || next.port_or_known_default() != previous.port_or_known_default(); |                          next.port_or_known_default() != previous.port_or_known_default(); | ||||||
|         if cross_host { |         if cross_host { | ||||||
|             headers.remove::<Authorization<String>>(); |             headers.remove::<Authorization<String>>(); | ||||||
|             headers.remove::<Cookie>(); |             headers.remove::<Cookie>(); | ||||||
| @@ -229,7 +231,8 @@ fn test_redirect_policy_limit() { | |||||||
|  |  | ||||||
|     previous.push(Url::parse("http://a.b.d/e/33").unwrap()); |     previous.push(Url::parse("http://a.b.d/e/33").unwrap()); | ||||||
|  |  | ||||||
|     assert_eq!(check_redirect(&policy, &next, &previous), Action::TooManyRedirects); |     assert_eq!(check_redirect(&policy, &next, &previous), | ||||||
|  |                Action::TooManyRedirects); | ||||||
| } | } | ||||||
|  |  | ||||||
| #[test] | #[test] | ||||||
| @@ -256,13 +259,9 @@ fn test_remove_sensitive_headers() { | |||||||
|     let mut headers = Headers::new(); |     let mut headers = Headers::new(); | ||||||
|     headers.set(Accept::star()); |     headers.set(Accept::star()); | ||||||
|     headers.set(Authorization("let me in".to_owned())); |     headers.set(Authorization("let me in".to_owned())); | ||||||
|     headers.set( |     headers.set(Cookie(vec![String::from("foo=bar")])); | ||||||
|         Cookie(vec![ |  | ||||||
|             String::from("foo=bar") |  | ||||||
|         ]) |  | ||||||
|     ); |  | ||||||
|  |  | ||||||
|     let next  = Url::parse("http://initial-domain.com/path").unwrap(); |     let next = Url::parse("http://initial-domain.com/path").unwrap(); | ||||||
|     let mut prev = vec![Url::parse("http://initial-domain.com/new_path").unwrap()]; |     let mut prev = vec![Url::parse("http://initial-domain.com/new_path").unwrap()]; | ||||||
|     let mut filtered_headers = headers.clone(); |     let mut filtered_headers = headers.clone(); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -18,7 +18,7 @@ pub struct Response { | |||||||
| pub fn new(res: ::hyper::client::Response, gzip: bool) -> Response { | pub fn new(res: ::hyper::client::Response, gzip: bool) -> Response { | ||||||
|     info!("Response: '{}' for {}", res.status, res.url); |     info!("Response: '{}' for {}", res.status, res.url); | ||||||
|     Response { |     Response { | ||||||
|         inner: Decoder::from_hyper_response(res, gzip) |         inner: Decoder::from_hyper_response(res, gzip), | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -32,8 +32,8 @@ impl fmt::Debug for Response { | |||||||
|                     .field("headers", &hyper_response.headers) |                     .field("headers", &hyper_response.headers) | ||||||
|                     .field("version", &hyper_response.version) |                     .field("version", &hyper_response.version) | ||||||
|                     .finish() |                     .finish() | ||||||
|             }, |             } | ||||||
|             Decoder::Gzip{ ref head, .. } | |             Decoder::Gzip { ref head, .. } | | ||||||
|             Decoder::Errored { ref head, .. } => { |             Decoder::Errored { ref head, .. } => { | ||||||
|                 f.debug_struct("Response") |                 f.debug_struct("Response") | ||||||
|                     .field("url", &head.url) |                     .field("url", &head.url) | ||||||
| @@ -52,7 +52,7 @@ impl Response { | |||||||
|     pub fn url(&self) -> &Url { |     pub fn url(&self) -> &Url { | ||||||
|         match self.inner { |         match self.inner { | ||||||
|             Decoder::PlainText(ref hyper_response) => &hyper_response.url, |             Decoder::PlainText(ref hyper_response) => &hyper_response.url, | ||||||
|             Decoder::Gzip{ ref head, .. } | |             Decoder::Gzip { ref head, .. } | | ||||||
|             Decoder::Errored { ref head, .. } => &head.url, |             Decoder::Errored { ref head, .. } => &head.url, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -62,7 +62,7 @@ impl Response { | |||||||
|     pub fn status(&self) -> &StatusCode { |     pub fn status(&self) -> &StatusCode { | ||||||
|         match self.inner { |         match self.inner { | ||||||
|             Decoder::PlainText(ref hyper_response) => &hyper_response.status, |             Decoder::PlainText(ref hyper_response) => &hyper_response.status, | ||||||
|             Decoder::Gzip{ ref head, .. } | |             Decoder::Gzip { ref head, .. } | | ||||||
|             Decoder::Errored { ref head, .. } => &head.status, |             Decoder::Errored { ref head, .. } => &head.status, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -72,7 +72,7 @@ impl Response { | |||||||
|     pub fn headers(&self) -> &Headers { |     pub fn headers(&self) -> &Headers { | ||||||
|         match self.inner { |         match self.inner { | ||||||
|             Decoder::PlainText(ref hyper_response) => &hyper_response.headers, |             Decoder::PlainText(ref hyper_response) => &hyper_response.headers, | ||||||
|             Decoder::Gzip{ ref head, .. } | |             Decoder::Gzip { ref head, .. } | | ||||||
|             Decoder::Errored { ref head, .. } => &head.headers, |             Decoder::Errored { ref head, .. } => &head.headers, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -82,7 +82,7 @@ impl Response { | |||||||
|     pub fn version(&self) -> &HttpVersion { |     pub fn version(&self) -> &HttpVersion { | ||||||
|         match self.inner { |         match self.inner { | ||||||
|             Decoder::PlainText(ref hyper_response) => &hyper_response.version, |             Decoder::PlainText(ref hyper_response) => &hyper_response.version, | ||||||
|             Decoder::Gzip{ ref head, .. } | |             Decoder::Gzip { ref head, .. } | | ||||||
|             Decoder::Errored { ref head, .. } => &head.version, |             Decoder::Errored { ref head, .. } => &head.version, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -148,12 +148,13 @@ impl Decoder { | |||||||
|         } |         } | ||||||
|         let content_encoding_gzip: bool; |         let content_encoding_gzip: bool; | ||||||
|         let mut is_gzip = { |         let mut is_gzip = { | ||||||
|             content_encoding_gzip = res.headers.get::<ContentEncoding>().map_or(false, |encs|{ |             content_encoding_gzip = res.headers | ||||||
|                 encs.contains(&Encoding::Gzip) |                 .get::<ContentEncoding>() | ||||||
|             }); |                 .map_or(false, |encs| encs.contains(&Encoding::Gzip)); | ||||||
|             content_encoding_gzip || res.headers.get::<TransferEncoding>().map_or(false, |encs|{ |             content_encoding_gzip || | ||||||
|                 encs.contains(&Encoding::Gzip) |             res.headers | ||||||
|             }) |                 .get::<TransferEncoding>() | ||||||
|  |                 .map_or(false, |encs| encs.contains(&Encoding::Gzip)) | ||||||
|         }; |         }; | ||||||
|         if is_gzip { |         if is_gzip { | ||||||
|             if let Some(content_length) = res.headers.get::<ContentLength>() { |             if let Some(content_length) = res.headers.get::<ContentLength>() { | ||||||
| @@ -184,7 +185,7 @@ fn new_gzip(mut res: ::hyper::client::Response) -> Decoder { | |||||||
|         Ok(0) => return Decoder::PlainText(res), |         Ok(0) => return Decoder::PlainText(res), | ||||||
|         Ok(n) => { |         Ok(n) => { | ||||||
|             debug_assert_eq!(n, 1); |             debug_assert_eq!(n, 1); | ||||||
|         }, |         } | ||||||
|         Err(e) => return Decoder::Errored { |         Err(e) => return Decoder::Errored { | ||||||
|             err: Some(e), |             err: Some(e), | ||||||
|             head: Head { |             head: Head { | ||||||
| @@ -193,7 +194,7 @@ fn new_gzip(mut res: ::hyper::client::Response) -> Decoder { | |||||||
|                 url: res.url.clone(), |                 url: res.url.clone(), | ||||||
|                 version: res.version, |                 version: res.version, | ||||||
|             } |             } | ||||||
|         }, |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     let head = Head { |     let head = Head { | ||||||
| @@ -249,12 +250,8 @@ impl Read for Peeked { | |||||||
| impl Read for Decoder { | impl Read for Decoder { | ||||||
|     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||||||
|         match *self { |         match *self { | ||||||
|             Decoder::PlainText(ref mut hyper_response) => { |             Decoder::PlainText(ref mut hyper_response) => hyper_response.read(buf), | ||||||
|                 hyper_response.read(buf) |             Decoder::Gzip { ref mut decoder, .. } => decoder.read(buf), | ||||||
|             }, |  | ||||||
|             Decoder::Gzip{ref mut decoder, ..} => { |  | ||||||
|                 decoder.read(buf) |  | ||||||
|             }, |  | ||||||
|             Decoder::Errored { ref mut err, .. } => { |             Decoder::Errored { ref mut err, .. } => { | ||||||
|                 Err(err.take().unwrap_or_else(previously_errored)) |                 Err(err.take().unwrap_or_else(previously_errored)) | ||||||
|             } |             } | ||||||
| @@ -274,4 +271,3 @@ impl Read for Response { | |||||||
|         self.inner.read(buf) |         self.inner.read(buf) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,7 +1,8 @@ | |||||||
| extern crate reqwest; | extern crate reqwest; | ||||||
| extern crate libflate; | extern crate libflate; | ||||||
|  |  | ||||||
| #[macro_use] mod server; | #[macro_use] | ||||||
|  | mod server; | ||||||
|  |  | ||||||
| use std::io::Read; | use std::io::Read; | ||||||
| use std::io::prelude::*; | use std::io::prelude::*; | ||||||
| @@ -31,8 +32,10 @@ fn test_get() { | |||||||
|     assert_eq!(res.url().as_str(), &url); |     assert_eq!(res.url().as_str(), &url); | ||||||
|     assert_eq!(res.status(), &reqwest::StatusCode::Ok); |     assert_eq!(res.status(), &reqwest::StatusCode::Ok); | ||||||
|     assert_eq!(res.version(), &reqwest::HttpVersion::Http11); |     assert_eq!(res.version(), &reqwest::HttpVersion::Http11); | ||||||
|     assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test".to_string()))); |     assert_eq!(res.headers().get(), | ||||||
|     assert_eq!(res.headers().get(), Some(&reqwest::header::ContentLength(0))); |                Some(&reqwest::header::Server("test".to_string()))); | ||||||
|  |     assert_eq!(res.headers().get(), | ||||||
|  |                Some(&reqwest::header::ContentLength(0))); | ||||||
|  |  | ||||||
|     let mut buf = [0; 1024]; |     let mut buf = [0; 1024]; | ||||||
|     let n = res.read(&mut buf).unwrap(); |     let n = res.read(&mut buf).unwrap(); | ||||||
| @@ -83,12 +86,11 @@ fn test_redirect_301_and_302_and_303_changes_post_to_get() { | |||||||
|  |  | ||||||
|         let url = format!("http://{}/{}", redirect.addr(), code); |         let url = format!("http://{}/{}", redirect.addr(), code); | ||||||
|         let dst = format!("http://{}/{}", redirect.addr(), "dst"); |         let dst = format!("http://{}/{}", redirect.addr(), "dst"); | ||||||
|         let res = client.post(&url) |         let res = client.post(&url).send().unwrap(); | ||||||
|             .send() |  | ||||||
|             .unwrap(); |  | ||||||
|         assert_eq!(res.url().as_str(), dst); |         assert_eq!(res.url().as_str(), dst); | ||||||
|         assert_eq!(res.status(), &reqwest::StatusCode::Ok); |         assert_eq!(res.status(), &reqwest::StatusCode::Ok); | ||||||
|         assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test-dst".to_string()))); |         assert_eq!(res.headers().get(), | ||||||
|  |                    Some(&reqwest::header::Server("test-dst".to_string()))); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -138,13 +140,11 @@ fn test_redirect_307_and_308_tries_to_post_again() { | |||||||
|  |  | ||||||
|         let url = format!("http://{}/{}", redirect.addr(), code); |         let url = format!("http://{}/{}", redirect.addr(), code); | ||||||
|         let dst = format!("http://{}/{}", redirect.addr(), "dst"); |         let dst = format!("http://{}/{}", redirect.addr(), "dst"); | ||||||
|         let res = client.post(&url) |         let res = client.post(&url).body("Hello").send().unwrap(); | ||||||
|             .body("Hello") |  | ||||||
|             .send() |  | ||||||
|             .unwrap(); |  | ||||||
|         assert_eq!(res.url().as_str(), dst); |         assert_eq!(res.url().as_str(), dst); | ||||||
|         assert_eq!(res.status(), &reqwest::StatusCode::Ok); |         assert_eq!(res.status(), &reqwest::StatusCode::Ok); | ||||||
|         assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test-dst".to_string()))); |         assert_eq!(res.headers().get(), | ||||||
|  |                    Some(&reqwest::header::Server("test-dst".to_string()))); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -177,7 +177,8 @@ fn test_redirect_307_does_not_try_if_reader_cannot_reset() { | |||||||
|         }; |         }; | ||||||
|  |  | ||||||
|         let url = format!("http://{}/{}", redirect.addr(), code); |         let url = format!("http://{}/{}", redirect.addr(), code); | ||||||
|         let res = client.post(&url) |         let res = client | ||||||
|  |             .post(&url) | ||||||
|             .body(reqwest::Body::new(&b"Hello"[..])) |             .body(reqwest::Body::new(&b"Hello"[..])) | ||||||
|             .send() |             .send() | ||||||
|             .unwrap(); |             .unwrap(); | ||||||
| @@ -226,13 +227,11 @@ fn test_redirect_removes_sensitive_headers() { | |||||||
|  |  | ||||||
|     let mut client = reqwest::Client::new().unwrap(); |     let mut client = reqwest::Client::new().unwrap(); | ||||||
|     client.referer(false); |     client.referer(false); | ||||||
|     client.get(&format!("http://{}/sensitive", mid_server.addr())) |     client | ||||||
|         .header( |         .get(&format!("http://{}/sensitive", mid_server.addr())) | ||||||
|             reqwest::header::Cookie(vec![ |         .header(reqwest::header::Cookie(vec![String::from("foo=bar")])) | ||||||
|                 String::from("foo=bar") |         .send() | ||||||
|             ]) |         .unwrap(); | ||||||
|         ) |  | ||||||
|         .send().unwrap(); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| #[test] | #[test] | ||||||
| @@ -282,13 +281,12 @@ fn test_redirect_policy_can_stop_redirects_without_an_error() { | |||||||
|     client.redirect(reqwest::RedirectPolicy::none()); |     client.redirect(reqwest::RedirectPolicy::none()); | ||||||
|  |  | ||||||
|     let url = format!("http://{}/no-redirect", server.addr()); |     let url = format!("http://{}/no-redirect", server.addr()); | ||||||
|     let res = client.get(&url) |     let res = client.get(&url).send().unwrap(); | ||||||
|         .send() |  | ||||||
|         .unwrap(); |  | ||||||
|  |  | ||||||
|     assert_eq!(res.url().as_str(), url); |     assert_eq!(res.url().as_str(), url); | ||||||
|     assert_eq!(res.status(), &reqwest::StatusCode::Found); |     assert_eq!(res.status(), &reqwest::StatusCode::Found); | ||||||
|     assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test-dont".to_string()))); |     assert_eq!(res.headers().get(), | ||||||
|  |                Some(&reqwest::header::Server("test-dont".to_string()))); | ||||||
| } | } | ||||||
|  |  | ||||||
| #[test] | #[test] | ||||||
| @@ -328,8 +326,10 @@ fn test_referer_is_not_set_if_disabled() { | |||||||
|     }; |     }; | ||||||
|     let mut client = reqwest::Client::new().unwrap(); |     let mut client = reqwest::Client::new().unwrap(); | ||||||
|     client.referer(false); |     client.referer(false); | ||||||
|     client.get(&format!("http://{}/no-refer", server.addr())) |     client | ||||||
|         .send().unwrap(); |         .get(&format!("http://{}/no-refer", server.addr())) | ||||||
|  |         .send() | ||||||
|  |         .unwrap(); | ||||||
| } | } | ||||||
|  |  | ||||||
| #[test] | #[test] | ||||||
| @@ -352,7 +352,8 @@ fn test_accept_header_is_not_changed_if_set() { | |||||||
|     }; |     }; | ||||||
|     let client = reqwest::Client::new().unwrap(); |     let client = reqwest::Client::new().unwrap(); | ||||||
|  |  | ||||||
|     let res = client.get(&format!("http://{}/accept", server.addr())) |     let res = client | ||||||
|  |         .get(&format!("http://{}/accept", server.addr())) | ||||||
|         .header(reqwest::header::Accept::json()) |         .header(reqwest::header::Accept::json()) | ||||||
|         .send() |         .send() | ||||||
|         .unwrap(); |         .unwrap(); | ||||||
| @@ -395,7 +396,7 @@ fn test_gzip_response() { | |||||||
|     let mut encoder = ::libflate::gzip::Encoder::new(Vec::new()).unwrap(); |     let mut encoder = ::libflate::gzip::Encoder::new(Vec::new()).unwrap(); | ||||||
|     match encoder.write(b"test request") { |     match encoder.write(b"test request") { | ||||||
|         Ok(n) => assert!(n > 0, "Failed to write to encoder."), |         Ok(n) => assert!(n > 0, "Failed to write to encoder."), | ||||||
|         _ => panic!("Failed to gzip encode string.") |         _ => panic!("Failed to gzip encode string."), | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     let gzipped_content = encoder.finish().into_result().unwrap(); |     let gzipped_content = encoder.finish().into_result().unwrap(); | ||||||
| @@ -420,13 +421,12 @@ fn test_gzip_response() { | |||||||
|             ", |             ", | ||||||
|         response: response |         response: response | ||||||
|     }; |     }; | ||||||
|     let mut res = reqwest::get(&format!("http://{}/gzip", server.addr())) |     let mut res = reqwest::get(&format!("http://{}/gzip", server.addr())).unwrap(); | ||||||
|         .unwrap(); |  | ||||||
|  |  | ||||||
|     let mut body = ::std::string::String::new(); |     let mut body = ::std::string::String::new(); | ||||||
|     match res.read_to_string(&mut body) { |     match res.read_to_string(&mut body) { | ||||||
|         Ok(n) => assert!(n > 0, "Failed to write to buffer."), |         Ok(n) => assert!(n > 0, "Failed to write to buffer."), | ||||||
|         _ => panic!("Failed to write to buffer.") |         _ => panic!("Failed to write to buffer."), | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     assert_eq!(body, "test request"); |     assert_eq!(body, "test request"); | ||||||
| @@ -452,7 +452,8 @@ fn test_gzip_empty_body() { | |||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     let client = reqwest::Client::new().unwrap(); |     let client = reqwest::Client::new().unwrap(); | ||||||
|     let mut res = client.head(&format!("http://{}/gzip", server.addr())) |     let mut res = client | ||||||
|  |         .head(&format!("http://{}/gzip", server.addr())) | ||||||
|         .send() |         .send() | ||||||
|         .unwrap(); |         .unwrap(); | ||||||
|  |  | ||||||
| @@ -482,8 +483,7 @@ fn test_gzip_invalid_body() { | |||||||
|             0" |             0" | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     let mut res = reqwest::get(&format!("http://{}/gzip", server.addr())) |     let mut res = reqwest::get(&format!("http://{}/gzip", server.addr())).unwrap(); | ||||||
|         .unwrap(); |  | ||||||
|     // this tests that the request.send() didn't error, but that the error |     // this tests that the request.send() didn't error, but that the error | ||||||
|     // is in reading the body |     // is in reading the body | ||||||
|  |  | ||||||
|   | |||||||
| @@ -14,13 +14,14 @@ impl Server { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| static DEFAULT_USER_AGENT: &'static str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); | static DEFAULT_USER_AGENT: &'static str = | ||||||
|  |     concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); | ||||||
|  |  | ||||||
| pub fn spawn(txns: Vec<(Vec<u8>, Vec<u8>)>) -> Server { | pub fn spawn(txns: Vec<(Vec<u8>, Vec<u8>)>) -> Server { | ||||||
|     let listener = net::TcpListener::bind("127.0.0.1:0").unwrap(); |     let listener = net::TcpListener::bind("127.0.0.1:0").unwrap(); | ||||||
|     let addr = listener.local_addr().unwrap(); |     let addr = listener.local_addr().unwrap(); | ||||||
|     thread::spawn(move || { |     thread::spawn( | ||||||
|         for (mut expected, reply) in txns { |         move || for (mut expected, reply) in txns { | ||||||
|             let (mut socket, _addr) = listener.accept().unwrap(); |             let (mut socket, _addr) = listener.accept().unwrap(); | ||||||
|             replace_expected_vars(&mut expected, addr.to_string().as_ref(), DEFAULT_USER_AGENT.as_ref()); |             replace_expected_vars(&mut expected, addr.to_string().as_ref(), DEFAULT_USER_AGENT.as_ref()); | ||||||
|             let mut buf = [0; 4096]; |             let mut buf = [0; 4096]; | ||||||
| @@ -28,11 +29,11 @@ pub fn spawn(txns: Vec<(Vec<u8>, Vec<u8>)>) -> Server { | |||||||
|  |  | ||||||
|             match (::std::str::from_utf8(&expected), ::std::str::from_utf8(&buf[..n])) { |             match (::std::str::from_utf8(&expected), ::std::str::from_utf8(&buf[..n])) { | ||||||
|                 (Ok(expected), Ok(received)) => assert_eq!(expected, received), |                 (Ok(expected), Ok(received)) => assert_eq!(expected, received), | ||||||
|                 _ => assert_eq!(expected, &buf[..n]) |                 _ => assert_eq!(expected, &buf[..n]), | ||||||
|             } |             } | ||||||
|             socket.write_all(&reply).unwrap(); |             socket.write_all(&reply).unwrap(); | ||||||
|         } |         } | ||||||
|     }); |     ); | ||||||
|  |  | ||||||
|     Server { |     Server { | ||||||
|         addr: addr, |         addr: addr, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user