NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
constants.hpp
Go to the documentation of this file.
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 <exception>
32 #include <map>
33 #include <string>
34 #include <vector>
35 #include <utility>
36 
37 namespace websocketpp {
39 namespace http {
41 
45  typedef std::map<std::string,std::string> attribute_list;
46 
48 
53  typedef std::vector< std::pair<std::string,attribute_list> > parameter_list;
54 
56  static char const header_delimiter[] = "\r\n";
57 
59  static char const header_separator[] = ":";
60 
62  static std::string const empty_header;
63 
65  size_t const max_header_size = 16000;
66 
68  size_t const max_body_size = 32000000;
69 
71  size_t const istream_buffer = 512;
72 
74 
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
95  };
96 
98  inline bool is_token_char(unsigned char c) {
99  return (header_token[c] == 1);
100  }
101 
103  inline bool is_not_token_char(unsigned char c) {
104  return !header_token[c];
105  }
106 
108 
111  inline bool is_whitespace_char(unsigned char c) {
112  return (c == 9 || c == 32);
113  }
114 
116  inline bool is_not_whitespace_char(unsigned char c) {
117  return (c != 9 && c != 32);
118  }
119 
121  namespace status_code {
122  enum value {
124 
127 
128  ok = 200,
129  created = 201,
130  accepted = 202,
132  no_content = 204,
135 
138  found = 302,
139  see_other = 303,
141  use_proxy = 305,
143 
144  bad_request = 400,
147  forbidden = 403,
148  not_found = 404,
153  conflict = 409,
154  gone = 410,
162  im_a_teapot = 418,
167 
170  bad_gateway = 502,
176  };
177 
178  // TODO: should this be inline?
179  inline std::string get_string(value c) {
180  switch (c) {
181  case uninitialized:
182  return "Uninitialized";
183  case continue_code:
184  return "Continue";
185  case switching_protocols:
186  return "Switching Protocols";
187  case ok:
188  return "OK";
189  case created:
190  return "Created";
191  case accepted:
192  return "Accepted";
194  return "Non Authoritative Information";
195  case no_content:
196  return "No Content";
197  case reset_content:
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";
205  case found:
206  return "Found";
207  case see_other:
208  return "See Other";
209  case not_modified:
210  return "Not Modified";
211  case use_proxy:
212  return "Use Proxy";
213  case temporary_redirect:
214  return "Temporary Redirect";
215  case bad_request:
216  return "Bad Request";
217  case unauthorized:
218  return "Unauthorized";
219  case payment_required:
220  return "Payment Required";
221  case forbidden:
222  return "Forbidden";
223  case not_found:
224  return "Not Found";
225  case method_not_allowed:
226  return "Method Not Allowed";
227  case not_acceptable:
228  return "Not Acceptable";
230  return "Proxy Authentication Required";
231  case request_timeout:
232  return "Request Timeout";
233  case conflict:
234  return "Conflict";
235  case gone:
236  return "Gone";
237  case length_required:
238  return "Length Required";
239  case precondition_failed:
240  return "Precondition Failed";
242  return "Request Entity Too Large";
244  return "Request-URI Too Long";
246  return "Unsupported Media Type";
248  return "Requested Range Not Satisfiable";
249  case expectation_failed:
250  return "Expectation Failed";
251  case im_a_teapot:
252  return "I'm a teapot";
253  case upgrade_required:
254  return "Upgrade Required";
256  return "Precondition Required";
257  case too_many_requests:
258  return "Too Many Requests";
260  return "Request Header Fields Too Large";
262  return "Internal Server Error";
263  case not_implemented:
264  return "Not Implemented";
265  case bad_gateway:
266  return "Bad Gateway";
267  case service_unavailable:
268  return "Service Unavailable";
269  case gateway_timeout:
270  return "Gateway Timeout";
272  return "HTTP Version Not Supported";
273  case not_extended:
274  return "Not Extended";
276  return "Network Authentication Required";
277  default:
278  return "Unknown";
279  }
280  }
281  }
282 
283  class exception : public std::exception {
284  public:
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())
289  : m_msg(log_msg)
290  , m_error_msg(error_msg)
291  , m_body(body)
292  , m_error_code(error_code) {}
293 
294  ~exception() throw() {}
295 
296  virtual const char* what() const throw() {
297  return m_msg.c_str();
298  }
299 
300  std::string m_msg;
301  std::string m_error_msg;
302  std::string m_body;
304  };
305 }
306 }
307 
308 #endif // HTTP_CONSTANTS_HPP
static std::string const empty_header
Literal value of an empty header.
Definition: constants.hpp:62
bool is_not_whitespace_char(unsigned char c)
Is the character non-whitespace.
Definition: constants.hpp:116
std::string get_string(value c)
Definition: constants.hpp:179
status_code::value m_error_code
Definition: constants.hpp:303
static char const header_delimiter[]
Literal value of the HTTP header delimiter.
Definition: constants.hpp:56
std::vector< std::pair< std::string, attribute_list > > parameter_list
The type of an HTTP parameter list.
Definition: constants.hpp:53
exception(const std::string &log_msg, status_code::value error_code, const std::string &error_msg=std::string(), const std::string &body=std::string())
Definition: constants.hpp:285
bool is_not_token_char(unsigned char c)
Is the character a non-token.
Definition: constants.hpp:103
virtual const char * what() const
Definition: constants.hpp:296
static char const header_separator[]
Literal value of the HTTP header separator.
Definition: constants.hpp:59
size_t const istream_buffer
Number of bytes to use for temporary istream read buffers.
Definition: constants.hpp:71
static char const header_token[]
invalid HTTP token characters
Definition: constants.hpp:78
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:65
std::map< std::string, std::string > attribute_list
The type of an HTTP attribute list.
Definition: constants.hpp:45
bool is_token_char(unsigned char c)
Is the character a token.
Definition: constants.hpp:98
bool is_whitespace_char(unsigned char c)
Is the character whitespace.
Definition: constants.hpp:111
size_t const max_body_size
Default Maximum size in bytes for HTTP message bodies.
Definition: constants.hpp:68