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