2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
8 #ifndef BEAST_WEBSOCKET_OPTION_HPP
9 #define BEAST_WEBSOCKET_OPTION_HPP
11 #include <beast/config.hpp>
12 #include <beast/websocket/rfc6455.hpp>
13 #include <beast/websocket/detail/decorator.hpp>
14 #include <beast/core/detail/type_traits.hpp>
19 #include <type_traits>
25 /** Automatic fragmentation option.
27 Determines if outgoing message payloads are broken up into
30 When the automatic fragmentation size is turned on, outgoing
31 message payloads are broken up into multiple frames no larger
32 than the write buffer size.
34 The default setting is to fragment messages.
36 @note Objects of this type are used with
37 @ref beast::websocket::stream::set_option.
40 Setting the automatic fragmentation option:
43 websocket::stream<ip::tcp::socket> stream(ios);
44 stream.set_option(auto_fragment{true});
48 using auto_fragment = implementation_defined;
62 /** HTTP decorator option.
64 The decorator transforms the HTTP requests and responses used
65 when requesting or responding to the WebSocket Upgrade. This may
66 be used to set or change header fields. For example to set the
67 Server or User-Agent fields. The default setting applies no
68 transformation to the HTTP message.
70 The context in which the decorator is called depends on the
71 type of operation performed:
73 @li For synchronous operations, the implementation will call the
74 decorator before the operation unblocks.
76 @li For asynchronous operations, the implementation guarantees
77 that calls to the decorator will be made from the same implicit
78 or explicit strand used to call the asynchronous initiation
81 The default setting is no decorator.
83 @note Objects of this type are used with
84 @ref beast::websocket::stream::set_option.
87 Setting the decorator.
91 template<bool isRequest, class Body, class Fields>
93 operator()(http::message<isRequest, Body, Fields>& m)
96 m.fields.replace("User-Agent", "MyClient");
98 m.fields.replace("Server", "MyServer");
102 websocket::stream<ip::tcp::socket> ws(ios);
103 ws.set_option(decorate(identity{}));
107 using decorate = implementation_defined;
109 using decorate = detail::decorator_type;
112 /** Keep-alive option.
114 Determines if the connection is closed after a failed upgrade
117 This setting only affects the behavior of HTTP requests that
118 implicitly or explicitly ask for a keepalive. For HTTP requests
119 that indicate the connection should be closed, the connection is
120 closed as per rfc7230.
122 The default setting is to close connections after a failed
125 @note Objects of this type are used with
126 @ref beast::websocket::stream::set_option.
129 Setting the keep alive option.
132 websocket::stream<ip::tcp::socket> ws(ios);
133 ws.set_option(keep_alive{8192});
137 using keep_alive = implementation_defined;
151 /** Message type option.
153 This controls the opcode set for outgoing messages. Valid
154 choices are opcode::binary or opcode::text. The setting is
155 only applied at the start when a caller begins a new message.
156 Changing the opcode after a message is started will only
157 take effect after the current message being sent is complete.
159 The default setting is opcode::text.
161 @note Objects of this type are used with
162 @ref beast::websocket::stream::set_option.
165 Setting the message type to binary.
168 websocket::stream<ip::tcp::socket> ws(ios);
169 ws.set_option(message_type{opcode::binary});
173 using message_type = implementation_defined;
180 message_type(opcode op)
182 if(op != opcode::binary && op != opcode::text)
183 throw beast::detail::make_exception<std::invalid_argument>(
184 "bad opcode", __FILE__, __LINE__);
192 using ping_cb = std::function<void(bool, ping_data const&)>;
196 /** permessage-deflate extension options.
198 These settings control the permessage-deflate extension,
199 which allows messages to be compressed.
201 @note Objects of this type are used with
202 @ref beast::websocket::stream::set_option.
204 struct permessage_deflate
206 /// `true` to offer the extension in the server role
207 bool server_enable = false;
209 /// `true` to offer the extension in the client role
210 bool client_enable = false;
212 /** Maximum server window bits to offer
214 @note Due to a bug in ZLib, this value must be greater than 8.
216 int server_max_window_bits = 15;
218 /** Maximum client window bits to offer
220 @note Due to a bug in ZLib, this value must be greater than 8.
222 int client_max_window_bits = 15;
224 /// `true` if server_no_context_takeover desired
225 bool server_no_context_takeover = false;
227 /// `true` if client_no_context_takeover desired
228 bool client_no_context_takeover = false;
230 /// Deflate compression level 0..9
233 /// Deflate memory level, 1..9
237 /** Ping callback option.
239 Sets the callback to be invoked whenever a ping or pong is
240 received during a call to one of the following functions:
242 @li @ref beast::websocket::stream::read
243 @li @ref beast::websocket::stream::read_frame
244 @li @ref beast::websocket::stream::async_read
245 @li @ref beast::websocket::stream::async_read_frame
247 Unlike completion handlers, the callback will be invoked
248 for each received ping and pong during a call to any
249 synchronous or asynchronous read function. The operation is
250 passive, with no associated error code, and triggered by reads.
252 The signature of the callback must be:
256 bool is_pong, // `true` if this is a pong
257 ping_data const& payload // Payload of the pong frame
261 The value of `is_pong` will be `true` if a pong control frame
262 is received, and `false` if a ping control frame is received.
264 If the read operation receiving a ping or pong frame is an
265 asynchronous operation, the callback will be invoked using
266 the same method as that used to invoke the final handler.
268 @note Objects of this type are used with
269 @ref beast::websocket::stream::set_option.
270 To remove the ping callback, construct the option with
271 no parameters: `set_option(ping_callback{})`
274 using ping_callback = implementation_defined;
278 detail::ping_cb value;
280 ping_callback() = default;
281 ping_callback(ping_callback&&) = default;
282 ping_callback(ping_callback const&) = default;
285 ping_callback(detail::ping_cb f)
286 : value(std::move(f))
292 /** Read buffer size option.
294 Sets the size of the read buffer used by the implementation to
295 receive frames. The read buffer is needed when permessage-deflate
298 Lowering the size of the buffer can decrease the memory requirements
299 for each connection, while increasing the size of the buffer can reduce
300 the number of calls made to the next layer to read data.
302 The default setting is 4096. The minimum value is 8.
304 @note Objects of this type are used with
305 @ref beast::websocket::stream::set_option.
308 Setting the read buffer size.
311 websocket::stream<ip::tcp::socket> ws(ios);
312 ws.set_option(read_buffer_size{16 * 1024});
316 using read_buffer_size = implementation_defined;
318 struct read_buffer_size
323 read_buffer_size(std::size_t n)
327 throw beast::detail::make_exception<std::invalid_argument>(
328 "read buffer size is too small", __FILE__, __LINE__);
333 /** Maximum incoming message size option.
335 Sets the largest permissible incoming message size. Message
336 frame fields indicating a size that would bring the total
337 message size over this limit will cause a protocol failure.
339 The default setting is 16 megabytes. A value of zero indicates
340 a limit of the maximum value of a `std::uint64_t`.
342 @note Objects of this type are used with
343 @ref beast::websocket::stream::set_option.
346 Setting the maximum read message size.
349 websocket::stream<ip::tcp::socket> ws(ios);
350 ws.set_option(read_message_max{65536});
354 using read_message_max = implementation_defined;
356 struct read_message_max
361 read_message_max(std::size_t n)
368 /** Write buffer size option.
370 Sets the size of the write buffer used by the implementation to
371 send frames. The write buffer is needed when masking payload data
372 in the client role, compressing frames, or auto-fragmenting message
375 Lowering the size of the buffer can decrease the memory requirements
376 for each connection, while increasing the size of the buffer can reduce
377 the number of calls made to the next layer to write data.
379 The default setting is 4096. The minimum value is 8.
381 The write buffer size can only be changed when the stream is not
382 open. Undefined behavior results if the option is modified after a
383 successful WebSocket handshake.
385 @note Objects of this type are used with
386 @ref beast::websocket::stream::set_option.
389 Setting the write buffer size.
392 websocket::stream<ip::tcp::socket> ws(ios);
393 ws.set_option(write_buffer_size{8192});
397 using write_buffer_size = implementation_defined;
399 struct write_buffer_size
404 write_buffer_size(std::size_t n)
408 throw beast::detail::make_exception<std::invalid_argument>(
409 "write buffer size is too small", __FILE__, __LINE__);