Skip to content

Commit

Permalink
feat(net): add net2 crate and TcpBuilderConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
Danyel Bayraktar committed Jul 25, 2015
1 parent 7e92633 commit 4cacbd8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ httparse = "0.1"
language-tags = "0.0.7"
log = "0.3"
mime = "0.0.12"
net2 = "0.1.3"
num_cpus = "0.2"
rustc-serialize = "0.3"
time = "0.1"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
//! implement `Reader` and can be read to get the data out of a `Response`.
//!

extern crate net2;
extern crate rustc_serialize as serialize;
extern crate time;
extern crate url;
Expand Down
42 changes: 38 additions & 4 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::io::{self, ErrorKind, Read, Write};
use std::net::{SocketAddr, ToSocketAddrs, TcpStream, TcpListener, Shutdown};
use std::mem;

use net2::TcpBuilder;

#[cfg(feature = "openssl")]
pub use self::openssl::Openssl;

Expand Down Expand Up @@ -149,9 +151,12 @@ impl HttpListener {

/// Start listening to an address over HTTP.
pub fn new<To: ToSocketAddrs>(addr: To) -> ::Result<HttpListener> {
Ok(HttpListener(try!(TcpListener::bind(addr))))
HttpListener::from_builder(try!(try!(TcpBuilder::new_v4()).bind(addr)))
}

pub fn from_builder(builder: &TcpBuilder) -> ::Result<HttpListener> {
Ok(HttpListener(try!(builder.listen(128))))
}
}

impl NetworkListener for HttpListener {
Expand Down Expand Up @@ -241,11 +246,41 @@ impl NetworkConnector for HttpConnector {
type Stream = HttpStream;

fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<HttpStream> {
let addr = &(host, port);
TcpBuilderConnector::default().connect(host, port, scheme)
}
}

pub struct TcpBuilderConnector<T: ToSocketAddrs> {
pub ipv6: bool,
pub addr: Option<T>
}

impl Default for TcpBuilderConnector<SocketAddr> {
fn default() -> TcpBuilderConnector<SocketAddr> {
return TcpBuilderConnector{
ipv6: false,
addr: None
}
}
}

impl<T: ToSocketAddrs> NetworkConnector for TcpBuilderConnector<T> {
type Stream = HttpStream;

fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<HttpStream> {
let remote_addr = &(host, port);
Ok(try!(match scheme {
"http" => {
debug!("http scheme");
Ok(HttpStream(try!(TcpStream::connect(addr))))
let builder = try!(if self.ipv6 {
TcpBuilder::new_v6()
} else {
TcpBuilder::new_v4()
});
if let Some(ref local_addr) = self.addr {
try!(builder.bind(local_addr));
}
Ok(HttpStream(try!(builder.connect(remote_addr))))
},
_ => {
Err(io::Error::new(io::ErrorKind::InvalidInput,
Expand All @@ -255,7 +290,6 @@ impl NetworkConnector for HttpConnector {
}
}


/// An abstraction to allow any SSL implementation to be used with HttpsStreams.
pub trait Ssl {
/// The protected stream.
Expand Down

0 comments on commit 4cacbd8

Please sign in to comment.