feat(lib): redesign API to use Futures and Tokio

There are many changes involved with this, but let's just talk about
user-facing changes.

- Creating a `Client` and `Server` now needs a Tokio `Core` event loop
to attach to.
- `Request` and `Response` both no longer implement the
`std::io::{Read,Write}` traits, but instead represent their bodies as a
`futures::Stream` of items, where each item is a `Chunk`.
- The `Client.request` method now takes a `Request`, instead of being
used as a builder, and returns a `Future` that resolves to `Response`.
- The `Handler` trait for servers is no more, and instead the Tokio
`Service` trait is used. This allows interoperability with generic
middleware.

BREAKING CHANGE: A big sweeping set of breaking changes.
This commit is contained in:
Sean McArthur
2016-11-17 17:31:42 -08:00
parent e23689122a
commit 2d2d5574a6
43 changed files with 2775 additions and 5033 deletions

View File

@@ -1,164 +1,57 @@
#![deny(warnings)]
//#![deny(warnings)]
extern crate futures;
extern crate hyper;
extern crate env_logger;
extern crate pretty_env_logger;
#[macro_use]
extern crate log;
use hyper::{Get, Post, StatusCode, RequestUri, Decoder, Encoder, HttpStream, Next};
use hyper::{Get, Post, StatusCode};
use hyper::header::ContentLength;
use hyper::server::{Server, Handler, Request, Response};
use hyper::server::{Server, Service, Request, Response};
struct Echo {
buf: Vec<u8>,
read_pos: usize,
write_pos: usize,
eof: bool,
route: Route,
}
enum Route {
NotFound,
Index,
Echo(Body),
}
#[derive(Clone, Copy)]
enum Body {
Len(u64),
Chunked
}
static INDEX: &'static [u8] = b"Try POST /echo";
impl Echo {
fn new() -> Echo {
Echo {
buf: vec![0; 4096],
read_pos: 0,
write_pos: 0,
eof: false,
route: Route::NotFound,
}
}
}
#[derive(Clone, Copy)]
struct Echo;
impl Handler<HttpStream> for Echo {
fn on_request(&mut self, req: Request<HttpStream>) -> Next {
match *req.uri() {
RequestUri::AbsolutePath { ref path, .. } => match (req.method(), &path[..]) {
(&Get, "/") | (&Get, "/echo") => {
info!("GET Index");
self.route = Route::Index;
Next::write()
}
(&Post, "/echo") => {
info!("POST Echo");
let mut is_more = true;
self.route = if let Some(len) = req.headers().get::<ContentLength>() {
is_more = **len > 0;
Route::Echo(Body::Len(**len))
} else {
Route::Echo(Body::Chunked)
};
if is_more {
Next::read_and_write()
} else {
Next::write()
}
}
_ => Next::write(),
impl Service for Echo {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = ::futures::Finished<Response, hyper::Error>;
fn call(&self, req: Request) -> Self::Future {
::futures::finished(match (req.method(), req.path()) {
(&Get, Some("/")) | (&Get, Some("/echo")) => {
Response::new()
.with_header(ContentLength(INDEX.len() as u64))
.with_body(INDEX)
},
_ => Next::write()
}
}
fn on_request_readable(&mut self, transport: &mut Decoder<HttpStream>) -> Next {
match self.route {
Route::Echo(ref body) => {
if self.read_pos < self.buf.len() {
match transport.try_read(&mut self.buf[self.read_pos..]) {
Ok(Some(0)) => {
debug!("Read 0, eof");
self.eof = true;
Next::write()
},
Ok(Some(n)) => {
self.read_pos += n;
match *body {
Body::Len(max) if max <= self.read_pos as u64 => {
self.eof = true;
Next::write()
},
_ => Next::read_and_write()
}
}
Ok(None) => Next::read_and_write(),
Err(e) => {
println!("read error {:?}", e);
Next::end()
}
}
} else {
Next::write()
(&Post, Some("/echo")) => {
let mut res = Response::new();
if let Some(len) = req.headers().get::<ContentLength>() {
res.headers_mut().set(len.clone());
}
res.with_body(req.body())
},
_ => {
Response::new()
.with_status(StatusCode::NotFound)
}
_ => unreachable!()
}
})
}
fn on_response(&mut self, res: &mut Response) -> Next {
match self.route {
Route::NotFound => {
res.set_status(StatusCode::NotFound);
Next::end()
}
Route::Index => {
res.headers_mut().set(ContentLength(INDEX.len() as u64));
Next::write()
}
Route::Echo(body) => {
if let Body::Len(len) = body {
res.headers_mut().set(ContentLength(len));
}
Next::read_and_write()
}
}
}
fn on_response_writable(&mut self, transport: &mut Encoder<HttpStream>) -> Next {
match self.route {
Route::Index => {
transport.write(INDEX).unwrap();
Next::end()
}
Route::Echo(..) => {
if self.write_pos < self.read_pos {
match transport.try_write(&self.buf[self.write_pos..self.read_pos]) {
Ok(Some(0)) => panic!("write ZERO"),
Ok(Some(n)) => {
self.write_pos += n;
Next::write()
}
Ok(None) => Next::write(),
Err(e) => {
println!("write error {:?}", e);
Next::end()
}
}
} else if !self.eof {
Next::read()
} else {
Next::end()
}
}
_ => unreachable!()
}
}
}
fn main() {
env_logger::init().unwrap();
let server = Server::http(&"127.0.0.1:1337".parse().unwrap()).unwrap();
let (listening, server) = server.handle(|_| Echo::new()).unwrap();
pretty_env_logger::init().unwrap();
let addr = "127.0.0.1:1337".parse().unwrap();
let (listening, server) = Server::standalone(|tokio| {
Server::http(&addr, tokio)?
.handle(|| Ok(Echo), tokio)
}).unwrap();
println!("Listening on http://{}", listening);
server.run();
}