diff --git a/src/client/mod.rs b/src/client/mod.rs index 386c9249..419e46cb 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -54,11 +54,14 @@ pub mod pool; pub mod request; pub mod response; +use message::Protocol; +use http11::Http11Protocol; + /// A Client to use additional features with Requests. /// /// Clients can handle things such as: redirect policy, connection pooling. pub struct Client { - connector: Connector, + protocol: Box, redirect_policy: RedirectPolicy, } @@ -77,15 +80,20 @@ impl Client { /// Create a new client with a specific connector. pub fn with_connector(connector: C) -> Client where C: NetworkConnector + Send + 'static, S: NetworkStream + Send { + Client::with_protocol(Http11Protocol::with_connector(connector)) + } + + /// Create a new client with a specific `Protocol`. + pub fn with_protocol(protocol: P) -> Client { Client { - connector: with_connector(connector), + protocol: Box::new(protocol), redirect_policy: Default::default() } } /// Set the SSL verifier callback for use with OpenSSL. pub fn set_ssl_verifier(&mut self, verifier: ContextVerifier) { - self.connector.set_ssl_verifier(verifier); + self.protocol.set_ssl_verifier(verifier); } /// Set the RedirectPolicy. @@ -131,44 +139,10 @@ impl Client { } } -fn with_connector + Send + 'static, S: NetworkStream + Send>(c: C) -> Connector { - Connector(Box::new(ConnAdapter(c))) -} - impl Default for Client { fn default() -> Client { Client::new() } } -struct ConnAdapter(C); - -impl + Send, S: NetworkStream + Send> NetworkConnector for ConnAdapter { - type Stream = Box; - #[inline] - fn connect(&self, host: &str, port: u16, scheme: &str) - -> ::Result> { - Ok(try!(self.0.connect(host, port, scheme)).into()) - } - #[inline] - fn set_ssl_verifier(&mut self, verifier: ContextVerifier) { - self.0.set_ssl_verifier(verifier); - } -} - -struct Connector(Box> + Send>); - -impl NetworkConnector for Connector { - type Stream = Box; - #[inline] - fn connect(&self, host: &str, port: u16, scheme: &str) - -> ::Result> { - Ok(try!(self.0.connect(host, port, scheme)).into()) - } - #[inline] - fn set_ssl_verifier(&mut self, verifier: ContextVerifier) { - self.0.set_ssl_verifier(verifier); - } -} - /// Options for an individual Request. /// /// One of these will be built for you if you use one of the convenience @@ -229,7 +203,11 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> { }; loop { - let mut req = try!(Request::with_connector(method.clone(), url.clone(), &client.connector)); + let message = { + let (host, port) = try!(get_host_and_port(&url)); + try!(client.protocol.new_message(&host, port, &*url.scheme)) + }; + let mut req = try!(Request::with_message(method.clone(), url.clone(), message)); headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter())); match (can_have_body, body.as_ref()) {