WebSocket++  0.8.0-dev
C++ websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
config_builder.hpp
1 /*
2  * Copyright (c) 2016, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the WebSocket++ Project nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef WEBSOCKETPP_CONFIG_BUILDER_HPP
29 #define WEBSOCKETPP_CONFIG_BUILDER_HPP
30 
31 // Non-Policy common stuff
32 #include <websocketpp/common/platforms.hpp>
33 #include <websocketpp/common/cpp11.hpp>
34 #include <websocketpp/common/stdint.hpp>
35 
36 // Concurrency
37 #include <websocketpp/concurrency/basic.hpp>
38 
39 // HTTP
40 #include <websocketpp/http/request.hpp>
41 #include <websocketpp/http/response.hpp>
42 
43 // Loggers
44 #include <websocketpp/logger/basic.hpp>
45 
46 // Messages
47 #include <websocketpp/message_buffer/message.hpp>
48 #include <websocketpp/message_buffer/alloc.hpp>
49 
50 // User stub base classes
51 #include <websocketpp/endpoint_base.hpp>
52 #include <websocketpp/connection_base.hpp>
53 
54 // Transport
55 #include <websocketpp/transport/iostream/endpoint.hpp>
56 
57 // Extensions
58 #include <websocketpp/extensions/permessage_deflate/disabled.hpp>
59 
60 namespace websocketpp {
61 namespace config {
62 
63 template <typename ConcurrencyType>
64 struct server {
65  typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
66 };
67 
68 template <typename ConcurrencyType>
69 struct client {
70  typedef websocketpp::random::random_device::int_generator<uint32_t,
71  ConcurrencyType> rng_type;
72 };
73 
74 /// Adds development/debug settings to a config
75 struct debug {
76  /// Debugging level static error logging channels
77  /**
78  * Which error logging channels to enable at compile time. Channels not
79  * enabled here will be unable to be selected by programs using the library.
80  * This option gives an optimizing compiler the ability to remove entirely
81  * code to test whether or not to print out log messages on a certain
82  * channel
83  *
84  * This package includes all logging levels, including development/debug
85  */
87 
88  /// Debugging static access logging channels
89  /**
90  * Which access logging channels to enable at compile time. Channels not
91  * enabled here will be unable to be selected by programs using the library.
92  * This option gives an optimizing compiler the ability to remove entirely
93  * code to test whether or not to print out log messages on a certain
94  * channel
95  *
96  * This package includes all logging levels, including development/debug
97  */
99 };
100 
101 /// Adds production settings to a config
102 struct production {
103  /// Production static error logging channels
104  /**
105  * Which error logging channels to enable at compile time. Channels not
106  * enabled here will be unable to be selected by programs using the library.
107  * This option gives an optimizing compiler the ability to remove entirely
108  * code to test whether or not to print out log messages on a certain
109  * channel
110  *
111  * This package includes all except for development/debug level errors
112  */
113  static const websocketpp::log::level elog_level =
115 
116  /// Production static access logging channels
117  /**
118  * Which access logging channels to enable at compile time. Channels not
119  * enabled here will be unable to be selected by programs using the library.
120  * This option gives an optimizing compiler the ability to remove entirely
121  * code to test whether or not to print out log messages on a certain
122  * channel
123  *
124  * This package includes all except for development/debug level errors
125  */
126  static const websocketpp::log::level alog_level =
128 };
129 
131 
132 
133 
135 
136 
138 
139 
141 #include <websocketpp/extensions/permessage_deflate/enabled.hpp>
142 
143 /// Adds PermessageDeflate extension support
144 struct permessage_deflate {
145  /// permessage_deflate extension config
147  typedef core_client::request_type request_type;
148 
149  /// If the remote endpoint requests that we reset the compression
150  /// context after each message should we honor the request?
151  static const bool allow_disabling_context_takeover = true;
152 
153  /// If the remote endpoint requests that we reduce the size of the
154  /// LZ77 sliding window size this is the lowest value that will be
155  /// allowed. Values range from 8 to 15. A value of 8 means we will
156  /// allow any possible window size. A value of 15 means do not allow
157  /// negotiation of the window size (ie require the default).
159  };
160 
161  typedef websocketpp::extensions::permessage_deflate::enabled
162  <permessage_deflate_config> permessage_deflate_type;
163 };
164 
165 /// No PermessageDeflate extension support
166 struct no_compression {
167  /// permessage_deflate extension config
169 
170  typedef websocketpp::extensions::permessage_deflate::disabled
171  <permessage_deflate_config> permessage_deflate_type;
172 };
173 
174 
175 
176 
177 
179 
181 // transport
183 
184 
186 
188 
190 
191 
192 
193 ///
194 /**
195  * Assumptions:
196  * - Thread safe
197  * - Bundled HTTP parser
198  * - Bundled Logger
199  * - Bundled RNG
200  * - Bundled Message policy (for now)
201  */
202 
203 template <template<typename> typename Role, typename Debug, typename Transport, typename PermessageDeflate = no_compression>
204 struct builder : public Role<websocketpp::concurrency::basic>, public Debug, public Transport, public PermessageDeflate {
205  typedef builder type;
206 
207  // ######## Concurrency ########
208 
209  typedef websocketpp::concurrency::basic concurrency_type;
210  /// Controls compile time enabling/disabling of thread syncronization code
211  /// Disabling can provide a minor performance improvement to single threaded
212  /// applications
213  static bool const enable_multithreading = true;
214 
215 
216  // ######## HTTP Parser Policies ########
217  typedef http::parser::request request_type;
218  typedef http::parser::response response_type;
219 
220  /// Default maximum http body size
221  /**
222  * Default value for the http parser's maximum body size. Maximum body size
223  * determines the point at which the library will abort reading an HTTP
224  * connection with the 413/request entity too large error.
225  *
226  * The default is 32MB
227  *
228  * @since 0.5.0
229  */
230  static const size_t max_http_body_size = 32000000;
231 
232 
233  // ######## Logging policies ########
234  typedef websocketpp::log::basic<concurrency_type,
235  websocketpp::log::elevel> elog_type;
236  typedef websocketpp::log::basic<concurrency_type,
237  websocketpp::log::alevel> alog_type;
238 
239  // logging levels are set by the Debug parameter
240 
241 
242  // ######## Message Policies ########
243  typedef message_buffer::message<message_buffer::alloc::con_msg_manager>
244  message_type;
245  typedef message_buffer::alloc::con_msg_manager<message_type>
246  con_msg_manager_type;
247  typedef message_buffer::alloc::endpoint_msg_manager<con_msg_manager_type>
248  endpoint_msg_manager_type;
249 
250 
251  // ######## RNG policy ########
252  // provided by Role
253 
254 
255  // ######## Custom base class ########
256 
257  /// User overridable Endpoint base class
259  /// User overridable Connection base class
261 
262 
263  // ######## Behavior & performance tuning settings ########
264 
265  // Default timer values (in ms)
266 
267  /// Length of time before an opening handshake is aborted (ms)
268  static const long timeout_open_handshake = 5000;
269  /// Length of time before a closing handshake is aborted (ms)
270  static const long timeout_close_handshake = 5000;
271  /// Length of time to wait for a pong after a ping (ms)
272  static const long timeout_pong = 5000;
273 
274  /// WebSocket Protocol version to use as a client
275  /**
276  * What version of the WebSocket Protocol to use for outgoing client
277  * connections. Setting this to a value other than 13 (RFC6455) is not
278  * recommended.
279  */
280  static const int client_version = 13; // RFC6455
281 
282  /// Connection read buffer
283  /**
284  * Determines the amount of memory dedicated per connection to the read
285  * buffer. Smaller values will use less memory but require more CPU to
286  * process the buffers more often for large messages.
287  *
288  * A message of size N will require N/read_buffer_size trips through the
289  * processing handler. There is no performance benefit when N is less than
290  * read_buffer_size.
291  */
292  static const size_t connection_read_buffer_size = 16384;
293 
294  /// Drop connections immediately on protocol error.
295  /**
296  * Drop connections on protocol error rather than sending a close frame.
297  * Off by default. This may result in legit messages near the error being
298  * dropped as well. It may free up resources otherwise spent dealing with
299  * misbehaving clients.
300  */
301  static const bool drop_on_protocol_error = false;
302 
303  /// Suppresses the return of detailed connection close information
304  /**
305  * Silence close suppresses the return of detailed connection close
306  * information during the closing handshake. This information is useful
307  * for debugging and presenting useful errors to end users but may be
308  * undesirable for security reasons in some production environments.
309  * Close reasons could be used by an attacker to confirm that the endpoint
310  * is out of resources or be used to identify the WebSocket implementation
311  * in use.
312  *
313  * Note: this will suppress *all* close codes, including those explicitly
314  * sent by local applications.
315  */
316  static const bool silent_close = false;
317 
318  /// Default maximum message size
319  /**
320  * Default value for the processor's maximum message size. Maximum message size
321  * determines the point at which the library will fail a connection with the
322  * message_too_big protocol error.
323  *
324  * The default is 32MB
325  *
326  * @since 0.3.0
327  */
328  static const size_t max_message_size = 32000000;
329 
330  // ######## Extensions ########
331 
332  /// Global flag for enabling/disabling extensions
333  static const bool enable_extensions = true;
334 
335  // Permessage Deflate extension settings are provided by the
336  // PermessageDeflate policy
337 };
338 
339 
340 
341 
342 typedef builder<server, production> foo;
343 
344 
345 typedef builder<client, production> foo;
346 
347 } // namespace config
348 } // namespace websocketpp
349 
350 #endif // WEBSOCKETPP_CONFIG_BUILDER_HPP
351 
352 
353 
354 
355 
356 ################################################################################################
357 
358 // Non-Policy common stuff
359 #include <websocketpp/common/platforms.hpp>
360 #include <websocketpp/common/cpp11.hpp>
361 #include <websocketpp/common/stdint.hpp>
362 
363 // Concurrency
364 #include <websocketpp/concurrency/basic.hpp>
365 
366 // Transport
367 #include <websocketpp/transport/iostream/endpoint.hpp>
368 
369 // HTTP
370 #include <websocketpp/http/request.hpp>
371 #include <websocketpp/http/response.hpp>
372 
373 // Messages
374 #include <websocketpp/message_buffer/message.hpp>
375 #include <websocketpp/message_buffer/alloc.hpp>
376 
377 // Loggers
378 #include <websocketpp/logger/basic.hpp>
379 
380 // RNG
381 #include <websocketpp/random/none.hpp>
382 
383 // User stub base classes
384 #include <websocketpp/endpoint_base.hpp>
385 #include <websocketpp/connection_base.hpp>
386 
387 // Extensions
388 #include <websocketpp/extensions/permessage_deflate/disabled.hpp>
389 
390 namespace websocketpp {
391 namespace config {
392 
393 /// Server config with iostream transport
394 struct core {
395  typedef core type;
396 
397  // Concurrency policy
398  typedef websocketpp::concurrency::basic concurrency_type;
399 
400  // HTTP Parser Policies
401  typedef http::parser::request request_type;
402  typedef http::parser::response response_type;
403 
404  // Message Policies
405  typedef message_buffer::message<message_buffer::alloc::con_msg_manager>
406  message_type;
407  typedef message_buffer::alloc::con_msg_manager<message_type>
408  con_msg_manager_type;
409  typedef message_buffer::alloc::endpoint_msg_manager<con_msg_manager_type>
410  endpoint_msg_manager_type;
411 
412  /// Logging policies
413  typedef websocketpp::log::basic<concurrency_type,
414  websocketpp::log::elevel> elog_type;
415  typedef websocketpp::log::basic<concurrency_type,
416  websocketpp::log::alevel> alog_type;
417 
418  /// RNG policies
419  typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
420 
421  /// Controls compile time enabling/disabling of thread syncronization
422  /// code Disabling can provide a minor performance improvement to single
423  /// threaded applications
424  static bool const enable_multithreading = true;
425 
427  typedef type::concurrency_type concurrency_type;
428  typedef type::elog_type elog_type;
429  typedef type::alog_type alog_type;
430  typedef type::request_type request_type;
431  typedef type::response_type response_type;
432 
433  /// Controls compile time enabling/disabling of thread syncronization
434  /// code Disabling can provide a minor performance improvement to single
435  /// threaded applications
436  static bool const enable_multithreading = true;
437 
438  /// Default timer values (in ms)
439 
440  /// Length of time to wait for socket pre-initialization
441  /**
442  * Exactly what this includes depends on the socket policy in use
443  */
444  static const long timeout_socket_pre_init = 5000;
445 
446  /// Length of time to wait before a proxy handshake is aborted
447  static const long timeout_proxy = 5000;
448 
449  /// Length of time to wait for socket post-initialization
450  /**
451  * Exactly what this includes depends on the socket policy in use.
452  * Often this means the TLS handshake
453  */
454  static const long timeout_socket_post_init = 5000;
455 
456  /// Length of time to wait for dns resolution
457  static const long timeout_dns_resolve = 5000;
458 
459  /// Length of time to wait for TCP connect
460  static const long timeout_connect = 5000;
461 
462  /// Length of time to wait for socket shutdown
463  static const long timeout_socket_shutdown = 5000;
464  };
465 
466  /// Transport Endpoint Component
467  typedef websocketpp::transport::iostream::endpoint<transport_config>
468  transport_type;
469 
470  /// User overridable Endpoint base class
472  /// User overridable Connection base class
474 
475  /// Default timer values (in ms)
476 
477  /// Length of time before an opening handshake is aborted
478  static const long timeout_open_handshake = 5000;
479  /// Length of time before a closing handshake is aborted
480  static const long timeout_close_handshake = 5000;
481  /// Length of time to wait for a pong after a ping
482  static const long timeout_pong = 5000;
483 
484  /// WebSocket Protocol version to use as a client
485  /**
486  * What version of the WebSocket Protocol to use for outgoing client
487  * connections. Setting this to a value other than 13 (RFC6455) is not
488  * recommended.
489  */
490  static const int client_version = 13; // RFC6455
491 
492  /// Default static error logging channels
493  /**
494  * Which error logging channels to enable at compile time. Channels not
495  * enabled here will be unable to be selected by programs using the library.
496  * This option gives an optimizing compiler the ability to remove entirely
497  * code to test whether or not to print out log messages on a certain
498  * channel
499  *
500  * Default is all except for development/debug level errors
501  */
502  static const websocketpp::log::level elog_level =
503  websocketpp::log::elevel::all ^ websocketpp::log::elevel::devel;
504 
505  /// Default static access logging channels
506  /**
507  * Which access logging channels to enable at compile time. Channels not
508  * enabled here will be unable to be selected by programs using the library.
509  * This option gives an optimizing compiler the ability to remove entirely
510  * code to test whether or not to print out log messages on a certain
511  * channel
512  *
513  * Default is all except for development/debug level access messages
514  */
515  static const websocketpp::log::level alog_level =
516  websocketpp::log::alevel::all ^ websocketpp::log::alevel::devel;
517 
518  ///
519  static const size_t connection_read_buffer_size = 16384;
520 
521  /// Drop connections immediately on protocol error.
522  /**
523  * Drop connections on protocol error rather than sending a close frame.
524  * Off by default. This may result in legit messages near the error being
525  * dropped as well. It may free up resources otherwise spent dealing with
526  * misbehaving clients.
527  */
528  static const bool drop_on_protocol_error = false;
529 
530  /// Suppresses the return of detailed connection close information
531  /**
532  * Silence close suppresses the return of detailed connection close
533  * information during the closing handshake. This information is useful
534  * for debugging and presenting useful errors to end users but may be
535  * undesirable for security reasons in some production environments.
536  * Close reasons could be used by an attacker to confirm that the endpoint
537  * is out of resources or be used to identify the WebSocket implementation
538  * in use.
539  *
540  * Note: this will suppress *all* close codes, including those explicitly
541  * sent by local applications.
542  */
543  static const bool silent_close = false;
545  /// Default maximum message size
546  /**
547  * Default value for the processor's maximum message size. Maximum message size
548  * determines the point at which the library will fail a connection with the
549  * message_too_big protocol error.
550  *
551  * The default is 32MB
552  *
553  * @since 0.3.0
554  */
555  static const size_t max_message_size = 32000000;
556 
557  /// Default maximum http body size
558  /**
559  * Default value for the http parser's maximum body size. Maximum body size
560  * determines the point at which the library will abort reading an HTTP
561  * connection with the 413/request entity too large error.
562  *
563  * The default is 32MB
564  *
565  * @since 0.5.0
566  */
567  static const size_t max_http_body_size = 32000000;
568 
569  /// Global flag for enabling/disabling extensions
570  static const bool enable_extensions = true;
571 
572  /// Extension specific settings:
573 
574  /// permessage_compress extension
576  typedef core::request_type request_type;
577 
578  /// If the remote endpoint requests that we reset the compression
579  /// context after each message should we honor the request?
580  static const bool allow_disabling_context_takeover = true;
581 
582  /// If the remote endpoint requests that we reduce the size of the
583  /// LZ77 sliding window size this is the lowest value that will be
584  /// allowed. Values range from 8 to 15. A value of 8 means we will
585  /// allow any possible window size. A value of 15 means do not allow
586  /// negotiation of the window size (ie require the default).
588  };
589 
590  typedef websocketpp::extensions::permessage_deflate::disabled
591  <permessage_deflate_config> permessage_deflate_type;
592 
593  /// Autonegotiate permessage-deflate
594  /**
595  * Automatically enables the permessage-deflate extension.
596  *
597  * For clients this results in a permessage-deflate extension request being
598  * sent with every request rather than requiring it to be requested manually
599  *
600  * For servers this results in accepting the first set of extension settings
601  * requested by the client that we understand being used. The alternative is
602  * requiring the extension to be manually negotiated in `validate`. With
603  * auto-negotiate on, you may still override the auto-negotiate manually if
604  * needed.
605  */
606  //static const bool autonegotiate_compression = false;
607 };
608 
609 } // namespace config
610 } // namespace websocketpp
611 
612 #endif // WEBSOCKETPP_CONFIG_CORE_HPP
static const int client_version
WebSocket Protocol version to use as a client.
static const size_t max_message_size
Default maximum message size.
websocketpp::connection_base connection_base
User overridable Connection base class.
static const long timeout_pong
Length of time to wait for a pong after a ping.
static const int client_version
WebSocket Protocol version to use as a client.
static const bool drop_on_protocol_error
Drop connections immediately on protocol error.
Adds development/debug settings to a config.
Adds production settings to a config.
static const size_t max_http_body_size
Default maximum http body size.
static const websocketpp::log::level elog_level
Default static error logging channels.
websocketpp::connection_base connection_base
User overridable Connection base class.
static const bool drop_on_protocol_error
Drop connections immediately on protocol error.
static const long timeout_proxy
Length of time to wait before a proxy handshake is aborted.
static const long timeout_close_handshake
Length of time before a closing handshake is aborted.
static const long timeout_dns_resolve
Length of time to wait for dns resolution.
static bool const enable_multithreading
static const long timeout_close_handshake
Length of time before a closing handshake is aborted (ms)
static const long timeout_socket_post_init
Length of time to wait for socket post-initialization.
static const size_t max_message_size
Default maximum message size.
static const websocketpp::log::level alog_level
Default static access logging channels.
static const size_t connection_read_buffer_size
Connection read buffer.
static const long timeout_pong
Length of time to wait for a pong after a ping (ms)
static const long timeout_open_handshake
Default timer values (in ms)
static const bool silent_close
Suppresses the return of detailed connection close information.
static const websocketpp::log::level alog_level
Debugging static access logging channels.
void handle_accept(connection_ptr con, lib::error_code const &ec)
Handler callback for start_accept.
websocketpp::endpoint_base endpoint_base
User overridable Endpoint base class.
static const long timeout_connect
Length of time to wait for TCP connect.
static const bool enable_extensions
Global flag for enabling/disabling extensions.
static const long timeout_open_handshake
Length of time before an opening handshake is aborted (ms)
static const long timeout_socket_shutdown
Length of time to wait for socket shutdown.
websocketpp::endpoint_base endpoint_base
User overridable Endpoint base class.
static bool const enable_multithreading
static const bool silent_close
Suppresses the return of detailed connection close information.
static const long timeout_socket_pre_init
Default timer values (in ms)
static const size_t connection_read_buffer_size
Size of the per-connection read buffer.
static const websocketpp::log::level elog_level
Production static error logging channels.
static const bool enable_extensions
Global flag for enabling/disabling extensions.
static const size_t max_http_body_size
Default maximum http body size.
static const websocketpp::log::level elog_level
Debugging level static error logging channels.
static const websocketpp::log::level alog_level
Production static access logging channels.