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.

346 lines
12 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_CLOSE_HPP
  28. #define WEBSOCKETPP_CLOSE_HPP
  29. /** \file
  30. * A package of types and methods for manipulating WebSocket close codes.
  31. */
  32. #include <string>
  33. #include <websocketpp/common/network.hpp>
  34. #include <websocketpp/common/stdint.hpp>
  35. #include <websocketpp/error.hpp>
  36. #include <websocketpp/utf8_validator.hpp>
  37. namespace websocketpp
  38. {
  39. /// A package of types and methods for manipulating WebSocket close codes.
  40. namespace close
  41. {
  42. /// A package of types and methods for manipulating WebSocket close status'
  43. namespace status
  44. {
  45. /// The type of a close code value.
  46. typedef uint16_t value;
  47. /// A blank value for internal use.
  48. static const value blank = 0;
  49. /// Close the connection without a WebSocket close handshake.
  50. /**
  51. * This special value requests that the WebSocket connection be closed
  52. * without performing the WebSocket closing handshake. This does not comply
  53. * with RFC6455, but should be safe to do if necessary. This could be useful
  54. * for clients that need to disconnect quickly and cannot afford the
  55. * complete handshake.
  56. */
  57. static const value omit_handshake = 1;
  58. /// Close the connection with a forced TCP drop.
  59. /**
  60. * This special value requests that the WebSocket connection be closed by
  61. * forcibly dropping the TCP connection. This will leave the other side of
  62. * the connection with a broken connection and some expensive timeouts. this
  63. * should not be done except in extreme cases or in cases of malicious
  64. * remote endpoints.
  65. */
  66. static const value force_tcp_drop = 2;
  67. /// Normal closure, meaning that the purpose for which the connection was
  68. /// established has been fulfilled.
  69. static const value normal = 1000;
  70. /// The endpoint was "going away", such as a server going down or a browser
  71. /// navigating away from a page.
  72. static const value going_away = 1001;
  73. /// A protocol error occurred.
  74. static const value protocol_error = 1002;
  75. /// The connection was terminated because an endpoint received a type of
  76. /// data it cannot accept.
  77. /**
  78. * (e.g., an endpoint that understands only text data MAY send this if it
  79. * receives a binary message).
  80. */
  81. static const value unsupported_data = 1003;
  82. /// A dummy value to indicate that no status code was received.
  83. /**
  84. * This value is illegal on the wire.
  85. */
  86. static const value no_status = 1005;
  87. /// A dummy value to indicate that the connection was closed abnormally.
  88. /**
  89. * In such a case there was no close frame to extract a value from. This
  90. * value is illegal on the wire.
  91. */
  92. static const value abnormal_close = 1006;
  93. /// An endpoint received message data inconsistent with its type.
  94. /**
  95. * For example: Invalid UTF8 bytes in a text message.
  96. */
  97. static const value invalid_payload = 1007;
  98. /// An endpoint received a message that violated its policy.
  99. /**
  100. * This is a generic status code that can be returned when there is no other
  101. * more suitable status code (e.g., 1003 or 1009) or if there is a need to
  102. * hide specific details about the policy.
  103. */
  104. static const value policy_violation = 1008;
  105. /// An endpoint received a message too large to process.
  106. static const value message_too_big = 1009;
  107. /// A client expected the server to accept a required extension request
  108. /**
  109. * The list of extensions that are needed SHOULD appear in the /reason/ part
  110. * of the Close frame. Note that this status code is not used by the server,
  111. * because it can fail the WebSocket handshake instead.
  112. */
  113. static const value extension_required = 1010;
  114. /// An endpoint encountered an unexpected condition that prevented it from
  115. /// fulfilling the request.
  116. static const value internal_endpoint_error = 1011;
  117. /// Indicates that the service is restarted. A client may reconnect and if
  118. /// if it chooses to do so, should reconnect using a randomized delay of
  119. /// 5-30s
  120. static const value service_restart = 1012;
  121. /// Indicates that the service is experiencing overload. A client should
  122. /// only connect to a different IP (when there are multiple for the target)
  123. /// or reconnect to the same IP upon user action.
  124. static const value try_again_later = 1013;
  125. /// Indicates that the server was acting as a gateway or proxy and received
  126. /// an invalid response from the upstream server. This is similar to 502
  127. /// HTTP Status Code.
  128. static const value bad_gateway = 1014;
  129. /// An endpoint failed to perform a TLS handshake
  130. /**
  131. * Designated for use in applications expecting a status code to indicate
  132. * that the connection was closed due to a failure to perform a TLS
  133. * handshake (e.g., the server certificate can't be verified). This value is
  134. * illegal on the wire.
  135. */
  136. static const value tls_handshake = 1015;
  137. /// A generic subprotocol error
  138. /**
  139. * Indicates that a subprotocol error occurred. Typically this involves
  140. * receiving a message that is not formatted as a valid message for the
  141. * subprotocol in use.
  142. */
  143. static const value subprotocol_error = 3000;
  144. /// A invalid subprotocol data
  145. /**
  146. * Indicates that data was received that violated the specification of the
  147. * subprotocol in use.
  148. */
  149. static const value invalid_subprotocol_data = 3001;
  150. /// First value in range reserved for future protocol use
  151. static const value rsv_start = 1016;
  152. /// Last value in range reserved for future protocol use
  153. static const value rsv_end = 2999;
  154. /// Test whether a close code is in a reserved range
  155. /**
  156. * @param [in] code The code to test
  157. * @return Whether or not code is reserved
  158. */
  159. inline bool reserved(value code)
  160. {
  161. return ((code >= rsv_start && code <= rsv_end) || code == 1004);
  162. }
  163. /// First value in range that is always invalid on the wire
  164. static const value invalid_low = 999;
  165. /// Last value in range that is always invalid on the wire
  166. static const value invalid_high = 5000;
  167. /// Test whether a close code is invalid on the wire
  168. /**
  169. * @param [in] code The code to test
  170. * @return Whether or not code is invalid on the wire
  171. */
  172. inline bool invalid(value code)
  173. {
  174. return (code <= invalid_low || code >= invalid_high || code == no_status || code == abnormal_close || code == tls_handshake);
  175. }
  176. /// Determine if the code represents an unrecoverable error
  177. /**
  178. * There is a class of errors for which once they are discovered normal
  179. * WebSocket functionality can no longer occur. This function determines
  180. * if a given code is one of these values. This information is used to
  181. * determine if the system has the capability of waiting for a close
  182. * acknowledgement or if it should drop the TCP connection immediately
  183. * after sending its close frame.
  184. *
  185. * @param [in] code The value to test.
  186. * @return True if the code represents an unrecoverable error
  187. */
  188. inline bool terminal(value code)
  189. {
  190. return (code == protocol_error || code == invalid_payload || code == policy_violation || code == message_too_big || code == internal_endpoint_error);
  191. }
  192. /// Return a human readable interpretation of a WebSocket close code
  193. /**
  194. * See https://tools.ietf.org/html/rfc6455#section-7.4 for more details.
  195. *
  196. * @since 0.3.0
  197. *
  198. * @param [in] code The code to look up.
  199. * @return A human readable interpretation of the code.
  200. */
  201. inline std::string get_string(value code)
  202. {
  203. switch (code)
  204. {
  205. case normal : return "Normal close";
  206. case going_away : return "Going away";
  207. case protocol_error : return "Protocol error";
  208. case unsupported_data : return "Unsupported data";
  209. case no_status : return "No status set";
  210. case abnormal_close : return "Abnormal close";
  211. case invalid_payload : return "Invalid payload";
  212. case policy_violation : return "Policy violoation";
  213. case message_too_big : return "Message too big";
  214. case extension_required : return "Extension required";
  215. case internal_endpoint_error : return "Internal endpoint error";
  216. case service_restart : return "Service restart";
  217. case try_again_later : return "Try again later";
  218. case bad_gateway : return "Bad gateway";
  219. case tls_handshake : return "TLS handshake failure";
  220. case subprotocol_error : return "Generic subprotocol error";
  221. case invalid_subprotocol_data: return "Invalid subprotocol data";
  222. default : return "Unknown";
  223. }
  224. }
  225. } // namespace status
  226. /// Type used to convert close statuses between integer and wire representations
  227. union code_converter
  228. {
  229. uint16_t i;
  230. char c[2];
  231. };
  232. /// Extract a close code value from a close payload
  233. /**
  234. * If there is no close value (ie string is empty) status::no_status is
  235. * returned. If a code couldn't be extracted (usually do to a short or
  236. * otherwise mangled payload) status::protocol_error is returned and the ec
  237. * value is flagged as an error. Note that this case is different than the case
  238. * where protocol error is received over the wire.
  239. *
  240. * If the value is in an invalid or reserved range ec is set accordingly.
  241. *
  242. * @param [in] payload Close frame payload value received over the wire.
  243. * @param [out] ec Set to indicate what error occurred, if any.
  244. * @return The extracted value
  245. */
  246. inline status::value extract_code(const std::string & payload, lib::error_code & ec)
  247. {
  248. ec = lib::error_code();
  249. if (payload.size() == 0)
  250. {
  251. return status::no_status;
  252. }
  253. else if (payload.size() == 1)
  254. {
  255. ec = make_error_code(error::bad_close_code);
  256. return status::protocol_error;
  257. }
  258. code_converter val;
  259. val.c[0] = payload[0];
  260. val.c[1] = payload[1];
  261. status::value code(ntohs(val.i));
  262. if (status::invalid(code))
  263. {
  264. ec = make_error_code(error::invalid_close_code);
  265. }
  266. if (status::reserved(code))
  267. {
  268. ec = make_error_code(error::reserved_close_code);
  269. }
  270. return code;
  271. }
  272. /// Extract the reason string from a close payload
  273. /**
  274. * The string should be a valid UTF8 message. error::invalid_utf8 will be set if
  275. * the function extracts a reason that is not valid UTF8.
  276. *
  277. * @param [in] payload The payload string to extract a reason from.
  278. * @param [out] ec Set to indicate what error occurred, if any.
  279. * @return The reason string.
  280. */
  281. inline std::string extract_reason(const std::string & payload, lib::error_code & ec)
  282. {
  283. std::string reason;
  284. ec = lib::error_code();
  285. if (payload.size() > 2)
  286. {
  287. reason.append(payload.begin() + 2, payload.end());
  288. }
  289. if (! websocketpp::utf8_validator::validate(reason))
  290. {
  291. ec = make_error_code(error::invalid_utf8);
  292. }
  293. return reason;
  294. }
  295. } // namespace close
  296. } // namespace websocketpp
  297. #endif // WEBSOCKETPP_CLOSE_HPP