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
28 #include <nghttp2/asio_http2_client.h>
29
30 using boost::asio::ip::tcp;
31
32 using namespace nghttp2::asio_http2;
33 using namespace nghttp2::asio_http2::client;
34
main(int argc,char * argv[])35 int main(int argc, char *argv[]) {
36 try {
37 if (argc < 2) {
38 std::cerr << "Usage: asio-cl URI" << std::endl;
39 return 1;
40 }
41 boost::system::error_code ec;
42 boost::asio::io_service io_service;
43
44 std::string uri = argv[1];
45 std::string scheme, host, service;
46
47 if (host_service_from_uri(ec, scheme, host, service, uri)) {
48 std::cerr << "error: bad URI: " << ec.message() << std::endl;
49 return 1;
50 }
51
52 boost::asio::ssl::context tls_ctx(boost::asio::ssl::context::sslv23);
53 tls_ctx.set_default_verify_paths();
54 // disabled to make development easier...
55 // tls_ctx.set_verify_mode(boost::asio::ssl::verify_peer);
56 configure_tls_context(ec, tls_ctx);
57
58 auto sess = scheme == "https" ? session(io_service, tls_ctx, host, service)
59 : session(io_service, host, service);
60
61 sess.on_connect([&sess, &uri](tcp::resolver::iterator endpoint_it) {
62 boost::system::error_code ec;
63 auto req = sess.submit(ec, "GET", uri);
64
65 if (ec) {
66 std::cerr << "error: " << ec.message() << std::endl;
67 return;
68 }
69
70 req->on_response([](const response &res) {
71 std::cerr << "HTTP/2 " << res.status_code() << std::endl;
72 for (auto &kv : res.header()) {
73 std::cerr << kv.first << ": " << kv.second.value << "\n";
74 }
75 std::cerr << std::endl;
76
77 res.on_data([](const uint8_t *data, std::size_t len) {
78 std::cerr.write(reinterpret_cast<const char *>(data), len);
79 std::cerr << std::endl;
80 });
81 });
82
83 req->on_close([&sess](uint32_t error_code) { sess.shutdown(); });
84 });
85
86 sess.on_error([](const boost::system::error_code &ec) {
87 std::cerr << "error: " << ec.message() << std::endl;
88 });
89
90 io_service.run();
91 } catch (std::exception &e) {
92 std::cerr << "exception: " << e.what() << "\n";
93 }
94
95 return 0;
96 }
97