2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
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.
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.
28 #ifndef HTTP_CONSTANTS_HPP
29 #define HTTP_CONSTANTS_HPP
37 namespace websocketpp {
38 /// HTTP handling support
40 /// The type of an HTTP attribute list
42 * The attribute list is an unordered key/value map. Encoded attribute
43 * values are delimited by semicolons.
45 typedef std::map<std::string,std::string> attribute_list;
47 /// The type of an HTTP parameter list
49 * The parameter list is an ordered pairing of a parameter and its
50 * associated attribute list. Encoded parameter values are delimited by
53 typedef std::vector< std::pair<std::string,attribute_list> > parameter_list;
55 /// Literal value of the HTTP header delimiter
56 static char const header_delimiter[] = "\r\n";
58 /// Literal value of the HTTP header separator
59 static char const header_separator[] = ":";
61 /// Literal value of an empty header
62 static std::string const empty_header;
64 /// Maximum size in bytes before rejecting an HTTP header as too big.
65 size_t const max_header_size = 16000;
67 /// Default Maximum size in bytes for HTTP message bodies.
68 size_t const max_body_size = 32000000;
70 /// Number of bytes to use for temporary istream read buffers
71 size_t const istream_buffer = 512;
73 /// invalid HTTP token characters
75 * 0x00 - 0x32, 0x7f-0xff
76 * ( ) < > @ , ; : \ " / [ ] ? = { }
78 static char const header_token[] = {
79 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..0f
80 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 10..1f
81 0,1,0,1,1,1,1,1,0,0,1,1,0,1,1,0, // 20..2f
82 1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, // 30..3f
83 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 40..4f
84 1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1, // 50..5f
85 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 60..6f
86 1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0, // 70..7f
87 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 80..8f
88 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 90..9f
89 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // a0..af
90 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // b0..bf
91 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // c0..cf
92 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // d0..df
93 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // e0..ef
94 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // f0..ff
97 /// Is the character a token
98 inline bool is_token_char(unsigned char c) {
99 return (header_token[c] == 1);
102 /// Is the character a non-token
103 inline bool is_not_token_char(unsigned char c) {
104 return !header_token[c];
107 /// Is the character whitespace
109 * whitespace is space (32) or horizontal tab (9)
111 inline bool is_whitespace_char(unsigned char c) {
112 return (c == 9 || c == 32);
115 /// Is the character non-whitespace
116 inline bool is_not_whitespace_char(unsigned char c) {
117 return (c != 9 && c != 32);
120 /// HTTP Status codes
121 namespace status_code {
126 switching_protocols = 101,
131 non_authoritative_information = 203,
134 partial_content = 206,
136 multiple_choices = 300,
137 moved_permanently = 301,
142 temporary_redirect = 307,
146 payment_required = 402,
149 method_not_allowed = 405,
150 not_acceptable = 406,
151 proxy_authentication_required = 407,
152 request_timeout = 408,
155 length_required = 411,
156 precondition_failed = 412,
157 request_entity_too_large = 413,
158 request_uri_too_long = 414,
159 unsupported_media_type = 415,
160 request_range_not_satisfiable = 416,
161 expectation_failed = 417,
163 upgrade_required = 426,
164 precondition_required = 428,
165 too_many_requests = 429,
166 request_header_fields_too_large = 431,
168 internal_server_error = 500,
169 not_implemented = 501,
171 service_unavailable = 503,
172 gateway_timeout = 504,
173 http_version_not_supported = 505,
175 network_authentication_required = 511
178 // TODO: should this be inline?
179 inline std::string get_string(value c) {
182 return "Uninitialized";
185 case switching_protocols:
186 return "Switching Protocols";
193 case non_authoritative_information:
194 return "Non Authoritative Information";
198 return "Reset Content";
199 case partial_content:
200 return "Partial Content";
201 case multiple_choices:
202 return "Multiple Choices";
203 case moved_permanently:
204 return "Moved Permanently";
210 return "Not Modified";
213 case temporary_redirect:
214 return "Temporary Redirect";
216 return "Bad Request";
218 return "Unauthorized";
219 case payment_required:
220 return "Payment Required";
225 case method_not_allowed:
226 return "Method Not Allowed";
228 return "Not Acceptable";
229 case proxy_authentication_required:
230 return "Proxy Authentication Required";
231 case request_timeout:
232 return "Request Timeout";
237 case length_required:
238 return "Length Required";
239 case precondition_failed:
240 return "Precondition Failed";
241 case request_entity_too_large:
242 return "Request Entity Too Large";
243 case request_uri_too_long:
244 return "Request-URI Too Long";
245 case unsupported_media_type:
246 return "Unsupported Media Type";
247 case request_range_not_satisfiable:
248 return "Requested Range Not Satisfiable";
249 case expectation_failed:
250 return "Expectation Failed";
252 return "I'm a teapot";
253 case upgrade_required:
254 return "Upgrade Required";
255 case precondition_required:
256 return "Precondition Required";
257 case too_many_requests:
258 return "Too Many Requests";
259 case request_header_fields_too_large:
260 return "Request Header Fields Too Large";
261 case internal_server_error:
262 return "Internal Server Error";
263 case not_implemented:
264 return "Not Implemented";
266 return "Bad Gateway";
267 case service_unavailable:
268 return "Service Unavailable";
269 case gateway_timeout:
270 return "Gateway Timeout";
271 case http_version_not_supported:
272 return "HTTP Version Not Supported";
274 return "Not Extended";
275 case network_authentication_required:
276 return "Network Authentication Required";
283 class exception : public std::exception {
285 exception(const std::string& log_msg,
286 status_code::value error_code,
287 const std::string& error_msg = std::string(),
288 const std::string& body = std::string())
290 , m_error_msg(error_msg)
292 , m_error_code(error_code) {}
294 ~exception() throw() {}
296 virtual const char* what() const throw() {
297 return m_msg.c_str();
301 std::string m_error_msg;
303 status_code::value m_error_code;
308 #endif // HTTP_CONSTANTS_HPP