]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/client/sync-ssl/websocket_client_sync_ssl.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / beast / example / websocket / client / sync-ssl / websocket_client_sync_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, synchronous
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/ip/tcp.hpp>
23 #include <boost/asio/ssl/stream.hpp>
24 #include <cstdlib>
25 #include <iostream>
26 #include <string>
27
28 using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
29 namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
30 namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
31
32 // Sends a WebSocket message and prints the response
33 int main(int argc, char** argv)
34 {
35 try
36 {
37 // Check command line arguments.
38 if(argc != 4)
39 {
40 std::cerr <<
41 "Usage: websocket-client-sync-ssl <host> <port> <text>\n" <<
42 "Example:\n" <<
43 " websocket-client-sync-ssl echo.websocket.org 443 \"Hello, world!\"\n";
44 return EXIT_FAILURE;
45 }
46 auto const host = argv[1];
47 auto const port = argv[2];
48 auto const text = argv[3];
49
50 // The io_context is required for all I/O
51 boost::asio::io_context ioc;
52
53 // The SSL context is required, and holds certificates
54 ssl::context ctx{ssl::context::sslv23_client};
55
56 // This holds the root certificate used for verification
57 load_root_certificates(ctx);
58
59 // These objects perform our I/O
60 tcp::resolver resolver{ioc};
61 websocket::stream<ssl::stream<tcp::socket>> ws{ioc, ctx};
62
63 // Look up the domain name
64 auto const results = resolver.resolve(host, port);
65
66 // Make the connection on the IP address we get from a lookup
67 boost::asio::connect(ws.next_layer().next_layer(), results.begin(), results.end());
68
69 // Perform the SSL handshake
70 ws.next_layer().handshake(ssl::stream_base::client);
71
72 // Perform the websocket handshake
73 ws.handshake(host, "/");
74
75 // Send the message
76 ws.write(boost::asio::buffer(std::string(text)));
77
78 // This buffer will hold the incoming message
79 boost::beast::multi_buffer b;
80
81 // Read a message into our buffer
82 ws.read(b);
83
84 // Close the WebSocket connection
85 ws.close(websocket::close_code::normal);
86
87 // If we get here then the connection is closed gracefully
88
89 // The buffers() function helps print a ConstBufferSequence
90 std::cout << boost::beast::buffers(b.data()) << std::endl;
91 }
92 catch(std::exception const& e)
93 {
94 std::cerr << "Error: " << e.what() << std::endl;
95 return EXIT_FAILURE;
96 }
97 return EXIT_SUCCESS;
98 }