• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2015 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <iostream>
27 #include <string>
28 
29 #include <nghttp2/asio_http2_client.h>
30 
31 using boost::asio::ip::tcp;
32 
33 using namespace nghttp2::asio_http2;
34 using namespace nghttp2::asio_http2::client;
35 
print_header(const header_map & h)36 void print_header(const header_map &h) {
37   for (auto &kv : h) {
38     std::cerr << kv.first << ": " << kv.second.value << "\n";
39   }
40   std::cerr << std::endl;
41 }
42 
print_header(const response & res)43 void print_header(const response &res) {
44   std::cerr << "HTTP/2 " << res.status_code() << "\n";
45   print_header(res.header());
46 }
47 
print_header(const request & req)48 void print_header(const request &req) {
49   auto &uri = req.uri();
50   std::cerr << req.method() << " " << uri.scheme << "://" << uri.host
51             << uri.path;
52   if (!uri.raw_query.empty()) {
53     std::cerr << "?" << uri.raw_query;
54   }
55   std::cerr << " HTTP/2\n";
56   print_header(req.header());
57 }
58 
main(int argc,char * argv[])59 int main(int argc, char *argv[]) {
60   try {
61     if (argc < 2) {
62       std::cerr << "Usage: asio-cl URI" << std::endl;
63       return 1;
64     }
65     boost::system::error_code ec;
66     boost::asio::io_service io_service;
67 
68     std::string uri = argv[1];
69     std::string scheme, host, service;
70 
71     if (host_service_from_uri(ec, scheme, host, service, uri)) {
72       std::cerr << "error: bad URI: " << ec.message() << std::endl;
73       return 1;
74     }
75 
76     boost::asio::ssl::context tls_ctx(boost::asio::ssl::context::sslv23);
77     tls_ctx.set_default_verify_paths();
78     // disabled to make development easier...
79     // tls_ctx.set_verify_mode(boost::asio::ssl::verify_peer);
80     configure_tls_context(ec, tls_ctx);
81 
82     auto sess = scheme == "https" ? session(io_service, tls_ctx, host, service)
83                                   : session(io_service, host, service);
84 
85     sess.on_connect([&sess, &uri](tcp::resolver::iterator endpoint_it) {
86       std::cerr << "connected to " << (*endpoint_it).endpoint() << std::endl;
87       boost::system::error_code ec;
88       auto req = sess.submit(ec, "GET", uri, {{"cookie", {"foo=bar", true}}});
89       if (ec) {
90         std::cerr << "error: " << ec.message() << std::endl;
91         return;
92       }
93 
94       req->on_response([](const response &res) {
95         std::cerr << "response header was received" << std::endl;
96         print_header(res);
97 
98         res.on_data([](const uint8_t *data, std::size_t len) {
99           std::cerr.write(reinterpret_cast<const char *>(data), len);
100           std::cerr << std::endl;
101         });
102       });
103 
104       req->on_close([](uint32_t error_code) {
105         std::cerr << "request done with error_code=" << error_code << std::endl;
106       });
107 
108       req->on_push([](const request &push_req) {
109         std::cerr << "push request was received" << std::endl;
110 
111         print_header(push_req);
112 
113         push_req.on_response([](const response &res) {
114           std::cerr << "push response header was received" << std::endl;
115 
116           res.on_data([](const uint8_t *data, std::size_t len) {
117             std::cerr.write(reinterpret_cast<const char *>(data), len);
118             std::cerr << std::endl;
119           });
120         });
121       });
122     });
123 
124     sess.on_error([](const boost::system::error_code &ec) {
125       std::cerr << "error: " << ec.message() << std::endl;
126     });
127 
128     io_service.run();
129   } catch (std::exception &e) {
130     std::cerr << "exception: " << e.what() << "\n";
131   }
132 
133   return 0;
134 }
135