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.
50 lines
1.1 KiB
Rust
50 lines
1.1 KiB
Rust
#![deny(warnings)]
|
|
extern crate futures;
|
|
extern crate hyper;
|
|
extern crate tokio_core;
|
|
|
|
extern crate pretty_env_logger;
|
|
|
|
use std::env;
|
|
use std::io::{self, Write};
|
|
|
|
use futures::Future;
|
|
use futures::stream::Stream;
|
|
|
|
use hyper::Client;
|
|
|
|
fn main() {
|
|
pretty_env_logger::init().unwrap();
|
|
|
|
let url = match env::args().nth(1) {
|
|
Some(url) => url,
|
|
None => {
|
|
println!("Usage: client <url>");
|
|
return;
|
|
}
|
|
};
|
|
|
|
let url = hyper::Url::parse(&url).unwrap();
|
|
if url.scheme() != "http" {
|
|
println!("This example only works with 'http' URLs.");
|
|
return;
|
|
}
|
|
|
|
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();
|
|
}
|