Files
h2/examples/client.rs
Carl Lerche c122e97127 Refactor errors (#46)
This patch does a bunch of refactoring, mostly around error types, but it also
paves the way to allow `Codec` to be used standalone.

* `Codec` (and `FramedRead` / `FramedWrite`) is broken out into a codec module.
* An h2-codec crate is created that re-exports the frame and codec modules.
* New error types are introduced in the internals:
  * `RecvError` represents errors caused by trying to receive a frame.
  * `SendError` represents errors caused by trying to send a frame.
  * `UserError` is an enum of potential errors caused by invalid usage
    by the user of the lib.
  * `ProtoError` is either a `Reason` or an `io::Error`. However it doesn't
    specify connection or stream level.
  * `h2::Error` is an opaque error type and is the only error type exposed
    by the public API (used to be `ConnectionError`).

There are misc code changes to enable this as well. The biggest is a new "sink"
API for `Codec`. It provides buffer which queues up a frame followed by flush
which writes everything that is queued. This departs from the `Sink` trait in
order to provide more accurate error values. For example, buffer can never fail
(but it will panic if `poll_ready` is not called first).
2017-09-02 11:12:50 -07:00

103 lines
2.3 KiB
Rust

extern crate h2;
extern crate http;
extern crate bytes;
extern crate futures;
extern crate tokio_io;
extern crate tokio_core;
extern crate io_dump;
extern crate env_logger;
use h2::*;
use h2::client::{Client, Body};
use http::*;
use futures::*;
use bytes::*;
use tokio_core::reactor;
use tokio_core::net::TcpStream;
struct Process {
body: Body<Bytes>,
trailers: bool,
}
impl Future for Process {
type Item = ();
type Error = h2::Error;
fn poll(&mut self) -> Poll<(), h2::Error> {
loop {
if self.trailers {
let trailers = try_ready!(self.body.poll_trailers());
println!("GOT TRAILERS: {:?}", trailers);
return Ok(().into());
} else {
match try_ready!(self.body.poll()) {
Some(chunk) => {
println!("GOT CHUNK = {:?}", chunk);
}
None => {
self.trailers = true;
}
}
}
}
}
}
pub fn main() {
let _ = env_logger::init();
let mut core = reactor::Core::new().unwrap();;
let handle = core.handle();
let tcp = TcpStream::connect(
&"127.0.0.1:5928".parse().unwrap(),
&handle);
let tcp = tcp.then(|res| {
let tcp = io_dump::Dump::to_stdout(res.unwrap());
Client::handshake(tcp)
})
.then(|res| {
let mut client = res.unwrap();
println!("sending request");
let request = Request::builder()
.uri("https://http2.akamai.com/")
.body(()).unwrap();
let mut trailers = HeaderMap::new();
trailers.insert("zomg", "hello".parse().unwrap());
let mut stream = client.request(request, false).unwrap();
// send trailers
stream.send_trailers(trailers).unwrap();
// Spawn a task to run the client...
handle.spawn(client.map_err(|e| println!("GOT ERR={:?}", e)));
stream.and_then(|response| {
println!("GOT RESPONSE: {:?}", response);
// Get the body
let (_, body) = response.into_parts();
Process {
body,
trailers: false,
}
}).map_err(|e| {
println!("GOT ERR={:?}", e);
})
})
;
core.run(tcp).unwrap();
}