feat(server): use SocketAddrs instead of Ipv4Addrs
This commit is contained in:
@@ -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<()>;
|
||||
@@ -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(
|
||||
@@ -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(),
|
||||
|
||||
@@ -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<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)
|
||||
@@ -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<
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user