Skip to content

Commit

Permalink
feat(server): use SocketAddrs instead of Ipv4Addrs
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Mar 21, 2015
1 parent c205144 commit 5d7be77
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
3 changes: 1 addition & 2 deletions benches/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern crate test;

use test::Bencher;
use std::io::{Read, Write};
use std::net::Ipv4Addr;

use hyper::method::Method::Get;
use hyper::server::{Request, Response};
Expand All @@ -27,7 +26,7 @@ fn hyper_handle(_: Request, res: Response) {
#[bench]
fn bench_hyper(b: &mut Bencher) {
let server = hyper::Server::http(hyper_handle);
let mut listener = server.listen(Ipv4Addr::new(127, 0, 0, 1), 0).unwrap();
let mut listener = server.listen("127.0.0.1").unwrap();

let url = hyper::Url::parse(&*format!("http://{}", listener.socket)).unwrap();
b.iter(|| request(url.clone()));
Expand Down
3 changes: 1 addition & 2 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate hyper;
extern crate env_logger;

use std::io::Write;
use std::net::Ipv4Addr;
use hyper::server::{Request, Response};

static PHRASE: &'static [u8] = b"Hello World!";
Expand All @@ -17,6 +16,6 @@ fn hello(_: Request, res: Response) {
fn main() {
env_logger::init().unwrap();
let _listening = hyper::Server::http(hello)
.listen(Ipv4Addr::new(127, 0, 0, 1), 3000).unwrap();
.listen("127.0.0.1:3000").unwrap();
println!("Listening on http://127.0.0.1:3000");
}
3 changes: 1 addition & 2 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate hyper;
extern crate env_logger;

use std::io::{Write, copy};
use std::net::Ipv4Addr;

use hyper::{Get, Post};
use hyper::header::ContentLength;
Expand Down Expand Up @@ -52,6 +51,6 @@ fn echo(mut req: Request, mut res: Response) {
fn main() {
env_logger::init().unwrap();
let server = Server::http(echo);
let _guard = server.listen(Ipv4Addr::new(127, 0, 0, 1), 1337).unwrap();
let _guard = server.listen("127.0.0.1:1337").unwrap();
println!("Listening on http://127.0.0.1:1337");
}
8 changes: 4 additions & 4 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait NetworkListener: Clone {
fn accept(&mut self) -> io::Result<Self::Stream>;

/// Get the address this Listener ended up listening on.
fn socket_addr(&mut self) -> io::Result<SocketAddr>;
fn local_addr(&mut self) -> io::Result<SocketAddr>;

/// Closes the Acceptor, so no more incoming connections will be handled.
// fn close(&mut self) -> io::Result<()>;
Expand Down Expand Up @@ -173,12 +173,12 @@ impl Clone for HttpListener {
impl HttpListener {

/// Start listening to an address over HTTP.
pub fn http<To: ToSocketAddrs>(addr: &To) -> io::Result<HttpListener> {
pub fn http<To: ToSocketAddrs>(addr: To) -> io::Result<HttpListener> {
Ok(HttpListener::Http(try!(TcpListener::bind(addr))))
}

/// Start listening to an address over HTTPS.
pub fn https<To: ToSocketAddrs>(addr: &To, cert: &Path, key: &Path) -> io::Result<HttpListener> {
pub fn https<To: ToSocketAddrs>(addr: To, cert: &Path, key: &Path) -> io::Result<HttpListener> {
let mut ssl_context = try!(SslContext::new(Sslv23).map_err(lift_ssl_error));
try_some!(ssl_context.set_cipher_list("DEFAULT").map(lift_ssl_error));
try_some!(ssl_context.set_certificate_file(
Expand Down Expand Up @@ -213,7 +213,7 @@ impl NetworkListener for HttpListener {
}

#[inline]
fn socket_addr(&mut self) -> io::Result<SocketAddr> {
fn local_addr(&mut self) -> io::Result<SocketAddr> {
match *self {
HttpListener::Http(ref mut tcp) => tcp.local_addr(),
HttpListener::Https(ref mut tcp, _) => tcp.local_addr(),
Expand Down
11 changes: 5 additions & 6 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! HTTP Server
use std::io::{BufReader, BufWriter, Write};
use std::marker::PhantomData;
use std::net::{Ipv4Addr, SocketAddr};
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::Path;
use std::thread::{self, JoinGuard};

Expand Down Expand Up @@ -76,8 +76,7 @@ impl<'a, H: Handler + 'static> Server<'a, H, HttpListener> {

impl<'a, H: Handler + 'static> Server<'a, H, HttpListener> {
/// Binds to a socket, and starts handling connections using a task pool.
pub fn listen_threads(self, ip: Ipv4Addr, port: u16, threads: usize) -> HttpResult<Listening> {
let addr = &(ip, port);
pub fn listen_threads<T: ToSocketAddrs>(self, addr: T, threads: usize) -> HttpResult<Listening> {
let listener = try!(match self.ssl {
Some((cert, key)) => HttpListener::https(addr, cert, key),
None => HttpListener::http(addr)
Expand All @@ -86,8 +85,8 @@ impl<'a, H: Handler + 'static> Server<'a, H, HttpListener> {
}

/// Binds to a socket and starts handling connections.
pub fn listen(self, ip: Ipv4Addr, port: u16) -> HttpResult<Listening> {
self.listen_threads(ip, port, num_cpus::get() * 5 / 4)
pub fn listen<T: ToSocketAddrs>(self, addr: T) -> HttpResult<Listening> {
self.listen_threads(addr, num_cpus::get() * 5 / 4)
}
}
impl<
Expand All @@ -97,7 +96,7 @@ L: NetworkListener<Stream=S> + Send + 'static,
S: NetworkStream + Clone + Send> Server<'a, H, L> {
/// Creates a new server that will handle `HttpStream`s.
pub fn with_listener(self, mut listener: L, threads: usize) -> HttpResult<Listening> {
let socket = try!(listener.socket_addr());
let socket = try!(listener.local_addr());
let handler = self.handler;

debug!("threads = {:?}", threads);
Expand Down

0 comments on commit 5d7be77

Please sign in to comment.