]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/client/coro-ssl/websocket_client_coro_ssl.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / beast / example / websocket / client / coro-ssl / websocket_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: 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/websocket.hpp>
20 #include <boost/beast/websocket/ssl.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/stream.hpp>
25 #include <cstdlib>
26 #include <functional>
27 #include <iostream>
28 #include <string>
29
30 using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
31 namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
32 namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
33
34 //------------------------------------------------------------------------------
35
36 // Report a failure
37 void
38 fail(boost::system::error_code ec, char const* what)
39 {
40 std::cerr << what << ": " << ec.message() << "\n";
41 }
42
43 // Sends a WebSocket message and prints the response
44 void
45 do_session(
46 std::string const& host,
47 std::string const& port,
48 std::string const& text,
49 boost::asio::io_context& ioc,
50 ssl::context& ctx,
51 boost::asio::yield_context yield)
52 {
53 boost::system::error_code ec;
54
55 // These objects perform our I/O
56 tcp::resolver resolver{ioc};
57 websocket::stream<ssl::stream<tcp::socket>> ws{ioc, ctx};
58
59 // Look up the domain name
60 auto const results = resolver.async_resolve(host, port, yield[ec]);
61 if(ec)
62 return fail(ec, "resolve");
63
64 // Make the connection on the IP address we get from a lookup
65 boost::asio::async_connect(ws.next_layer().next_layer(), results.begin(), results.end(), yield[ec]);
66 if(ec)
67 return fail(ec, "connect");
68
69 // Perform the SSL handshake
70 ws.next_layer().async_handshake(ssl::stream_base::client, yield[ec]);
71 if(ec)
72 return fail(ec, "ssl_handshake");
73
74 // Perform the websocket handshake
75 ws.async_handshake(host, "/", yield[ec]);
76 if(ec)
77 return fail(ec, "handshake");
78
79 // Send the message
80 ws.async_write(boost::asio::buffer(std::string(text)), yield[ec]);
81 if(ec)
82 return fail(ec, "write");
83
84 // This buffer will hold the incoming message
85 boost::beast::multi_buffer b;
86
87 // Read a message into our buffer
88 ws.async_read(b, yield[ec]);
89 if(ec)
90 return fail(ec, "read");
91
92 // Close the WebSocket connection
93 ws.async_close(websocket::close_code::normal, yield[ec]);
94 if(ec)
95 return fail(ec, "close");
96
97 // If we get here then the connection is closed gracefully
98
99 // The buffers() function helps print a ConstBufferSequence
100 std::cout << boost::beast::buffers(b.data()) << std::endl;
101 }
102
103 //------------------------------------------------------------------------------
104
105 int main(int argc, char** argv)
106 {
107 // Check command line arguments.
108 if(argc != 4)
109 {
110 std::cerr <<
111 "Usage: websocket-client-coro-ssl <host> <port> <text>\n" <<
112 "Example:\n" <<
113 " websocket-client-coro-ssl echo.websocket.org 443 \"Hello, world!\"\n";
114 return EXIT_FAILURE;
115 }
116 auto const host = argv[1];
117 auto const port = argv[2];
118 auto const text = argv[3];
119
120 // The io_context is required for all I/O
121 boost::asio::io_context ioc;
122
123 // The SSL context is required, and holds certificates
124 ssl::context ctx{ssl::context::sslv23_client};
125
126 // This holds the root certificate used for verification
127 load_root_certificates(ctx);
128
129 // Launch the asynchronous operation
130 boost::asio::spawn(ioc, std::bind(
131 &do_session,
132 std::string(host),
133 std::string(port),
134 std::string(text),
135 std::ref(ioc),
136 std::ref(ctx),
137 std::placeholders::_1));
138
139 // Run the I/O service. The call will return when
140 // the get operation is complete.
141 ioc.run();
142
143 return EXIT_SUCCESS;
144 }