minimal_server.hpp 11 KB

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