From 5d7be77e4ac0d5c1d852c1208abc77a913c4f4d1 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Fri, 20 Mar 2015 17:32:51 -0700 Subject: [PATCH] feat(server): use SocketAddrs instead of Ipv4Addrs --- benches/server.rs | 3 +-- examples/hello.rs | 3 +-- examples/server.rs | 3 +-- src/net.rs | 8 ++++---- src/server/mod.rs | 11 +++++------ 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/benches/server.rs b/benches/server.rs index 74f3155808..c06f0bc7df 100644 --- a/benches/server.rs +++ b/benches/server.rs @@ -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}; @@ -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())); diff --git a/examples/hello.rs b/examples/hello.rs index dc9d3069a4..013201e2c4 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -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!"; @@ -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"); } diff --git a/examples/server.rs b/examples/server.rs index 1ada938010..85a5c198a3 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -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; @@ -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"); } diff --git a/src/net.rs b/src/net.rs index 3765e9caf7..e8b2a552b7 100644 --- a/src/net.rs +++ b/src/net.rs @@ -38,7 +38,7 @@ pub trait NetworkListener: Clone { fn accept(&mut self) -> io::Result; /// Get the address this Listener ended up listening on. - fn socket_addr(&mut self) -> io::Result; + fn local_addr(&mut self) -> io::Result; /// Closes the Acceptor, so no more incoming connections will be handled. // fn close(&mut self) -> io::Result<()>; @@ -173,12 +173,12 @@ impl Clone for HttpListener { impl HttpListener { /// Start listening to an address over HTTP. - pub fn http(addr: &To) -> io::Result { + pub fn http(addr: To) -> io::Result { Ok(HttpListener::Http(try!(TcpListener::bind(addr)))) } /// Start listening to an address over HTTPS. - pub fn https(addr: &To, cert: &Path, key: &Path) -> io::Result { + pub fn https(addr: To, cert: &Path, key: &Path) -> io::Result { 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( @@ -213,7 +213,7 @@ impl NetworkListener for HttpListener { } #[inline] - fn socket_addr(&mut self) -> io::Result { + fn local_addr(&mut self) -> io::Result { match *self { HttpListener::Http(ref mut tcp) => tcp.local_addr(), HttpListener::Https(ref mut tcp, _) => tcp.local_addr(), diff --git a/src/server/mod.rs b/src/server/mod.rs index 3448db1fd5..5b718d2a77 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -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}; @@ -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 { - let addr = &(ip, port); + pub fn listen_threads(self, addr: T, threads: usize) -> HttpResult { let listener = try!(match self.ssl { Some((cert, key)) => HttpListener::https(addr, cert, key), None => HttpListener::http(addr) @@ -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 { - self.listen_threads(ip, port, num_cpus::get() * 5 / 4) + pub fn listen(self, addr: T) -> HttpResult { + self.listen_threads(addr, num_cpus::get() * 5 / 4) } } impl< @@ -97,7 +96,7 @@ L: NetworkListener + 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 { - let socket = try!(listener.socket_addr()); + let socket = try!(listener.local_addr()); let handler = self.handler; debug!("threads = {:?}", threads);