WebSocket++ 0.8.3-dev
C++ websocket client/server library
Loading...
Searching...
No Matches
error.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_ERROR_HPP
29#define WEBSOCKETPP_ERROR_HPP
30
31#include <exception>
32#include <string>
33#include <utility>
34
35#include <websocketpp/common/cpp11.hpp>
36#include <websocketpp/common/system_error.hpp>
37
38namespace websocketpp {
39
40/// Combination error code / string type for returning two values
41typedef std::pair<lib::error_code,std::string> err_str_pair;
42
43/// Library level error codes
44namespace error {
45enum value {
46 /// Catch-all library error
48
49 /// send attempted when endpoint write queue was full
51
52 /// Attempted an operation using a payload that was improperly formatted
53 /// ex: invalid UTF8 encoding on a text message.
55
56 /// Attempted to open a secure connection with an insecure endpoint
58
59 /// Attempted an operation that required an endpoint that is no longer
60 /// available. This is usually because the endpoint went out of scope
61 /// before a connection that it created.
63
64 /// An invalid uri was supplied
66
67 /// The endpoint is out of outgoing message buffers
69
70 /// The endpoint is out of incoming message buffers
72
73 /// The connection was in the wrong state for this operation
75
76 /// Unable to parse close code
78
79 /// Close code is in a reserved range
81
82 /// Close code is invalid
84
85 /// Invalid UTF-8
87
88 /// Invalid subprotocol
90
91 /// An operation was attempted on a connection that did not exist or was
92 /// already deleted.
94
95 /// Unit testing utility error code
97
98 /// Connection creation attempted failed
100
101 /// Selected subprotocol was not requested by the client
103
104 /// Attempted to use a client specific feature on a server endpoint
106
107 /// Attempted to use a server specific feature on a client endpoint
109
110 /// HTTP connection ended
112
113 /// WebSocket opening handshake timed out
115
116 /// WebSocket close handshake timed out
118
119 /// Invalid port in URI
121
122 /// An async accept operation failed because the underlying transport has been
123 /// requested to not listen for new connections anymore.
125
126 /// The requested operation was canceled
128
129 /// Connection rejected
131
132 /// Upgrade Required. This happens if an HTTP request is made to a
133 /// WebSocket++ server that doesn't implement an http handler
135
136 /// Invalid WebSocket protocol version
138
139 /// Unsupported WebSocket protocol version
141
142 /// HTTP parse error
144
145 /// Extension negotiation failed
147
148 /// General transport error, consult more specific transport error code
150}; // enum value
151
152
153class category : public lib::error_category {
154public:
155 category() {}
156
157 char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
158 return "websocketpp";
159 }
160
161 std::string message(int value) const {
162 switch(value) {
163 case error::general:
164 return "Generic error";
166 return "send queue full";
168 return "payload violation";
170 return "endpoint not secure";
172 return "endpoint not available";
174 return "invalid uri";
176 return "no outgoing message buffers";
178 return "no incoming message buffers";
180 return "invalid state";
182 return "Unable to extract close code";
184 return "Extracted close code is in an invalid range";
186 return "Extracted close code is in a reserved range";
188 return "Invalid UTF-8";
190 return "Invalid subprotocol";
192 return "Bad Connection";
193 case error::test:
194 return "Test Error";
196 return "Connection creation attempt failed";
198 return "Selected subprotocol was not requested by the client";
200 return "Feature not available on server endpoints";
202 return "Feature not available on client endpoints";
204 return "HTTP connection ended";
206 return "The opening handshake timed out";
208 return "The closing handshake timed out";
210 return "Invalid URI port";
212 return "Async Accept not listening";
214 return "Operation canceled";
215 case error::rejected:
216 return "Connection rejected";
218 return "Upgrade required";
220 return "Invalid version";
222 return "Unsupported version";
224 return "HTTP parse error";
226 return "Extension negotiation failed";
228 return "An error occurred in the underlying transport. Consult transport error code for more details.";
229 default:
230 return "Unknown";
231 }
232 }
233};
234
235inline const lib::error_category& get_category() {
236 static category instance;
237 return instance;
238}
239
240inline lib::error_code make_error_code(error::value e) {
241 return lib::error_code(static_cast<int>(e), get_category());
242}
243
244} // namespace error
245} // namespace websocketpp
246
249{
250 static bool const value = true;
251};
253
254namespace websocketpp {
255
256class exception : public std::exception {
257public:
258 exception(std::string const & msg, lib::error_code ec = make_error_code(error::general))
259 : m_msg(msg.empty() ? ec.message() : msg), m_code(ec)
260 {}
261
262 explicit exception(lib::error_code ec)
263 : m_msg(ec.message()), m_code(ec)
264 {}
265
266 ~exception() throw() {}
267
268 virtual char const * what() const throw() {
269 return m_msg.c_str();
270 }
271
272 lib::error_code code() const throw() {
273 return m_code;
274 }
275
276 const std::string m_msg;
277 lib::error_code m_code;
278};
279
280} // namespace websocketpp
281
282#endif // WEBSOCKETPP_ERROR_HPP
#define _WEBSOCKETPP_CPP11_FUNCTIONAL_
#define _WEBSOCKETPP_CPP11_THREAD_
#define _WEBSOCKETPP_CPP11_MEMORY_
#define _WEBSOCKETPP_CPP11_SYSTEM_ERROR_
Concurrency policy that uses std::mutex / boost::mutex.
Definition basic.hpp:37
Stub for user supplied base class.
Stub for user supplied base class.
Stub class for use when disabling permessage_deflate extension.
Definition disabled.hpp:53
Stores, parses, and manipulates HTTP requests.
Definition request.hpp:50
Stores, parses, and manipulates HTTP responses.
Definition response.hpp:57
Basic logger that outputs to an ostream.
Definition basic.hpp:59
Thread safe stub "random" integer generator.
Definition none.hpp:46
Server endpoint role based on the given config.
Basic ASIO endpoint socket component.
Definition none.hpp:318
Asio based endpoint transport component.
Definition endpoint.hpp:54
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
connection_hdl get_handle() const
Get the connection handle.
config::alog_type alog_type
Type of this transport's access logging policy.
lib::error_code dispatch(dispatch_handler handler)
Call given handler back within the transport's event system (if present).
void async_shutdown(transport::shutdown_handler handler)
Perform cleanup on socket shutdown_handler.
void set_write_handler(write_handler h)
Sets the write handler.
void set_secure(bool value)
Set whether or not this connection is secure.
void set_shutdown_handler(shutdown_handler h)
Sets the shutdown handler.
connection< config > type
Type of this connection transport component.
config::elog_type elog_type
Type of this transport's error logging policy.
void fatal_error()
Signal transport error.
size_t read_some(char const *buf, size_t len)
Manual input supply (read some).
size_t read_all(char const *buf, size_t len)
Manual input supply (read all).
void async_write(char const *buf, size_t len, transport::write_handler handler)
Asyncronous Transport Write.
size_t readsome(char const *buf, size_t len)
Manual input supply (DEPRECATED).
config::concurrency_type concurrency_type
transport concurrency policy
void init(init_handler handler)
Initialize the connection transport.
timer_ptr set_timer(long, timer_handler)
Call back a function after a period of time.
friend std::istream & operator>>(std::istream &in, type &t)
Overloaded stream input operator.
void set_vector_write_handler(vector_write_handler h)
Sets the vectored write handler.
bool is_secure() const
Tests whether or not the underlying transport is secure.
std::string get_remote_endpoint() const
Get human readable remote endpoint address.
void set_handle(connection_hdl hdl)
Set Connection Handle.
void register_ostream(std::ostream *o)
Register a std::ostream with the transport for writing output.
void async_read_at_least(size_t num_bytes, char *buf, size_t len, read_handler handler)
Initiate an async_read for at least num_bytes bytes into buf.
void async_write(std::vector< buffer > const &bufs, transport::write_handler handler)
Asyncronous Transport Write (scatter-gather).
ptr get_shared()
Get a shared pointer to this component.
iostream::connection< config > transport_con_type
Definition endpoint.hpp:62
config::elog_type elog_type
Type of this endpoint's error logging policy.
Definition endpoint.hpp:56
void set_write_handler(write_handler h)
Sets the write handler.
Definition endpoint.hpp:134
void set_shutdown_handler(shutdown_handler h)
Sets the shutdown handler.
Definition endpoint.hpp:154
bool is_secure() const
Tests whether or not the underlying transport is secure.
Definition endpoint.hpp:116
lib::shared_ptr< type > ptr
Type of a pointer to this endpoint transport component.
Definition endpoint.hpp:51
transport_con_type::ptr transport_con_ptr
Definition endpoint.hpp:65
void async_connect(transport_con_ptr, uri_ptr, connect_handler cb)
Initiate a new connection.
Definition endpoint.hpp:183
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
Definition endpoint.hpp:197
void init_logging(lib::shared_ptr< alog_type > a, lib::shared_ptr< elog_type > e)
Initialize logging.
Definition endpoint.hpp:171
endpoint type
Type of this endpoint transport component.
Definition endpoint.hpp:49
void register_ostream(std::ostream *o)
Register a default output stream.
Definition endpoint.hpp:80
config::concurrency_type concurrency_type
Type of this endpoint's concurrency policy.
Definition endpoint.hpp:54
void set_secure(bool value)
Set whether or not endpoint can create secure connections.
Definition endpoint.hpp:102
config::alog_type alog_type
Type of this endpoint's access logging policy.
Definition endpoint.hpp:58
iostream transport error category
Definition base.hpp:85
std::string get_query() const
Return the query portion.
Definition uri.hpp:732
bool is_ipv6_literal() const
Definition uri.hpp:651
#define _WEBSOCKETPP_NOEXCEPT_TOKEN_
Definition cpp11.hpp:115
#define __has_extension
Definition cpp11.hpp:40
#define __has_feature(x)
Definition cpp11.hpp:37
Concurrency handling support.
Definition basic.hpp:34
Library level error codes.
Definition error.hpp:44
@ general
Catch-all library error.
Definition error.hpp:47
@ unrequested_subprotocol
Selected subprotocol was not requested by the client.
Definition error.hpp:102
@ invalid_port
Invalid port in URI.
Definition error.hpp:120
@ client_only
Attempted to use a client specific feature on a server endpoint.
Definition error.hpp:105
@ http_connection_ended
HTTP connection ended.
Definition error.hpp:111
@ operation_canceled
The requested operation was canceled.
Definition error.hpp:127
@ no_outgoing_buffers
The endpoint is out of outgoing message buffers.
Definition error.hpp:68
@ http_parse_error
HTTP parse error.
Definition error.hpp:143
@ reserved_close_code
Close code is in a reserved range.
Definition error.hpp:80
@ con_creation_failed
Connection creation attempted failed.
Definition error.hpp:99
@ no_incoming_buffers
The endpoint is out of incoming message buffers.
Definition error.hpp:71
@ invalid_state
The connection was in the wrong state for this operation.
Definition error.hpp:74
@ extension_neg_failed
Extension negotiation failed.
Definition error.hpp:146
@ rejected
Connection rejected.
Definition error.hpp:130
@ unsupported_version
Unsupported WebSocket protocol version.
Definition error.hpp:140
@ invalid_utf8
Invalid UTF-8.
Definition error.hpp:86
@ invalid_close_code
Close code is invalid.
Definition error.hpp:83
@ server_only
Attempted to use a server specific feature on a client endpoint.
Definition error.hpp:108
@ endpoint_not_secure
Attempted to open a secure connection with an insecure endpoint.
Definition error.hpp:57
@ close_handshake_timeout
WebSocket close handshake timed out.
Definition error.hpp:117
@ invalid_subprotocol
Invalid subprotocol.
Definition error.hpp:89
@ bad_close_code
Unable to parse close code.
Definition error.hpp:77
@ open_handshake_timeout
WebSocket opening handshake timed out.
Definition error.hpp:114
@ invalid_version
Invalid WebSocket protocol version.
Definition error.hpp:137
@ transport_error
General transport error, consult more specific transport error code.
Definition error.hpp:149
@ send_queue_full
send attempted when endpoint write queue was full
Definition error.hpp:50
@ test
Unit testing utility error code.
Definition error.hpp:96
@ invalid_uri
An invalid uri was supplied.
Definition error.hpp:65
Implementation of RFC 7692, the permessage-deflate WebSocket extension.
Definition disabled.hpp:44
HTTP handling support.
Definition request.hpp:37
Stub RNG policy that always returns 0.
Definition none.hpp:35
Random number generation policies.
Transport policy that uses asio.
Definition endpoint.hpp:46
Generic transport related errors.
@ pass_through
underlying transport pass through
@ operation_not_supported
Operation not supported.
@ operation_aborted
Operation aborted.
@ invalid_num_bytes
async_read_at_least call requested more bytes than buffer can store
@ action_after_shutdown
read or write after shutdown
@ tls_short_read
TLS short read.
@ double_read
async_read called while another async_read was in progress
iostream transport errors
Definition base.hpp:64
@ invalid_num_bytes
async_read_at_least call requested more bytes than buffer can store
Definition base.hpp:71
@ double_read
async_read called while another async_read was in progress
Definition base.hpp:74
lib::error_code make_error_code(error::value e)
Get an error code with the given value and the iostream transport category.
Definition base.hpp:118
lib::error_category const & get_category()
Get a reference to a static copy of the iostream transport error category.
Definition base.hpp:112
Transport policy that uses STL iostream for I/O and does not support timers.
Definition endpoint.hpp:43
lib::function< lib::error_code(connection_hdl, std::vector< transport::buffer > const &bufs)> vector_write_handler
Definition base.hpp:57
lib::function< lib::error_code(connection_hdl)> shutdown_handler
Definition base.hpp:61
lib::function< lib::error_code(connection_hdl, char const *, size_t)> write_handler
The type and signature of the callback used by iostream transport to write.
Definition base.hpp:48
Transport policies provide network connectivity and timers.
Definition endpoint.hpp:45
lib::function< void(lib::error_code const &, size_t)> read_handler
The type and signature of the callback passed to the read method.
lib::function< void()> dispatch_handler
The type and signature of the callback passed to the dispatch method.
lib::function< void()> interrupt_handler
The type and signature of the callback passed to the interrupt method.
lib::function< void(lib::error_code const &)> accept_handler
The type and signature of the callback passed to the accept method.
Definition endpoint.hpp:80
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
lib::function< void(lib::error_code const &)> connect_handler
The type and signature of the callback passed to the connect method.
Definition endpoint.hpp:83
lib::function< void(lib::error_code const &)> write_handler
The type and signature of the callback passed to the write method.
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
lib::function< void(lib::error_code const &)> shutdown_handler
The type and signature of the callback passed to the shutdown method.
A group of helper methods for parsing and validating URIs against RFC 3986.
Definition uri.hpp:52
bool digit(char c)
RFC3986 digit character test.
Definition uri.hpp:184
bool gen_delim(char c)
RFC3986 generic delimiter character test.
Definition uri.hpp:78
bool digit(std::string::const_iterator it)
RFC3986 digit character test (iterator version).
Definition uri.hpp:195
bool ipv4_literal(std::string::const_iterator start, std::string::const_iterator end)
Tests a range for a valid IPv4 literal.
Definition uri.hpp:250
bool sub_delim(char c)
RFC3986 subcomponent delimiter character test.
Definition uri.hpp:100
bool reg_name(std::string::const_iterator start, std::string::const_iterator end)
Tests a range for validity for a registry name.
Definition uri.hpp:373
bool pct_encoded(std::string::const_iterator it)
RFC3986 per cent encoded character test.
Definition uri.hpp:211
bool scheme(char c)
RFC3986 scheme character test.
Definition uri.hpp:165
bool unreserved(char c)
RFC3986 unreserved character test.
Definition uri.hpp:61
bool reg_name(char c)
Tests a character for validity for a registry name.
Definition uri.hpp:361
bool hex4(std::string::const_iterator start, std::string::const_iterator end)
Tests a range for a valid IPv6 hex quad.
Definition uri.hpp:279
bool dec_octet(std::string::const_iterator start, std::string::const_iterator end)
Tests a range for a valid IPv4 decimal octet.
Definition uri.hpp:223
bool ipv6_literal(std::string::const_iterator start, std::string::const_iterator end)
Tests a range for a valid IPv6 literal.
Definition uri.hpp:299
bool hexdigit(char c)
RFC3986 hex digit character test.
Definition uri.hpp:128
Namespace for the WebSocket++ project.
static uint16_t const uri_default_secure_port
Default port for wss://.
Definition uri.hpp:47
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
static uint16_t const uri_default_port
Default port for ws://.
Definition uri.hpp:45
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition uri.hpp:791
std::pair< lib::error_code, std::string > err_str_pair
Combination error code / string type for returning two values.
Definition error.hpp:41
Server config with asio transport and TLS disabled.
static const long timeout_socket_shutdown
Length of time to wait for socket shutdown.
Definition core.hpp:137
static const long timeout_connect
Length of time to wait for TCP connect.
Definition core.hpp:134
static const long timeout_dns_resolve
Length of time to wait for dns resolution.
Definition core.hpp:131
static const long timeout_proxy
Length of time to wait before a proxy handshake is aborted.
Definition core.hpp:121
static const long timeout_socket_pre_init
Default timer values (in ms).
Definition core.hpp:118
static const long timeout_socket_post_init
Length of time to wait for socket post-initialization.
Definition core.hpp:128
Server config with iostream transport.
Definition core.hpp:68
websocketpp::random::none::int_generator< uint32_t > rng_type
RNG policies.
Definition core.hpp:93
static const websocketpp::log::level elog_level
Default static error logging channels.
Definition core.hpp:176
websocketpp::transport::iostream::endpoint< transport_config > transport_type
Transport Endpoint Component.
Definition core.hpp:142
static const size_t max_http_body_size
Default maximum http body size.
Definition core.hpp:252
static const long timeout_open_handshake
Default timer values (in ms).
Definition core.hpp:152
static const size_t max_message_size
Default maximum message size.
Definition core.hpp:240
static const bool drop_on_protocol_error
Drop connections immediately on protocol error.
Definition core.hpp:213
static const long timeout_close_handshake
Length of time before a closing handshake is aborted.
Definition core.hpp:154
static const websocketpp::log::level alog_level
Default static access logging channels.
Definition core.hpp:189
websocketpp::log::basic< concurrency_type, websocketpp::log::elevel > elog_type
Logging policies.
Definition core.hpp:88
static const long timeout_pong
Length of time to wait for a pong after a ping.
Definition core.hpp:156
static const bool silent_close
Suppresses the return of detailed connection close information.
Definition core.hpp:228
static bool const enable_multithreading
Definition core.hpp:98
static const size_t connection_read_buffer_size
Size of the per-connection read buffer.
Definition core.hpp:204
static const bool enable_extensions
Global flag for enabling/disabling extensions.
Definition core.hpp:255
static const int client_version
WebSocket Protocol version to use as a client.
Definition core.hpp:164
Package of log levels for logging access events.
Definition levels.hpp:112
static level const devel
Development messages (warning: very chatty).
Definition levels.hpp:141
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
Package of log levels for logging errors.
Definition levels.hpp:59
static level const devel
Low level debugging information (warning: very chatty).
Definition levels.hpp:63
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:80
A simple utility buffer class.
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_