]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/http/client/coro-ssl/http_client_coro_ssl.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / beast / example / http / client / coro-ssl / http_client_coro_ssl.cpp
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 //------------------------------------------------------------------------------
11 //
12 // Example: HTTP SSL client, coroutine
13 //
14 //------------------------------------------------------------------------------
15
16 #include "example/common/root_certificates.hpp"
17
18 #include <boost/beast/core.hpp>
19 #include <boost/beast/http.hpp>
20 #include <boost/beast/version.hpp>
21 #include <boost/asio/connect.hpp>
22 #include <boost/asio/spawn.hpp>
23 #include <boost/asio/ip/tcp.hpp>
24 #include <boost/asio/ssl/error.hpp>
25 #include <boost/asio/ssl/stream.hpp>
26 #include <cstdlib>
27 #include <functional>
28 #include <iostream>
29 #include <string>
30
31 using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
32 namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
33 namespace http = boost::beast::http; // from <boost/beast/http.hpp>
34
35 //------------------------------------------------------------------------------
36
37 // Report a failure
38 void
39 fail(boost::system::error_code ec, char const* what)
40 {
41 std::cerr << what << ": " << ec.message() << "\n";
42 }
43
44 // Performs an HTTP GET and prints the response
45 void
46 do_session(
47 std::string const& host,
48 std::string const& port,
49 std::string const& target,
50 int version,
51 boost::asio::io_context& ioc,
52 ssl::context& ctx,
53 boost::asio::yield_context yield)
54 {
55 boost::system::error_code ec;
56
57 // These objects perform our I/O
58 tcp::resolver resolver{ioc};
59 ssl::stream<tcp::socket> stream{ioc, ctx};
60
61 // Set SNI Hostname (many hosts need this to handshake successfully)
62 if(! SSL_set_tlsext_host_name(stream.native_handle(), host.c_str()))
63 {
64 ec.assign(static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category());
65 std::cerr << ec.message() << "\n";
66 return;
67 }
68
69 // Look up the domain name
70 auto const results = resolver.async_resolve(host, port, yield[ec]);
71 if(ec)
72 return fail(ec, "resolve");
73
74 // Make the connection on the IP address we get from a lookup
75 boost::asio::async_connect(stream.next_layer(), results.begin(), results.end(), yield[ec]);
76 if(ec)
77 return fail(ec, "connect");
78
79 // Perform the SSL handshake
80 stream.async_handshake(ssl::stream_base::client, yield[ec]);
81 if(ec)
82 return fail(ec, "handshake");
83
84 // Set up an HTTP GET request message
85 http::request<http::string_body> req{http::verb::get, target, version};
86 req.set(http::field::host, host);
87 req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
88
89 // Send the HTTP request to the remote host
90 http::async_write(stream, req, yield[ec]);
91 if(ec)
92 return fail(ec, "write");
93
94 // This buffer is used for reading and must be persisted
95 boost::beast::flat_buffer b;
96
97 // Declare a container to hold the response
98 http::response<http::dynamic_body> res;
99
100 // Receive the HTTP response
101 http::async_read(stream, b, res, yield[ec]);
102 if(ec)
103 return fail(ec, "read");
104
105 // Write the message to standard out
106 std::cout << res << std::endl;
107
108 // Gracefully close the stream
109 stream.async_shutdown(yield[ec]);
110 if(ec == boost::asio::error::eof)
111 {
112 // Rationale:
113 // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
114 ec.assign(0, ec.category());
115 }
116 if(ec)
117 return fail(ec, "shutdown");
118
119 // If we get here then the connection is closed gracefully
120 }
121
122 //------------------------------------------------------------------------------
123
124 int main(int argc, char** argv)
125 {
126 // Check command line arguments.
127 if(argc != 4 && argc != 5)
128 {
129 std::cerr <<
130 "Usage: http-client-coro-ssl <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" <<
131 "Example:\n" <<
132 " http-client-coro-ssl www.example.com 443 /\n" <<
133 " http-client-coro-ssl www.example.com 443 / 1.0\n";
134 return EXIT_FAILURE;
135 }
136 auto const host = argv[1];
137 auto const port = argv[2];
138 auto const target = argv[3];
139 int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
140
141 // The io_context is required for all I/O
142 boost::asio::io_context ioc;
143
144 // The SSL context is required, and holds certificates
145 ssl::context ctx{ssl::context::sslv23_client};
146
147 // This holds the root certificate used for verification
148 load_root_certificates(ctx);
149
150 // Launch the asynchronous operation
151 boost::asio::spawn(ioc, std::bind(
152 &do_session,
153 std::string(host),
154 std::string(port),
155 std::string(target),
156 version,
157 std::ref(ioc),
158 std::ref(ctx),
159 std::placeholders::_1));
160
161 // Run the I/O service. The call will return when
162 // the get operation is complete.
163 ioc.run();
164
165 return EXIT_SUCCESS;
166 }