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.

276 lines
9.6 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 HTTP_CONSTANTS_HPP
  28. #define HTTP_CONSTANTS_HPP
  29. #include <exception>
  30. #include <map>
  31. #include <string>
  32. #include <utility>
  33. #include <vector>
  34. namespace websocketpp
  35. {
  36. /// HTTP handling support
  37. namespace http
  38. {
  39. /// The type of an HTTP attribute list
  40. /**
  41. * The attribute list is an unordered key/value map. Encoded attribute
  42. * values are delimited by semicolons.
  43. */
  44. typedef std::map<std::string, std::string> attribute_list;
  45. /// The type of an HTTP parameter list
  46. /**
  47. * The parameter list is an ordered pairing of a parameter and its
  48. * associated attribute list. Encoded parameter values are delimited by
  49. * commas.
  50. */
  51. typedef std::vector<std::pair<std::string, attribute_list>> parameter_list;
  52. /// Literal value of the HTTP header delimiter
  53. static const char header_delimiter[] = "\r\n";
  54. /// Literal value of the HTTP header separator
  55. static const char header_separator[] = ":";
  56. /// Literal value of an empty header
  57. static const std::string empty_header;
  58. /// Maximum size in bytes before rejecting an HTTP header as too big.
  59. const size_t max_header_size = 16000;
  60. /// Default Maximum size in bytes for HTTP message bodies.
  61. const size_t max_body_size = 32000000;
  62. /// Number of bytes to use for temporary istream read buffers
  63. const size_t istream_buffer = 512;
  64. /// invalid HTTP token characters
  65. /**
  66. * 0x00 - 0x32, 0x7f-0xff
  67. * ( ) < > @ , ; : \ " / [ ] ? = { }
  68. */
  69. static const char header_token[] = {
  70. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..0f
  71. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10..1f
  72. 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 20..2f
  73. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 30..3f
  74. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 40..4f
  75. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 50..5f
  76. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 60..6f
  77. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, // 70..7f
  78. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80..8f
  79. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90..9f
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // a0..af
  81. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // b0..bf
  82. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // c0..cf
  83. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // d0..df
  84. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // e0..ef
  85. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // f0..ff
  86. };
  87. /// Is the character a token
  88. inline bool is_token_char(unsigned char c)
  89. {
  90. return (header_token[c] == 1);
  91. }
  92. /// Is the character a non-token
  93. inline bool is_not_token_char(unsigned char c)
  94. {
  95. return ! header_token[c];
  96. }
  97. /// Is the character whitespace
  98. /**
  99. * whitespace is space (32) or horizontal tab (9)
  100. */
  101. inline bool is_whitespace_char(unsigned char c)
  102. {
  103. return (c == 9 || c == 32);
  104. }
  105. /// Is the character non-whitespace
  106. inline bool is_not_whitespace_char(unsigned char c)
  107. {
  108. return (c != 9 && c != 32);
  109. }
  110. /// HTTP Status codes
  111. namespace status_code
  112. {
  113. enum value
  114. {
  115. uninitialized = 0,
  116. continue_code = 100,
  117. switching_protocols = 101,
  118. ok = 200,
  119. created = 201,
  120. accepted = 202,
  121. non_authoritative_information = 203,
  122. no_content = 204,
  123. reset_content = 205,
  124. partial_content = 206,
  125. multiple_choices = 300,
  126. moved_permanently = 301,
  127. found = 302,
  128. see_other = 303,
  129. not_modified = 304,
  130. use_proxy = 305,
  131. temporary_redirect = 307,
  132. bad_request = 400,
  133. unauthorized = 401,
  134. payment_required = 402,
  135. forbidden = 403,
  136. not_found = 404,
  137. method_not_allowed = 405,
  138. not_acceptable = 406,
  139. proxy_authentication_required = 407,
  140. request_timeout = 408,
  141. conflict = 409,
  142. gone = 410,
  143. length_required = 411,
  144. precondition_failed = 412,
  145. request_entity_too_large = 413,
  146. request_uri_too_long = 414,
  147. unsupported_media_type = 415,
  148. request_range_not_satisfiable = 416,
  149. expectation_failed = 417,
  150. im_a_teapot = 418,
  151. upgrade_required = 426,
  152. precondition_required = 428,
  153. too_many_requests = 429,
  154. request_header_fields_too_large = 431,
  155. internal_server_error = 500,
  156. not_implemented = 501,
  157. bad_gateway = 502,
  158. service_unavailable = 503,
  159. gateway_timeout = 504,
  160. http_version_not_supported = 505,
  161. not_extended = 510,
  162. network_authentication_required = 511
  163. };
  164. // TODO: should this be inline?
  165. inline std::string get_string(value c)
  166. {
  167. switch (c)
  168. {
  169. case uninitialized : return "Uninitialized";
  170. case continue_code : return "Continue";
  171. case switching_protocols : return "Switching Protocols";
  172. case ok : return "OK";
  173. case created : return "Created";
  174. case accepted : return "Accepted";
  175. case non_authoritative_information : return "Non Authoritative Information";
  176. case no_content : return "No Content";
  177. case reset_content : return "Reset Content";
  178. case partial_content : return "Partial Content";
  179. case multiple_choices : return "Multiple Choices";
  180. case moved_permanently : return "Moved Permanently";
  181. case found : return "Found";
  182. case see_other : return "See Other";
  183. case not_modified : return "Not Modified";
  184. case use_proxy : return "Use Proxy";
  185. case temporary_redirect : return "Temporary Redirect";
  186. case bad_request : return "Bad Request";
  187. case unauthorized : return "Unauthorized";
  188. case payment_required : return "Payment Required";
  189. case forbidden : return "Forbidden";
  190. case not_found : return "Not Found";
  191. case method_not_allowed : return "Method Not Allowed";
  192. case not_acceptable : return "Not Acceptable";
  193. case proxy_authentication_required : return "Proxy Authentication Required";
  194. case request_timeout : return "Request Timeout";
  195. case conflict : return "Conflict";
  196. case gone : return "Gone";
  197. case length_required : return "Length Required";
  198. case precondition_failed : return "Precondition Failed";
  199. case request_entity_too_large : return "Request Entity Too Large";
  200. case request_uri_too_long : return "Request-URI Too Long";
  201. case unsupported_media_type : return "Unsupported Media Type";
  202. case request_range_not_satisfiable : return "Requested Range Not Satisfiable";
  203. case expectation_failed : return "Expectation Failed";
  204. case im_a_teapot : return "I'm a teapot";
  205. case upgrade_required : return "Upgrade Required";
  206. case precondition_required : return "Precondition Required";
  207. case too_many_requests : return "Too Many Requests";
  208. case request_header_fields_too_large: return "Request Header Fields Too Large";
  209. case internal_server_error : return "Internal Server Error";
  210. case not_implemented : return "Not Implemented";
  211. case bad_gateway : return "Bad Gateway";
  212. case service_unavailable : return "Service Unavailable";
  213. case gateway_timeout : return "Gateway Timeout";
  214. case http_version_not_supported : return "HTTP Version Not Supported";
  215. case not_extended : return "Not Extended";
  216. case network_authentication_required: return "Network Authentication Required";
  217. default : return "Unknown";
  218. }
  219. }
  220. } // namespace status_code
  221. class exception : public std::exception
  222. {
  223. public:
  224. exception(const std::string & log_msg, status_code::value error_code, const std::string & error_msg = std::string(), const std::string & body = std::string()) :
  225. m_msg(log_msg),
  226. m_error_msg(error_msg),
  227. m_body(body),
  228. m_error_code(error_code)
  229. {
  230. }
  231. ~exception() throw() {}
  232. virtual const char * what() const throw()
  233. {
  234. return m_msg.c_str();
  235. }
  236. std::string m_msg;
  237. std::string m_error_msg;
  238. std::string m_body;
  239. status_code::value m_error_code;
  240. };
  241. } // namespace http
  242. } // namespace websocketpp
  243. #endif // HTTP_CONSTANTS_HPP