Files
h2/examples/client.rs
Brian Smith b6724f7d7a Upgrade to env_logger 0.5 & log 0.4; reduce related dependencies (#226)
Upgrade to env_logger 0.5 and log 0.4 so that projects that use those
versions don't have to build both those versions and the older ones
that h2 is currently using.

Don't enable the regex support in env_logger. Applications that want
the regex support can enable it themselves; this will happen
automatically when they add their env_logger dependency.

Disable the env_logger dependency in quickcheck.

The result of this is that there are fewer dependencies. For example,
regex and its dependencies are no longer required at all, as can be
seen by observing the changes to the Cargo.lock. That said,
env_logger 0.5 does add more dependencies itself; however it seems
applications are going to use env_logger 0.5 anyway so this is still
a net gain.

Submitted on behalf of Buoyant, Inc.

Signed-off-by: Brian Smith <brian@briansmith.org>
2018-02-23 20:25:42 -08:00

98 lines
2.4 KiB
Rust

extern crate env_logger;
extern crate futures;
extern crate h2;
extern crate http;
extern crate tokio_core;
use h2::client;
use h2::RecvStream;
use futures::*;
use http::*;
use tokio_core::net::TcpStream;
use tokio_core::reactor;
struct Process {
body: RecvStream,
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::try_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 = res.unwrap();
client::handshake(tcp)
}).then(|res| {
let (mut client, h2) = 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 (response, mut stream) = client.send_request(request, false).unwrap();
// send trailers
stream.send_trailers(trailers).unwrap();
// Spawn a task to run the conn...
handle.spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
response
.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();
}