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.
29 #include <nghttp2/asio_http2_client.h>
31 using boost::asio::ip::tcp;
33 using namespace nghttp2::asio_http2;
34 using namespace nghttp2::asio_http2::client;
36 void print_header(const header_map &h) {
38 std::cerr << kv.first << ": " << kv.second.value << "\n";
40 std::cerr << std::endl;
43 void print_header(const response &res) {
44 std::cerr << "HTTP/2 " << res.status_code() << "\n";
45 print_header(res.header());
48 void print_header(const request &req) {
49 auto &uri = req.uri();
50 std::cerr << req.method() << " " << uri.scheme << "://" << uri.host
52 if (!uri.raw_query.empty()) {
53 std::cerr << "?" << uri.raw_query;
55 std::cerr << " HTTP/2\n";
56 print_header(req.header());
59 int main(int argc, char *argv[]) {
62 std::cerr << "Usage: asio-cl URI" << std::endl;
65 boost::system::error_code ec;
66 boost::asio::io_service io_service;
68 std::string uri = argv[1];
69 std::string scheme, host, service;
71 if (host_service_from_uri(ec, scheme, host, service, uri)) {
72 std::cerr << "error: bad URI: " << ec.message() << std::endl;
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);
82 auto sess = scheme == "https" ? session(io_service, tls_ctx, host, service)
83 : session(io_service, host, service);
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}}});
90 std::cerr << "error: " << ec.message() << std::endl;
94 req->on_response([&sess, req](const response &res) {
95 std::cerr << "response header was received" << std::endl;
98 res.on_data([&sess](const uint8_t *data, std::size_t len) {
99 std::cerr.write(reinterpret_cast<const char *>(data), len);
100 std::cerr << std::endl;
104 req->on_close([&sess](uint32_t error_code) {
105 std::cerr << "request done with error_code=" << error_code << std::endl;
108 req->on_push([](const request &push_req) {
109 std::cerr << "push request was received" << std::endl;
111 print_header(push_req);
113 push_req.on_response([](const response &res) {
114 std::cerr << "push response header was received" << std::endl;
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;
124 sess.on_error([](const boost::system::error_code &ec) {
125 std::cerr << "error: " << ec.message() << std::endl;
129 } catch (std::exception &e) {
130 std::cerr << "exception: " << e.what() << "\n";