• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
fail(beast::error_code ec,char const * what)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
do_session(std::string host,std::string const & port,std::string const & text,net::io_context & ioc,ssl::context & ctx,net::yield_context yield)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, port, yield[ec]);
63     if(ec)
64         return fail(ec, "resolve");
65 
66     // Set a timeout on the operation
67     beast::get_lowest_layer(ws).expires_after(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(ws).async_connect(results, yield[ec]);
71     if(ec)
72         return fail(ec, "connect");
73 
74     // Update the host_ string. This will provide the value of the
75     // Host HTTP header during the WebSocket handshake.
76     // See https://tools.ietf.org/html/rfc7230#section-5.4
77     host += ':' + std::to_string(ep.port());
78 
79     // Set a timeout on the operation
80     beast::get_lowest_layer(ws).expires_after(std::chrono::seconds(30));
81 
82     // Set a decorator to change the User-Agent of the handshake
83     ws.set_option(websocket::stream_base::decorator(
84         [](websocket::request_type& req)
85         {
86             req.set(http::field::user_agent,
87                 std::string(BOOST_BEAST_VERSION_STRING) +
88                     " websocket-client-coro");
89         }));
90 
91     // Perform the SSL handshake
92     ws.next_layer().async_handshake(ssl::stream_base::client, yield[ec]);
93     if(ec)
94         return fail(ec, "ssl_handshake");
95 
96     // Turn off the timeout on the tcp_stream, because
97     // the websocket stream has its own timeout system.
98     beast::get_lowest_layer(ws).expires_never();
99 
100     // Set suggested timeout settings for the websocket
101     ws.set_option(
102         websocket::stream_base::timeout::suggested(
103             beast::role_type::client));
104 
105     // Perform the websocket handshake
106     ws.async_handshake(host, "/", yield[ec]);
107     if(ec)
108         return fail(ec, "handshake");
109 
110     // Send the message
111     ws.async_write(net::buffer(std::string(text)), yield[ec]);
112     if(ec)
113         return fail(ec, "write");
114 
115     // This buffer will hold the incoming message
116     beast::flat_buffer buffer;
117 
118     // Read a message into our buffer
119     ws.async_read(buffer, yield[ec]);
120     if(ec)
121         return fail(ec, "read");
122 
123     // Close the WebSocket connection
124     ws.async_close(websocket::close_code::normal, yield[ec]);
125     if(ec)
126         return fail(ec, "close");
127 
128     // If we get here then the connection is closed gracefully
129 
130     // The make_printable() function helps print a ConstBufferSequence
131     std::cout << beast::make_printable(buffer.data()) << std::endl;
132 }
133 
134 //------------------------------------------------------------------------------
135 
main(int argc,char ** argv)136 int main(int argc, char** argv)
137 {
138     // Check command line arguments.
139     if(argc != 4)
140     {
141         std::cerr <<
142             "Usage: websocket-client-coro-ssl <host> <port> <text>\n" <<
143             "Example:\n" <<
144             "    websocket-client-coro-ssl echo.websocket.org 443 \"Hello, world!\"\n";
145         return EXIT_FAILURE;
146     }
147     auto const host = argv[1];
148     auto const port = argv[2];
149     auto const text = argv[3];
150 
151     // The io_context is required for all I/O
152     net::io_context ioc;
153 
154     // The SSL context is required, and holds certificates
155     ssl::context ctx{ssl::context::tlsv12_client};
156 
157     // This holds the root certificate used for verification
158     load_root_certificates(ctx);
159 
160     // Launch the asynchronous operation
161     boost::asio::spawn(ioc, std::bind(
162         &do_session,
163         std::string(host),
164         std::string(port),
165         std::string(text),
166         std::ref(ioc),
167         std::ref(ctx),
168         std::placeholders::_1));
169 
170     // Run the I/O service. The call will return when
171     // the socket is closed.
172     ioc.run();
173 
174     return EXIT_SUCCESS;
175 }
176