constants.hpp 11 KB

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