1 //
2 // Copyright (c) 2016-2019 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/ssl.hpp>
21 #include <boost/beast/version.hpp>
22 #include <boost/asio/spawn.hpp>
23 #include <cstdlib>
24 #include <functional>
25 #include <iostream>
26 #include <string>
27
28 namespace beast = boost::beast; // from <boost/beast.hpp>
29 namespace http = beast::http; // from <boost/beast/http.hpp>
30 namespace net = boost::asio; // from <boost/asio.hpp>
31 namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
32 using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
33
34 //------------------------------------------------------------------------------
35
36 // Report a failure
37 void
fail(beast::error_code ec,char const * what)38 fail(beast::error_code ec, char const* what)
39 {
40 std::cerr << what << ": " << ec.message() << "\n";
41 }
42
43 // Performs an HTTP GET and prints the response
44 void
do_session(std::string const & host,std::string const & port,std::string const & target,int version,net::io_context & ioc,ssl::context & ctx,net::yield_context yield)45 do_session(
46 std::string const& host,
47 std::string const& port,
48 std::string const& target,
49 int version,
50 net::io_context& ioc,
51 ssl::context& ctx,
52 net::yield_context yield)
53 {
54 beast::error_code ec;
55
56 // These objects perform our I/O
57 tcp::resolver resolver(ioc);
58 beast::ssl_stream<beast::tcp_stream> stream(ioc, ctx);
59
60 // Set SNI Hostname (many hosts need this to handshake successfully)
61 if(! SSL_set_tlsext_host_name(stream.native_handle(), host.c_str()))
62 {
63 ec.assign(static_cast<int>(::ERR_get_error()), net::error::get_ssl_category());
64 std::cerr << ec.message() << "\n";
65 return;
66 }
67
68 // Look up the domain name
69 auto const results = resolver.async_resolve(host, port, yield[ec]);
70 if(ec)
71 return fail(ec, "resolve");
72
73 // Set the timeout.
74 beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
75
76 // Make the connection on the IP address we get from a lookup
77 get_lowest_layer(stream).async_connect(results, yield[ec]);
78 if(ec)
79 return fail(ec, "connect");
80
81 // Set the timeout.
82 beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
83
84 // Perform the SSL handshake
85 stream.async_handshake(ssl::stream_base::client, yield[ec]);
86 if(ec)
87 return fail(ec, "handshake");
88
89 // Set up an HTTP GET request message
90 http::request<http::string_body> req{http::verb::get, target, version};
91 req.set(http::field::host, host);
92 req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
93
94 // Set the timeout.
95 beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
96
97 // Send the HTTP request to the remote host
98 http::async_write(stream, req, yield[ec]);
99 if(ec)
100 return fail(ec, "write");
101
102 // This buffer is used for reading and must be persisted
103 beast::flat_buffer b;
104
105 // Declare a container to hold the response
106 http::response<http::dynamic_body> res;
107
108 // Receive the HTTP response
109 http::async_read(stream, b, res, yield[ec]);
110 if(ec)
111 return fail(ec, "read");
112
113 // Write the message to standard out
114 std::cout << res << std::endl;
115
116 // Set the timeout.
117 beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
118
119 // Gracefully close the stream
120 stream.async_shutdown(yield[ec]);
121 if(ec == net::error::eof)
122 {
123 // Rationale:
124 // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
125 ec = {};
126 }
127 if(ec)
128 return fail(ec, "shutdown");
129
130 // If we get here then the connection is closed gracefully
131 }
132
133 //------------------------------------------------------------------------------
134
main(int argc,char ** argv)135 int main(int argc, char** argv)
136 {
137 // Check command line arguments.
138 if(argc != 4 && argc != 5)
139 {
140 std::cerr <<
141 "Usage: http-client-coro-ssl <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" <<
142 "Example:\n" <<
143 " http-client-coro-ssl www.example.com 443 /\n" <<
144 " http-client-coro-ssl www.example.com 443 / 1.0\n";
145 return EXIT_FAILURE;
146 }
147 auto const host = argv[1];
148 auto const port = argv[2];
149 auto const target = argv[3];
150 int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
151
152 // The io_context is required for all I/O
153 net::io_context ioc;
154
155 // The SSL context is required, and holds certificates
156 ssl::context ctx{ssl::context::tlsv12_client};
157
158 // This holds the root certificate used for verification
159 load_root_certificates(ctx);
160
161 // Verify the remote server's certificate
162 ctx.set_verify_mode(ssl::verify_peer);
163
164 // Launch the asynchronous operation
165 boost::asio::spawn(ioc, std::bind(
166 &do_session,
167 std::string(host),
168 std::string(port),
169 std::string(target),
170 version,
171 std::ref(ioc),
172 std::ref(ctx),
173 std::placeholders::_1));
174
175 // Run the I/O service. The call will return when
176 // the get operation is complete.
177 ioc.run();
178
179 return EXIT_SUCCESS;
180 }
181