Files
hyper/examples/server.rs
Sean McArthur 3c10a8a191 feat(server): change Incoming to iterator over Connections
A connection is returned from Incoming.next(), and can be passed to a
separate thread before any parsing happens. Call conn.open() to get a
Result<(Request, Response)>.

BREAKING CHANGE
2014-11-10 13:55:11 -08:00

58 lines
1.7 KiB
Rust

#![feature(macro_rules)]
extern crate hyper;
use std::io::util::copy;
use std::io::net::ip::Ipv4Addr;
use hyper::{Get, Post};
use hyper::header::common::ContentLength;
use hyper::server::{Server, Incoming};
macro_rules! try_continue(
($e:expr) => {{
match $e {
Ok(v) => v,
Err(e) => { println!("Error: {}", e); continue; }
}
}}
)
fn echo(mut incoming: Incoming) {
for conn in incoming {
let (mut req, mut res) = try_continue!(conn.open());
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;
}
},
_ => {
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());
}
}
fn main() {
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 1337);
server.listen(echo).unwrap();
}