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,68 +1,20 @@
#![deny(warnings)]
extern crate futures;
extern crate hyper;
extern crate tokio_core;
extern crate env_logger;
extern crate pretty_env_logger;
use std::env;
use std::io;
use std::sync::mpsc;
use std::time::Duration;
use std::io::{self, Write};
use hyper::client::{Client, Request, Response, DefaultTransport as HttpStream};
use hyper::header::Connection;
use hyper::{Decoder, Encoder, Next};
use futures::Future;
use futures::stream::Stream;
#[derive(Debug)]
struct Dump(mpsc::Sender<()>);
impl Drop for Dump {
fn drop(&mut self) {
let _ = self.0.send(());
}
}
fn read() -> Next {
Next::read().timeout(Duration::from_secs(10))
}
impl hyper::client::Handler<HttpStream> for Dump {
fn on_request(&mut self, req: &mut Request) -> Next {
req.headers_mut().set(Connection::close());
read()
}
fn on_request_writable(&mut self, _encoder: &mut Encoder<HttpStream>) -> Next {
read()
}
fn on_response(&mut self, res: Response) -> Next {
println!("Response: {}", res.status());
println!("Headers:\n{}", res.headers());
read()
}
fn on_response_readable(&mut self, decoder: &mut Decoder<HttpStream>) -> Next {
match io::copy(decoder, &mut io::stdout()) {
Ok(0) => Next::end(),
Ok(_) => read(),
Err(e) => match e.kind() {
io::ErrorKind::WouldBlock => Next::read(),
_ => {
println!("ERROR:example: {}", e);
Next::end()
}
}
}
}
fn on_error(&mut self, err: hyper::Error) -> Next {
println!("ERROR:example: {}", err);
Next::remove()
}
}
use hyper::Client;
fn main() {
env_logger::init().unwrap();
pretty_env_logger::init().unwrap();
let url = match env::args().nth(1) {
Some(url) => url,
@@ -72,11 +24,26 @@ fn main() {
}
};
let (tx, rx) = mpsc::channel();
let client = Client::new().expect("Failed to create a Client");
client.request(url.parse().unwrap(), Dump(tx)).unwrap();
let url = hyper::Url::parse(&url).unwrap();
if url.scheme() != "http" {
println!("This example only works with 'http' URLs.");
return;
}
// wait till done
let _ = rx.recv();
client.close();
let mut core = tokio_core::reactor::Core::new().unwrap();
let handle = core.handle();
let client = Client::new(&handle);
let work = client.get(url).and_then(|res| {
println!("Response: {}", res.status());
println!("Headers: \n{}", res.headers());
res.body().for_each(|chunk| {
io::stdout().write_all(&chunk).map_err(From::from)
})
}).map(|_| {
println!("\n\nDone.");
});
core.run(work).unwrap();
}