]> git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/doc/examples.qbk
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / Beast / doc / examples.qbk
1 [/
2 Copyright (c) 2013-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
8 [section:example Examples]
9
10 These usage examples are intended to quickly impress upon readers the
11 flavor of the library. They are complete programs which may be built
12 and run. Source code and build scripts for these programs may be found
13 in the examples directory.
14
15 [heading HTTP GET]
16
17 Use HTTP to request the root page from a website and print the response:
18
19 ```
20 #include <beast/http.hpp>
21 #include <boost/asio.hpp>
22 #include <boost/lexical_cast.hpp>
23 #include <iostream>
24 #include <string>
25
26 int main()
27 {
28 // Normal boost::asio setup
29 std::string const host = "boost.org";
30 boost::asio::io_service ios;
31 boost::asio::ip::tcp::resolver r{ios};
32 boost::asio::ip::tcp::socket sock{ios};
33 boost::asio::connect(sock,
34 r.resolve(boost::asio::ip::tcp::resolver::query{host, "http"}));
35
36 // Send HTTP request using beast
37 beast::http::request<beast::http::string_body> req;
38 req.method = "GET";
39 req.url = "/";
40 req.version = 11;
41 req.fields.replace("Host", host + ":" +
42 boost::lexical_cast<std::string>(sock.remote_endpoint().port()));
43 req.fields.replace("User-Agent", "Beast");
44 beast::http::prepare(req);
45 beast::http::write(sock, req);
46
47 // Receive and print HTTP response using beast
48 beast::streambuf sb;
49 beast::http::response<beast::http::streambuf_body> resp;
50 beast::http::read(sock, sb, resp);
51 std::cout << resp;
52 }
53 ```
54 [heading WebSocket]
55
56 Establish a WebSocket connection, send a message and receive the reply:
57 ```
58 #include <beast/core/to_string.hpp>
59 #include <beast/websocket.hpp>
60 #include <boost/asio.hpp>
61 #include <iostream>
62 #include <string>
63
64 int main()
65 {
66 // Normal boost::asio setup
67 std::string const host = "echo.websocket.org";
68 boost::asio::io_service ios;
69 boost::asio::ip::tcp::resolver r{ios};
70 boost::asio::ip::tcp::socket sock{ios};
71 boost::asio::connect(sock,
72 r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"}));
73
74 // WebSocket connect and send message using beast
75 beast::websocket::stream<boost::asio::ip::tcp::socket&> ws{sock};
76 ws.handshake(host, "/");
77 ws.write(boost::asio::buffer(std::string("Hello, world!")));
78
79 // Receive WebSocket message, print and close using beast
80 beast::streambuf sb;
81 beast::websocket::opcode op;
82 ws.read(op, sb);
83 ws.close(beast::websocket::close_code::normal);
84 std::cout << beast::to_string(sb.data()) << "\n";
85 }
86 ```
87
88 [heading WebSocket Echo Server]
89
90 This example demonstrates both synchronous and asynchronous
91 WebSocket server implementations.
92
93 * [@examples/websocket_async_echo_server.hpp]
94 * [@examples/websocket_sync_echo_server.hpp]
95 * [@examples/websocket_echo.cpp]
96
97 [heading Secure WebSocket]
98
99 Establish a WebSocket connection over an encrypted TLS connection,
100 send a message and receive the reply. Requires OpenSSL to build.
101
102 * [@examples/websocket_ssl_example.cpp]
103
104 [heading HTTPS GET]
105
106 This example demonstrates sending and receiving HTTP messages
107 over a TLS connection. Requires OpenSSL to build.
108
109 * [@examples/http_ssl_example.cpp]
110
111 [heading HTTP Crawl]
112
113 This example retrieves the page at each of the most popular domains
114 as measured by Alexa.
115
116 * [@examples/http_crawl.cpp]
117
118 [heading HTTP Server]
119
120 This example demonstrates both synchronous and asynchronous server
121 implementations. It also provides an example of implementing a [*Body]
122 type, in `file_body`.
123
124 * [@examples/file_body.hpp]
125 * [@examples/http_async_server.hpp]
126 * [@examples/http_sync_server.hpp]
127 * [@examples/http_server.cpp]
128
129 [heading Listings]
130
131 These are stand-alone listings of the HTTP and WebSocket examples.
132
133 * [@examples/http_example.cpp]
134 * [@examples/websocket_example.cpp]
135
136 [endsect]