Skip to content

Commit

Permalink
refactor(client): use a Protocol to create a message for a Request
Browse files Browse the repository at this point in the history
  • Loading branch information
mlalic committed Jun 2, 2015
1 parent dccdf8d commit d3e3a45
Showing 1 changed file with 16 additions and 38 deletions.
54 changes: 16 additions & 38 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Protocol + Send>,
redirect_policy: RedirectPolicy,
}

Expand All @@ -77,15 +80,20 @@ impl Client {
/// Create a new client with a specific connector.
pub fn with_connector<C, S>(connector: C) -> Client
where C: NetworkConnector<Stream=S> + Send + 'static, S: NetworkStream + Send {
Client::with_protocol(Http11Protocol::with_connector(connector))
}

/// Create a new client with a specific `Protocol`.
pub fn with_protocol<P: Protocol + Send + 'static>(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.
Expand Down Expand Up @@ -131,44 +139,10 @@ impl Client {
}
}

fn with_connector<C: NetworkConnector<Stream=S> + 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: NetworkConnector + Send>(C);

impl<C: NetworkConnector<Stream=S> + Send, S: NetworkStream + Send> NetworkConnector for ConnAdapter<C> {
type Stream = Box<NetworkStream + Send>;
#[inline]
fn connect(&self, host: &str, port: u16, scheme: &str)
-> ::Result<Box<NetworkStream + Send>> {
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<NetworkConnector<Stream=Box<NetworkStream + Send>> + Send>);

impl NetworkConnector for Connector {
type Stream = Box<NetworkStream + Send>;
#[inline]
fn connect(&self, host: &str, port: u16, scheme: &str)
-> ::Result<Box<NetworkStream + Send>> {
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
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit d3e3a45

Please sign in to comment.