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
@@ -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