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,50 +1,39 @@
#![deny(warnings)]
extern crate hyper;
extern crate env_logger;
extern crate num_cpus;
extern crate futures;
extern crate pretty_env_logger;
//extern crate num_cpus;
use hyper::{Decoder, Encoder, Next, HttpStream};
use hyper::server::{Server, Handler, Request, Response, HttpListener};
use hyper::header::{ContentLength, ContentType};
use hyper::server::{Server, Service, Request, Response};
static PHRASE: &'static [u8] = b"Hello World!";
#[derive(Clone, Copy)]
struct Hello;
impl Handler<HttpStream> for Hello {
fn on_request(&mut self, _: Request<HttpStream>) -> Next {
Next::write()
}
fn on_request_readable(&mut self, _: &mut Decoder<HttpStream>) -> Next {
Next::write()
}
fn on_response(&mut self, response: &mut Response) -> Next {
use hyper::header::ContentLength;
response.headers_mut().set(ContentLength(PHRASE.len() as u64));
Next::write()
}
fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next {
let n = encoder.write(PHRASE).unwrap();
debug_assert_eq!(n, PHRASE.len());
Next::end()
impl Service for Hello {
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(
Response::new()
.with_header(ContentLength(PHRASE.len() as u64))
.with_header(ContentType::plaintext())
.with_body(PHRASE)
)
}
}
fn main() {
env_logger::init().unwrap();
let listener = HttpListener::bind(&"127.0.0.1:3000".parse().unwrap()).unwrap();
let mut handles = Vec::new();
for _ in 0..num_cpus::get() {
let listener = listener.try_clone().unwrap();
handles.push(::std::thread::spawn(move || {
Server::new(listener)
.handle(|_| Hello).unwrap();
}));
}
println!("Listening on http://127.0.0.1:3000");
for handle in handles {
handle.join().unwrap();
}
pretty_env_logger::init().unwrap();
let addr = "127.0.0.1:3000".parse().unwrap();
let _server = Server::standalone(|tokio| {
Server::http(&addr, tokio)?
.handle(|| Ok(Hello), tokio)
}).unwrap();
println!("Listening on http://{}", addr);
}