websocketpp  0.4.0
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
base.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 WEBSOCKETPP_PROCESSOR_BASE_HPP
29 #define WEBSOCKETPP_PROCESSOR_BASE_HPP
30 
31 #include <websocketpp/common/cpp11.hpp>
32 #include <websocketpp/common/system_error.hpp>
33 
34 #include <websocketpp/close.hpp>
35 #include <websocketpp/utilities.hpp>
36 #include <websocketpp/uri.hpp>
37 
38 #include <map>
39 #include <string>
40 
41 namespace websocketpp {
42 namespace processor {
43 
45 namespace constants {
46 
47 static char const upgrade_token[] = "websocket";
48 static char const connection_token[] = "upgrade";
49 static char const handshake_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
50 
51 } // namespace constants
52 
53 
55 namespace error_cat {
56 enum value {
57  BAD_REQUEST = 0, // Error was the result of improperly formatted user input
58  INTERNAL_ERROR = 1, // Error was a logic error internal to WebSocket++
59  PROTOCOL_VIOLATION = 2,
60  MESSAGE_TOO_BIG = 3,
61  PAYLOAD_VIOLATION = 4 // Error was due to receiving invalid payload data
62 };
63 } // namespace error_cat
64 
66 namespace error {
70  general = 1,
71 
74 
77 
80 
83 
86 
89 
92 
95 
98 
101 
104 
107 
110 
113 
116 
119 
122 
125 
128 
131 
134 
137 
140 
143 
146 
149 
152 
155 };
156 
158 class processor_category : public lib::error_category {
159 public:
160  processor_category() {}
161 
162  char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
163  return "websocketpp.processor";
164  }
165 
166  std::string message(int value) const {
167  switch(value) {
168  case error::general:
169  return "Generic processor error";
170  case error::bad_request:
171  return "invalid user input";
173  return "Generic protocol violation";
175  return "A message was too large";
177  return "A payload contained invalid data";
179  return "invalid function arguments";
181  return "invalid opcode";
183  return "Control messages are limited to fewer than 125 characters";
185  return "Invalid use of reserved bits";
187  return "Control messages cannot be fragmented";
189  return "Invalid message continuation";
191  return "Clients may not send unmasked frames";
193  return "Servers may not send masked frames";
195  return "Payload length was not minimally encoded";
197  return "64 bit frames are not supported on 32 bit systems";
198  case error::invalid_utf8:
199  return "Invalid UTF8 encoding";
201  return "Operation required not implemented functionality";
203  return "Invalid HTTP method.";
205  return "Invalid HTTP version.";
207  return "Invalid HTTP status.";
209  return "A required HTTP header is missing";
210  case error::sha1_library:
211  return "SHA-1 library error";
213  return "The WebSocket protocol version in use does not support this feature";
215  return "Reserved close code used";
217  return "Invalid close code used";
219  return "Using a close reason requires a valid close code";
221  return "Error parsing subprotocol header";
223  return "Error parsing extension header";
225  return "Extensions are disabled";
226  default:
227  return "Unknown";
228  }
229  }
230 };
231 
233 inline lib::error_category const & get_processor_category() {
234  static processor_category instance;
235  return instance;
236 }
237 
239 inline lib::error_code make_error_code(error::processor_errors e) {
240  return lib::error_code(static_cast<int>(e), get_processor_category());
241 }
242 
244 
256 inline close::status::value to_ws(lib::error_code ec) {
257  if (ec.category() != get_processor_category()) {
258  return close::status::blank;
259  }
260 
261  switch (ec.value()) {
274  case error::invalid_utf8:
278  default:
280  }
281 }
282 
283 } // namespace error
284 } // namespace processor
285 } // namespace websocketpp
286 
287 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
288 template<> struct is_error_code_enum<websocketpp::processor::error::processor_errors>
289 {
290  static bool const value = true;
291 };
292 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
293 
294 #endif //WEBSOCKETPP_PROCESSOR_BASE_HPP
static value const internal_endpoint_error
Definition: close.hpp:133
uint16_t value
The type of a close code value.
Definition: close.hpp:49
static value const invalid_payload
An endpoint received message data inconsistent with its type.
Definition: close.hpp:110
Embedded SHA-1 library error.
Definition: base.hpp:133
Clients may not send unmasked frames.
Definition: base.hpp:103
WebSocket protocol processor abstract base class.
Definition: processor.hpp:154
Using a reason requires a close code.
Definition: base.hpp:145
Illegal use of reserved bit.
Definition: base.hpp:94
No support for this feature in this protocol version.
Definition: base.hpp:136
static value const protocol_error
A protocol error occurred.
Definition: close.hpp:83
lib::error_category const & get_processor_category()
Get a reference to a static copy of the processor error category.
Definition: base.hpp:233
Processor encountered invalid payload data.
Definition: base.hpp:82
Operation required not implemented functionality.
Definition: base.hpp:118
Continuation without message.
Definition: base.hpp:100
Error was the result of improperly formatted user input.
Definition: base.hpp:73
Processor encountered a protocol violation in an incoming message.
Definition: base.hpp:76
Not supported on 32 bit systems.
Definition: base.hpp:112
Extension related operation was ignored because extensions are disabled.
Definition: base.hpp:154
close::status::value to_ws(lib::error_code ec)
Converts a processor error_code into a websocket close code.
Definition: base.hpp:256
lib::error_code make_error_code(error::processor_errors e)
Create an error code with the given value and the processor category.
Definition: base.hpp:239
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
Servers may not send masked frames.
Definition: base.hpp:106
Fragmented control message.
Definition: base.hpp:97
Processor encountered a message that was too large.
Definition: base.hpp:79
static value const message_too_big
An endpoint received a message too large to process.
Definition: close.hpp:121
Category for processor errors.
Definition: base.hpp:158
Opcode was invalid for requested operation.
Definition: base.hpp:88
The processor method was called with invalid arguments.
Definition: base.hpp:85
Payload length not minimally encoded.
Definition: base.hpp:109
static value const blank
A blank value for internal use.
Definition: close.hpp:52