1 extern crate env_logger;
2 extern crate futures;
3 extern crate h2;
4 extern crate http;
5 extern crate tokio;
6
7 use h2::client;
8 use h2::RecvStream;
9
10 use futures::*;
11 use http::*;
12
13 use tokio::net::TcpStream;
14
15 struct Process {
16 body: RecvStream,
17 trailers: bool,
18 }
19
20 impl Future for Process {
21 type Item = ();
22 type Error = h2::Error;
23
poll(&mut self) -> Poll<(), h2::Error>24 fn poll(&mut self) -> Poll<(), h2::Error> {
25 loop {
26 if self.trailers {
27 let trailers = try_ready!(self.body.poll_trailers());
28
29 println!("GOT TRAILERS: {:?}", trailers);
30
31 return Ok(().into());
32 } else {
33 match try_ready!(self.body.poll()) {
34 Some(chunk) => {
35 println!("GOT CHUNK = {:?}", chunk);
36 },
37 None => {
38 self.trailers = true;
39 },
40 }
41 }
42 }
43 }
44 }
45
main()46 pub fn main() {
47 let _ = env_logger::try_init();
48
49 let tcp = TcpStream::connect(&"127.0.0.1:5928".parse().unwrap());
50
51 let tcp = tcp.then(|res| {
52 let tcp = res.unwrap();
53 client::handshake(tcp)
54 }).then(|res| {
55 let (mut client, h2) = res.unwrap();
56
57 println!("sending request");
58
59 let request = Request::builder()
60 .uri("https://http2.akamai.com/")
61 .body(())
62 .unwrap();
63
64 let mut trailers = HeaderMap::new();
65 trailers.insert("zomg", "hello".parse().unwrap());
66
67 let (response, mut stream) = client.send_request(request, false).unwrap();
68
69 // send trailers
70 stream.send_trailers(trailers).unwrap();
71
72 // Spawn a task to run the conn...
73 tokio::spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
74
75 response
76 .and_then(|response| {
77 println!("GOT RESPONSE: {:?}", response);
78
79 // Get the body
80 let (_, body) = response.into_parts();
81
82 Process {
83 body,
84 trailers: false,
85 }
86 })
87 .map_err(|e| {
88 println!("GOT ERR={:?}", e);
89 })
90 });
91
92 tokio::run(tcp);
93 }
94