NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
close.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2014, Peter Thorson. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the WebSocket++ Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #ifndef WEBSOCKETPP_CLOSE_HPP
30 #define WEBSOCKETPP_CLOSE_HPP
31 
36 #include <websocketpp/error.hpp>
40 
41 #include <string>
42 
43 namespace websocketpp {
45 namespace close {
47 namespace status {
49  typedef uint16_t value;
50 
52  static value const blank = 0;
53 
55 
62  static value const omit_handshake = 1;
63 
65 
72  static value const force_tcp_drop = 2;
73 
76  static value const normal = 1000;
77 
80  static value const going_away = 1001;
81 
83  static value const protocol_error = 1002;
84 
87 
91  static value const unsupported_data = 1003;
92 
94 
97  static value const no_status = 1005;
98 
100 
104  static value const abnormal_close = 1006;
105 
107 
110  static value const invalid_payload = 1007;
111 
113 
118  static value const policy_violation = 1008;
119 
121  static value const message_too_big = 1009;
122 
124 
129  static value const extension_required = 1010;
130 
133  static value const internal_endpoint_error = 1011;
134 
138  static value const service_restart = 1012;
139 
143  static value const try_again_later = 1013;
144 
146 
152  static value const tls_handshake = 1015;
153 
155 
160  static value const subprotocol_error = 3000;
161 
163 
167  static value const invalid_subprotocol_data = 3001;
168 
170  static value const rsv_start = 1016;
172  static value const rsv_end = 2999;
173 
175 
179  inline bool reserved(value code) {
180  return ((code >= rsv_start && code <= rsv_end) ||
181  code == 1004 || code == 1014);
182  }
183 
185  static value const invalid_low = 999;
187  static value const invalid_high = 5000;
188 
190 
194  inline bool invalid(value code) {
195  return (code <= invalid_low || code >= invalid_high ||
196  code == no_status || code == abnormal_close ||
197  code == tls_handshake);
198  }
199 
201 
212  inline bool terminal(value code) {
213  return (code == protocol_error || code == invalid_payload ||
214  code == policy_violation || code == message_too_big ||
215  code == internal_endpoint_error);
216  }
217 
219 
227  inline std::string get_string(value code) {
228  switch (code) {
229  case normal:
230  return "Normal close";
231  case going_away:
232  return "Going away";
233  case protocol_error:
234  return "Protocol error";
235  case unsupported_data:
236  return "Unsupported data";
237  case no_status:
238  return "No status set";
239  case abnormal_close:
240  return "Abnormal close";
241  case invalid_payload:
242  return "Invalid payload";
243  case policy_violation:
244  return "Policy violoation";
245  case message_too_big:
246  return "Message too big";
247  case extension_required:
248  return "Extension required";
249  case internal_endpoint_error:
250  return "Internal endpoint error";
251  case tls_handshake:
252  return "TLS handshake failure";
253  case subprotocol_error:
254  return "Generic subprotocol error";
255  case invalid_subprotocol_data:
256  return "Invalid subprotocol data";
257  default:
258  return "Unknown";
259  }
260  }
261 } // namespace status
262 
265  uint16_t i;
266  char c[2];
267 };
268 
270 
283 inline status::value extract_code(std::string const & payload, lib::error_code
284  & ec)
285 {
286  ec = lib::error_code();
287 
288  if (payload.size() == 0) {
289  return status::no_status;
290  } else if (payload.size() == 1) {
292  return status::protocol_error;
293  }
294 
295  code_converter val;
296 
297  val.c[0] = payload[0];
298  val.c[1] = payload[1];
299 
300  status::value code(ntohs(val.i));
301 
302  if (status::invalid(code)) {
304  }
305 
306  if (status::reserved(code)) {
308  }
309 
310  return code;
311 }
312 
314 
322 inline std::string extract_reason(std::string const & payload, lib::error_code
323  & ec)
324 {
325  std::string reason;
326  ec = lib::error_code();
327 
328  if (payload.size() > 2) {
329  reason.append(payload.begin()+2,payload.end());
330  }
331 
334  }
335 
336  return reason;
337 }
338 
339 } // namespace close
340 } // namespace websocketpp
341 
342 #endif // WEBSOCKETPP_CLOSE_HPP
static value const internal_endpoint_error
An endpoint encountered an unexpected condition that prevented it from fulfilling the request...
Definition: close.hpp:133
uint16_t value
The type of a close code value.
Definition: close.hpp:49
lib::error_code make_error_code(error::value e)
Definition: error.hpp:235
static value const invalid_payload
An endpoint received message data inconsistent with its type.
Definition: close.hpp:110
bool terminal(value code)
Determine if the code represents an unrecoverable error.
Definition: close.hpp:212
static value const extension_required
A client expected the server to accept a required extension request.
Definition: close.hpp:129
static value const try_again_later
Indicates that the service is experiencing overload.
Definition: close.hpp:143
static value const invalid_high
Last value in range that is always invalid on the wire.
Definition: close.hpp:187
bool reserved(value code)
Test whether a close code is in a reserved range.
Definition: close.hpp:179
static value const protocol_error
A protocol error occurred.
Definition: close.hpp:83
static value const normal
Normal closure, meaning that the purpose for which the connection was established has been fulfilled...
Definition: close.hpp:76
static value const invalid_low
First value in range that is always invalid on the wire.
Definition: close.hpp:185
static value const invalid_subprotocol_data
A invalid subprotocol data.
Definition: close.hpp:167
Close code is invalid.
Definition: error.hpp:83
status::value extract_code(std::string const &payload, lib::error_code &ec)
Extract a close code value from a close payload.
Definition: close.hpp:283
static value const policy_violation
An endpoint received a message that violated its policy.
Definition: close.hpp:118
static value const subprotocol_error
A generic subprotocol error.
Definition: close.hpp:160
static value const tls_handshake
An endpoint failed to perform a TLS handshake.
Definition: close.hpp:152
bool invalid(value code)
Test whether a close code is invalid on the wire.
Definition: close.hpp:194
Unable to parse close code.
Definition: error.hpp:77
static value const unsupported_data
The connection was terminated because an endpoint received a type of data it cannot accept...
Definition: close.hpp:91
bool validate(std::string const &s)
Validate a UTF8 string.
Close code is in a reserved range.
Definition: error.hpp:80
static value const rsv_start
First value in range reserved for future protocol use.
Definition: close.hpp:170
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
static value const going_away
The endpoint was "going away", such as a server going down or a browser navigating away from a page...
Definition: close.hpp:80
static value const service_restart
Indicates that the service is restarted.
Definition: close.hpp:138
void close(T *e, websocketpp::connection_hdl hdl)
static value const omit_handshake
Close the connection without a WebSocket close handshake.
Definition: close.hpp:62
static value const rsv_end
Last value in range reserved for future protocol use.
Definition: close.hpp:172
static value const no_status
A dummy value to indicate that no status code was received.
Definition: close.hpp:97
static value const message_too_big
An endpoint received a message too large to process.
Definition: close.hpp:121
static value const force_tcp_drop
Close the connection with a forced TCP drop.
Definition: close.hpp:72
std::string get_string(value code)
Return a human readable interpretation of a WebSocket close code.
Definition: close.hpp:227
static value const abnormal_close
A dummy value to indicate that the connection was closed abnormally.
Definition: close.hpp:104
Type used to convert close statuses between integer and wire representations.
Definition: close.hpp:264
std::string extract_reason(std::string const &payload, lib::error_code &ec)
Extract the reason string from a close payload.
Definition: close.hpp:322
static value const blank
A blank value for internal use.
Definition: close.hpp:52