Merge pull request #13 from reem/handler-iterator

Change the Handler trait to receive an Iterator of (Request, Response) pairs.
This commit is contained in:
Sean McArthur
2014-09-07 10:49:25 -07:00
2 changed files with 78 additions and 52 deletions

View File

@@ -1,38 +1,55 @@
#![feature(macro_rules)]
extern crate hyper; extern crate hyper;
extern crate debug; extern crate debug;
use std::io::{IoResult};
use std::io::util::copy; use std::io::util::copy;
use std::io::net::ip::Ipv4Addr; use std::io::net::ip::Ipv4Addr;
use hyper::{Get, Post}; use hyper::{Get, Post};
use hyper::server::{Server, Handler, Request, Response}; use hyper::server::{Server, Handler, Incoming};
use hyper::header::ContentLength; use hyper::header::ContentLength;
struct Echo; struct Echo;
macro_rules! try_continue(
($e:expr) => {{
match $e {
Ok(v) => v,
Err(e) => { println!("Error: {}", e); continue; }
}
}}
)
impl Handler for Echo { impl Handler for Echo {
fn handle(&mut self, mut req: Request, mut res: Response) -> IoResult<()> { fn handle(self, mut incoming: Incoming) {
for (mut req, mut res) in incoming {
match req.uri { match req.uri {
hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) { hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
(&Get, "/") | (&Get, "/echo") => { (&Get, "/") | (&Get, "/echo") => {
let out = b"Try POST /echo"; let out = b"Try POST /echo";
res.headers.set(ContentLength(out.len())); res.headers.set(ContentLength(out.len()));
try!(res.write(out)); try_continue!(res.write(out));
return res.end(); try_continue!(res.end());
continue;
}, },
(&Post, "/echo") => (), // fall through, fighting mutable borrows (&Post, "/echo") => (), // fall through, fighting mutable borrows
_ => { _ => {
res.status = hyper::status::NotFound; res.status = hyper::status::NotFound;
return res.end(); try_continue!(res.end());
continue;
} }
}, },
_ => return res.end() _ => {
try_continue!(res.end());
continue;
}
}; };
try!(copy(&mut req, &mut res)); try_continue!(copy(&mut req, &mut res));
res.end() try_continue!(res.end());
}
} }
} }

View File

@@ -1,6 +1,6 @@
//! HTTP Server //! HTTP Server
use std::io::net::tcp::{TcpListener, TcpAcceptor}; use std::io::net::tcp::{TcpListener, TcpAcceptor};
use std::io::{Acceptor, Listener, IoResult, EndOfFile}; use std::io::{Acceptor, Listener, IoResult, EndOfFile, IncomingConnections};
use std::io::net::ip::{IpAddr, Port, SocketAddr}; use std::io::net::ip::{IpAddr, Port, SocketAddr};
pub use self::request::Request; pub use self::request::Request;
@@ -30,7 +30,7 @@ impl Server {
} }
/// Binds to a socket, and starts handling connections. /// Binds to a socket, and starts handling connections.
pub fn listen<H: Handler + 'static>(self, mut handler: H) -> IoResult<Listening> { pub fn listen<H: Handler + 'static>(self, handler: H) -> IoResult<Listening> {
let mut listener = try!(TcpListener::bind(self.ip.to_string().as_slice(), self.port)); let mut listener = try!(TcpListener::bind(self.ip.to_string().as_slice(), self.port));
let socket = try!(listener.socket_name()); let socket = try!(listener.socket_name());
let acceptor = try!(listener.listen()); let acceptor = try!(listener.listen());
@@ -38,7 +38,26 @@ impl Server {
spawn(proc() { spawn(proc() {
let mut acceptor = worker; let mut acceptor = worker;
for conn in acceptor.incoming() { handler.handle(Incoming { from: acceptor.incoming() });
});
Ok(Listening {
acceptor: acceptor,
socket_addr: socket,
})
}
}
/// An iterator over incoming connections, represented as pairs of
/// hyper Requests and Responses.
pub struct Incoming<'a> {
from: IncomingConnections<'a, TcpAcceptor>
}
impl<'a> Iterator<(Request, Response)> for Incoming<'a> {
fn next(&mut self) -> Option<(Request, Response)> {
for conn in self.from {
match conn { match conn {
Ok(stream) => { Ok(stream) => {
debug!("Incoming stream"); debug!("Incoming stream");
@@ -52,28 +71,17 @@ impl Server {
}; };
let mut res = Response::new(clone); let mut res = Response::new(clone);
res.version = req.version; res.version = req.version;
match handler.handle(req, res) { return Some((req, res))
Ok(..) => debug!("Stream handled"),
Err(e) => {
error!("Error from handler: {}", e)
//TODO try to send a status code
}
}
}, },
Err(ref e) if e.kind == EndOfFile => break, // server closed Err(ref e) if e.kind == EndOfFile => return None, // server closed
Err(e) => { Err(e) => {
error!("Connection failed: {}", e); error!("Connection failed: {}", e);
continue;
} }
} }
} }
}); None
Ok(Listening {
acceptor: acceptor,
socket_addr: socket,
})
} }
} }
/// A listening server, which can later be closed. /// A listening server, which can later be closed.
@@ -96,11 +104,12 @@ pub trait Handler: Send {
/// Receives a `Request`/`Response` pair, and should perform some action on them. /// Receives a `Request`/`Response` pair, and should perform some action on them.
/// ///
/// This could reading from the request, and writing to the response. /// This could reading from the request, and writing to the response.
fn handle(&mut self, req: Request, res: Response) -> IoResult<()>; fn handle(self, Incoming);
} }
impl Handler for fn(Request, Response) -> IoResult<()> { impl Handler for fn(Incoming) {
fn handle(&mut self, req: Request, res: Response) -> IoResult<()> { fn handle(self, incoming: Incoming) {
(*self)(req, res) (self)(incoming)
} }
} }