Merge pull request #25 from reem/server-bind-multiple-addresses
Change Server to contain a Vec of (ip, port) pairs to allow repeat
This commit is contained in:
@@ -13,6 +13,12 @@ git = "https://github.com/seanmonstar/mime.rs"
|
||||
[dependencies.unsafe-any]
|
||||
git = "https://github.com/reem/rust-unsafe-any"
|
||||
|
||||
[dependencies.intertwine]
|
||||
git = "https://github.com/reem/rust-intertwine"
|
||||
|
||||
[dependencies.move-acceptor]
|
||||
git = "https://github.com/reem/rust-move-acceptor"
|
||||
|
||||
[dev-dependencies.curl]
|
||||
git = "https://github.com/carllerche/curl-rust"
|
||||
|
||||
|
||||
@@ -8,9 +8,8 @@ extern crate test;
|
||||
use std::fmt::{mod, Show};
|
||||
use std::io::net::ip::Ipv4Addr;
|
||||
use hyper::server::{Incoming, Server};
|
||||
use hyper::net::HttpAcceptor;
|
||||
|
||||
fn listen() -> hyper::server::Listening<HttpAcceptor> {
|
||||
fn listen() -> hyper::server::Listening {
|
||||
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 0);
|
||||
server.listen(handle).unwrap()
|
||||
}
|
||||
@@ -34,8 +33,8 @@ fn handle(mut incoming: Incoming) {
|
||||
|
||||
#[bench]
|
||||
fn bench_curl(b: &mut test::Bencher) {
|
||||
let listening = listen();
|
||||
let s = format!("http://{}/", listening.socket_addr);
|
||||
let mut listening = listen();
|
||||
let s = format!("http://{}/", listening.sockets[0]);
|
||||
let url = s.as_slice();
|
||||
b.iter(|| {
|
||||
curl::http::handle()
|
||||
@@ -63,24 +62,24 @@ impl hyper::header::Header for Foo {
|
||||
|
||||
#[bench]
|
||||
fn bench_hyper(b: &mut test::Bencher) {
|
||||
let listening = listen();
|
||||
let s = format!("http://{}/", listening.socket_addr);
|
||||
let mut listening = listen();
|
||||
let s = format!("http://{}/", listening.sockets[0]);
|
||||
let url = s.as_slice();
|
||||
b.iter(|| {
|
||||
let mut req = hyper::get(hyper::Url::parse(url).unwrap()).unwrap();
|
||||
req.headers.set(Foo);
|
||||
|
||||
req
|
||||
.send().unwrap()
|
||||
.read_to_string().unwrap()
|
||||
req
|
||||
.send().unwrap()
|
||||
.read_to_string().unwrap()
|
||||
});
|
||||
listening.close().unwrap()
|
||||
listening.close().unwrap()
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_http(b: &mut test::Bencher) {
|
||||
let listening = listen();
|
||||
let s = format!("http://{}/", listening.socket_addr);
|
||||
let mut listening = listen();
|
||||
let s = format!("http://{}/", listening.sockets[0]);
|
||||
let url = s.as_slice();
|
||||
b.iter(|| {
|
||||
let mut req: http::client::RequestWriter = http::client::RequestWriter::new(
|
||||
|
||||
@@ -28,9 +28,9 @@ fn hyper_handle(mut incoming: hyper::server::Incoming) {
|
||||
#[bench]
|
||||
fn bench_hyper(b: &mut Bencher) {
|
||||
let server = hyper::Server::http(Ipv4Addr(127, 0, 0, 1), 0);
|
||||
let listener = server.listen(hyper_handle).unwrap();
|
||||
let mut listener = server.listen(hyper_handle).unwrap();
|
||||
|
||||
let url = hyper::Url::parse(format!("http://{}", listener.socket_addr).as_slice()).unwrap();
|
||||
let url = hyper::Url::parse(format!("http://{}", listener.sockets[0]).as_slice()).unwrap();
|
||||
b.iter(|| request(url.clone()));
|
||||
listener.close().unwrap();
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ trait ConcurrentHandler: Send + Sync {
|
||||
struct Concurrent<H: ConcurrentHandler> { handler: Arc<H> }
|
||||
|
||||
impl<H: ConcurrentHandler> Handler<HttpAcceptor, HttpStream> for Concurrent<H> {
|
||||
fn handle(self, mut incoming: Incoming<HttpAcceptor>) {
|
||||
fn handle(self, mut incoming: Incoming) {
|
||||
for (mut req, mut res) in incoming {
|
||||
let clone = self.handler.clone();
|
||||
spawn(proc() { clone.handle(req, res) })
|
||||
|
||||
@@ -9,6 +9,8 @@ extern crate url;
|
||||
#[phase(plugin,link)] extern crate log;
|
||||
#[cfg(test)] extern crate test;
|
||||
extern crate "unsafe-any" as uany;
|
||||
extern crate "move-acceptor" as macceptor;
|
||||
extern crate intertwine;
|
||||
|
||||
pub use std::io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr, Port};
|
||||
pub use mimewrapper::mime;
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
//! HTTP Server
|
||||
use std::io::{Acceptor, Listener, IoResult, EndOfFile, IncomingConnections};
|
||||
use std::io::{Listener, IoResult, EndOfFile};
|
||||
use std::io::net::ip::{IpAddr, Port, SocketAddr};
|
||||
|
||||
use intertwine::{Intertwine, Intertwined};
|
||||
use macceptor::MoveAcceptor;
|
||||
|
||||
pub use self::request::Request;
|
||||
pub use self::response::{Response, Fresh, Streaming};
|
||||
|
||||
use net::{NetworkListener, NetworkAcceptor, NetworkStream, HttpAcceptor, HttpListener};
|
||||
use net::{NetworkListener, NetworkAcceptor, NetworkStream, HttpAcceptor, HttpListener, HttpStream};
|
||||
|
||||
use {HttpResult};
|
||||
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
@@ -15,55 +20,79 @@ pub mod response;
|
||||
/// Once listening, it will create a `Request`/`Response` pair for each
|
||||
/// incoming connection, and hand them to the provided handler.
|
||||
pub struct Server<L = HttpListener> {
|
||||
ip: IpAddr,
|
||||
port: Port
|
||||
pairs: Vec<(IpAddr, Port)>
|
||||
}
|
||||
|
||||
macro_rules! try_option(
|
||||
($e:expr) => {{
|
||||
match $e {
|
||||
Some(v) => v,
|
||||
None => return None
|
||||
}
|
||||
}}
|
||||
)
|
||||
|
||||
impl Server<HttpListener> {
|
||||
/// Creates a new server that will handle `HttpStream`s.
|
||||
pub fn http(ip: IpAddr, port: Port) -> Server {
|
||||
Server {
|
||||
ip: ip,
|
||||
port: port
|
||||
}
|
||||
Server { pairs: vec![(ip, port)] }
|
||||
}
|
||||
|
||||
/// Creates a server that can listen to many (ip, port) pairs.
|
||||
pub fn many(pairs: Vec<(IpAddr, Port)>) -> Server {
|
||||
Server { pairs: pairs }
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: NetworkListener<S, A>, S: NetworkStream, A: NetworkAcceptor<S>> Server<L> {
|
||||
/// Creates a server that can listen for and handle `NetworkStreams`.
|
||||
pub fn new(ip: IpAddr, port: Port) -> Server<L> {
|
||||
Server {
|
||||
ip: ip,
|
||||
port: port
|
||||
}
|
||||
}
|
||||
|
||||
/// Binds to a socket, and starts handling connections.
|
||||
pub fn listen<H: Handler<A, S>>(self, handler: H) -> IoResult<Listening<A>> {
|
||||
let mut listener: L = try!(NetworkListener::bind(self.ip.to_string().as_slice(), self.port));
|
||||
let socket = try!(listener.socket_name());
|
||||
let acceptor = try!(listener.listen());
|
||||
let mut worker = acceptor.clone();
|
||||
///
|
||||
/// This method has unbound type parameters, so can be used when you want to use
|
||||
/// something other than the provided HttpStream, HttpAcceptor, and HttpListener.
|
||||
pub fn listen_network<H, S, A, L>(self, handler: H) -> HttpResult<Listening<A>>
|
||||
where H: Handler<A, S>,
|
||||
S: NetworkStream,
|
||||
A: NetworkAcceptor<S>,
|
||||
L: NetworkListener<S, A>, {
|
||||
let mut acceptors = Vec::new();
|
||||
let mut sockets = Vec::new();
|
||||
for (ip, port) in self.pairs.move_iter() {
|
||||
let mut listener: L = try_io!(NetworkListener::<S, A>::bind(ip.to_string().as_slice(), port));
|
||||
|
||||
sockets.push(try_io!(listener.socket_name()));
|
||||
|
||||
let acceptor = try_io!(listener.listen());
|
||||
acceptors.push(acceptor.clone());
|
||||
}
|
||||
|
||||
let connections = acceptors.clone()
|
||||
.move_iter()
|
||||
.map(|acceptor| acceptor.move_incoming())
|
||||
.intertwine();
|
||||
|
||||
spawn(proc() {
|
||||
handler.handle(Incoming { from: worker.incoming() });
|
||||
handler.handle(Incoming { from: connections });
|
||||
});
|
||||
|
||||
Ok(Listening {
|
||||
acceptor: acceptor,
|
||||
socket_addr: socket,
|
||||
acceptors: acceptors,
|
||||
sockets: sockets,
|
||||
})
|
||||
}
|
||||
|
||||
/// Binds to a socket and starts handling connections.
|
||||
pub fn listen<H: Handler<HttpAcceptor, HttpStream>>(self, handler: H) -> HttpResult<Listening<HttpAcceptor>> {
|
||||
self.listen_network::<H, HttpStream, HttpAcceptor, HttpListener>(handler)
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over incoming connections, represented as pairs of
|
||||
/// hyper Requests and Responses.
|
||||
pub struct Incoming<'a, A: 'a = HttpAcceptor> {
|
||||
from: IncomingConnections<'a, A>
|
||||
pub struct Incoming<S: Send = HttpStream> {
|
||||
from: Intertwined<IoResult<S>>
|
||||
}
|
||||
|
||||
impl<'a, S: NetworkStream, A: NetworkAcceptor<S>> Iterator<(Request, Response<Fresh>)> for Incoming<'a, A> {
|
||||
impl<S: NetworkStream + 'static> Iterator<(Request, Response<Fresh>)> for Incoming<S> {
|
||||
fn next(&mut self) -> Option<(Request, Response<Fresh>)> {
|
||||
for conn in self.from {
|
||||
match conn {
|
||||
@@ -93,17 +122,23 @@ impl<'a, S: NetworkStream, A: NetworkAcceptor<S>> Iterator<(Request, Response<Fr
|
||||
}
|
||||
|
||||
/// A listening server, which can later be closed.
|
||||
pub struct Listening<A> {
|
||||
acceptor: A,
|
||||
/// The socket address that the server is bound to.
|
||||
pub socket_addr: SocketAddr,
|
||||
pub struct Listening<A = HttpAcceptor> {
|
||||
acceptors: Vec<A>,
|
||||
/// The socket addresses that the server is bound to.
|
||||
pub sockets: Vec<SocketAddr>,
|
||||
}
|
||||
|
||||
impl<A: NetworkAcceptor<S>, S: NetworkStream> Listening<A> {
|
||||
/// Stop the server from listening to it's socket address.
|
||||
pub fn close(mut self) -> IoResult<()> {
|
||||
/// Stop the server from listening to all of its socket addresses.
|
||||
///
|
||||
/// If closing any of the servers acceptors fails, this function returns Err
|
||||
/// and does not close the rest of the acceptors.
|
||||
pub fn close(&mut self) -> HttpResult<()> {
|
||||
debug!("closing server");
|
||||
self.acceptor.close()
|
||||
for acceptor in self.acceptors.mut_iter() {
|
||||
try_io!(acceptor.close());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,11 +147,11 @@ pub trait Handler<A: NetworkAcceptor<S>, S: NetworkStream>: Send {
|
||||
/// Receives a `Request`/`Response` pair, and should perform some action on them.
|
||||
///
|
||||
/// This could reading from the request, and writing to the response.
|
||||
fn handle(self, Incoming<A>);
|
||||
fn handle(self, Incoming<S>);
|
||||
}
|
||||
|
||||
impl<A: NetworkAcceptor<S>, S: NetworkStream> Handler<A, S> for fn(Incoming<A>) {
|
||||
fn handle(self, incoming: Incoming<A>) {
|
||||
impl<A: NetworkAcceptor<S>, S: NetworkStream> Handler<A, S> for fn(Incoming<S>) {
|
||||
fn handle(self, incoming: Incoming<S>) {
|
||||
(self)(incoming)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user