2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
28#ifndef WEBSOCKETPP_TRANSPORT_ASIO_CON_HPP
29#define WEBSOCKETPP_TRANSPORT_ASIO_CON_HPP
31#include <websocketpp/transport/asio/base.hpp>
33#include <websocketpp/transport/base/connection.hpp>
35#include <websocketpp/logger/levels.hpp>
36#include <websocketpp/http/constants.hpp>
38#include <websocketpp/base64/base64.hpp>
39#include <websocketpp/error.hpp>
40#include <websocketpp/uri.hpp>
42#include <websocketpp/common/asio.hpp>
43#include <websocketpp/common/chrono.hpp>
44#include <websocketpp/common/cpp11.hpp>
45#include <websocketpp/common/memory.hpp>
46#include <websocketpp/common/functional.hpp>
47#include <websocketpp/common/connection_hdl.hpp>
62
63
64
65
66template <
typename config>
67class connection :
public config::socket_type::socket_con_type {
83 typedef typename config::request_type request_type;
84 typedef typename request_type::ptr request_ptr;
85 typedef typename config::response_type response_type;
86 typedef typename response_type::ptr response_ptr;
91 typedef lib::shared_ptr<lib::asio::io_context::strand>
strand_ptr;
93 typedef lib::shared_ptr<lib::asio::steady_timer>
timer_ptr;
101 explicit connection(
bool is_server,
const lib::shared_ptr<
alog_type> & alog,
const lib::shared_ptr<
elog_type> & elog)
102 : m_is_server(is_server)
114 bool is_secure()
const {
120
121
122
123
124
125
126
127
128
129
136
137
138
139
140
141
142
143
145 m_tcp_pre_init_handler = h;
150
151
152
153
154
155
156
157
164
165
166
167
168
169
170
171
172
174 m_tcp_post_init_handler = h;
179
180
181
182
183
184
185
186
187
188
189 void set_proxy(std::string
const & uri, lib::error_code & ec) {
193 m_proxy_data = lib::make_shared<proxy_data>();
194 ec = lib::error_code();
197#ifndef _WEBSOCKETPP_NO_EXCEPTIONS_
199 void set_proxy(std::string
const & uri) {
208
209
210
211
212
213
214
215
216
217
218
219 void set_proxy_basic_auth(std::string
const & username, std::string
const &
220 password, lib::error_code & ec)
228 std::string val =
"Basic "+base64_encode(username +
":" + password);
229 m_proxy_data->req.replace_header(
"Proxy-Authorization",val);
230 ec = lib::error_code();
233#ifndef _WEBSOCKETPP_NO_EXCEPTIONS_
235 void set_proxy_basic_auth(std::string
const & username, std::string
const &
239 set_proxy_basic_auth(username,password,ec);
246
247
248
249
250
251
252
253
260 m_proxy_data->timeout_proxy = duration;
261 ec = lib::error_code();
264#ifndef _WEBSOCKETPP_NO_EXCEPTIONS_
268 set_proxy_timeout(duration,ec);
273 std::string
const & get_proxy()
const {
279
280
281
282
283
284
285
286
302 return m_connection_hdl;
307
308
309
310
311
312
313
314
315
316
317
318
321 new lib::asio::steady_timer(
323 lib::asio::milliseconds(duration))
326 if (config::enable_multithreading) {
327 new_timer->async_wait(lib::asio::bind_executor(*m_strand, lib::bind(
331 lib::placeholders::_1
334 new_timer->async_wait(lib::bind(
338 lib::placeholders::_1
347
348
349
350
351
352
353
354
355
357 lib::asio::error_code
const & ec)
360 if (ec == lib::asio::error::operation_aborted) {
367 callback(lib::error_code());
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
427 &
type::handle_pre_init,
430 lib::placeholders::_1
437
438
439
440
441
442
443 lib::error_code proxy_init(std::string
const & authority) {
448 m_proxy_data->req.set_version(
"HTTP/1.1");
449 m_proxy_data->req.set_method(
"CONNECT");
451 m_proxy_data->req.set_uri(authority);
452 m_proxy_data->req.replace_header(
"Host",authority);
454 return lib::error_code();
459
460
461
462
463
464
465
466
468 m_io_context = io_context;
470 if (config::enable_multithreading) {
471 m_strand.reset(
new lib::asio::io_context::strand(*io_context));
480 void handle_pre_init(
init_handler callback, lib::error_code
const & ec) {
485 if (m_tcp_pre_init_handler) {
486 m_tcp_pre_init_handler(m_connection_hdl);
495 if (!m_proxy.empty()) {
496 proxy_write(callback);
509 if (config::timeout_socket_post_init > 0) {
510 post_timer = set_timer(
511 config::timeout_socket_post_init,
513 &type::handle_post_init_timeout,
517 lib::placeholders::_1
528 lib::placeholders::_1
535
536
537
538
539
540
541
543 lib::error_code
const & ec)
545 lib::error_code ret_ec;
550 "asio post init timer cancelled");
564 m_alog->write(log
::alevel::devel,
"Asio transport post-init timed out");
571
572
573
574
575
576
577
579 lib::error_code
const & ec)
582 (post_timer && lib::asio::is_neg(post_timer->expiry() - timer_ptr::element_type::clock_type::now())))
589 post_timer->cancel();
596 if (m_tcp_post_init_handler) {
597 m_tcp_post_init_handler(m_connection_hdl);
610 "assertion failed: !m_proxy_data in asio::connection::proxy_write");
615 m_proxy_data->write_buf = m_proxy_data->req.raw();
617 m_bufs.push_back(lib::asio::buffer(m_proxy_data->write_buf.data(),
618 m_proxy_data->write_buf.size()));
623 m_proxy_data->timer =
this->set_timer(
624 m_proxy_data->timeout_proxy,
626 &
type::handle_proxy_timeout,
629 lib::placeholders::_1
634 if (config::enable_multithreading) {
635 lib::asio::async_write(
638 lib::asio::bind_executor(*m_strand, lib::bind(
641 lib::placeholders::_1
645 lib::asio::async_write(
651 lib::placeholders::_1
657 void handle_proxy_timeout(
init_handler callback, lib::error_code
const & ec)
661 "asio handle_proxy_write timer cancelled");
668 "asio handle_proxy_write timer expired");
675 lib::asio::error_code
const & ec)
679 "asio connection handle_proxy_write");
687 if (ec == lib::asio::error::operation_aborted ||
688 lib::asio::is_neg(m_proxy_data->timer->expiry() - timer_ptr::element_type::clock_type::now()))
696 m_proxy_data->timer->cancel();
701 proxy_read(callback);
711 "assertion failed: !m_proxy_data in asio::connection::proxy_read");
716 if (config::enable_multithreading) {
717 lib::asio::async_read_until(
719 m_proxy_data->read_buf,
721 lib::asio::bind_executor(*m_strand, lib::bind(
724 lib::placeholders::_1, lib::placeholders::_2
728 lib::asio::async_read_until(
730 m_proxy_data->read_buf,
735 lib::placeholders::_1, lib::placeholders::_2
743
744
745
746
748 lib::asio::error_code
const & ec,
size_t)
752 "asio connection handle_proxy_read");
758 if (ec == lib::asio::error::operation_aborted ||
759 lib::asio::is_neg(m_proxy_data->timer->expiry() - timer_ptr::element_type::clock_type::now()))
766 m_proxy_data->timer->cancel();
770 "asio handle_proxy_read error: "+ec.message());
775 "assertion failed: !m_proxy_data in asio::connection::handle_proxy_read");
781 std::istream input(&m_proxy_data->read_buf);
783 lib::error_code istream_ec;
784 m_proxy_data->res.consume(input, istream_ec);
788 "An HTTP handling error occurred while reading a response from the proxy server: "+istream_ec.message());
790 callback(istream_ec);
794 if (!m_proxy_data->res.headers_ready()) {
808 s <<
"Proxy connection error: "
809 << m_proxy_data->res.get_status_code()
811 << m_proxy_data->res.get_status_msg()
827 m_proxy_data.reset();
840 s <<
"asio async_read_at_least: " << num_bytes;
847
848
849
850
851
852
854 if (config::enable_multithreading) {
855 lib::asio::async_read(
857 lib::asio::buffer(buf,len),
858 lib::asio::transfer_at_least(num_bytes),
859 lib::asio::bind_executor(*m_strand, make_custom_alloc_handler(
860 m_read_handler_allocator,
864 lib::placeholders::_1, lib::placeholders::_2
869 lib::asio::async_read(
871 lib::asio::buffer(buf,len),
872 lib::asio::transfer_at_least(num_bytes),
873 make_custom_alloc_handler(
874 m_read_handler_allocator,
878 lib::placeholders::_1, lib::placeholders::_2
886 void handle_async_read(
read_handler handler, lib::asio::error_code
const & ec,
887 size_t bytes_transferred)
893 if (ec == lib::asio::error::eof) {
911 handler(tec,bytes_transferred);
916 "handle_async_read called with null read handler");
922 m_bufs.push_back(lib::asio::buffer(buf,len));
924 if (config::enable_multithreading) {
925 lib::asio::async_write(
928 lib::asio::bind_executor(*m_strand, make_custom_alloc_handler(
929 m_write_handler_allocator,
933 lib::placeholders::_1, lib::placeholders::_2
938 lib::asio::async_write(
941 make_custom_alloc_handler(
942 m_write_handler_allocator,
946 lib::placeholders::_1, lib::placeholders::_2
955 std::vector<buffer>::const_iterator it;
960 for (it = bufs.begin(); it != bufs.end(); ++it) {
961 m_bufs.push_back(lib::asio::buffer((*it).buf,(*it).len));
964 if (config::enable_multithreading) {
965 lib::asio::async_write(
968 lib::asio::bind_executor(*m_strand, make_custom_alloc_handler(
969 m_write_handler_allocator,
973 lib::placeholders::_1, lib::placeholders::_2
978 lib::asio::async_write(
981 make_custom_alloc_handler(
982 m_write_handler_allocator,
986 lib::placeholders::_1, lib::placeholders::_2
995
996
997
1000 lib::error_code tec;
1011 "handle_async_write called with null write handler");
1017
1018
1019
1020
1021
1023 m_connection_hdl = hdl;
1029
1030
1032 if (config::enable_multithreading) {
1033 lib::asio::post(m_io_context->get_executor(), lib::asio::bind_executor(*m_strand, handler));
1035 lib::asio::post(m_io_context->get_executor(), handler);
1037 return lib::error_code();
1041 if (config::enable_multithreading) {
1042 lib::asio::post(m_io_context->get_executor(), lib::asio::bind_executor(*m_strand, handler));
1044 lib::asio::post(m_io_context->get_executor(), handler);
1046 return lib::error_code();
1050
1051
1060 shutdown_timer = set_timer(
1061 config::timeout_socket_shutdown,
1063 &type::handle_async_shutdown_timeout,
1067 lib::placeholders::_1
1073 &
type::handle_async_shutdown,
1077 lib::placeholders::_1
1084
1085
1086
1087
1089 lib::error_code
const & ec)
1091 lib::error_code ret_ec;
1096 "asio socket shutdown timer cancelled");
1107 "Asio transport socket shutdown timed out");
1113 callback, lib::asio::error_code
const & ec)
1115 if (ec == lib::asio::error::operation_aborted ||
1116 lib::asio::is_neg(shutdown_timer->expiry() - timer_ptr::element_type::clock_type::now()))
1122 shutdown_timer->cancel();
1124 lib::error_code tec;
1126 if (ec == lib::asio::error::not_connected) {
1145 "asio con handle_async_shutdown");
1155 if (cec == lib::asio::error::operation_not_supported) {
1166 template <
typename error_type>
1167 void log_err(log::level l,
const char * msg,
const error_type & ec) {
1168 std::stringstream s;
1169 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1170 m_elog->write(l,s.str());
1174 const bool m_is_server;
1179 proxy_data() : timeout_proxy(config::timeout_proxy) {}
1183 std::string write_buf;
1184 lib::asio::streambuf read_buf;
1189 std::string m_proxy;
1190 lib::shared_ptr<proxy_data> m_proxy_data;
1197 std::vector<lib::asio::const_buffer> m_bufs;
1200 lib::asio::error_code m_tec;
1203 tcp_init_handler m_tcp_pre_init_handler;
1204 tcp_init_handler m_tcp_post_init_handler;
#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.
Stub for user supplied base class.
Stub for user supplied base class.
Stub class for use when disabling permessage_deflate extension.
bool is_implemented() const
err_str_pair negotiate(http::attribute_list const &)
Negotiate extension.
std::string generate_offer() const
Generate extension offer.
lib::error_code init(bool)
Initialize state.
HTTP parser error category.
An exception type specific to HTTP errors.
header_list const & get_headers() const
Return a list of all HTTP headers.
std::string const & get_body() const
Get HTTP body.
bool body_ready() const
Check if the parser is done parsing the body.
void set_max_body_size(size_t value)
Set body size limit.
bool prepare_body(lib::error_code &ec)
Prepare the parser to begin parsing body data.
std::string raw_headers() const
Generate and return the HTTP headers as a string.
size_t process_body(char const *buf, size_t len, lib::error_code &ec)
Process body data.
std::string const & get_version() const
Get the HTTP version string.
size_t get_max_body_size() const
Get body size limit.
lib::error_code process_header(std::string::iterator begin, std::string::iterator end)
Process a header line.
Stores, parses, and manipulates HTTP requests.
std::string raw() const
Returns the full raw request (including the body).
std::string const & get_uri() const
Return the requested URI.
std::string const & get_method() const
Return the request method.
bool ready() const
Returns whether or not the request is ready for reading.
size_t consume(char const *buf, size_t len, lib::error_code &ec)
Process bytes in the input buffer.
std::string raw_head() const
Returns the raw request headers only (similar to an HTTP HEAD request).
Stores, parses, and manipulates HTTP responses.
std::string raw() const
Returns the full raw response.
size_t consume(char const *buf, size_t len, lib::error_code &ec)
Process bytes in the input buffer.
bool headers_ready() const
Returns true if the response headers are fully parsed.
size_t consume(std::istream &s, lib::error_code &ec)
Process bytes in the input buffer (istream version).
lib::error_code set_status(status_code::value code)
Set response status code and message.
bool ready() const
Returns true if the response is ready.
const std::string & get_status_msg() const
Return the response status message.
status_code::value get_status_code() const
Return the response status code.
Basic logger that outputs to an ostream.
void write(level channel, char const *msg)
Write a cstring message to the given channel.
basic(basic< concurrency, names > const &other)
Copy constructor.
bool recycle(message *)
Recycle a message.
message_ptr get_message(frame::opcode::value op, size_t size)
Get a message buffer with specified size and opcode.
message_ptr get_message()
Get an empty message buffer.
con_msg_man_ptr get_manager() const
Get a pointer to a connection message manager.
Represents a buffer for a single WebSocket message.
message(const con_msg_man_ptr manager, frame::opcode::value op, size_t size=128)
Construct a message and fill in some values.
std::string & get_raw_payload()
Get a non-const reference to the payload string.
bool recycle()
Recycle the message.
bool get_compressed() const
Return whether or not the message is flagged as compressed.
bool get_terminal() const
Get whether or not the message is terminal.
std::string const & get_header() const
Return the prepared frame header.
void set_payload(void const *payload, size_t len)
Set payload data.
bool get_fin() const
Read the fin bit.
void append_payload(void const *payload, size_t len)
Append payload data.
void set_opcode(frame::opcode::value op)
Set the opcode.
void set_prepared(bool value)
Set or clear the flag that indicates that the message has been prepared.
frame::opcode::value get_opcode() const
Return the message opcode.
void set_terminal(bool value)
Set the terminal flag.
bool get_prepared() const
Return whether or not the message has been prepared for sending.
void set_compressed(bool value)
Set or clear the compression flag.
message(const con_msg_man_ptr manager)
Construct an empty message.
void set_fin(bool value)
Set the fin bit.
std::string const & get_payload() const
Get a reference to the payload string.
Thread safe stub "random" integer generator.
int_type operator()()
advances the engine's state and returns the generated value
Server endpoint role based on the given config.
Basic ASIO endpoint socket component.
Asio based connection transport component.
void set_proxy_timeout(long duration)
Set the proxy timeout duration (exception).
void set_tcp_post_init_handler(tcp_init_handler h)
Sets the tcp post init handler.
void set_proxy_timeout(long duration, lib::error_code &ec)
Set the proxy timeout duration (exception free).
config::elog_type elog_type
Type of this transport's error logging policy.
strand_ptr get_strand()
Get a pointer to this connection's strand.
void async_read_at_least(size_t num_bytes, char *buf, size_t len, read_handler handler)
read at least num_bytes bytes into buf and then call handler.
socket_con_type::ptr socket_con_ptr
Type of a shared pointer to the socket connection component.
lib::error_code interrupt(interrupt_handler handler)
Trigger the on_interrupt handler.
lib::shared_ptr< lib::asio::io_context::strand > strand_ptr
Type of a pointer to the Asio io_context::strand being used.
config::alog_type alog_type
Type of this transport's access logging policy.
lib::asio::io_context * io_context_ptr
Type of a pointer to the Asio io_context being used.
void handle_async_write(write_handler handler, lib::asio::error_code const &ec, size_t)
Async write callback.
void handle_timer(timer_ptr, timer_handler callback, lib::asio::error_code const &ec)
Timer callback.
void handle_post_init(timer_ptr post_timer, init_handler callback, lib::error_code const &ec)
Post init timeout callback.
lib::error_code init_asio(io_context_ptr io_context)
Finish constructing the transport.
void async_shutdown(shutdown_handler callback)
close and clean up the underlying socket
lib::asio::error_code get_transport_ec() const
Get the internal transport error code for a closed/failed connection.
config::socket_type::socket_con_type socket_con_type
Type of the socket connection component.
void handle_post_init_timeout(timer_ptr, init_handler callback, lib::error_code const &ec)
Post init timeout callback.
connection< config > type
Type of this connection transport component.
void handle_async_shutdown_timeout(timer_ptr, init_handler callback, lib::error_code const &ec)
Async shutdown timeout handler.
lib::shared_ptr< lib::asio::steady_timer > timer_ptr
Type of a pointer to the Asio timer class.
void async_write(const char *buf, size_t len, write_handler handler)
Initiate a potentially asyncronous write of the given buffer.
void async_write(std::vector< buffer > const &bufs, write_handler handler)
Initiate a potentially asyncronous write of the given buffers.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
timer_ptr set_timer(long duration, timer_handler callback)
Call back a function after a period of time.
void set_tcp_init_handler(tcp_init_handler h)
Sets the tcp pre init handler (deprecated).
void handle_proxy_read(init_handler callback, lib::asio::error_code const &ec, size_t)
Proxy read callback.
void set_uri(uri_ptr u)
Set uri hook.
std::string get_remote_endpoint() const
Get the remote endpoint address.
ptr get_shared()
Get a shared pointer to this component.
void cancel_socket_checked()
Cancel the underlying socket and log any errors.
void set_handle(connection_hdl hdl)
Set Connection Handle.
void set_tcp_pre_init_handler(tcp_init_handler h)
Sets the tcp pre init handler.
void init(init_handler callback)
Initialize transport for reading.
connection_hdl get_handle() const
Get the connection handle.
Asio based endpoint transport component.
lib::shared_ptr< lib::asio::executor_work_guard< lib::asio::io_context::executor_type > > work_guard_ptr
Type of a shared pointer to an io_context work object.
std::size_t run_one()
wraps the run_one method of the internal io_context object
socket_type::socket_con_type socket_con_type
Type of the socket connection component.
void stop_listening(lib::error_code &ec)
Stop listening (exception free).
config::socket_type socket_type
Type of the socket policy.
lib::shared_ptr< lib::asio::steady_timer > timer_ptr
Type of timer handle.
void async_connect(transport_con_ptr tcon, uri_ptr u, connect_handler cb)
Initiate a new connection.
void init_asio()
Initialize asio transport with internal io_context.
config::elog_type elog_type
Type of the error logging policy.
void init_logging(const lib::shared_ptr< alog_type > &a, const lib::shared_ptr< elog_type > &e)
Initialize logging.
lib::asio::io_context * io_context_ptr
Type of a pointer to the ASIO io_context being used.
bool is_secure() const
Return whether or not the endpoint produces secure connections.
lib::asio::ip::tcp::endpoint get_local_endpoint(lib::asio::error_code &ec)
Get local TCP endpoint.
void set_reuse_addr(bool value)
Sets whether to use the SO_REUSEADDR flag when opening listening sockets.
void set_tcp_init_handler(tcp_init_handler h)
Sets the tcp pre init handler (deprecated).
void handle_timer(timer_ptr, timer_handler callback, lib::asio::error_code const &ec)
Timer handler.
void start_perpetual()
Marks the endpoint as perpetual, stopping it from exiting when empty.
void stop()
wraps the stop method of the internal io_context object
std::size_t run()
wraps the run method of the internal io_context object
void set_tcp_post_init_handler(tcp_init_handler h)
Sets the tcp post init handler.
void set_listen_backlog(int backlog)
Sets the maximum length of the queue of pending connections.
bool stopped() const
wraps the stopped method of the internal io_context object
timer_ptr set_timer(long duration, timer_handler callback)
Call back a function after a period of time.
socket_con_type::ptr socket_con_ptr
Type of a shared pointer to the socket connection component.
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
asio::connection< config > transport_con_type
void init_asio(lib::error_code &ec)
Initialize asio transport with internal io_context (exception free).
void async_accept(transport_con_ptr tcon, accept_handler callback)
Accept the next connection attempt and assign it to con.
void listen(uint16_t port)
Set up endpoint for listening on a port.
endpoint< config > type
Type of this endpoint transport component.
void init_asio(io_context_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_context (exception free)
config::concurrency_type concurrency_type
Type of the concurrency policy.
std::size_t poll()
wraps the poll method of the internal io_context object
void stop_perpetual()
Clears the endpoint's perpetual flag, allowing it to exit when empty.
void init_asio(io_context_ptr ptr)
initialize asio transport with external io_context
lib::shared_ptr< lib::asio::ip::tcp::acceptor > acceptor_ptr
Type of a shared pointer to the acceptor being used.
void listen(lib::asio::ip::tcp::endpoint const &ep)
Set up endpoint for listening manually.
lib::asio::io_context & get_io_context()
Retrieve a reference to the endpoint's io_context.
void handle_resolve_timeout(timer_ptr, connect_handler callback, lib::error_code const &ec)
DNS resolution timeout handler.
void listen(lib::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free).
transport_con_type::ptr transport_con_ptr
void reset()
wraps the restart method of the internal io_context object
void set_tcp_pre_bind_handler(tcp_pre_bind_handler h)
Sets the tcp pre bind handler.
config::alog_type alog_type
Type of the access logging policy.
void listen(InternetProtocol const &internet_protocol, uint16_t port)
Set up endpoint for listening with protocol and port.
void set_tcp_pre_init_handler(tcp_init_handler h)
Sets the tcp pre init handler.
void handle_connect_timeout(transport_con_ptr tcon, timer_ptr, connect_handler callback, lib::error_code const &ec)
Asio connect timeout handler.
void async_accept(transport_con_ptr tcon, accept_handler callback, lib::error_code &ec)
Accept the next connection attempt and assign it to con (exception free).
std::size_t poll_one()
wraps the poll_one method of the internal io_context object
void listen(InternetProtocol const &internet_protocol, uint16_t port, lib::error_code &ec)
Set up endpoint for listening with protocol and port (exception free).
void listen(uint16_t port, lib::error_code &ec)
Set up endpoint for listening on a port (exception free).
bool is_listening() const
Check if the endpoint is listening.
void stop_listening()
Stop listening.
lib::shared_ptr< lib::asio::ip::tcp::resolver > resolver_ptr
Type of a shared pointer to the resolver being used.
lib::function< lib::error_code(acceptor_ptr)> tcp_pre_bind_handler
Type of socket pre-bind handler.
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 set_uri(uri_ptr)
Set uri hook.
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
config::elog_type elog_type
Type of this endpoint's error logging policy.
void set_write_handler(write_handler h)
Sets the write handler.
void set_shutdown_handler(shutdown_handler h)
Sets the shutdown handler.
bool is_secure() const
Tests whether or not the underlying transport is secure.
lib::shared_ptr< type > ptr
Type of a pointer to this endpoint transport component.
transport_con_type::ptr transport_con_ptr
void async_connect(transport_con_ptr, uri_ptr, connect_handler cb)
Initiate a new connection.
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
void init_logging(lib::shared_ptr< alog_type > a, lib::shared_ptr< elog_type > e)
Initialize logging.
endpoint type
Type of this endpoint transport component.
void register_ostream(std::ostream *o)
Register a default output stream.
config::concurrency_type concurrency_type
Type of this endpoint's concurrency policy.
void set_secure(bool value)
Set whether or not endpoint can create secure connections.
config::alog_type alog_type
Type of this endpoint's access logging policy.
iostream transport error category
std::string get_query() const
Return the query portion.
bool is_ipv6_literal() const
#define _WEBSOCKETPP_CONSTEXPR_TOKEN_
#define _WEBSOCKETPP_NOEXCEPT_TOKEN_
Concurrency handling support.
Library level error codes.
@ general
Catch-all library error.
@ unrequested_subprotocol
Selected subprotocol was not requested by the client.
@ invalid_port
Invalid port in URI.
@ client_only
Attempted to use a client specific feature on a server endpoint.
@ http_connection_ended
HTTP connection ended.
@ async_accept_not_listening
@ operation_canceled
The requested operation was canceled.
@ no_outgoing_buffers
The endpoint is out of outgoing message buffers.
@ http_parse_error
HTTP parse error.
@ reserved_close_code
Close code is in a reserved range.
@ con_creation_failed
Connection creation attempted failed.
@ no_incoming_buffers
The endpoint is out of incoming message buffers.
@ invalid_state
The connection was in the wrong state for this operation.
@ extension_neg_failed
Extension negotiation failed.
@ rejected
Connection rejected.
@ unsupported_version
Unsupported WebSocket protocol version.
@ invalid_utf8
Invalid UTF-8.
@ invalid_close_code
Close code is invalid.
@ server_only
Attempted to use a server specific feature on a client endpoint.
@ endpoint_not_secure
Attempted to open a secure connection with an insecure endpoint.
@ close_handshake_timeout
WebSocket close handshake timed out.
@ invalid_subprotocol
Invalid subprotocol.
@ bad_close_code
Unable to parse close code.
@ open_handshake_timeout
WebSocket opening handshake timed out.
@ invalid_version
Invalid WebSocket protocol version.
@ transport_error
General transport error, consult more specific transport error code.
@ send_queue_full
send attempted when endpoint write queue was full
@ test
Unit testing utility error code.
@ invalid_uri
An invalid uri was supplied.
Implementation of RFC 7692, the permessage-deflate WebSocket extension.
Constants related to frame and payload limits.
static uint8_t const close_reason_size
Maximum size of close frame reason.
static uint64_t const payload_size_jumbo
Maximum size of a jumbo WebSocket payload (basic payload = 127).
static unsigned int const max_extended_header_length
Maximum length of the variable portion of the WebSocket header.
static unsigned int const max_header_length
Maximum length of a WebSocket header.
static uint16_t const payload_size_extended
Maximum size of an extended WebSocket payload (basic payload = 126).
static uint8_t const payload_size_basic
Maximum size of a basic WebSocket payload.
static unsigned int const basic_header_length
Minimum length of a WebSocket frame header.
Constants and utility functions related to WebSocket opcodes.
bool invalid(value v)
Check if an opcode is invalid.
bool reserved(value v)
Check if an opcode is reserved.
bool is_control(value v)
Check if an opcode is for a control frame.
Data structures and utility functions for manipulating WebSocket frames.
unsigned int get_masking_key_offset(basic_header const &)
Calculate the offset location of the masking key within the extended header.
void set_rsv2(basic_header &h, bool value)
Set the frame's RSV2 bit.
static unsigned int const MAX_HEADER_LENGTH
Maximum length of a WebSocket header.
opcode::value get_opcode(basic_header const &h)
Extract opcode from basic header.
void set_rsv3(basic_header &h, bool value)
Set the frame's RSV3 bit.
uint64_t get_payload_size(basic_header const &, extended_header const &)
Extract the full payload size field from a WebSocket header.
uint8_t get_basic_size(basic_header const &)
Extracts the raw payload length specified in the basic header.
size_t byte_mask_circ(uint8_t *input, uint8_t *output, size_t length, size_t prepared_key)
Circular byte aligned mask/unmask.
void byte_mask(input_iter b, input_iter e, output_iter o, masking_key_type const &key, size_t key_offset=0)
Byte by byte mask/unmask.
static unsigned int const MAX_EXTENDED_HEADER_LENGTH
Maximum length of the variable portion of the WebSocket header.
bool get_rsv3(basic_header const &h)
check whether the frame's RSV3 bit is set
bool get_masked(basic_header const &h)
check whether the frame is masked
bool get_rsv2(basic_header const &h)
check whether the frame's RSV2 bit is set
void byte_mask(iter_type b, iter_type e, masking_key_type const &key, size_t key_offset=0)
Byte by byte mask/unmask (in place).
uint16_t get_extended_size(extended_header const &)
Extract the extended size field from an extended header.
size_t byte_mask_circ(uint8_t *data, size_t length, size_t prepared_key)
Circular byte aligned mask/unmask (in place).
bool get_fin(basic_header const &h)
Check whether the frame's FIN bit is set.
size_t circshift_prepared_key(size_t prepared_key, size_t offset)
circularly shifts the supplied prepared masking key by offset bytes
bool get_rsv1(basic_header const &h)
check whether the frame's RSV1 bit is set
void set_masked(basic_header &h, bool value)
Set the frame's MASK bit.
size_t word_mask_circ(uint8_t *input, uint8_t *output, size_t length, size_t prepared_key)
Circular word aligned mask/unmask.
void word_mask_exact(uint8_t *data, size_t length, masking_key_type const &key)
Exact word aligned mask/unmask (in place).
void set_rsv1(basic_header &h, bool value)
Set the frame's RSV1 bit.
size_t get_header_len(basic_header const &)
Calculates the full length of the header based on the first bytes.
void set_fin(basic_header &h, bool value)
Set the frame's FIN bit.
uint64_t get_jumbo_size(extended_header const &)
Extract the jumbo size field from an extended header.
void word_mask_exact(uint8_t *input, uint8_t *output, size_t length, masking_key_type const &key)
Exact word aligned mask/unmask.
std::string prepare_header(const basic_header &h, const extended_header &e)
Generate a properly sized contiguous string that encodes a full frame header.
masking_key_type get_masking_key(basic_header const &, extended_header const &)
Extract the masking key from a frame header.
static unsigned int const BASIC_HEADER_LENGTH
Minimum length of a WebSocket frame header.
size_t word_mask_circ(uint8_t *data, size_t length, size_t prepared_key)
Circular word aligned mask/unmask (in place).
size_t prepare_masking_key(masking_key_type const &key)
Extract a masking key into a value the size of a machine word.
status_code::value get_status_code(error::value value)
Get the HTTP status code associated with the error.
lib::error_category const & get_category()
Get a reference to a static copy of the asio transport error category.
@ unknown_transfer_encoding
The transfer encoding is unknown.
@ istream_bad
An istream read command returned with the bad flag set.
@ invalid_format
The specified data contains illegal characters for the context.
@ body_too_large
The body value is larger than the configured maximum size.
@ incomplete_request
The request was missing some required values.
@ unsupported_transfer_encoding
The transfer encoding is not supported.
@ incomplete_status_line
The response status line was missing some required values.
@ missing_header_separator
A header line was missing a separator.
@ invalid_header_name
The header name specified contains illegal characters.
@ request_header_fields_too_large
The request headers are larger than the configured maximum size.
lib::error_code make_error_code(error::value e)
Create an error code with the given value and the asio transport category.
std::string get_string(value code)
Given a status code value, return the default status message.
value
Known values for HTTP Status codes.
size_t const max_body_size
Default Maximum size in bytes for HTTP message bodies.
static char const header_separator[]
Literal value of the HTTP header separator.
std::vector< std::pair< std::string, attribute_list > > parameter_list
The type of an HTTP parameter list.
size_t const istream_buffer
Number of bytes to use for temporary istream read buffers.
bool is_not_token_char(unsigned char c)
Is the character a non-token.
size_t const max_header_size
Maximum size in bytes before rejecting an HTTP header as too big.
static char const header_delimiter[]
Literal value of the HTTP header delimiter.
bool is_whitespace_char(unsigned char c)
Is the character whitespace.
static char const header_token[]
invalid HTTP token characters
bool is_not_whitespace_char(unsigned char c)
Is the character non-whitespace.
std::map< std::string, std::string > attribute_list
The type of an HTTP attribute list.
bool is_token_char(unsigned char c)
Is the character a token.
static std::string const empty_header
Literal value of an empty header.
Stub RNG policy that always returns 0.
Random number generation policies.
lib::error_code make_error_code(error::value e)
Create an error code with the given value and the asio transport category.
@ proxy_invalid
Invalid Proxy URI.
@ pass_through
there was an error in the underlying transport library
@ proxy_failed
The connection to the requested proxy server failed.
@ invalid_host_service
Invalid host or service.
Transport policy that uses asio.
Generic transport related errors.
@ pass_through
underlying transport pass through
@ operation_not_supported
Operation not supported.
@ operation_aborted
Operation aborted.
@ tls_error
Other TLS error.
@ 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
@ invalid_num_bytes
async_read_at_least call requested more bytes than buffer can store
@ double_read
async_read called while another async_read was in progress
lib::error_code make_error_code(error::value e)
Get an error code with the given value and the iostream transport category.
lib::error_category const & get_category()
Get a reference to a static copy of the iostream transport error category.
Transport policy that uses STL iostream for I/O and does not support timers.
lib::function< lib::error_code(connection_hdl, std::vector< transport::buffer > const &bufs)> vector_write_handler
lib::function< lib::error_code(connection_hdl)> shutdown_handler
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.
Transport policies provide network connectivity and timers.
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.
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.
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.
bool digit(char c)
RFC3986 digit character test.
bool gen_delim(char c)
RFC3986 generic delimiter character test.
bool digit(std::string::const_iterator it)
RFC3986 digit character test (iterator version).
bool ipv4_literal(std::string::const_iterator start, std::string::const_iterator end)
Tests a range for a valid IPv4 literal.
bool sub_delim(char c)
RFC3986 subcomponent delimiter character test.
bool reg_name(std::string::const_iterator start, std::string::const_iterator end)
Tests a range for validity for a registry name.
bool pct_encoded(std::string::const_iterator it)
RFC3986 per cent encoded character test.
bool scheme(char c)
RFC3986 scheme character test.
bool unreserved(char c)
RFC3986 unreserved character test.
bool reg_name(char c)
Tests a character for validity for a registry name.
bool hex4(std::string::const_iterator start, std::string::const_iterator end)
Tests a range for a valid IPv6 hex quad.
bool dec_octet(std::string::const_iterator start, std::string::const_iterator end)
Tests a range for a valid IPv4 decimal octet.
bool ipv6_literal(std::string::const_iterator start, std::string::const_iterator end)
Tests a range for a valid IPv6 literal.
bool hexdigit(char c)
RFC3986 hex digit character test.
Generic non-websocket specific utility functions and data structures.
std::string to_hex(uint8_t const *input, size_t length)
Convert byte array (uint8_t) to ascii printed string of hex digits.
T::const_iterator ci_find_substr(T const &haystack, T const &needle, std::locale const &loc=std::locale())
Find substring (case insensitive).
T::const_iterator ci_find_substr(T const &haystack, typename T::value_type const *needle, typename T::size_type size, std::locale const &loc=std::locale())
Find substring (case insensitive).
std::string to_hex(char const *input, size_t length)
Convert char array to ascii printed string of hex digits.
Namespace for the WebSocket++ project.
static uint16_t const uri_default_secure_port
Default port for wss://.
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
static uint16_t const uri_default_port
Default port for ws://.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
std::pair< lib::error_code, std::string > err_str_pair
Combination error code / string type for returning two values.
Server config with asio transport and TLS disabled.
Extension specific settings:
static const uint8_t minimum_outgoing_window_bits
static const bool allow_disabling_context_takeover
static const long timeout_socket_shutdown
Length of time to wait for socket shutdown.
static const long timeout_connect
Length of time to wait for TCP connect.
static const long timeout_dns_resolve
Length of time to wait for dns resolution.
static const long timeout_proxy
Length of time to wait before a proxy handshake is aborted.
static const long timeout_socket_pre_init
Default timer values (in ms).
static bool const enable_multithreading
static const long timeout_socket_post_init
Length of time to wait for socket post-initialization.
Server config with iostream transport.
websocketpp::random::none::int_generator< uint32_t > rng_type
RNG policies.
static const websocketpp::log::level elog_level
Default static error logging channels.
websocketpp::transport::iostream::endpoint< transport_config > transport_type
Transport Endpoint Component.
static const size_t max_http_body_size
Default maximum http body size.
static const long timeout_open_handshake
Default timer values (in ms).
static const size_t max_message_size
Default maximum message size.
static const bool drop_on_protocol_error
Drop connections immediately on protocol error.
static const long timeout_close_handshake
Length of time before a closing handshake is aborted.
static const websocketpp::log::level alog_level
Default static access logging channels.
websocketpp::log::basic< concurrency_type, websocketpp::log::elevel > elog_type
Logging policies.
static const long timeout_pong
Length of time to wait for a pong after a ping.
static const bool silent_close
Suppresses the return of detailed connection close information.
static bool const enable_multithreading
static const size_t connection_read_buffer_size
Size of the per-connection read buffer.
static const bool enable_extensions
Global flag for enabling/disabling extensions.
static const int client_version
WebSocket Protocol version to use as a client.
Package of log levels for logging access events.
static char const * channel_name(level channel)
Get the textual name of a channel given a channel id.
static level const fail
One line for each failed WebSocket connection with details.
static level const none
Special aggregate value representing "no levels".
static level const debug_handshake
Extra information about opening handshakes.
static level const devel
Development messages (warning: very chatty).
static level const all
Special aggregate value representing "all levels".
static level const debug_close
Extra information about closing handshakes.
static level const frame_payload
One line per frame, includes the full message payload (warning: chatty).
static level const connect
Information about new connections.
static level const app
Special channel for application specific logs. Not used by the library.
static level const frame_header
One line per frame, includes the full frame header.
static level const message_payload
Reserved.
static level const endpoint
Reserved.
static level const message_header
Reserved.
static level const control
One line per control frame.
static level const disconnect
One line for each closed connection. Includes closing codes and reasons.
static level const access_core
static level const http
Access related to HTTP requests.
Package of values for hinting at the nature of a given logger.
static value const none
No information.
static value const access
Access log.
static value const error
Error log.
uint32_t value
Type of a channel type hint value.
Package of log levels for logging errors.
static level const devel
Low level debugging information (warning: very chatty).
static char const * channel_name(level channel)
Get the textual name of a channel given a channel id.
static level const library
static level const all
Special aggregate value representing "all levels".
static level const none
Special aggregate value representing "no levels".
static level const rerror
A simple utility buffer class.
Helper less than functor for case insensitive find.
Helper functor for case insensitive find.
bool operator()(charT ch1, charT ch2)
Perform a case insensitive comparison.
my_equal(std::locale const &loc)
Construct the functor with the given locale.
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
Two byte conversion union.
Four byte conversion union.
Eight byte conversion union.