| 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: WebSocket SSL client, coroutine |
| 13 | // |
| 14 | //------------------------------------------------------------------------------ |
| 15 | |
| 16 | #include "example/common/root_certificates.hpp" |
| 17 | |
| 18 | #include <boost/beast/core.hpp> |
| 19 | #include <boost/beast/ssl.hpp> |
| 20 | #include <boost/beast/websocket.hpp> |
| 21 | #include <boost/beast/websocket/ssl.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 websocket = beast::websocket; // from <boost/beast/websocket.hpp> |
| 31 | namespace net = boost::asio; // from <boost/asio.hpp> |
| 32 | namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp> |
| 33 | using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp> |
| 34 | |
| 35 | //------------------------------------------------------------------------------ |
| 36 | |
| 37 | // Report a failure |
| 38 | void |
| 39 | fail(beast::error_code ec, char const* what) |
| 40 | { |
| 41 | std::cerr << what << ": " << ec.message() << "\n" ; |
| 42 | } |
| 43 | |
| 44 | // Sends a WebSocket message and prints the response |
| 45 | void |
| 46 | do_session( |
| 47 | std::string host, |
| 48 | std::string const& port, |
| 49 | std::string const& text, |
| 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 | websocket::stream< |
| 59 | beast::ssl_stream<beast::tcp_stream>> ws(ioc, ctx); |
| 60 | |
| 61 | // Look up the domain name |
| 62 | auto const results = resolver.async_resolve(host, service: port, token: yield[ec]); |
| 63 | if(ec) |
| 64 | return fail(ec, what: "resolve" ); |
| 65 | |
| 66 | // Set a timeout on the operation |
| 67 | beast::get_lowest_layer(t&: ws).expires_after(expiry_time: std::chrono::seconds(30)); |
| 68 | |
| 69 | // Make the connection on the IP address we get from a lookup |
| 70 | auto ep = beast::get_lowest_layer(t&: ws).async_connect(endpoints: results, handler: yield[ec]); |
| 71 | if(ec) |
| 72 | return fail(ec, what: "connect" ); |
| 73 | |
| 74 | // Set SNI Hostname (many hosts need this to handshake successfully) |
| 75 | if(! SSL_set_tlsext_host_name( |
| 76 | ws.next_layer().native_handle(), |
| 77 | host.c_str())) |
| 78 | { |
| 79 | ec = beast::error_code(static_cast<int>(::ERR_get_error()), |
| 80 | net::error::get_ssl_category()); |
| 81 | return fail(ec, what: "connect" ); |
| 82 | } |
| 83 | |
| 84 | // Update the host string. This will provide the value of the |
| 85 | // Host HTTP header during the WebSocket handshake. |
| 86 | // See https://tools.ietf.org/html/rfc7230#section-5.4 |
| 87 | host += ':' + std::to_string(val: ep.port()); |
| 88 | |
| 89 | // Set a timeout on the operation |
| 90 | beast::get_lowest_layer(t&: ws).expires_after(expiry_time: std::chrono::seconds(30)); |
| 91 | |
| 92 | // Set a decorator to change the User-Agent of the handshake |
| 93 | ws.set_option(websocket::stream_base::decorator( |
| 94 | [](websocket::request_type& req) |
| 95 | { |
| 96 | req.set(name: http::field::user_agent, |
| 97 | value: std::string(BOOST_BEAST_VERSION_STRING) + |
| 98 | " websocket-client-coro" ); |
| 99 | })); |
| 100 | |
| 101 | // Perform the SSL handshake |
| 102 | ws.next_layer().async_handshake(type: ssl::stream_base::client, handler: yield[ec]); |
| 103 | if(ec) |
| 104 | return fail(ec, what: "ssl_handshake" ); |
| 105 | |
| 106 | // Turn off the timeout on the tcp_stream, because |
| 107 | // the websocket stream has its own timeout system. |
| 108 | beast::get_lowest_layer(t&: ws).expires_never(); |
| 109 | |
| 110 | // Set suggested timeout settings for the websocket |
| 111 | ws.set_option( |
| 112 | websocket::stream_base::timeout::suggested( |
| 113 | role: beast::role_type::client)); |
| 114 | |
| 115 | // Perform the websocket handshake |
| 116 | ws.async_handshake(host, target: "/" , handler: yield[ec]); |
| 117 | if(ec) |
| 118 | return fail(ec, what: "handshake" ); |
| 119 | |
| 120 | // Send the message |
| 121 | ws.async_write(bs: net::buffer(data: std::string(text)), handler: yield[ec]); |
| 122 | if(ec) |
| 123 | return fail(ec, what: "write" ); |
| 124 | |
| 125 | // This buffer will hold the incoming message |
| 126 | beast::flat_buffer buffer; |
| 127 | |
| 128 | // Read a message into our buffer |
| 129 | ws.async_read(buffer, handler: yield[ec]); |
| 130 | if(ec) |
| 131 | return fail(ec, what: "read" ); |
| 132 | |
| 133 | // Close the WebSocket connection |
| 134 | ws.async_close(cr: websocket::close_code::normal, handler: yield[ec]); |
| 135 | if(ec) |
| 136 | return fail(ec, what: "close" ); |
| 137 | |
| 138 | // If we get here then the connection is closed gracefully |
| 139 | |
| 140 | // The make_printable() function helps print a ConstBufferSequence |
| 141 | std::cout << beast::make_printable(buffers: buffer.data()) << std::endl; |
| 142 | } |
| 143 | |
| 144 | //------------------------------------------------------------------------------ |
| 145 | |
| 146 | int main(int argc, char** argv) |
| 147 | { |
| 148 | // Check command line arguments. |
| 149 | if(argc != 4) |
| 150 | { |
| 151 | std::cerr << |
| 152 | "Usage: websocket-client-coro-ssl <host> <port> <text>\n" << |
| 153 | "Example:\n" << |
| 154 | " websocket-client-coro-ssl echo.websocket.org 443 \"Hello, world!\"\n" ; |
| 155 | return EXIT_FAILURE; |
| 156 | } |
| 157 | auto const host = argv[1]; |
| 158 | auto const port = argv[2]; |
| 159 | auto const text = argv[3]; |
| 160 | |
| 161 | // The io_context is required for all I/O |
| 162 | net::io_context ioc; |
| 163 | |
| 164 | // The SSL context is required, and holds certificates |
| 165 | ssl::context ctx{ssl::context::tlsv12_client}; |
| 166 | |
| 167 | // This holds the root certificate used for verification |
| 168 | load_root_certificates(ctx); |
| 169 | |
| 170 | // Launch the asynchronous operation |
| 171 | boost::asio::spawn(ctx&: ioc, function: std::bind( |
| 172 | f: &do_session, |
| 173 | args: std::string(host), |
| 174 | args: std::string(port), |
| 175 | args: std::string(text), |
| 176 | args: std::ref(t&: ioc), |
| 177 | args: std::ref(t&: ctx), |
| 178 | args: std::placeholders::_1), |
| 179 | // on completion, spawn will call this function |
| 180 | token: [](std::exception_ptr ex) |
| 181 | { |
| 182 | // if an exception occurred in the coroutine, |
| 183 | // it's something critical, e.g. out of memory |
| 184 | // we capture normal errors in the ec |
| 185 | // so we just rethrow the exception here, |
| 186 | // which will cause `ioc.run()` to throw |
| 187 | if (ex) |
| 188 | std::rethrow_exception(ex); |
| 189 | }); |
| 190 | |
| 191 | // Run the I/O service. The call will return when |
| 192 | // the socket is closed. |
| 193 | ioc.run(); |
| 194 | |
| 195 | return EXIT_SUCCESS; |
| 196 | } |
| 197 | |