Skip to content

feat(client): add connection timeout #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::default::Default;
use std::old_io::IoResult;
use std::old_io::util::copy;
use std::iter::Extend;
use std::time;

use url::UrlParser;
use url::ParseError as UrlError;
Expand Down Expand Up @@ -51,14 +52,21 @@ impl<'v> Client<HttpConnector<'v>> {

/// Create a new Client.
pub fn new() -> Client<HttpConnector<'v>> {
Client::with_connector(HttpConnector(None))
Client::with_connector(HttpConnector {
verifier: None,
connect_timeout: None
})
}

/// Set the SSL verifier callback for use with OpenSSL.
pub fn set_ssl_verifier(&mut self, verifier: ContextVerifier<'v>) {
self.connector = HttpConnector(Some(verifier));
self.connector.verifier = Some(verifier);
}

/// Set connect and read/write timeouts.
pub fn set_connect_timeout(&mut self, connect_timeout: Option<time::Duration>) {
self.connector.connect_timeout = connect_timeout;
}
}

impl<C: NetworkConnector> Client<C> {
Expand Down
5 changes: 4 additions & 1 deletion src/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ impl<W> Request<W> {
impl Request<Fresh> {
/// Create a new client request.
pub fn new(method: method::Method, url: Url) -> HttpResult<Request<Fresh>> {
let mut conn = HttpConnector(None);
let mut conn = HttpConnector {
verifier: None,
connect_timeout: None
};
Request::with_connector(method, url, &mut conn)
}

Expand Down
25 changes: 20 additions & 5 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::old_io::net::tcp::{TcpStream, TcpListener, TcpAcceptor};
use std::mem;
use std::raw::{self, TraitObject};
use std::sync::Arc;
use std::time::Duration;

use uany::UnsafeAnyExt;
use openssl::ssl::{Ssl, SslStream, SslContext};
Expand Down Expand Up @@ -307,8 +308,11 @@ impl NetworkStream for HttpStream {
}

/// A connector that will produce HttpStreams.
#[allow(missing_copy_implementations)]
pub struct HttpConnector<'v>(pub Option<ContextVerifier<'v>>);
#[allow(missing_copy_implementations, missing_docs)]
pub struct HttpConnector<'v> {
pub verifier: Option<ContextVerifier<'v>>,
pub connect_timeout: Option<Duration>
}

/// A method that can set verification methods on an SSL context
pub type ContextVerifier<'v> = Box<FnMut(&mut SslContext) -> ()+'v>;
Expand All @@ -318,16 +322,27 @@ impl<'v> NetworkConnector for HttpConnector<'v> {

fn connect(&mut self, host: &str, port: Port, scheme: &str) -> IoResult<HttpStream> {
let addr = (host, port);

match scheme {
"http" => {
debug!("http scheme");
Ok(HttpStream::Http(try!(TcpStream::connect(addr))))
let stream = if let Some(t) = self.connect_timeout {
try!(TcpStream::connect_timeout(addr, t))
} else {
try!(TcpStream::connect(addr))
};

Ok(HttpStream::Http(stream))
},
"https" => {
debug!("https scheme");
let stream = try!(TcpStream::connect(addr));
let stream = if let Some(t) = self.connect_timeout {
try!(TcpStream::connect_timeout(addr, t))
} else {
try!(TcpStream::connect(addr))
};
let mut context = try!(SslContext::new(Sslv23).map_err(lift_ssl_error));
if let Some(ref mut verifier) = self.0 {
if let Some(ref mut verifier) = self.verifier {
verifier(&mut context);
}
let ssl = try!(Ssl::new(&context).map_err(lift_ssl_error));
Expand Down