You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
9.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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_PROCESSOR_BASE_HPP
  28. #define WEBSOCKETPP_PROCESSOR_BASE_HPP
  29. #include <string>
  30. #include <websocketpp/close.hpp>
  31. #include <websocketpp/common/cpp11.hpp>
  32. #include <websocketpp/common/system_error.hpp>
  33. #include <websocketpp/uri.hpp>
  34. #include <websocketpp/utilities.hpp>
  35. namespace websocketpp
  36. {
  37. namespace processor
  38. {
  39. /// Constants related to processing WebSocket connections
  40. namespace constants
  41. {
  42. static const char upgrade_token[] = "websocket";
  43. static const char connection_token[] = "Upgrade";
  44. static const char handshake_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  45. } // namespace constants
  46. /// Processor class related error codes
  47. namespace error_cat
  48. {
  49. enum value
  50. {
  51. BAD_REQUEST = 0, // Error was the result of improperly formatted user input
  52. INTERNAL_ERROR = 1, // Error was a logic error internal to WebSocket++
  53. PROTOCOL_VIOLATION = 2,
  54. MESSAGE_TOO_BIG = 3,
  55. PAYLOAD_VIOLATION = 4 // Error was due to receiving invalid payload data
  56. };
  57. } // namespace error_cat
  58. /// Error code category and codes used by all processor types
  59. namespace error
  60. {
  61. enum processor_errors
  62. {
  63. /// Catch-all error for processor policy errors that don't fit in other
  64. /// categories
  65. general = 1,
  66. /// Error was the result of improperly formatted user input
  67. bad_request,
  68. /// Processor encountered a protocol violation in an incoming message
  69. protocol_violation,
  70. /// Processor encountered a message that was too large
  71. message_too_big,
  72. /// Processor encountered invalid payload data.
  73. invalid_payload,
  74. /// The processor method was called with invalid arguments
  75. invalid_arguments,
  76. /// Opcode was invalid for requested operation
  77. invalid_opcode,
  78. /// Control frame too large
  79. control_too_big,
  80. /// Illegal use of reserved bit
  81. invalid_rsv_bit,
  82. /// Fragmented control message
  83. fragmented_control,
  84. /// Continuation without message
  85. invalid_continuation,
  86. /// Clients may not send unmasked frames
  87. masking_required,
  88. /// Servers may not send masked frames
  89. masking_forbidden,
  90. /// Payload length not minimally encoded
  91. non_minimal_encoding,
  92. /// Not supported on 32 bit systems
  93. requires_64bit,
  94. /// Invalid UTF-8 encoding
  95. invalid_utf8,
  96. /// Operation required not implemented functionality
  97. not_implemented,
  98. /// Invalid HTTP method
  99. invalid_http_method,
  100. /// Invalid HTTP version
  101. invalid_http_version,
  102. /// Invalid HTTP status
  103. invalid_http_status,
  104. /// Missing Required Header
  105. missing_required_header,
  106. /// Embedded SHA-1 library error
  107. sha1_library,
  108. /// No support for this feature in this protocol version.
  109. no_protocol_support,
  110. /// Reserved close code used
  111. reserved_close_code,
  112. /// Invalid close code used
  113. invalid_close_code,
  114. /// Using a reason requires a close code
  115. reason_requires_code,
  116. /// Error parsing subprotocols
  117. subprotocol_parse_error,
  118. /// Error parsing extensions
  119. extension_parse_error,
  120. /// Extension related operation was ignored because extensions are disabled
  121. extensions_disabled,
  122. /// Short Ke3 read. Hybi00 requires a third key to be read from the 8 bytes
  123. /// after the handshake. Less than 8 bytes were read.
  124. short_key3
  125. };
  126. /// Category for processor errors
  127. class processor_category : public lib::error_category
  128. {
  129. public:
  130. processor_category() {}
  131. const char * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_
  132. {
  133. return "websocketpp.processor";
  134. }
  135. std::string message(int value) const
  136. {
  137. switch (value)
  138. {
  139. case error::general : return "Generic processor error";
  140. case error::bad_request : return "invalid user input";
  141. case error::protocol_violation : return "Generic protocol violation";
  142. case error::message_too_big : return "A message was too large";
  143. case error::invalid_payload : return "A payload contained invalid data";
  144. case error::invalid_arguments : return "invalid function arguments";
  145. case error::invalid_opcode : return "invalid opcode";
  146. case error::control_too_big : return "Control messages are limited to fewer than 125 characters";
  147. case error::invalid_rsv_bit : return "Invalid use of reserved bits";
  148. case error::fragmented_control : return "Control messages cannot be fragmented";
  149. case error::invalid_continuation : return "Invalid message continuation";
  150. case error::masking_required : return "Clients may not send unmasked frames";
  151. case error::masking_forbidden : return "Servers may not send masked frames";
  152. case error::non_minimal_encoding : return "Payload length was not minimally encoded";
  153. case error::requires_64bit : return "64 bit frames are not supported on 32 bit systems";
  154. case error::invalid_utf8 : return "Invalid UTF8 encoding";
  155. case error::not_implemented : return "Operation required not implemented functionality";
  156. case error::invalid_http_method : return "Invalid HTTP method.";
  157. case error::invalid_http_version : return "Invalid HTTP version.";
  158. case error::invalid_http_status : return "Invalid HTTP status.";
  159. case error::missing_required_header: return "A required HTTP header is missing";
  160. case error::sha1_library : return "SHA-1 library error";
  161. case error::no_protocol_support : return "The WebSocket protocol version in use does not support this feature";
  162. case error::reserved_close_code : return "Reserved close code used";
  163. case error::invalid_close_code : return "Invalid close code used";
  164. case error::reason_requires_code : return "Using a close reason requires a valid close code";
  165. case error::subprotocol_parse_error: return "Error parsing subprotocol header";
  166. case error::extension_parse_error : return "Error parsing extension header";
  167. case error::extensions_disabled : return "Extensions are disabled";
  168. case error::short_key3 : return "Short Hybi00 Key 3 read";
  169. default : return "Unknown";
  170. }
  171. }
  172. };
  173. /// Get a reference to a static copy of the processor error category
  174. inline const lib::error_category & get_processor_category()
  175. {
  176. static processor_category instance;
  177. return instance;
  178. }
  179. /// Create an error code with the given value and the processor category
  180. inline lib::error_code make_error_code(error::processor_errors e)
  181. {
  182. return lib::error_code(static_cast<int>(e), get_processor_category());
  183. }
  184. /// Converts a processor error_code into a websocket close code
  185. /**
  186. * Looks up the appropriate WebSocket close code that should be sent after an
  187. * error of this sort occurred.
  188. *
  189. * If the error is not in the processor category close::status::blank is
  190. * returned.
  191. *
  192. * If the error isn't normally associated with reasons to close a connection
  193. * (such as errors intended to be used internally or delivered to client
  194. * applications, ex: invalid arguments) then
  195. * close::status::internal_endpoint_error is returned.
  196. */
  197. inline close::status::value to_ws(lib::error_code ec)
  198. {
  199. if (ec.category() != get_processor_category())
  200. {
  201. return close::status::blank;
  202. }
  203. switch (ec.value())
  204. {
  205. case error::protocol_violation:
  206. case error::control_too_big:
  207. case error::invalid_opcode:
  208. case error::invalid_rsv_bit:
  209. case error::fragmented_control:
  210. case error::invalid_continuation:
  211. case error::masking_required:
  212. case error::masking_forbidden:
  213. case error::reserved_close_code:
  214. case error::invalid_close_code : return close::status::protocol_error;
  215. case error::invalid_payload :
  216. case error::invalid_utf8 : return close::status::invalid_payload;
  217. case error::message_too_big : return close::status::message_too_big;
  218. default : return close::status::internal_endpoint_error;
  219. }
  220. }
  221. } // namespace error
  222. } // namespace processor
  223. } // namespace websocketpp
  224. _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
  225. template <> struct is_error_code_enum<websocketpp::processor::error::processor_errors>
  226. {
  227. static const bool value = true;
  228. };
  229. _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
  230. #endif // WEBSOCKETPP_PROCESSOR_BASE_HPP