Abstract out NetworkStream
This introduces a new Trait, NetworkStream, which abstracts over the functionality provided by TcpStream so that it can be easily mocked and extended in testing and hyper can be used for other connection sources.
This commit is contained in:
committed by
Jonathan Reem
parent
a8d7b681da
commit
0285fc2acc
@@ -1,4 +1,4 @@
|
||||
#![feature(macro_rules)]
|
||||
#![feature(macro_rules, default_type_params)]
|
||||
|
||||
extern crate hyper;
|
||||
extern crate debug;
|
||||
@@ -10,15 +10,16 @@ use std::sync::Arc;
|
||||
use hyper::{Get, Post};
|
||||
use hyper::server::{Server, Handler, Incoming, Request, Response, Fresh};
|
||||
use hyper::header::common::ContentLength;
|
||||
use hyper::net::{HttpStream, HttpAcceptor};
|
||||
|
||||
trait ConcurrentHandler: Send + Sync {
|
||||
fn handle(&self, req: Request, res: Response<Fresh>);
|
||||
fn handle(&self, req: Request, res: Response<Fresh, HttpStream>);
|
||||
}
|
||||
|
||||
struct Concurrent<H: ConcurrentHandler> { handler: Arc<H> }
|
||||
|
||||
impl<H: ConcurrentHandler> Handler for Concurrent<H> {
|
||||
fn handle(self, mut incoming: Incoming) {
|
||||
impl<H: ConcurrentHandler> Handler<HttpAcceptor, HttpStream> for Concurrent<H> {
|
||||
fn handle(self, mut incoming: Incoming<HttpAcceptor>) {
|
||||
for (mut req, mut res) in incoming {
|
||||
let clone = self.handler.clone();
|
||||
spawn(proc() { clone.handle(req, res) })
|
||||
@@ -38,7 +39,7 @@ macro_rules! try_abort(
|
||||
struct Echo;
|
||||
|
||||
impl ConcurrentHandler for Echo {
|
||||
fn handle(&self, mut req: Request, mut res: Response<Fresh>) {
|
||||
fn handle(&self, mut req: Request, mut res: Response<Fresh, HttpStream>) {
|
||||
match req.uri {
|
||||
hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
|
||||
(&Get, "/") | (&Get, "/echo") => {
|
||||
|
||||
@@ -7,10 +7,8 @@ use std::io::util::copy;
|
||||
use std::io::net::ip::Ipv4Addr;
|
||||
|
||||
use hyper::{Get, Post};
|
||||
use hyper::server::{Server, Handler, Incoming};
|
||||
use hyper::header::common::ContentLength;
|
||||
|
||||
struct Echo;
|
||||
use hyper::server::{Server, Incoming};
|
||||
|
||||
macro_rules! try_continue(
|
||||
($e:expr) => {{
|
||||
@@ -21,41 +19,39 @@ macro_rules! try_continue(
|
||||
}}
|
||||
)
|
||||
|
||||
impl Handler for Echo {
|
||||
fn handle(self, mut incoming: Incoming) {
|
||||
for (mut req, mut res) in incoming {
|
||||
match req.uri {
|
||||
hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
|
||||
(&Get, "/") | (&Get, "/echo") => {
|
||||
let out = b"Try POST /echo";
|
||||
fn echo(mut incoming: Incoming) {
|
||||
for (mut req, mut res) in incoming {
|
||||
match req.uri {
|
||||
hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
|
||||
(&Get, "/") | (&Get, "/echo") => {
|
||||
let out = b"Try POST /echo";
|
||||
|
||||
res.headers_mut().set(ContentLength(out.len()));
|
||||
let mut res = try_continue!(res.start());
|
||||
try_continue!(res.write(out));
|
||||
try_continue!(res.end());
|
||||
continue;
|
||||
},
|
||||
(&Post, "/echo") => (), // fall through, fighting mutable borrows
|
||||
_ => {
|
||||
*res.status_mut() = hyper::status::NotFound;
|
||||
try_continue!(res.start().and_then(|res| res.end()));
|
||||
continue;
|
||||
}
|
||||
res.headers_mut().set(ContentLength(out.len()));
|
||||
let mut res = try_continue!(res.start());
|
||||
try_continue!(res.write(out));
|
||||
try_continue!(res.end());
|
||||
continue;
|
||||
},
|
||||
(&Post, "/echo") => (), // fall through, fighting mutable borrows
|
||||
_ => {
|
||||
*res.status_mut() = hyper::status::NotFound;
|
||||
try_continue!(res.start().and_then(|res| res.end()));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
},
|
||||
_ => {
|
||||
try_continue!(res.start().and_then(|res| res.end()));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let mut res = try_continue!(res.start());
|
||||
try_continue!(copy(&mut req, &mut res));
|
||||
try_continue!(res.end());
|
||||
}
|
||||
let mut res = try_continue!(res.start());
|
||||
try_continue!(copy(&mut req, &mut res));
|
||||
try_continue!(res.end());
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 1337);
|
||||
server.listen(Echo).unwrap();
|
||||
server.listen(echo).unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user