2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2015 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
28 #include <nghttp2/asio_http2_client.h>
30 using boost::asio::ip::tcp;
32 using namespace nghttp2::asio_http2;
33 using namespace nghttp2::asio_http2::client;
35 int main(int argc, char *argv[]) {
38 std::cerr << "Usage: asio-cl URI" << std::endl;
41 boost::system::error_code ec;
42 boost::asio::io_service io_service;
44 std::string uri = argv[1];
45 std::string scheme, host, service;
47 if (host_service_from_uri(ec, scheme, host, service, uri)) {
48 std::cerr << "error: bad URI: " << ec.message() << std::endl;
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);
58 auto sess = scheme == "https" ? session(io_service, tls_ctx, host, service)
59 : session(io_service, host, service);
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);
66 std::cerr << "error: " << ec.message() << std::endl;
70 req->on_response([&sess](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";
75 std::cerr << std::endl;
77 res.on_data([&sess](const uint8_t *data, std::size_t len) {
78 std::cerr.write(reinterpret_cast<const char *>(data), len);
79 std::cerr << std::endl;
83 req->on_close([&sess](uint32_t error_code) { sess.shutdown(); });
86 sess.on_error([](const boost::system::error_code &ec) {
87 std::cerr << "error: " << ec.message() << std::endl;
91 } catch (std::exception &e) {
92 std::cerr << "exception: " << e.what() << "\n";