WebSocket++  0.8.3-dev
C++ websocket client/server library
core_client.hpp
1 /*
2  * Copyright (c) 2014, 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_CORE_CLIENT_HPP
29 #define WEBSOCKETPP_CONFIG_CORE_CLIENT_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 #ifndef _WEBSOCKETPP_NO_THREADING_
38 #include <websocketpp/concurrency/basic.hpp>
39 #else
40 #include <websocketpp/concurrency/none.hpp>
41 #endif
42 
43 // Transport
44 #include <websocketpp/transport/iostream/endpoint.hpp>
45 
46 // HTTP
47 #include <websocketpp/http/request.hpp>
48 #include <websocketpp/http/response.hpp>
49 
50 // Messages
51 #include <websocketpp/message_buffer/message.hpp>
52 #include <websocketpp/message_buffer/alloc.hpp>
53 
54 // Loggers
55 #include <websocketpp/logger/basic.hpp>
56 
57 // RNG
58 #include <websocketpp/random/random_device.hpp>
59 
60 // User stub base classes
61 #include <websocketpp/endpoint_base.hpp>
62 #include <websocketpp/connection_base.hpp>
63 
64 // Extensions
65 #include <websocketpp/extensions/permessage_deflate/disabled.hpp>
66 
67 namespace websocketpp {
68 namespace config {
69 
70 /// Client config with iostream transport
71 struct core_client {
72  typedef core_client type;
73 
74  // Concurrency policy
75 #ifndef _WEBSOCKETPP_NO_THREADING_
76  typedef websocketpp::concurrency::basic concurrency_type;
77 #else
78  typedef websocketpp::concurrency::none concurrency_type;
79 #endif
80 
81  // HTTP Parser Policies
82  typedef http::parser::request request_type;
83  typedef http::parser::response response_type;
84 
85  // Message Policies
86  typedef message_buffer::message<message_buffer::alloc::con_msg_manager>
87  message_type;
88  typedef message_buffer::alloc::con_msg_manager<message_type>
89  con_msg_manager_type;
90  typedef message_buffer::alloc::endpoint_msg_manager<con_msg_manager_type>
91  endpoint_msg_manager_type;
92 
93  /// Logging policies
96  typedef websocketpp::log::basic<concurrency_type,
97  websocketpp::log::alevel> alog_type;
98 
99  /// RNG policies
102 
103  /// Controls compile time enabling/disabling of thread syncronization code
104  /// Disabling can provide a minor performance improvement to single threaded
105  /// applications
106  static bool const enable_multithreading = true;
107 
109  typedef type::concurrency_type concurrency_type;
110  typedef type::elog_type elog_type;
111  typedef type::alog_type alog_type;
112  typedef type::request_type request_type;
113  typedef type::response_type response_type;
114 
115  /// Controls compile time enabling/disabling of thread syncronization
116  /// code Disabling can provide a minor performance improvement to single
117  /// threaded applications
118  static bool const enable_multithreading = true;
119 
120  /// Default timer values (in ms)
121 
122  /// Length of time to wait for socket pre-initialization
123  /**
124  * Exactly what this includes depends on the socket policy in use
125  */
126  static const long timeout_socket_pre_init = 5000;
127 
128  /// Length of time to wait before a proxy handshake is aborted
129  static const long timeout_proxy = 5000;
130 
131  /// Length of time to wait for socket post-initialization
132  /**
133  * Exactly what this includes depends on the socket policy in use.
134  * Often this means the TLS handshake
135  */
136  static const long timeout_socket_post_init = 5000;
137 
138  /// Length of time to wait for dns resolution
139  static const long timeout_dns_resolve = 5000;
140 
141  /// Length of time to wait for TCP connect
142  static const long timeout_connect = 5000;
143 
144  /// Length of time to wait for socket shutdown
145  static const long timeout_socket_shutdown = 5000;
146  };
147 
148  /// Transport Endpoint Component
149  typedef websocketpp::transport::iostream::endpoint<transport_config>
150  transport_type;
151 
152  /// User overridable Endpoint base class
154  /// User overridable Connection base class
156 
157  /// Default timer values (in ms)
158 
159  /// Length of time before an opening handshake is aborted
160  static const long timeout_open_handshake = 5000;
161  /// Length of time before a closing handshake is aborted
162  static const long timeout_close_handshake = 5000;
163  /// Length of time to wait for a pong after a ping
164  static const long timeout_pong = 5000;
165 
166  /// WebSocket Protocol version to use as a client
167  /**
168  * What version of the WebSocket Protocol to use for outgoing client
169  * connections. Setting this to a value other than 13 (RFC6455) is not
170  * recommended.
171  */
172  static const int client_version = 13; // RFC6455
173 
174  /// Default static error logging channels
175  /**
176  * Which error logging channels to enable at compile time. Channels not
177  * enabled here will be unable to be selected by programs using the library.
178  * This option gives an optimizing compiler the ability to remove entirely
179  * code to test whether or not to print out log messages on a certain
180  * channel
181  *
182  * Default is all except for development/debug level errors
183  */
184  static const websocketpp::log::level elog_level =
185  websocketpp::log::elevel::all ^ websocketpp::log::elevel::devel;
186 
187  /// Default static access logging channels
188  /**
189  * Which access logging channels to enable at compile time. Channels not
190  * enabled here will be unable to be selected by programs using the library.
191  * This option gives an optimizing compiler the ability to remove entirely
192  * code to test whether or not to print out log messages on a certain
193  * channel
194  *
195  * Default is all except for development/debug level access messages
196  */
197  static const websocketpp::log::level alog_level =
198  websocketpp::log::alevel::all ^ websocketpp::log::alevel::devel;
199 
200  ///
201  static const size_t connection_read_buffer_size = 16384;
202 
203  /// Drop connections immediately on protocol error.
204  /**
205  * Drop connections on protocol error rather than sending a close frame.
206  * Off by default. This may result in legit messages near the error being
207  * dropped as well. It may free up resources otherwise spent dealing with
208  * misbehaving clients.
209  */
210  static const bool drop_on_protocol_error = false;
211 
212  /// Suppresses the return of detailed connection close information
213  /**
214  * Silence close suppresses the return of detailed connection close
215  * information during the closing handshake. This information is useful
216  * for debugging and presenting useful errors to end users but may be
217  * undesirable for security reasons in some production environments.
218  * Close reasons could be used by an attacker to confirm that the endpoint
219  * is out of resources or be used to identify the WebSocket implementation
220  * in use.
221  *
222  * Note: this will suppress *all* close codes, including those explicitly
223  * sent by local applications.
224  */
225  static const bool silent_close = false;
226 
227  /// Default maximum message size
228  /**
229  * Default value for the processor's maximum message size. Maximum message size
230  * determines the point at which the library will fail a connection with the
231  * message_too_big protocol error.
232  *
233  * The default is 32MB
234  *
235  * @since 0.3.0
236  */
237  static const size_t max_message_size = 32000000;
238 
239  /// Default maximum http body size
240  /**
241  * Default value for the http parser's maximum body size. Maximum body size
242  * determines the point at which the library will abort reading an HTTP
243  * connection with the 413/request entity too large error.
244  *
245  * The default is 32MB
246  *
247  * @since 0.5.0
248  */
249  static const size_t max_http_body_size = 32000000;
250 
251  /// Global flag for enabling/disabling extensions
252  static const bool enable_extensions = true;
253 
254  /// Extension specific settings:
255 
256  /// permessage_deflate extension
258  typedef core_client::request_type request_type;
259 
260  /// If the remote endpoint requests that we reset the compression
261  /// context after each message should we honor the request?
262  static const bool allow_disabling_context_takeover = true;
263 
264  /// If the remote endpoint requests that we reduce the size of the
265  /// LZ77 sliding window size this is the lowest value that will be
266  /// allowed. Values range from 8 to 15. A value of 8 means we will
267  /// allow any possible window size. A value of 15 means do not allow
268  /// negotiation of the window size (ie require the default).
270  };
271 
272  typedef websocketpp::extensions::permessage_deflate::disabled
273  <permessage_deflate_config> permessage_deflate_type;
274 
275  /// Autonegotiate permessage-compress
276  /**
277  * Automatically enables the permessage-compress extension.
278  *
279  * For clients this results in a permessage-compress extension request being
280  * sent with every request rather than requiring it to be requested manually
281  *
282  * For servers this results in accepting the first set of extension settings
283  * requested by the client that we understand being used. The alternative is
284  * requiring the extension to be manually negotiated in `validate`. With
285  * auto-negotiate on, you may still override the auto-negotiate manually if
286  * needed.
287  */
288  //static const bool autonegotiate_compression = false;
289 };
290 
291 } // namespace config
292 } // namespace websocketpp
293 
294 #endif // WEBSOCKETPP_CONFIG_CORE_CLIENT_HPP
websocketpp::config::core_client::transport_config::timeout_connect
static const long timeout_connect
Length of time to wait for TCP connect.
Definition: core_client.hpp:142
websocketpp::config::core_client::client_version
static const int client_version
WebSocket Protocol version to use as a client.
Definition: core_client.hpp:172
websocketpp::config::core_client::silent_close
static const bool silent_close
Suppresses the return of detailed connection close information.
Definition: core_client.hpp:225
websocketpp::config::core_client::permessage_deflate_config::minimum_outgoing_window_bits
static const uint8_t minimum_outgoing_window_bits
Definition: core_client.hpp:269
websocketpp::config::core_client::transport_config::timeout_dns_resolve
static const long timeout_dns_resolve
Length of time to wait for dns resolution.
Definition: core_client.hpp:139
websocketpp::config::core_client::transport_config
Definition: core_client.hpp:108
websocketpp::config::core_client::rng_type
websocketpp::random::random_device::int_generator< uint32_t, concurrency_type > rng_type
RNG policies.
Definition: core_client.hpp:101
websocketpp::config::core_client::elog_level
static const websocketpp::log::level elog_level
Default static error logging channels.
Definition: core_client.hpp:184
websocketpp::config::core_client::transport_config::enable_multithreading
static bool const enable_multithreading
Definition: core_client.hpp:118
websocketpp::config::core_client::max_message_size
static const size_t max_message_size
Default maximum message size.
Definition: core_client.hpp:237
websocketpp::config::core_client::timeout_close_handshake
static const long timeout_close_handshake
Length of time before a closing handshake is aborted.
Definition: core_client.hpp:162
websocketpp::config::core_client::transport_config::timeout_socket_shutdown
static const long timeout_socket_shutdown
Length of time to wait for socket shutdown.
Definition: core_client.hpp:145
websocketpp::versions_supported
static std::vector< int > const versions_supported(helper, helper+4)
Container that stores the list of protocol versions supported.
websocketpp::config::core_client::endpoint_base
websocketpp::endpoint_base endpoint_base
User overridable Endpoint base class.
Definition: core_client.hpp:153
websocketpp::config::core_client::enable_extensions
static const bool enable_extensions
Global flag for enabling/disabling extensions.
Definition: core_client.hpp:252
websocketpp::config::core_client::connection_base
websocketpp::connection_base connection_base
User overridable Connection base class.
Definition: core_client.hpp:155
websocketpp::config::core_client::transport_config::timeout_socket_pre_init
static const long timeout_socket_pre_init
Default timer values (in ms)
Definition: core_client.hpp:126
websocketpp::config::core_client::drop_on_protocol_error
static const bool drop_on_protocol_error
Drop connections immediately on protocol error.
Definition: core_client.hpp:210
websocketpp::config::core_client::elog_type
websocketpp::log::basic< concurrency_type, websocketpp::log::elevel > elog_type
Logging policies.
Definition: core_client.hpp:95
websocketpp::config::core_client::max_http_body_size
static const size_t max_http_body_size
Default maximum http body size.
Definition: core_client.hpp:249
websocketpp::config::core_client::timeout_open_handshake
static const long timeout_open_handshake
Default timer values (in ms)
Definition: core_client.hpp:160
websocketpp::config::core_client::transport_config::timeout_proxy
static const long timeout_proxy
Length of time to wait before a proxy handshake is aborted.
Definition: core_client.hpp:129
websocketpp::config::core_client::enable_multithreading
static bool const enable_multithreading
Definition: core_client.hpp:106
websocketpp::config::core_client::transport_config::timeout_socket_post_init
static const long timeout_socket_post_init
Length of time to wait for socket post-initialization.
Definition: core_client.hpp:136
websocketpp::config::core_client::permessage_deflate_config::allow_disabling_context_takeover
static const bool allow_disabling_context_takeover
Definition: core_client.hpp:262
websocketpp::config::core_client::permessage_deflate_config
Extension specific settings:
Definition: core_client.hpp:257
websocketpp::config::core_client::alog_level
static const websocketpp::log::level alog_level
Default static access logging channels.
Definition: core_client.hpp:197
websocketpp::config::core_client::timeout_pong
static const long timeout_pong
Length of time to wait for a pong after a ping.
Definition: core_client.hpp:164