Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
websocketpp::connection< config > Class Template Reference

Represents an individual WebSocket connection. More...

#include <connection.hpp>

Inheritance diagram for websocketpp::connection< config >:
Collaboration diagram for websocketpp::connection< config >:

Public Types

typedef connection< configtype
 Type of this connection.
 
typedef lib::shared_ptr< typeptr
 Type of a shared pointer to this connection.
 
typedef lib::weak_ptr< typeweak_ptr
 Type of a weak pointer to this connection.
 
typedef config::concurrency_type concurrency_type
 Type of the concurrency component of this connection.
 
typedef config::alog_type alog_type
 Type of the access logging policy.
 
typedef config::elog_type elog_type
 Type of the error logging policy.
 
typedef config::transport_type::transport_con_type transport_con_type
 Type of the transport component of this connection.
 
typedef transport_con_type::ptr transport_con_ptr
 Type of a shared pointer to the transport component of this connection.
 
typedef lib::function< void(ptr)> termination_handler
 
typedef concurrency_type::scoped_lock_type scoped_lock_type
 
typedef concurrency_type::mutex_type mutex_type
 
typedef config::request_type request_type
 
typedef config::response_type response_type
 
typedef config::message_type message_type
 
typedef message_type::ptr message_ptr
 
typedef config::con_msg_manager_type con_msg_manager_type
 
typedef con_msg_manager_type::ptr con_msg_manager_ptr
 
typedef config::rng_type rng_type
 Type of RNG.
 
typedef processor::processor< configprocessor_type
 
typedef lib::shared_ptr< processor_typeprocessor_ptr
 
typedef lib::function< void(connection_hdl, message_ptr)> message_handler
 
typedef transport_con_type::timer_ptr timer_ptr
 Type of a pointer to a transport timer handle.
 
typedef session::internal_state::value istate_type
 

Public Member Functions

 connection (bool p_is_server, std::string const &ua, alog_type &alog, elog_type &elog, rng_type &rng)
 
ptr get_shared ()
 Get a shared pointer to this component.
 
void set_open_handler (open_handler h)
 Set open handler.
 
void set_close_handler (close_handler h)
 Set close handler.
 
void set_fail_handler (fail_handler h)
 Set fail handler.
 
void set_ping_handler (ping_handler h)
 Set ping handler.
 
void set_pong_handler (pong_handler h)
 Set pong handler.
 
void set_pong_timeout_handler (pong_timeout_handler h)
 Set pong timeout handler.
 
void set_interrupt_handler (interrupt_handler h)
 Set interrupt handler.
 
void set_http_handler (http_handler h)
 Set http handler.
 
void set_validate_handler (validate_handler h)
 Set validate handler.
 
void set_message_handler (message_handler h)
 Set message handler.
 
void set_open_handshake_timeout (long dur)
 Set open handshake timeout.
 
void set_close_handshake_timeout (long dur)
 Set close handshake timeout.
 
void set_pong_timeout (long dur)
 Set pong timeout.
 
size_t get_max_message_size () const
 Get maximum message size.
 
void set_max_message_size (size_t new_value)
 Set maximum message size.
 
size_t get_max_http_body_size () const
 Get maximum HTTP message body size.
 
void set_max_http_body_size (size_t new_value)
 Set maximum HTTP message body size.
 
size_t get_buffered_amount () const
 Get the size of the outgoing write buffer (in payload bytes)
 
size_t buffered_amount () const
 Get the size of the outgoing write buffer (in payload bytes)
 
lib::error_code send (std::string const &payload, frame::opcode::value op=frame::opcode::text)
 Create a message and then add it to the outgoing send queue.
 
lib::error_code send (void const *payload, size_t len, frame::opcode::value op=frame::opcode::binary)
 Send a message (raw array overload)
 
lib::error_code send (message_ptr msg)
 Add a message to the outgoing send queue.
 
lib::error_code interrupt ()
 Asyncronously invoke handler::on_inturrupt.
 
void handle_interrupt ()
 Transport inturrupt callback.
 
lib::error_code pause_reading ()
 Pause reading of new data.
 
void handle_pause_reading ()
 Pause reading callback.
 
lib::error_code resume_reading ()
 Resume reading of new data.
 
void handle_resume_reading ()
 Resume reading callback.
 
void ping (std::string const &payload)
 Send a ping.
 
void ping (std::string const &payload, lib::error_code &ec)
 exception free variant of ping
 
void handle_pong_timeout (std::string payload, lib::error_code const &ec)
 Utility method that gets called back when the ping timer expires.
 
void pong (std::string const &payload)
 Send a pong.
 
void pong (std::string const &payload, lib::error_code &ec)
 exception free variant of pong
 
void close (close::status::value const code, std::string const &reason)
 Close the connection.
 
void close (close::status::value const code, std::string const &reason, lib::error_code &ec)
 exception free variant of close
 
bool get_secure () const
 Returns the secure flag from the connection URI.
 
std::string const & get_host () const
 Returns the host component of the connection URI.
 
std::string const & get_resource () const
 Returns the resource component of the connection URI.
 
uint16_t get_port () const
 Returns the port component of the connection URI.
 
uri_ptr get_uri () const
 Gets the connection URI.
 
void set_uri (uri_ptr uri)
 Sets the connection URI.
 
std::string const & get_subprotocol () const
 Gets the negotated subprotocol.
 
std::vector< std::string > const & get_requested_subprotocols () const
 Gets all of the subprotocols requested by the client.
 
void add_subprotocol (std::string const &request, lib::error_code &ec)
 Adds the given subprotocol string to the request list (exception free)
 
void add_subprotocol (std::string const &request)
 Adds the given subprotocol string to the request list.
 
void select_subprotocol (std::string const &value, lib::error_code &ec)
 Select a subprotocol to use (exception free)
 
void select_subprotocol (std::string const &value)
 Select a subprotocol to use.
 
std::string const & get_request_header (std::string const &key) const
 Retrieve a request header.
 
std::string const & get_request_body () const
 Retrieve a request body.
 
std::string const & get_response_header (std::string const &key) const
 Retrieve a response header.
 
http::status_code::value get_response_code () const
 Get response HTTP status code.
 
std::string const & get_response_msg () const
 Get response HTTP status message.
 
void set_status (http::status_code::value code)
 Set response status code and message.
 
void set_status (http::status_code::value code, std::string const &msg)
 Set response status code and message.
 
void set_body (std::string const &value)
 Set response body content.
 
void set_body (std::string &&value)
 
void append_header (std::string const &key, std::string const &val)
 Append a header.
 
void replace_header (std::string const &key, std::string const &val)
 Replace a header.
 
void remove_header (std::string const &key)
 Remove a header.
 
request_type const & get_request () const
 Get request object.
 
response_type const & get_response () const
 Get response object.
 
lib::error_code defer_http_response ()
 Defer HTTP Response until later (Exception free)
 
void send_http_response (lib::error_code &ec)
 Send deferred HTTP Response (exception free)
 
void send_http_response ()
 Send deferred HTTP Response.
 
connection_hdl get_handle () const
 Get Connection Handle.
 
bool is_server () const
 Get whether or not this connection is part of a server or client.
 
std::string const & get_origin () const
 Return the same origin policy origin value from the opening request.
 
session::state::value get_state () const
 Return the connection state.
 
close::status::value get_local_close_code () const
 Get the WebSocket close code sent by this endpoint.
 
std::string const & get_local_close_reason () const
 Get the WebSocket close reason sent by this endpoint.
 
close::status::value get_remote_close_code () const
 Get the WebSocket close code sent by the remote endpoint.
 
std::string const & get_remote_close_reason () const
 Get the WebSocket close reason sent by the remote endpoint.
 
lib::error_code get_ec () const
 Get the internal error code for a closed/failed connection.
 
message_ptr get_message (websocketpp::frame::opcode::value op, size_t size) const
 Get a message buffer.
 
void read_handshake (size_t num_bytes)
 
void handle_read_handshake (lib::error_code const &ec, size_t bytes_transferred)
 
void handle_read_http_response (lib::error_code const &ec, size_t bytes_transferred)
 
void handle_write_http_response (lib::error_code const &ec)
 
void handle_send_http_request (lib::error_code const &ec)
 
void handle_open_handshake_timeout (lib::error_code const &ec)
 
void handle_close_handshake_timeout (lib::error_code const &ec)
 
void handle_read_frame (lib::error_code const &ec, size_t bytes_transferred)
 
void read_frame ()
 Issue a new transport read unless reading is paused.
 
std::vector< int > const & get_supported_versions () const
 Get array of WebSocket protocol versions that this connection supports.
 
void set_termination_handler (termination_handler new_handler)
 
void terminate (lib::error_code const &ec)
 
void handle_terminate (terminate_status tstat, lib::error_code const &ec)
 
void write_frame ()
 Checks if there are frames in the send queue and if there are sends one.
 
void handle_write_frame (lib::error_code const &ec)
 Process the results of a frame write operation and start the next write.
 
void start ()
 Start the connection state machine.
 
void set_handle (connection_hdl hdl)
 Set Connection Handle.
 

Protected Member Functions

void handle_transport_init (lib::error_code const &ec)
 
lib::error_code initialize_processor ()
 
lib::error_code process_handshake_request ()
 

Detailed Description

template<typename config>
class websocketpp::connection< config >

Definition at line 235 of file connection.hpp.

Member Typedef Documentation

◆ alog_type

template<typename config >
config::alog_type websocketpp::connection< config >::alog_type

Definition at line 250 of file connection.hpp.

◆ con_msg_manager_ptr

template<typename config >
con_msg_manager_type::ptr websocketpp::connection< config >::con_msg_manager_ptr

Definition at line 272 of file connection.hpp.

◆ con_msg_manager_type

template<typename config >
config::con_msg_manager_type websocketpp::connection< config >::con_msg_manager_type

Definition at line 271 of file connection.hpp.

◆ concurrency_type

template<typename config >
config::concurrency_type websocketpp::connection< config >::concurrency_type

Definition at line 248 of file connection.hpp.

◆ elog_type

template<typename config >
config::elog_type websocketpp::connection< config >::elog_type

Definition at line 252 of file connection.hpp.

◆ istate_type

Definition at line 287 of file connection.hpp.

◆ message_handler

template<typename config >
lib::function<void(connection_hdl,message_ptr)> websocketpp::connection< config >::message_handler

Definition at line 281 of file connection.hpp.

◆ message_ptr

template<typename config >
message_type::ptr websocketpp::connection< config >::message_ptr

Definition at line 269 of file connection.hpp.

◆ message_type

template<typename config >
config::message_type websocketpp::connection< config >::message_type

Definition at line 268 of file connection.hpp.

◆ mutex_type

template<typename config >
concurrency_type::mutex_type websocketpp::connection< config >::mutex_type

Definition at line 263 of file connection.hpp.

◆ processor_ptr

template<typename config >
lib::shared_ptr<processor_type> websocketpp::connection< config >::processor_ptr

Definition at line 278 of file connection.hpp.

◆ processor_type

template<typename config >
processor::processor<config> websocketpp::connection< config >::processor_type

Definition at line 277 of file connection.hpp.

◆ ptr

template<typename config >
lib::shared_ptr<type> websocketpp::connection< config >::ptr

Definition at line 243 of file connection.hpp.

◆ request_type

template<typename config >
config::request_type websocketpp::connection< config >::request_type

Definition at line 265 of file connection.hpp.

◆ response_type

template<typename config >
config::response_type websocketpp::connection< config >::response_type

Definition at line 266 of file connection.hpp.

◆ rng_type

template<typename config >
config::rng_type websocketpp::connection< config >::rng_type

Definition at line 275 of file connection.hpp.

◆ scoped_lock_type

template<typename config >
concurrency_type::scoped_lock_type websocketpp::connection< config >::scoped_lock_type

Definition at line 262 of file connection.hpp.

◆ termination_handler

template<typename config >
lib::function<void(ptr)> websocketpp::connection< config >::termination_handler

Definition at line 260 of file connection.hpp.

◆ timer_ptr

template<typename config >
transport_con_type::timer_ptr websocketpp::connection< config >::timer_ptr

Definition at line 284 of file connection.hpp.

◆ transport_con_ptr

template<typename config >
transport_con_type::ptr websocketpp::connection< config >::transport_con_ptr

Definition at line 258 of file connection.hpp.

◆ transport_con_type

template<typename config >
config::transport_type::transport_con_type websocketpp::connection< config >::transport_con_type

Definition at line 256 of file connection.hpp.

◆ type

template<typename config >
connection<config> websocketpp::connection< config >::type

Definition at line 241 of file connection.hpp.

◆ weak_ptr

template<typename config >
lib::weak_ptr<type> websocketpp::connection< config >::weak_ptr

Definition at line 245 of file connection.hpp.

Constructor & Destructor Documentation

◆ connection()

template<typename config >
websocketpp::connection< config >::connection ( bool p_is_server,
std::string const & ua,
alog_type & alog,
elog_type & elog,
rng_type & rng )
inlineexplicit

Definition at line 297 of file connection.hpp.

299 : transport_con_type(p_is_server, alog, elog)
300 , m_handle_read_frame(lib::bind(
302 this,
303 lib::placeholders::_1,
304 lib::placeholders::_2
305 ))
306 , m_write_frame_handler(lib::bind(
308 this,
309 lib::placeholders::_1
310 ))
311 , m_user_agent(ua)
312 , m_open_handshake_timeout_dur(config::timeout_open_handshake)
313 , m_close_handshake_timeout_dur(config::timeout_close_handshake)
314 , m_pong_timeout_dur(config::timeout_pong)
315 , m_max_message_size(config::max_message_size)
317 , m_internal_state(session::internal_state::USER_INIT)
318 , m_msg_manager(new con_msg_manager_type())
319 , m_send_buffer_size(0)
320 , m_write_flag(false)
321 , m_read_flag(true)
322 , m_is_server(p_is_server)
323 , m_alog(alog)
324 , m_elog(elog)
325 , m_rng(rng)
326 , m_local_close_code(close::status::abnormal_close)
327 , m_remote_close_code(close::status::abnormal_close)
328 , m_is_http(false)
329 , m_http_state(session::http_state::init)
330 , m_was_clean(false)
331 {
332 m_alog.write(log::alevel::devel,"connection constructor");
333 }
config::con_msg_manager_type con_msg_manager_type
void handle_write_frame(lib::error_code const &ec)
Process the results of a frame write operation and start the next write.
void handle_read_frame(lib::error_code const &ec, size_t bytes_transferred)
config::transport_type::transport_con_type transport_con_type
Type of the transport component of this connection.
#define elog(FORMAT,...)
Definition logger.hpp:130
static level const devel
Development messages (warning: very chatty)
Definition levels.hpp:141
Here is the call graph for this function:

Member Function Documentation

◆ add_subprotocol() [1/2]

template<typename config >
void websocketpp::connection< config >::add_subprotocol ( std::string const & request)

Adds a subprotocol to the list to send with the opening handshake. This may be called multiple times to request more than one. If the server supports one of these, it may choose one. If so, it will return it in it's handshake reponse and the value will be available via get_subprotocol(). Subprotocol requests should be added in order of preference.

Parameters
requestThe subprotocol to request

Definition at line 478 of file connection_impl.hpp.

478 {
479 lib::error_code ec;
480 this->add_subprotocol(value,ec);
481 if (ec) {
482 throw exception(ec);
483 }
484}
void add_subprotocol(std::string const &request, lib::error_code &ec)
Adds the given subprotocol string to the request list (exception free)
#define value
Definition pkcs11.h:157

◆ add_subprotocol() [2/2]

template<typename config >
void websocketpp::connection< config >::add_subprotocol ( std::string const & request,
lib::error_code & ec )

Adds a subprotocol to the list to send with the opening handshake. This may be called multiple times to request more than one. If the server supports one of these, it may choose one. If so, it will return it in it's handshake reponse and the value will be available via get_subprotocol(). Subprotocol requests should be added in order of preference.

Parameters
requestThe subprotocol to request
ecA reference to an error code that will be filled in the case of errors

Definition at line 458 of file connection_impl.hpp.

460{
461 if (m_is_server) {
463 return;
464 }
465
466 // If the value is empty or has a non-RFC2616 token character it is invalid.
467 if (value.empty() || std::find_if(value.begin(),value.end(),
468 http::is_not_token_char) != value.end())
469 {
471 return;
472 }
473
474 m_requested_subprotocols.push_back(value);
475}
@ client_only
Attempted to use a client specific feature on a server endpoint.
Definition error.hpp:105
@ invalid_subprotocol
Invalid subprotocol.
Definition error.hpp:89
lib::error_code make_error_code(error::value e)
Definition error.hpp:235
Here is the call graph for this function:

◆ append_header()

template<typename config >
void websocketpp::connection< config >::append_header ( std::string const & key,
std::string const & val )

If a header with this name already exists the value will be appended to the existing header to form a comma separated list of values. Use connection::replace_header to overwrite existing values.

This member function is valid only from the http() and validate() handler callbacks, or to a client connection before connect has been called.

Parameters
keyName of the header to set
valValue to add
See also
replace_header
websocketpp::http::parser::append_header

Definition at line 590 of file connection_impl.hpp.

592{
593 if (m_is_server) {
594 if (m_internal_state == istate::PROCESS_HTTP_REQUEST) {
595 // we are setting response headers for an incoming server connection
596 m_response.append_header(key,val);
597 } else {
598 throw exception("Call to append_header from invalid state",
600 }
601 } else {
602 if (m_internal_state == istate::USER_INIT) {
603 // we are setting initial headers for an outgoing client connection
604 m_request.append_header(key,val);
605 } else {
606 throw exception("Call to append_header from invalid state",
608 }
609 }
610}
@ invalid_state
The connection was in the wrong state for this operation.
Definition error.hpp:74
Here is the call graph for this function:

◆ buffered_amount()

template<typename config >
size_t websocketpp::connection< config >::buffered_amount ( ) const
inline
Deprecated
use get_buffered_amount instead

Definition at line 635 of file connection.hpp.

635 {
636 return get_buffered_amount();
637 }
size_t get_buffered_amount() const
Get the size of the outgoing write buffer (in payload bytes)
Here is the call graph for this function:

◆ close() [1/2]

template<typename config >
void websocketpp::connection< config >::close ( close::status::value const code,
std::string const & reason )

Initiates the close handshake process.

If close returns successfully the connection will be in the closing state and no additional messages may be sent. All messages sent prior to calling close will be written out before the connection is closed.

If no reason is specified none will be sent. If no code is specified then no code will be sent.

The handler's on_close callback will be called once the close handshake is complete.

Reasons will be automatically truncated to the maximum length (123 bytes) if necessary.

Parameters
codeThe close code to send
reasonThe close reason to send

Definition at line 326 of file connection_impl.hpp.

328{
329 lib::error_code ec;
330 close(code,reason,ec);
331 if (ec) {
332 throw exception(ec);
333 }
334}
void close(close::status::value const code, std::string const &reason)
Close the connection.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ close() [2/2]

template<typename config >
void websocketpp::connection< config >::close ( close::status::value const code,
std::string const & reason,
lib::error_code & ec )

Definition at line 304 of file connection_impl.hpp.

306{
307 if (m_alog.static_test(log::alevel::devel)) {
308 m_alog.write(log::alevel::devel,"connection close");
309 }
310
311 // Truncate reason to maximum size allowable in a close frame.
312 std::string tr(reason,0,std::min<size_t>(reason.size(),
313 frame::limits::close_reason_size));
314
315 scoped_lock_type lock(m_connection_state_lock);
316
317 if (m_state != session::state::open) {
319 return;
320 }
321
322 ec = this->send_close_frame(code,tr,false,close::status::terminal(code));
323}
concurrency_type::scoped_lock_type scoped_lock_type
bool terminal(value code)
Determine if the code represents an unrecoverable error.
Definition close.hpp:212
void lock()
Here is the call graph for this function:

◆ defer_http_response()

template<typename config >
lib::error_code websocketpp::connection< config >::defer_http_response ( )

Defer HTTP Response until later.

Used in the http handler to defer the HTTP response for this connection until later. Handshake timers will be canceled and the connection will be left open until send_http_response or an equivalent is called.

Warning: deferred connections won't time out and as a result can tie up resources.

Since
0.6.0
Returns
A status code, zero on success, non-zero otherwise

Used in the http handler to defer the HTTP response for this connection until later. Handshake timers will be canceled and the connection will be left open until send_http_response or an equivalent is called.

Warning: deferred connections won't time out and as a result can tie up resources.

Returns
A status code, zero on success, non-zero otherwise

Definition at line 671 of file connection_impl.hpp.

671 {
672 // Cancel handshake timer, otherwise the connection will time out and we'll
673 // close the connection before the app has a chance to send a response.
674 if (m_handshake_timer) {
675 m_handshake_timer->cancel();
676 m_handshake_timer.reset();
677 }
678
679 // Do something to signal deferral
680 m_http_state = session::http_state::deferred;
681
682 return lib::error_code();
683}

◆ get_buffered_amount()

template<typename config >
size_t websocketpp::connection< config >::get_buffered_amount ( ) const

Retrieves the number of bytes in the outgoing write buffer that have not already been dispatched to the transport layer. This represents the bytes that are presently cancelable without uncleanly ending the websocket connection

This method invokes the m_write_lock mutex

Returns
The current number of bytes in the outgoing send buffer.

Definition at line 71 of file connection_impl.hpp.

71 {
72 //scoped_lock_type lock(m_connection_state_lock);
73 return m_send_buffer_size;
74}
Here is the caller graph for this function:

◆ get_ec()

template<typename config >
lib::error_code websocketpp::connection< config >::get_ec ( ) const
inline

Retrieves a machine readable detailed error code indicating the reason that the connection was closed or failed. Valid only after the close or fail handler is called.

Returns
Error code indicating the reason the connection was closed or failed

Definition at line 1255 of file connection.hpp.

1255 {
1256 return m_ec;
1257 }

◆ get_handle()

template<typename config >
connection_hdl websocketpp::connection< config >::get_handle ( ) const
inline

The connection handle is a token that can be shared outside the WebSocket++ core for the purposes of identifying a connection and sending it messages.

Returns
A handle to the connection

Definition at line 1184 of file connection.hpp.

1184 {
1185 return m_connection_hdl;
1186 }

◆ get_host()

template<typename config >
std::string const & websocketpp::connection< config >::get_host ( ) const

This value is available after the HTTP request has been fully read and may be called from any thread.

Returns
The host component of the connection URI

Definition at line 412 of file connection_impl.hpp.

412 {
413 //scoped_lock_type lock(m_connection_state_lock);
414 return m_uri->get_host();
415}

◆ get_local_close_code()

template<typename config >
close::status::value websocketpp::connection< config >::get_local_close_code ( ) const
inline
Returns
The WebSocket close code sent by this endpoint.

Definition at line 1218 of file connection.hpp.

1218 {
1219 return m_local_close_code;
1220 }

◆ get_local_close_reason()

template<typename config >
std::string const & websocketpp::connection< config >::get_local_close_reason ( ) const
inline
Returns
The WebSocket close reason sent by this endpoint.

Definition at line 1226 of file connection.hpp.

1226 {
1227 return m_local_close_reason;
1228 }

◆ get_max_http_body_size()

template<typename config >
size_t websocketpp::connection< config >::get_max_http_body_size ( ) const
inline

Get maximum HTTP message body size. Maximum message body size determines the point at which the connection will stop reading an HTTP request whose body is too large.

The default is set by the endpoint that creates the connection.

Since
0.5.0
Returns
The maximum HTTP message body size

Definition at line 594 of file connection.hpp.

594 {
595 return m_request.get_max_body_size();
596 }

◆ get_max_message_size()

template<typename config >
size_t websocketpp::connection< config >::get_max_message_size ( ) const
inline

Get maximum message size. Maximum message size determines the point at which the connection will fail with the message_too_big protocol error.

The default is set by the endpoint that creates the connection.

Since
0.3.0

Definition at line 559 of file connection.hpp.

559 {
560 return m_max_message_size;
561 }

◆ get_message()

template<typename config >
message_ptr websocketpp::connection< config >::get_message ( websocketpp::frame::opcode::value op,
size_t size ) const
inline

Warning: The API related to directly sending message buffers may change before the 1.0 release. If you plan to use it, please keep an eye on any breaking changes notifications in future release notes. Also if you have any feedback about usage and capabilities now is a great time to provide it.

Message buffers are used to store message payloads and other message metadata.

The size parameter is a hint only. Your final payload does not need to match it. There may be some performance benefits if the initial size guess is equal to or slightly higher than the final payload size.

Parameters
opThe opcode for the new message
sizeA hint to optimize the initial allocation of payload space.
Returns
A new message buffer

Definition at line 1278 of file connection.hpp.

1280 {
1281 return m_msg_manager->get_message(op, size);
1282 }

◆ get_origin()

template<typename config >
std::string const & websocketpp::connection< config >::get_origin ( ) const

This value is available after the HTTP request has been fully read and may be called from any thread.

Returns
The connection's origin value from the opening handshake.

Definition at line 65 of file connection_impl.hpp.

65 {
66 //scoped_lock_type lock(m_connection_state_lock);
67 return m_processor->get_origin(m_request);
68}

◆ get_port()

template<typename config >
uint16_t websocketpp::connection< config >::get_port ( ) const

This value is available after the HTTP request has been fully read and may be called from any thread.

Returns
The port component of the connection URI

Definition at line 424 of file connection_impl.hpp.

424 {
425 //scoped_lock_type lock(m_connection_state_lock);
426 return m_uri->get_port();
427}

◆ get_remote_close_code()

template<typename config >
close::status::value websocketpp::connection< config >::get_remote_close_code ( ) const
inline
Returns
The WebSocket close code sent by the remote endpoint.

Definition at line 1234 of file connection.hpp.

1234 {
1235 return m_remote_close_code;
1236 }

◆ get_remote_close_reason()

template<typename config >
std::string const & websocketpp::connection< config >::get_remote_close_reason ( ) const
inline
Returns
The WebSocket close reason sent by the remote endpoint.

Definition at line 1242 of file connection.hpp.

1242 {
1243 return m_remote_close_reason;
1244 }

◆ get_request()

template<typename config >
request_type const & websocketpp::connection< config >::get_request ( ) const
inline

Direct access to request object. This can be used to call methods of the request object that are not part of the standard request API that connection wraps.

Note use of this method involves using behavior specific to the configured HTTP policy. Such behavior may not work with alternate HTTP policies.

Since
0.3.0-alpha3
Returns
A const reference to the raw request object

Definition at line 1103 of file connection.hpp.

1103 {
1104 return m_request;
1105 }

◆ get_request_body()

template<typename config >
std::string const & websocketpp::connection< config >::get_request_body ( ) const

Retrieve the value of the request body. This value is typically used with PUT and POST requests to upload files or other data. Only HTTP connections will ever have bodies. WebSocket connection's will always have blank bodies.

Returns
The value of the request body.

Definition at line 533 of file connection_impl.hpp.

533 {
534 return m_request.get_body();
535}

◆ get_request_header()

template<typename config >
std::string const & websocketpp::connection< config >::get_request_header ( std::string const & key) const

Retrieve the value of a header from the handshake HTTP request.

Parameters
keyName of the header to get
Returns
The value of the header

Definition at line 527 of file connection_impl.hpp.

527 {
528 return m_request.get_header(key);
529}

◆ get_requested_subprotocols()

template<typename config >
std::vector< std::string > const & websocketpp::connection< config >::get_requested_subprotocols ( ) const

Retrieves the subprotocols that were requested during the handshake. This method is valid in the validate handler and later.

Returns
A vector of the requested subprotocol

Definition at line 453 of file connection_impl.hpp.

453 {
454 return m_requested_subprotocols;
455}

◆ get_resource()

template<typename config >
std::string const & websocketpp::connection< config >::get_resource ( ) const

This value is available after the HTTP request has been fully read and may be called from any thread.

Returns
The resource component of the connection URI

Definition at line 418 of file connection_impl.hpp.

418 {
419 //scoped_lock_type lock(m_connection_state_lock);
420 return m_uri->get_resource();
421}

◆ get_response()

template<typename config >
response_type const & websocketpp::connection< config >::get_response ( ) const
inline

Direct access to the HTTP response sent or received as a part of the opening handshake. This can be used to call methods of the response object that are not part of the standard request API that connection wraps.

Note use of this method involves using behavior specific to the configured HTTP policy. Such behavior may not work with alternate HTTP policies.

Since
0.7.0
Returns
A const reference to the raw response object

Definition at line 1122 of file connection.hpp.

1122 {
1123 return m_response;
1124 }

◆ get_response_code()

template<typename config >
http::status_code::value websocketpp::connection< config >::get_response_code ( ) const
inline

Gets the response status code

Since
0.7.0
Returns
The response status code sent

Definition at line 983 of file connection.hpp.

983 {
984 return m_response.get_status_code();
985 }

◆ get_response_header()

template<typename config >
std::string const & websocketpp::connection< config >::get_response_header ( std::string const & key) const

Retrieve the value of a header from the handshake HTTP request.

Parameters
keyName of the header to get
Returns
The value of the header

Definition at line 539 of file connection_impl.hpp.

539 {
540 return m_response.get_header(key);
541}

◆ get_response_msg()

template<typename config >
std::string const & websocketpp::connection< config >::get_response_msg ( ) const
inline

Gets the response status message

Since
0.7.0
Returns
The response status message sent

Definition at line 995 of file connection.hpp.

995 {
996 return m_response.get_status_msg();
997 }

◆ get_secure()

template<typename config >
bool websocketpp::connection< config >::get_secure ( ) const

This value is available after the HTTP request has been fully read and may be called from any thread.

Returns
Whether or not the connection URI is flagged secure.

Definition at line 406 of file connection_impl.hpp.

406 {
407 //scoped_lock_type lock(m_connection_state_lock);
408 return m_uri->get_secure();
409}

◆ get_shared()

template<typename config >
ptr websocketpp::connection< config >::get_shared ( )
inline

Definition at line 336 of file connection.hpp.

336 {
337 return lib::static_pointer_cast<type>(transport_con_type::get_shared());
338 }

◆ get_state()

template<typename config >
session::state::value websocketpp::connection< config >::get_state ( ) const

Values can be connecting, open, closing, and closed

Returns
The connection's current state.

Definition at line 77 of file connection_impl.hpp.

77 {
78 //scoped_lock_type lock(m_connection_state_lock);
79 return m_state;
80}

◆ get_subprotocol()

template<typename config >
std::string const & websocketpp::connection< config >::get_subprotocol ( ) const

Retrieves the subprotocol that was negotiated during the handshake. This method is valid in the open handler and later.

Returns
The negotiated subprotocol

Definition at line 447 of file connection_impl.hpp.

447 {
448 return m_subprotocol;
449}

◆ get_supported_versions()

template<typename config >
std::vector< int > const & websocketpp::connection< config >::get_supported_versions ( ) const

Definition at line 1940 of file connection_impl.hpp.

1941{
1942 return versions_supported;
1943}

◆ get_uri()

template<typename config >
uri_ptr websocketpp::connection< config >::get_uri ( ) const

This should really only be called by internal library methods unless you really know what you are doing.

Returns
A pointer to the connection's URI

Definition at line 430 of file connection_impl.hpp.

430 {
431 //scoped_lock_type lock(m_connection_state_lock);
432 return m_uri;
433}

◆ handle_close_handshake_timeout()

template<typename config >
void websocketpp::connection< config >::handle_close_handshake_timeout ( lib::error_code const & ec)

Definition at line 1691 of file connection_impl.hpp.

1693{
1695 m_alog.write(log::alevel::devel,"asio close handshake timer cancelled");
1696 } else if (ec) {
1697 m_alog.write(log::alevel::devel,
1698 "asio open handle_close_handshake_timeout error: "+ec.message());
1699 // TODO: ignore or fail here?
1700 } else {
1701 m_alog.write(log::alevel::devel, "asio close handshake timer expired");
1702 terminate(make_error_code(error::close_handshake_timeout));
1703 }
1704}
@ close_handshake_timeout
WebSocket close handshake timed out.
Definition error.hpp:117
@ operation_aborted
Operation aborted.
bool terminate

◆ handle_interrupt()

template<typename config >
void websocketpp::connection< config >::handle_interrupt ( )

Definition at line 353 of file connection_impl.hpp.

353 {
354 if (m_interrupt_handler) {
355 m_interrupt_handler(m_connection_hdl);
356 }
357}

◆ handle_open_handshake_timeout()

template<typename config >
void websocketpp::connection< config >::handle_open_handshake_timeout ( lib::error_code const & ec)

Definition at line 1675 of file connection_impl.hpp.

1677{
1679 m_alog.write(log::alevel::devel,"open handshake timer cancelled");
1680 } else if (ec) {
1681 m_alog.write(log::alevel::devel,
1682 "open handle_open_handshake_timeout error: "+ec.message());
1683 // TODO: ignore or fail here?
1684 } else {
1685 m_alog.write(log::alevel::devel,"open handshake timer expired");
1686 terminate(make_error_code(error::open_handshake_timeout));
1687 }
1688}
@ open_handshake_timeout
WebSocket opening handshake timed out.
Definition error.hpp:114

◆ handle_pause_reading()

template<typename config >
void websocketpp::connection< config >::handle_pause_reading ( )

Pause reading handler. Not safe to call directly.

Definition at line 372 of file connection_impl.hpp.

372 {
373 m_alog.write(log::alevel::devel,"connection connection::handle_pause_reading");
374 m_read_flag = false;
375}

◆ handle_pong_timeout()

template<typename config >
void websocketpp::connection< config >::handle_pong_timeout ( std::string payload,
lib::error_code const & ec )

Definition at line 233 of file connection_impl.hpp.

235{
236 if (ec) {
238 // ignore, this is expected
239 return;
240 }
241
242 m_elog.write(log::elevel::devel,"pong_timeout error: "+ec.message());
243 return;
244 }
245
246 if (m_pong_timeout_handler) {
247 m_pong_timeout_handler(m_connection_hdl,payload);
248 }
249}
static level const devel
Low level debugging information (warning: very chatty)
Definition levels.hpp:63

◆ handle_read_frame()

template<typename config >
void websocketpp::connection< config >::handle_read_frame ( lib::error_code const & ec,
size_t bytes_transferred )

Definition at line 975 of file connection_impl.hpp.

977{
978 //m_alog.write(log::alevel::devel,"connection handle_read_frame");
979
980 lib::error_code ecm = ec;
981
982 if (!ecm && m_internal_state != istate::PROCESS_CONNECTION) {
984 }
985
986 if (ecm) {
988
989 if (ecm == transport::error::eof) {
990 if (m_state == session::state::closed) {
991 // we expect to get eof if the connection is closed already
992 // just ignore it
993 m_alog.write(log::alevel::devel,"got eof from closed con");
994 return;
995 } else if (m_state == session::state::closing && !m_is_server) {
996 // If we are a client we expect to get eof in the closing state,
997 // this is a signal to terminate our end of the connection after
998 // the closing handshake
999 terminate(lib::error_code());
1000 return;
1001 }
1002 } else if (ecm == error::invalid_state) {
1003 // In general, invalid state errors in the closed state are the
1004 // result of handlers that were in the system already when the state
1005 // changed and should be ignored as they pose no problems and there
1006 // is nothing useful that we can do about them.
1007 if (m_state == session::state::closed) {
1008 m_alog.write(log::alevel::devel,
1009 "handle_read_frame: got invalid istate in closed state");
1010 return;
1011 }
1012 } else if (ecm == transport::error::tls_short_read) {
1013 if (m_state == session::state::closed) {
1014 // We expect to get a TLS short read if we try to read after the
1015 // connection is closed. If this happens ignore and exit the
1016 // read frame path.
1017 terminate(lib::error_code());
1018 return;
1019 }
1020 echannel = log::elevel::rerror;
1021 } else if (ecm == transport::error::action_after_shutdown) {
1022 echannel = log::elevel::info;
1023 }
1024
1025 log_err(echannel, "handle_read_frame", ecm);
1026 this->terminate(ecm);
1027 return;
1028 }
1029
1030 // Boundaries checking. TODO: How much of this should be done?
1031 /*if (bytes_transferred > config::connection_read_buffer_size) {
1032 m_elog.write(log::elevel::fatal,"Fatal boundaries checking error");
1033 this->terminate(make_error_code(error::general));
1034 return;
1035 }*/
1036
1037 size_t p = 0;
1038
1039 if (m_alog.static_test(log::alevel::devel)) {
1040 std::stringstream s;
1041 s << "p = " << p << " bytes transferred = " << bytes_transferred;
1042 m_alog.write(log::alevel::devel,s.str());
1043 }
1044
1045 while (p < bytes_transferred) {
1046 if (m_alog.static_test(log::alevel::devel)) {
1047 std::stringstream s;
1048 s << "calling consume with " << bytes_transferred-p << " bytes";
1049 m_alog.write(log::alevel::devel,s.str());
1050 }
1051
1052 lib::error_code consume_ec;
1053
1054 if (m_alog.static_test(log::alevel::devel)) {
1055 std::stringstream s;
1056 s << "Processing Bytes: " << utility::to_hex(reinterpret_cast<uint8_t*>(m_buf)+p,bytes_transferred-p);
1057 m_alog.write(log::alevel::devel,s.str());
1058 }
1059
1060 p += m_processor->consume(
1061 reinterpret_cast<uint8_t*>(m_buf)+p,
1062 bytes_transferred-p,
1063 consume_ec
1064 );
1065
1066 if (m_alog.static_test(log::alevel::devel)) {
1067 std::stringstream s;
1068 s << "bytes left after consume: " << bytes_transferred-p;
1069 m_alog.write(log::alevel::devel,s.str());
1070 }
1071 if (consume_ec) {
1072 log_err(log::elevel::rerror, "consume", consume_ec);
1073
1074 if (config::drop_on_protocol_error) {
1075 this->terminate(consume_ec);
1076 return;
1077 } else {
1078 lib::error_code close_ec;
1079 this->close(
1080 processor::error::to_ws(consume_ec),
1081 consume_ec.message(),
1082 close_ec
1083 );
1084
1085 if (close_ec) {
1086 log_err(log::elevel::fatal, "Protocol error close frame ", close_ec);
1087 this->terminate(close_ec);
1088 return;
1089 }
1090 }
1091 return;
1092 }
1093
1094 if (m_processor->ready()) {
1095 if (m_alog.static_test(log::alevel::devel)) {
1096 std::stringstream s;
1097 s << "Complete message received. Dispatching";
1098 m_alog.write(log::alevel::devel,s.str());
1099 }
1100
1101 message_ptr msg = m_processor->get_message();
1102
1103 if (!msg) {
1104 m_alog.write(log::alevel::devel, "null message from m_processor");
1105 } else if (!is_control(msg->get_opcode())) {
1106 // data message, dispatch to user
1107 if (m_state != session::state::open) {
1108 m_elog.write(log::elevel::warn, "got non-close frame while closing");
1109 } else if (m_message_handler) {
1110 m_message_handler(m_connection_hdl, msg);
1111 }
1112 } else {
1113 process_control_frame(msg);
1114 }
1115 }
1116 }
1117
1118 read_frame();
1119}
const mie::Vuint & p
Definition bn.cpp:27
void read_frame()
Issue a new transport read unless reading is paused.
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
bool is_control(value v)
Check if an opcode is for a control frame.
Definition frame.hpp:139
uint32_t level
Type of a channel package.
Definition levels.hpp:37
close::status::value to_ws(lib::error_code ec)
Converts a processor error_code into a websocket close code.
Definition base.hpp:261
@ action_after_shutdown
read or write after shutdown
@ tls_short_read
TLS short read.
std::string to_hex(std::string const &input)
Convert std::string to ascii printed string of hex digits.
unsigned char uint8_t
Definition stdint.h:124
static level const info
Definition levels.hpp:69
static level const fatal
Definition levels.hpp:78
static level const rerror
Definition levels.hpp:75
static level const warn
Definition levels.hpp:72
char * s
Here is the call graph for this function:

◆ handle_read_handshake()

template<typename config >
void websocketpp::connection< config >::handle_read_handshake ( lib::error_code const & ec,
size_t bytes_transferred )

Definition at line 814 of file connection_impl.hpp.

816{
817 m_alog.write(log::alevel::devel,"connection handle_read_handshake");
818
819 lib::error_code ecm = ec;
820
821 if (!ecm) {
822 scoped_lock_type lock(m_connection_state_lock);
823
824 if (m_state == session::state::connecting) {
825 if (m_internal_state != istate::READ_HTTP_REQUEST) {
827 }
828 } else if (m_state == session::state::closed) {
829 // The connection was canceled while the response was being sent,
830 // usually by the handshake timer. This is basically expected
831 // (though hopefully rare) and there is nothing we can do so ignore.
832 m_alog.write(log::alevel::devel,
833 "handle_read_handshake invoked after connection was closed");
834 return;
835 } else {
837 }
838 }
839
840 if (ecm) {
841 if (ecm == transport::error::eof && m_state == session::state::closed) {
842 // we expect to get eof if the connection is closed already
843 m_alog.write(log::alevel::devel,
844 "got (expected) eof/state error from closed con");
845 return;
846 }
847
848 log_err(log::elevel::rerror,"handle_read_handshake",ecm);
849 this->terminate(ecm);
850 return;
851 }
852
853 // Boundaries checking. TODO: How much of this should be done?
854 if (bytes_transferred > config::connection_read_buffer_size) {
855 m_elog.write(log::elevel::fatal,"Fatal boundaries checking error.");
856 this->terminate(make_error_code(error::general));
857 return;
858 }
859
860 size_t bytes_processed = 0;
861 try {
862 bytes_processed = m_request.consume(m_buf,bytes_transferred);
863 } catch (http::exception &e) {
864 // All HTTP exceptions will result in this request failing and an error
865 // response being returned. No more bytes will be read in this con.
866 m_response.set_status(e.m_error_code,e.m_error_msg);
867 this->write_http_response_error(error::make_error_code(error::http_parse_error));
868 return;
869 }
870
871 // More paranoid boundaries checking.
872 // TODO: Is this overkill?
873 if (bytes_processed > bytes_transferred) {
874 m_elog.write(log::elevel::fatal,"Fatal boundaries checking error.");
875 this->terminate(make_error_code(error::general));
876 return;
877 }
878
879 if (m_alog.static_test(log::alevel::devel)) {
880 std::stringstream s;
881 s << "bytes_transferred: " << bytes_transferred
882 << " bytes, bytes processed: " << bytes_processed << " bytes";
883 m_alog.write(log::alevel::devel,s.str());
884 }
885
886 if (m_request.ready()) {
887 lib::error_code processor_ec = this->initialize_processor();
888 if (processor_ec) {
889 this->write_http_response_error(processor_ec);
890 return;
891 }
892
893 if (m_processor && m_processor->get_version() == 0) {
894 // Version 00 has an extra requirement to read some bytes after the
895 // handshake
896 if (bytes_transferred-bytes_processed >= 8) {
897 m_request.replace_header(
898 "Sec-WebSocket-Key3",
899 std::string(m_buf+bytes_processed,m_buf+bytes_processed+8)
900 );
901 bytes_processed += 8;
902 } else {
903 // TODO: need more bytes
904 m_alog.write(log::alevel::devel,"short key3 read");
905 m_response.set_status(http::status_code::internal_server_error);
906 this->write_http_response_error(processor::error::make_error_code(processor::error::short_key3));
907 return;
908 }
909 }
910
911 if (m_alog.static_test(log::alevel::devel)) {
912 m_alog.write(log::alevel::devel,m_request.raw());
913 if (!m_request.get_header("Sec-WebSocket-Key3").empty()) {
914 m_alog.write(log::alevel::devel,
915 utility::to_hex(m_request.get_header("Sec-WebSocket-Key3")));
916 }
917 }
918
919 // The remaining bytes in m_buf are frame data. Copy them to the
920 // beginning of the buffer and note the length. They will be read after
921 // the handshake completes and before more bytes are read.
922 std::copy(m_buf+bytes_processed,m_buf+bytes_transferred,m_buf);
923 m_buf_cursor = bytes_transferred-bytes_processed;
924
925
926 m_internal_state = istate::PROCESS_HTTP_REQUEST;
927
928 // We have the complete request. Process it.
929 lib::error_code handshake_ec = this->process_handshake_request();
930
931 // Write a response if this is a websocket connection or if it is an
932 // HTTP connection for which the response has not been deferred or
933 // started yet by a different system (i.e. still in init state).
934 if (!m_is_http || m_http_state == session::http_state::init) {
935 this->write_http_response(handshake_ec);
936 }
937 } else {
938 // read at least 1 more byte
939 transport_con_type::async_read_at_least(
940 1,
941 m_buf,
942 config::connection_read_buffer_size,
943 lib::bind(
946 lib::placeholders::_1,
947 lib::placeholders::_2
948 )
949 );
950 }
951}
ptr get_shared()
Get a shared pointer to this component.
lib::error_code process_handshake_request()
void handle_read_handshake(lib::error_code const &ec, size_t bytes_transferred)
lib::error_code initialize_processor()
@ general
Catch-all library error.
Definition error.hpp:47
@ http_parse_error
HTTP parse error.
Definition error.hpp:143
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:244

◆ handle_read_http_response()

template<typename config >
void websocketpp::connection< config >::handle_read_http_response ( lib::error_code const & ec,
size_t bytes_transferred )

Definition at line 1555 of file connection_impl.hpp.

1557{
1558 m_alog.write(log::alevel::devel,"handle_read_http_response");
1559
1560 lib::error_code ecm = ec;
1561
1562 if (!ecm) {
1563 scoped_lock_type lock(m_connection_state_lock);
1564
1565 if (m_state == session::state::connecting) {
1566 if (m_internal_state != istate::READ_HTTP_RESPONSE) {
1568 }
1569 } else if (m_state == session::state::closed) {
1570 // The connection was canceled while the response was being sent,
1571 // usually by the handshake timer. This is basically expected
1572 // (though hopefully rare) and there is nothing we can do so ignore.
1573 m_alog.write(log::alevel::devel,
1574 "handle_read_http_response invoked after connection was closed");
1575 return;
1576 } else {
1578 }
1579 }
1580
1581 if (ecm) {
1582 if (ecm == transport::error::eof && m_state == session::state::closed) {
1583 // we expect to get eof if the connection is closed already
1584 m_alog.write(log::alevel::devel,
1585 "got (expected) eof/state error from closed con");
1586 return;
1587 }
1588
1589 log_err(log::elevel::rerror,"handle_read_http_response",ecm);
1590 this->terminate(ecm);
1591 return;
1592 }
1593
1594 size_t bytes_processed = 0;
1595 // TODO: refactor this to use error codes rather than exceptions
1596 try {
1597 bytes_processed = m_response.consume(m_buf,bytes_transferred);
1598 } catch (http::exception & e) {
1599 m_elog.write(log::elevel::rerror,
1600 std::string("error in handle_read_http_response: ")+e.what());
1601 this->terminate(make_error_code(error::general));
1602 return;
1603 }
1604
1605 m_alog.write(log::alevel::devel,std::string("Raw response: ")+m_response.raw());
1606
1607 if (m_response.headers_ready()) {
1608 if (m_handshake_timer) {
1609 m_handshake_timer->cancel();
1610 m_handshake_timer.reset();
1611 }
1612
1613 lib::error_code validate_ec = m_processor->validate_server_handshake_response(
1614 m_request,
1615 m_response
1616 );
1617 if (validate_ec) {
1618 log_err(log::elevel::rerror,"Server handshake response",validate_ec);
1619 this->terminate(validate_ec);
1620 return;
1621 }
1622
1623 // Read extension parameters and set up values necessary for the end
1624 // user to complete extension negotiation.
1625 std::pair<lib::error_code,std::string> neg_results;
1626 neg_results = m_processor->negotiate_extensions(m_response);
1627
1628 if (neg_results.first) {
1629 // There was a fatal error in extension negotiation. For the moment
1630 // kill all connections that fail extension negotiation.
1631
1632 // TODO: deal with cases where the response is well formed but
1633 // doesn't match the options requested by the client. Its possible
1634 // that the best behavior in this cases is to log and continue with
1635 // an unextended connection.
1636 m_alog.write(log::alevel::devel, "Extension negotiation failed: "
1637 + neg_results.first.message());
1638 this->terminate(make_error_code(error::extension_neg_failed));
1639 // TODO: close connection with reason 1010 (and list extensions)
1640 }
1641
1642 // response is valid, connection can now be assumed to be open
1643 m_internal_state = istate::PROCESS_CONNECTION;
1644 m_state = session::state::open;
1645
1646 this->log_open_result();
1647
1648 if (m_open_handler) {
1649 m_open_handler(m_connection_hdl);
1650 }
1651
1652 // The remaining bytes in m_buf are frame data. Copy them to the
1653 // beginning of the buffer and note the length. They will be read after
1654 // the handshake completes and before more bytes are read.
1655 std::copy(m_buf+bytes_processed,m_buf+bytes_transferred,m_buf);
1656 m_buf_cursor = bytes_transferred-bytes_processed;
1657
1658 this->handle_read_frame(lib::error_code(), m_buf_cursor);
1659 } else {
1660 transport_con_type::async_read_at_least(
1661 1,
1662 m_buf,
1663 config::connection_read_buffer_size,
1664 lib::bind(
1667 lib::placeholders::_1,
1668 lib::placeholders::_2
1669 )
1670 );
1671 }
1672}
void handle_read_http_response(lib::error_code const &ec, size_t bytes_transferred)
@ extension_neg_failed
Extension negotiation failed.
Definition error.hpp:146
Here is the call graph for this function:

◆ handle_resume_reading()

template<typename config >
void websocketpp::connection< config >::handle_resume_reading ( )

Resume reading helper method. Not safe to call directly.

Definition at line 390 of file connection_impl.hpp.

390 {
391 m_read_flag = true;
392 read_frame();
393}

◆ handle_send_http_request()

template<typename config >
void websocketpp::connection< config >::handle_send_http_request ( lib::error_code const & ec)

Definition at line 1502 of file connection_impl.hpp.

1502 {
1503 m_alog.write(log::alevel::devel,"handle_send_http_request");
1504
1505 lib::error_code ecm = ec;
1506
1507 if (!ecm) {
1508 scoped_lock_type lock(m_connection_state_lock);
1509
1510 if (m_state == session::state::connecting) {
1511 if (m_internal_state != istate::WRITE_HTTP_REQUEST) {
1513 } else {
1514 m_internal_state = istate::READ_HTTP_RESPONSE;
1515 }
1516 } else if (m_state == session::state::closed) {
1517 // The connection was canceled while the response was being sent,
1518 // usually by the handshake timer. This is basically expected
1519 // (though hopefully rare) and there is nothing we can do so ignore.
1520 m_alog.write(log::alevel::devel,
1521 "handle_send_http_request invoked after connection was closed");
1522 return;
1523 } else {
1525 }
1526 }
1527
1528 if (ecm) {
1529 if (ecm == transport::error::eof && m_state == session::state::closed) {
1530 // we expect to get eof if the connection is closed already
1531 m_alog.write(log::alevel::devel,
1532 "got (expected) eof/state error from closed con");
1533 return;
1534 }
1535
1536 log_err(log::elevel::rerror,"handle_send_http_request",ecm);
1537 this->terminate(ecm);
1538 return;
1539 }
1540
1541 transport_con_type::async_read_at_least(
1542 1,
1543 m_buf,
1544 config::connection_read_buffer_size,
1545 lib::bind(
1548 lib::placeholders::_1,
1549 lib::placeholders::_2
1550 )
1551 );
1552}
Here is the call graph for this function:

◆ handle_terminate()

template<typename config >
void websocketpp::connection< config >::handle_terminate ( terminate_status tstat,
lib::error_code const & ec )

Definition at line 1760 of file connection_impl.hpp.

1762{
1763 if (m_alog.static_test(log::alevel::devel)) {
1764 m_alog.write(log::alevel::devel,"connection handle_terminate");
1765 }
1766
1767 if (ec) {
1768 // there was an error actually shutting down the connection
1769 log_err(log::elevel::devel,"handle_terminate",ec);
1770 }
1771
1772 // clean shutdown
1773 if (tstat == failed) {
1774 if (m_ec != error::http_connection_ended) {
1775 if (m_fail_handler) {
1776 m_fail_handler(m_connection_hdl);
1777 }
1778 }
1779 } else if (tstat == closed) {
1780 if (m_close_handler) {
1781 m_close_handler(m_connection_hdl);
1782 }
1783 log_close_result();
1784 } else {
1785 m_elog.write(log::elevel::rerror,"Unknown terminate_status");
1786 }
1787
1788 // call the termination handler if it exists
1789 // if it exists it might (but shouldn't) refer to a bad memory location.
1790 // If it does, we don't care and should catch and ignore it.
1791 if (m_termination_handler) {
1792 try {
1793 m_termination_handler(type::get_shared());
1794 } catch (std::exception const & e) {
1795 m_elog.write(log::elevel::warn,
1796 std::string("termination_handler call failed. Reason was: ")+e.what());
1797 }
1798 }
1799}
@ http_connection_ended
HTTP connection ended.
Definition error.hpp:111

◆ handle_transport_init()

template<typename config >
void websocketpp::connection< config >::handle_transport_init ( lib::error_code const & ec)
protected

Definition at line 750 of file connection_impl.hpp.

750 {
751 m_alog.write(log::alevel::devel,"connection handle_transport_init");
752
753 lib::error_code ecm = ec;
754
755 if (m_internal_state != istate::TRANSPORT_INIT) {
756 m_alog.write(log::alevel::devel,
757 "handle_transport_init must be called from transport init state");
759 }
760
761 if (ecm) {
762 std::stringstream s;
763 s << "handle_transport_init received error: "<< ecm.message();
764 m_elog.write(log::elevel::rerror,s.str());
765
766 this->terminate(ecm);
767 return;
768 }
769
770 // At this point the transport is ready to read and write bytes.
771 if (m_is_server) {
772 m_internal_state = istate::READ_HTTP_REQUEST;
773 this->read_handshake(1);
774 } else {
775 // We are a client. Set the processor to the version specified in the
776 // config file and send a handshake request.
777 m_internal_state = istate::WRITE_HTTP_REQUEST;
778 m_processor = get_processor(config::client_version);
779 this->send_http_request();
780 }
781}
void read_handshake(size_t num_bytes)

◆ handle_write_frame()

template<typename config >
void websocketpp::connection< config >::handle_write_frame ( lib::error_code const & ec)
Todo
unit tests

This method locks the m_write_lock mutex

Parameters
terminateWhether or not to terminate the connection upon completion of this write.
ecA status code from the transport layer, zero on success, non-zero otherwise.

Definition at line 1898 of file connection_impl.hpp.

1899{
1900 if (m_alog.static_test(log::alevel::devel)) {
1901 m_alog.write(log::alevel::devel,"connection handle_write_frame");
1902 }
1903
1904 bool terminal = m_current_msgs.back()->get_terminal();
1905
1906 m_send_buffer.clear();
1907 m_current_msgs.clear();
1908 // TODO: recycle instead of deleting
1909
1910 if (ec) {
1911 log_err(log::elevel::fatal,"handle_write_frame",ec);
1912 this->terminate(ec);
1913 return;
1914 }
1915
1916 if (terminal) {
1917 this->terminate(lib::error_code());
1918 return;
1919 }
1920
1921 bool needs_writing = false;
1922 {
1923 scoped_lock_type lock(m_write_lock);
1924
1925 // release write flag
1926 m_write_flag = false;
1927
1928 needs_writing = !m_send_queue.empty();
1929 }
1930
1931 if (needs_writing) {
1932 transport_con_type::dispatch(lib::bind(
1935 ));
1936 }
1937}
void write_frame()
Checks if there are frames in the send queue and if there are sends one.
Here is the call graph for this function:

◆ handle_write_http_response()

template<typename config >
void websocketpp::connection< config >::handle_write_http_response ( lib::error_code const & ec)

Definition at line 1360 of file connection_impl.hpp.

1360 {
1361 m_alog.write(log::alevel::devel,"handle_write_http_response");
1362
1363 lib::error_code ecm = ec;
1364
1365 if (!ecm) {
1366 scoped_lock_type lock(m_connection_state_lock);
1367
1368 if (m_state == session::state::connecting) {
1369 if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
1371 }
1372 } else if (m_state == session::state::closed) {
1373 // The connection was canceled while the response was being sent,
1374 // usually by the handshake timer. This is basically expected
1375 // (though hopefully rare) and there is nothing we can do so ignore.
1376 m_alog.write(log::alevel::devel,
1377 "handle_write_http_response invoked after connection was closed");
1378 return;
1379 } else {
1381 }
1382 }
1383
1384 if (ecm) {
1385 if (ecm == transport::error::eof && m_state == session::state::closed) {
1386 // we expect to get eof if the connection is closed already
1387 m_alog.write(log::alevel::devel,
1388 "got (expected) eof/state error from closed con");
1389 return;
1390 }
1391
1392 log_err(log::elevel::rerror,"handle_write_http_response",ecm);
1393 this->terminate(ecm);
1394 return;
1395 }
1396
1397 if (m_handshake_timer) {
1398 m_handshake_timer->cancel();
1399 m_handshake_timer.reset();
1400 }
1401
1402 if (m_response.get_status_code() != http::status_code::switching_protocols)
1403 {
1404 /*if (m_processor || m_ec == error::http_parse_error ||
1405 m_ec == error::invalid_version || m_ec == error::unsupported_version
1406 || m_ec == error::upgrade_required)
1407 {*/
1408 if (!m_is_http) {
1409 std::stringstream s;
1410 s << "Handshake ended with HTTP error: "
1411 << m_response.get_status_code();
1412 m_elog.write(log::elevel::rerror,s.str());
1413 } else {
1414 // if this was not a websocket connection, we have written
1415 // the expected response and the connection can be closed.
1416
1417 this->log_http_result();
1418
1419 if (m_ec) {
1420 m_alog.write(log::alevel::devel,
1421 "got to writing HTTP results with m_ec set: "+m_ec.message());
1422 }
1423 m_ec = make_error_code(error::http_connection_ended);
1424 }
1425
1426 this->terminate(m_ec);
1427 return;
1428 }
1429
1430 this->log_open_result();
1431
1432 m_internal_state = istate::PROCESS_CONNECTION;
1433 m_state = session::state::open;
1434
1435 if (m_open_handler) {
1436 m_open_handler(m_connection_hdl);
1437 }
1438
1439 this->handle_read_frame(lib::error_code(), m_buf_cursor);
1440}

◆ initialize_processor()

template<typename config >
lib::error_code websocketpp::connection< config >::initialize_processor ( )
protected

Set m_processor based on information in m_request. Set m_response status and return an error code indicating status.

Definition at line 1144 of file connection_impl.hpp.

1144 {
1145 m_alog.write(log::alevel::devel,"initialize_processor");
1146
1147 // if it isn't a websocket handshake nothing to do.
1148 if (!processor::is_websocket_handshake(m_request)) {
1149 return lib::error_code();
1150 }
1151
1153
1154 if (version < 0) {
1155 m_alog.write(log::alevel::devel, "BAD REQUEST: can't determine version");
1156 m_response.set_status(http::status_code::bad_request);
1158 }
1159
1160 m_processor = get_processor(version);
1161
1162 // if the processor is not null we are done
1163 if (m_processor) {
1164 return lib::error_code();
1165 }
1166
1167 // We don't have a processor for this version. Return bad request
1168 // with Sec-WebSocket-Version header filled with values we do accept
1169 m_alog.write(log::alevel::devel, "BAD REQUEST: no processor for version");
1170 m_response.set_status(http::status_code::bad_request);
1171
1172 std::stringstream ss;
1173 std::string sep;
1174 std::vector<int>::const_iterator it;
1175 for (it = versions_supported.begin(); it != versions_supported.end(); it++)
1176 {
1177 ss << sep << *it;
1178 sep = ",";
1179 }
1180
1181 m_response.replace_header("Sec-WebSocket-Version",ss.str());
1183}
static const Segment ss(Segment::ss)
@ unsupported_version
Unsupported WebSocket protocol version.
Definition error.hpp:140
@ invalid_version
Invalid WebSocket protocol version.
Definition error.hpp:137
int get_websocket_version(request_type &r)
Extract the version from a WebSocket handshake request.
bool is_websocket_handshake(request_type &r)
Determine whether or not a generic HTTP request is a WebSocket handshake.
Definition processor.hpp:68
Here is the call graph for this function:

◆ interrupt()

template<typename config >
lib::error_code websocketpp::connection< config >::interrupt ( )

Trigger the on_interrupt handler.

Signals to the connection to asyncronously invoke the on_inturrupt callback for this connection's handler once it is safe to do so.

When the on_inturrupt handler callback is called it will be from within the transport event loop with all the thread safety features guaranteed by the transport to regular handlers

Multiple inturrupt signals can be active at once on the same connection

Returns
An error code

This is thread safe if the transport is thread safe

Definition at line 341 of file connection_impl.hpp.

341 {
342 m_alog.write(log::alevel::devel,"connection connection::interrupt");
343 return transport_con_type::interrupt(
344 lib::bind(
347 )
348 );
349}
void handle_interrupt()
Transport inturrupt callback.

◆ is_server()

template<typename config >
bool websocketpp::connection< config >::is_server ( ) const
inline
Returns
whether or not the connection is attached to a server endpoint

Definition at line 1192 of file connection.hpp.

1192 {
1193 return m_is_server;
1194 }
Here is the caller graph for this function:

◆ pause_reading()

template<typename config >
lib::error_code websocketpp::connection< config >::pause_reading ( )

Signals to the connection to halt reading of new data. While reading is paused, the connection will stop reading from its associated socket. In turn this will result in TCP based flow control kicking in and slowing data flow from the remote endpoint.

This is useful for applications that push new requests to a queue to be processed by another thread and need a way to signal when their request queue is full without blocking the network processing thread.

Use resume_reading() to resume.

If supported by the transport this is done asynchronously. As such reading may not stop until the current read operation completes. Typically you can expect to receive no more bytes after initiating a read pause than the size of the read buffer.

If reading is paused for this connection already nothing is changed.

Definition at line 360 of file connection_impl.hpp.

360 {
361 m_alog.write(log::alevel::devel,"connection connection::pause_reading");
362 return transport_con_type::dispatch(
363 lib::bind(
366 )
367 );
368}
void handle_pause_reading()
Pause reading callback.

◆ ping() [1/2]

template<typename config >
void websocketpp::connection< config >::ping ( std::string const & payload)

Initiates a ping with the given payload/

There is no feedback directly from ping except in cases of immediately detectable errors. Feedback will be provided via on_pong or on_pong_timeout callbacks.

Ping locks the m_write_lock mutex

Parameters
payloadPayload to be used for the ping

Definition at line 224 of file connection_impl.hpp.

224 {
225 lib::error_code ec;
226 ping(payload,ec);
227 if (ec) {
228 throw exception(ec);
229 }
230}
void ping(std::string const &payload)
Send a ping.

◆ ping() [2/2]

template<typename config >
void websocketpp::connection< config >::ping ( std::string const & payload,
lib::error_code & ec )

Definition at line 155 of file connection_impl.hpp.

155 {
156 if (m_alog.static_test(log::alevel::devel)) {
157 m_alog.write(log::alevel::devel,"connection ping");
158 }
159
160 {
161 scoped_lock_type lock(m_connection_state_lock);
162 if (m_state != session::state::open) {
163 std::stringstream ss;
164 ss << "connection::ping called from invalid state " << m_state;
165 m_alog.write(log::alevel::devel,ss.str());
167 return;
168 }
169 }
170
171 message_ptr msg = m_msg_manager->get_message();
172 if (!msg) {
174 return;
175 }
176
177 ec = m_processor->prepare_ping(payload,msg);
178 if (ec) {return;}
179
180 // set ping timer if we are listening for one
181 if (m_pong_timeout_handler) {
182 // Cancel any existing timers
183 if (m_ping_timer) {
184 m_ping_timer->cancel();
185 }
186
187 if (m_pong_timeout_dur > 0) {
188 m_ping_timer = transport_con_type::set_timer(
189 m_pong_timeout_dur,
190 lib::bind(
193 payload,
194 lib::placeholders::_1
195 )
196 );
197 }
198
199 if (!m_ping_timer) {
200 // Our transport doesn't support timers
201 m_elog.write(log::elevel::warn,"Warning: a pong_timeout_handler is \
202 set but the transport in use does not support timeouts.");
203 }
204 }
205
206 bool needs_writing = false;
207 {
208 scoped_lock_type lock(m_write_lock);
209 write_push(msg);
210 needs_writing = !m_write_flag && !m_send_queue.empty();
211 }
212
213 if (needs_writing) {
214 transport_con_type::dispatch(lib::bind(
217 ));
218 }
219
220 ec = lib::error_code();
221}
void handle_pong_timeout(std::string payload, lib::error_code const &ec)
Utility method that gets called back when the ping timer expires.
@ no_outgoing_buffers
The endpoint is out of outgoing message buffers.
Definition error.hpp:68
Here is the call graph for this function:

◆ pong() [1/2]

template<typename config >
void websocketpp::connection< config >::pong ( std::string const & payload)

Initiates a pong with the given payload.

There is no feedback from a pong once sent.

Pong locks the m_write_lock mutex

Parameters
payloadPayload to be used for the pong

Definition at line 295 of file connection_impl.hpp.

295 {
296 lib::error_code ec;
297 pong(payload,ec);
298 if (ec) {
299 throw exception(ec);
300 }
301}
void pong(std::string const &payload)
Send a pong.

◆ pong() [2/2]

template<typename config >
void websocketpp::connection< config >::pong ( std::string const & payload,
lib::error_code & ec )

Definition at line 252 of file connection_impl.hpp.

252 {
253 if (m_alog.static_test(log::alevel::devel)) {
254 m_alog.write(log::alevel::devel,"connection pong");
255 }
256
257 {
258 scoped_lock_type lock(m_connection_state_lock);
259 if (m_state != session::state::open) {
260 std::stringstream ss;
261 ss << "connection::pong called from invalid state " << m_state;
262 m_alog.write(log::alevel::devel,ss.str());
264 return;
265 }
266 }
267
268 message_ptr msg = m_msg_manager->get_message();
269 if (!msg) {
271 return;
272 }
273
274 ec = m_processor->prepare_pong(payload,msg);
275 if (ec) {return;}
276
277 bool needs_writing = false;
278 {
279 scoped_lock_type lock(m_write_lock);
280 write_push(msg);
281 needs_writing = !m_write_flag && !m_send_queue.empty();
282 }
283
284 if (needs_writing) {
285 transport_con_type::dispatch(lib::bind(
288 ));
289 }
290
291 ec = lib::error_code();
292}
Here is the call graph for this function:

◆ process_handshake_request()

template<typename config >
lib::error_code websocketpp::connection< config >::process_handshake_request ( )
protected

Perform WebSocket handshake validation of m_request using m_processor. set m_response and return an error code indicating status.

Definition at line 1186 of file connection_impl.hpp.

1186 {
1187 m_alog.write(log::alevel::devel,"process handshake request");
1188
1189 if (!processor::is_websocket_handshake(m_request)) {
1190 // this is not a websocket handshake. Process as plain HTTP
1191 m_alog.write(log::alevel::devel,"HTTP REQUEST");
1192
1193 // extract URI from request
1195 m_request,
1196 (transport_con_type::is_secure() ? "https" : "http")
1197 );
1198
1199 if (!m_uri->get_valid()) {
1200 m_alog.write(log::alevel::devel, "Bad request: failed to parse uri");
1201 m_response.set_status(http::status_code::bad_request);
1203 }
1204
1205 if (m_http_handler) {
1206 m_is_http = true;
1207 m_http_handler(m_connection_hdl);
1208
1209 if (m_state == session::state::closed) {
1211 }
1212 } else {
1215 }
1216
1217 return lib::error_code();
1218 }
1219
1220 lib::error_code ec = m_processor->validate_handshake(m_request);
1221
1222 // Validate: make sure all required elements are present.
1223 if (ec){
1224 // Not a valid handshake request
1225 m_alog.write(log::alevel::devel, "Bad request " + ec.message());
1226 m_response.set_status(http::status_code::bad_request);
1227 return ec;
1228 }
1229
1230 // Read extension parameters and set up values necessary for the end user
1231 // to complete extension negotiation.
1232 std::pair<lib::error_code,std::string> neg_results;
1233 neg_results = m_processor->negotiate_extensions(m_request);
1234
1235 if (neg_results.first) {
1236 // There was a fatal error in extension parsing that should result in
1237 // a failed connection attempt.
1238 m_alog.write(log::alevel::devel, "Bad request: " + neg_results.first.message());
1239 m_response.set_status(http::status_code::bad_request);
1240 return neg_results.first;
1241 } else {
1242 // extension negotiation succeeded, set response header accordingly
1243 // we don't send an empty extensions header because it breaks many
1244 // clients.
1245 if (neg_results.second.size() > 0) {
1246 m_response.replace_header("Sec-WebSocket-Extensions",
1247 neg_results.second);
1248 }
1249 }
1250
1251 // extract URI from request
1252 m_uri = m_processor->get_uri(m_request);
1253
1254
1255 if (!m_uri->get_valid()) {
1256 m_alog.write(log::alevel::devel, "Bad request: failed to parse uri");
1257 m_response.set_status(http::status_code::bad_request);
1259 }
1260
1261 // extract subprotocols
1262 lib::error_code subp_ec = m_processor->extract_subprotocols(m_request,
1263 m_requested_subprotocols);
1264
1265 if (subp_ec) {
1266 // should we do anything?
1267 }
1268
1269 // Ask application to validate the connection
1270 if (!m_validate_handler || m_validate_handler(m_connection_hdl)) {
1271 m_response.set_status(http::status_code::switching_protocols);
1272
1273 // Write the appropriate response headers based on request and
1274 // processor version
1275 ec = m_processor->process_handshake(m_request,m_subprotocol,m_response);
1276
1277 if (ec) {
1278 std::stringstream s;
1279 s << "Processing error: " << ec << "(" << ec.message() << ")";
1280 m_alog.write(log::alevel::devel, s.str());
1281
1282 m_response.set_status(http::status_code::internal_server_error);
1283 return ec;
1284 }
1285 } else {
1286 // User application has rejected the handshake
1287 m_alog.write(log::alevel::devel, "USER REJECT");
1288
1289 // Use Bad Request if the user handler did not provide a more
1290 // specific http response error code.
1291 // TODO: is there a better default?
1292 if (m_response.get_status_code() == http::status_code::uninitialized) {
1293 m_response.set_status(http::status_code::bad_request);
1294 }
1295
1297 }
1298
1299 return lib::error_code();
1300}
void set_status(http::status_code::value code)
Set response status code and message.
@ rejected
Connection rejected.
Definition error.hpp:130
@ invalid_uri
An invalid uri was supplied.
Definition error.hpp:65
uri_ptr get_uri_from_host(request_type &request, std::string scheme)
Extract a URI ptr from the host header of the request.
Here is the call graph for this function:

◆ read_frame()

template<typename config >
void websocketpp::connection< config >::read_frame ( )

Definition at line 1123 of file connection_impl.hpp.

1123 {
1124 if (!m_read_flag) {
1125 return;
1126 }
1127
1128 transport_con_type::async_read_at_least(
1129 // std::min wont work with undefined static const values.
1130 // TODO: is there a more elegant way to do this?
1131 // Need to determine if requesting 1 byte or the exact number of bytes
1132 // is better here. 1 byte lets us be a bit more responsive at a
1133 // potential expense of additional runs through handle_read_frame
1134 /*(m_processor->get_bytes_needed() > config::connection_read_buffer_size ?
1135 config::connection_read_buffer_size : m_processor->get_bytes_needed())*/
1136 1,
1137 m_buf,
1138 config::connection_read_buffer_size,
1139 m_handle_read_frame
1140 );
1141}

◆ read_handshake()

template<typename config >
void websocketpp::connection< config >::read_handshake ( size_t num_bytes)

Definition at line 784 of file connection_impl.hpp.

784 {
785 m_alog.write(log::alevel::devel,"connection read_handshake");
786
787 if (m_open_handshake_timeout_dur > 0) {
788 m_handshake_timer = transport_con_type::set_timer(
789 m_open_handshake_timeout_dur,
790 lib::bind(
793 lib::placeholders::_1
794 )
795 );
796 }
797
798 transport_con_type::async_read_at_least(
799 num_bytes,
800 m_buf,
801 config::connection_read_buffer_size,
802 lib::bind(
805 lib::placeholders::_1,
806 lib::placeholders::_2
807 )
808 );
809}
void handle_open_handshake_timeout(lib::error_code const &ec)

◆ remove_header()

template<typename config >
void websocketpp::connection< config >::remove_header ( std::string const & key)

Removes a header from the response.

This member function is valid only from the http() and validate() handler callbacks, or to a client connection before connect has been called.

Parameters
keyThe name of the header to remove
See also
websocketpp::http::parser::remove_header

Definition at line 638 of file connection_impl.hpp.

639{
640 if (m_is_server) {
641 if (m_internal_state == istate::PROCESS_HTTP_REQUEST) {
642 // we are setting response headers for an incoming server connection
643 m_response.remove_header(key);
644 } else {
645 throw exception("Call to remove_header from invalid state",
647 }
648 } else {
649 if (m_internal_state == istate::USER_INIT) {
650 // we are setting initial headers for an outgoing client connection
651 m_request.remove_header(key);
652 } else {
653 throw exception("Call to remove_header from invalid state",
655 }
656 }
657}
Here is the call graph for this function:

◆ replace_header()

template<typename config >
void websocketpp::connection< config >::replace_header ( std::string const & key,
std::string const & val )

If a header with this name already exists the old value will be replaced Use connection::append_header to append to a list of existing values.

This member function is valid only from the http() and validate() handler callbacks, or to a client connection before connect has been called.

Parameters
keyName of the header to set
valValue to set
See also
append_header
websocketpp::http::parser::replace_header

Definition at line 614 of file connection_impl.hpp.

616{
617 if (m_is_server) {
618 if (m_internal_state == istate::PROCESS_HTTP_REQUEST) {
619 // we are setting response headers for an incoming server connection
620 m_response.replace_header(key,val);
621 } else {
622 throw exception("Call to replace_header from invalid state",
624 }
625 } else {
626 if (m_internal_state == istate::USER_INIT) {
627 // we are setting initial headers for an outgoing client connection
628 m_request.replace_header(key,val);
629 } else {
630 throw exception("Call to replace_header from invalid state",
632 }
633 }
634}
Here is the call graph for this function:

◆ resume_reading()

template<typename config >
lib::error_code websocketpp::connection< config >::resume_reading ( )

Signals to the connection to resume reading of new data after it was paused by pause_reading().

If reading is not paused for this connection already nothing is changed.

Definition at line 378 of file connection_impl.hpp.

378 {
379 m_alog.write(log::alevel::devel,"connection connection::resume_reading");
380 return transport_con_type::dispatch(
381 lib::bind(
384 )
385 );
386}
void handle_resume_reading()
Resume reading callback.

◆ select_subprotocol() [1/2]

template<typename config >
void websocketpp::connection< config >::select_subprotocol ( std::string const & value)

Indicates which subprotocol should be used for this connection. Valid only during the validate handler callback. Subprotocol selected must have been requested by the client. Consult get_requested_subprotocols() for a list of valid subprotocols.

This member function is valid on server endpoints/connections only

Parameters
valueThe subprotocol to select

Definition at line 516 of file connection_impl.hpp.

516 {
517 lib::error_code ec;
518 this->select_subprotocol(value,ec);
519 if (ec) {
520 throw exception(ec);
521 }
522}
void select_subprotocol(std::string const &value, lib::error_code &ec)
Select a subprotocol to use (exception free)

◆ select_subprotocol() [2/2]

template<typename config >
void websocketpp::connection< config >::select_subprotocol ( std::string const & value,
lib::error_code & ec )

Indicates which subprotocol should be used for this connection. Valid only during the validate handler callback. Subprotocol selected must have been requested by the client. Consult get_requested_subprotocols() for a list of valid subprotocols.

This member function is valid on server endpoints/connections only

Parameters
valueThe subprotocol to select
ecA reference to an error code that will be filled in the case of errors

Definition at line 488 of file connection_impl.hpp.

490{
491 if (!m_is_server) {
493 return;
494 }
495
496 if (value.empty()) {
497 ec = lib::error_code();
498 return;
499 }
500
501 std::vector<std::string>::iterator it;
502
503 it = std::find(m_requested_subprotocols.begin(),
504 m_requested_subprotocols.end(),
505 value);
506
507 if (it == m_requested_subprotocols.end()) {
509 return;
510 }
511
512 m_subprotocol = value;
513}
@ unrequested_subprotocol
Selected subprotocol was not requested by the client.
Definition error.hpp:102
@ server_only
Attempted to use a server specific feature on a client endpoint.
Definition error.hpp:108
Here is the call graph for this function:

◆ send() [1/3]

template<typename config >
lib::error_code websocketpp::connection< config >::send ( message_ptr msg)

If presented with a prepared message it is added without validation or framing. If presented with an unprepared message it is validated, framed, and then added

Errors are returned via an exception

Todo
make exception system_error rather than error_code

This method invokes the m_write_lock mutex

Parameters
msgA message_ptr to the message to send.
Here is the call graph for this function:

◆ send() [2/3]

template<typename config >
lib::error_code websocketpp::connection< config >::send ( std::string const & payload,
frame::opcode::value op = frame::opcode::text )

Convenience method to send a message given a payload string and optionally an opcode. Default opcode is utf8 text.

This method locks the m_write_lock mutex

Parameters
payloadThe payload string to generated the message with
opThe opcode to generated the message with. Default is frame::opcode::text

Definition at line 83 of file connection_impl.hpp.

85{
86 message_ptr msg = m_msg_manager->get_message(op,payload.size());
87 msg->append_payload(payload);
88 msg->set_compressed(true);
89
90 return send(msg);
91}
lib::error_code send(std::string const &payload, frame::opcode::value op=frame::opcode::text)
Create a message and then add it to the outgoing send queue.

◆ send() [3/3]

template<typename config >
lib::error_code websocketpp::connection< config >::send ( void const * payload,
size_t len,
frame::opcode::value op = frame::opcode::binary )

Convenience method to send a message given a raw array and optionally an opcode. Default opcode is binary.

This method locks the m_write_lock mutex

Parameters
payloadA pointer to the array containing the bytes to send.
lenLength of the array.
opThe opcode to generated the message with. Default is frame::opcode::binary

Definition at line 94 of file connection_impl.hpp.

96{
97 message_ptr msg = m_msg_manager->get_message(op,len);
98 msg->append_payload(payload,len);
99
100 return send(msg);
101}
size_t len

◆ send_http_response() [1/2]

template<typename config >
void websocketpp::connection< config >::send_http_response ( )

Definition at line 712 of file connection_impl.hpp.

712 {
713 lib::error_code ec;
714 this->send_http_response(ec);
715 if (ec) {
716 throw exception(ec);
717 }
718}
void send_http_response()
Send deferred HTTP Response.

◆ send_http_response() [2/2]

template<typename config >
void websocketpp::connection< config >::send_http_response ( lib::error_code & ec)

Sends an http response to an HTTP connection that was deferred. This will send a complete response including all headers, status line, and body text. The connection will be closed afterwards.

Since
0.6.0
Parameters
ecA status code, zero on success, non-zero otherwise

Definition at line 696 of file connection_impl.hpp.

696 {
697 {
698 scoped_lock_type lock(m_connection_state_lock);
699 if (m_http_state != session::http_state::deferred) {
701 return;
702 }
703
705 }
706
707 this->write_http_response(lib::error_code());
708 ec = lib::error_code();
709}
Here is the call graph for this function:

◆ set_body() [1/2]

template<typename config >
void websocketpp::connection< config >::set_body ( std::string && value)

Definition at line 579 of file connection_impl.hpp.

579 {
580 if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
581 throw exception("Call to set_status from invalid state",
583 }
584
585 m_response.set_body(std::move(value));
586}
Here is the call graph for this function:

◆ set_body() [2/2]

template<typename config >
void websocketpp::connection< config >::set_body ( std::string const & value)

Set the body content of the HTTP response to the parameter string. Note set_body will also set the Content-Length HTTP header to the appropriate value. If you want the Content-Length header to be something else set it to something else after calling set_body

This member function is valid only from the http() and validate() handler callbacks.

Parameters
valueString data to include as the body content.
See also
websocketpp::http::response::set_body

Definition at line 569 of file connection_impl.hpp.

569 {
570 if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
571 throw exception("Call to set_status from invalid state",
573 }
574
575 m_response.set_body(value);
576}
Here is the call graph for this function:

◆ set_close_handler()

template<typename config >
void websocketpp::connection< config >::set_close_handler ( close_handler h)
inline

The close handler is called immediately after the connection is closed.

Parameters
hThe new close_handler

Definition at line 361 of file connection.hpp.

361 {
362 m_close_handler = h;
363 }

◆ set_close_handshake_timeout()

template<typename config >
void websocketpp::connection< config >::set_close_handshake_timeout ( long dur)
inline

Sets the length of time the library will wait after a closing handshake has been initiated before cancelling it. This can be used to prevent excessive wait times for outgoing clients or excessive resource usage from broken clients or DoS attacks on servers.

Connections that time out will have their close handlers called with the close_handshake_timeout error code.

The default value is specified via the compile time config value 'timeout_close_handshake'. The default value in the core config is 5000ms. A value of 0 will disable the timer entirely.

To be effective, the transport you are using must support timers. See the documentation for your transport policy for details about its timer support.

Parameters
durThe length of the close handshake timeout in ms

Definition at line 525 of file connection.hpp.

525 {
526 m_close_handshake_timeout_dur = dur;
527 }

◆ set_fail_handler()

template<typename config >
void websocketpp::connection< config >::set_fail_handler ( fail_handler h)
inline

The fail handler is called whenever the connection fails while the handshake is bring processed.

Parameters
hThe new fail_handler

Definition at line 372 of file connection.hpp.

372 {
373 m_fail_handler = h;
374 }

◆ set_handle()

template<typename config >
void websocketpp::connection< config >::set_handle ( connection_hdl hdl)
inline

The connection handle is a token that can be shared outside the WebSocket++ core for the purposes of identifying a connection and sending it messages.

Parameters
hdlA connection_hdl that the connection will use to refer to itself.

Definition at line 1357 of file connection.hpp.

1357 {
1358 m_connection_hdl = hdl;
1359 transport_con_type::set_handle(hdl);
1360 }

◆ set_http_handler()

template<typename config >
void websocketpp::connection< config >::set_http_handler ( http_handler h)
inline

The http handler is called after an HTTP request other than a WebSocket upgrade request is received. It allows a WebSocket++ server to respond to regular HTTP requests on the same port as it processes WebSocket connections. This can be useful for hosting error messages, flash policy files, status pages, and other simple HTTP responses. It is not intended to be used as a primary web server.

Parameters
hThe new http_handler

Definition at line 447 of file connection.hpp.

447 {
448 m_http_handler = h;
449 }

◆ set_interrupt_handler()

template<typename config >
void websocketpp::connection< config >::set_interrupt_handler ( interrupt_handler h)
inline

The interrupt handler is called whenever the connection is manually interrupted by the application.

Parameters
hThe new interrupt_handler

Definition at line 432 of file connection.hpp.

432 {
433 m_interrupt_handler = h;
434 }

◆ set_max_http_body_size()

template<typename config >
void websocketpp::connection< config >::set_max_http_body_size ( size_t new_value)
inline

Set maximum HTTP message body size. Maximum message body size determines the point at which the connection will stop reading an HTTP request whose body is too large.

The default is set by the endpoint that creates the connection.

Since
0.5.0
Parameters
new_valueThe value to set as the maximum message size.

Definition at line 610 of file connection.hpp.

610 {
611 m_request.set_max_body_size(new_value);
612 }

◆ set_max_message_size()

template<typename config >
void websocketpp::connection< config >::set_max_message_size ( size_t new_value)
inline

Set maximum message size. Maximum message size determines the point at which the connection will fail with the message_too_big protocol error. This value may be changed during the connection.

The default is set by the endpoint that creates the connection.

Since
0.3.0
Parameters
new_valueThe value to set as the maximum message size.

Definition at line 575 of file connection.hpp.

575 {
576 m_max_message_size = new_value;
577 if (m_processor) {
578 m_processor->set_max_message_size(new_value);
579 }
580 }

◆ set_message_handler()

template<typename config >
void websocketpp::connection< config >::set_message_handler ( message_handler h)
inline

The message handler is called after a new message has been received.

Parameters
hThe new message_handler

Definition at line 473 of file connection.hpp.

473 {
474 m_message_handler = h;
475 }

◆ set_open_handler()

template<typename config >
void websocketpp::connection< config >::set_open_handler ( open_handler h)
inline

The open handler is called after the WebSocket handshake is complete and the connection is considered OPEN.

Parameters
hThe new open_handler

Definition at line 351 of file connection.hpp.

351 {
352 m_open_handler = h;
353 }

◆ set_open_handshake_timeout()

template<typename config >
void websocketpp::connection< config >::set_open_handshake_timeout ( long dur)
inline

Sets the length of time the library will wait after an opening handshake has been initiated before cancelling it. This can be used to prevent excessive wait times for outgoing clients or excessive resource usage from broken clients or DoS attacks on servers.

Connections that time out will have their fail handlers called with the open_handshake_timeout error code.

The default value is specified via the compile time config value 'timeout_open_handshake'. The default value in the core config is 5000ms. A value of 0 will disable the timer entirely.

To be effective, the transport you are using must support timers. See the documentation for your transport policy for details about its timer support.

Parameters
durThe length of the open handshake timeout in ms

Definition at line 501 of file connection.hpp.

501 {
502 m_open_handshake_timeout_dur = dur;
503 }

◆ set_ping_handler()

template<typename config >
void websocketpp::connection< config >::set_ping_handler ( ping_handler h)
inline

The ping handler is called whenever the connection receives a ping control frame. The ping payload is included.

The ping handler's return time controls whether or not a pong is sent in response to this ping. Returning false will suppress the return pong. If no ping handler is set a pong will be sent.

Parameters
hThe new ping_handler

Definition at line 387 of file connection.hpp.

387 {
388 m_ping_handler = h;
389 }

◆ set_pong_handler()

template<typename config >
void websocketpp::connection< config >::set_pong_handler ( pong_handler h)
inline

The pong handler is called whenever the connection receives a pong control frame. The pong payload is included.

Parameters
hThe new pong_handler

Definition at line 398 of file connection.hpp.

398 {
399 m_pong_handler = h;
400 }

◆ set_pong_timeout()

template<typename config >
void websocketpp::connection< config >::set_pong_timeout ( long dur)
inline

Sets the length of time the library will wait for a pong response to a ping. This can be used as a keepalive or to detect broken connections.

Pong responses that time out will have the pong timeout handler called.

The default value is specified via the compile time config value 'timeout_pong'. The default value in the core config is 5000ms. A value of 0 will disable the timer entirely.

To be effective, the transport you are using must support timers. See the documentation for your transport policy for details about its timer support.

Parameters
durThe length of the pong timeout in ms

Definition at line 546 of file connection.hpp.

546 {
547 m_pong_timeout_dur = dur;
548 }

◆ set_pong_timeout_handler()

template<typename config >
void websocketpp::connection< config >::set_pong_timeout_handler ( pong_timeout_handler h)
inline

If the transport component being used supports timers, the pong timeout handler is called whenever a pong control frame is not received with the configured timeout period after the application sends a ping.

The config setting timeout_pong controls the length of the timeout period. It is specified in milliseconds.

This can be used to probe the health of the remote endpoint's WebSocket implementation. This does not guarantee that the remote application itself is still healthy but can be a useful diagnostic.

Note: receipt of this callback doesn't mean the pong will never come. This functionality will not suppress delivery of the pong in question should it arrive after the timeout.

Parameters
hThe new pong_timeout_handler

Definition at line 421 of file connection.hpp.

421 {
422 m_pong_timeout_handler = h;
423 }

◆ set_status() [1/2]

template<typename config >
void websocketpp::connection< config >::set_status ( http::status_code::value code)

Sets the response status code to code and looks up the corresponding message for standard codes. Non-standard codes will be entered as Unknown use set_status(status_code::value,std::string) overload to set both values explicitly.

This member function is valid only from the http() and validate() handler callbacks.

Parameters
codeCode to set
msgMessage to set
See also
websocketpp::http::response::set_status

Definition at line 545 of file connection_impl.hpp.

546{
547 if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
548 throw exception("Call to set_status from invalid state",
550 }
551 m_response.set_status(code);
552}
Here is the call graph for this function:

◆ set_status() [2/2]

template<typename config >
void websocketpp::connection< config >::set_status ( http::status_code::value code,
std::string const & msg )

Sets the response status code and message to independent custom values. use set_status(status_code::value) to set the code and have the standard message be automatically set.

This member function is valid only from the http() and validate() handler callbacks.

Parameters
codeCode to set
msgMessage to set
See also
websocketpp::http::response::set_status

Definition at line 556 of file connection_impl.hpp.

558{
559 if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
560 throw exception("Call to set_status from invalid state",
562 }
563
564 m_response.set_status(code,msg);
565}
Here is the call graph for this function:

◆ set_termination_handler()

template<typename config >
void websocketpp::connection< config >::set_termination_handler ( termination_handler new_handler)

Sets the handler for a terminating connection. Should only be used internally by the endpoint class.

Definition at line 53 of file connection_impl.hpp.

55{
56 m_alog.write(log::alevel::devel,
57 "connection set_termination_handler");
58
59 //scoped_lock_type lock(m_connection_state_lock);
60
61 m_termination_handler = new_handler;
62}

◆ set_uri()

template<typename config >
void websocketpp::connection< config >::set_uri ( uri_ptr uri)

This should really only be called by internal library methods unless you really know what you are doing.

Parameters
uriThe new URI to set

Definition at line 436 of file connection_impl.hpp.

436 {
437 //scoped_lock_type lock(m_connection_state_lock);
438 m_uri = uri;
439}

◆ set_validate_handler()

template<typename config >
void websocketpp::connection< config >::set_validate_handler ( validate_handler h)
inline

The validate handler is called after a WebSocket handshake has been parsed but before a response is returned. It provides the application a chance to examine the request and determine whether or not it wants to accept the connection.

Returning false from the validate handler will reject the connection. If no validate handler is present, all connections will be allowed.

Parameters
hThe new validate_handler

Definition at line 463 of file connection.hpp.

463 {
464 m_validate_handler = h;
465 }

◆ start()

template<typename config >
void websocketpp::connection< config >::start ( )

Definition at line 726 of file connection_impl.hpp.

726 {
727 m_alog.write(log::alevel::devel,"connection start");
728
729 if (m_internal_state != istate::USER_INIT) {
730 m_alog.write(log::alevel::devel,"Start called in invalid state");
732 return;
733 }
734
735 m_internal_state = istate::TRANSPORT_INIT;
736
737 // Depending on how the transport implements init this function may return
738 // immediately and call handle_transport_init later or call
739 // handle_transport_init from this function.
740 transport_con_type::init(
741 lib::bind(
744 lib::placeholders::_1
745 )
746 );
747}
void handle_transport_init(lib::error_code const &ec)

◆ terminate()

template<typename config >
void websocketpp::connection< config >::terminate ( lib::error_code const & ec)

Definition at line 1707 of file connection_impl.hpp.

1707 {
1708 if (m_alog.static_test(log::alevel::devel)) {
1709 m_alog.write(log::alevel::devel,"connection terminate");
1710 }
1711
1712 // Cancel close handshake timer
1713 if (m_handshake_timer) {
1714 m_handshake_timer->cancel();
1715 m_handshake_timer.reset();
1716 }
1717
1718 terminate_status tstat = unknown;
1719 if (ec) {
1720 m_ec = ec;
1721 m_local_close_code = close::status::abnormal_close;
1722 m_local_close_reason = ec.message();
1723 }
1724
1725 // TODO: does any of this need a mutex?
1726 if (m_is_http) {
1727 m_http_state = session::http_state::closed;
1728 }
1729 if (m_state == session::state::connecting) {
1730 m_state = session::state::closed;
1731 tstat = failed;
1732
1733 // Log fail result here before socket is shut down and we can't get
1734 // the remote address, etc anymore
1735 if (m_ec != error::http_connection_ended) {
1736 log_fail_result();
1737 }
1738 } else if (m_state != session::state::closed) {
1739 m_state = session::state::closed;
1740 tstat = closed;
1741 } else {
1742 m_alog.write(log::alevel::devel,
1743 "terminate called on connection that was already terminated");
1744 return;
1745 }
1746
1747 // TODO: choose between shutdown and close based on error code sent
1748
1749 transport_con_type::async_shutdown(
1750 lib::bind(
1753 tstat,
1754 lib::placeholders::_1
1755 )
1756 );
1757}
void handle_terminate(terminate_status tstat, lib::error_code const &ec)

◆ write_frame()

template<typename config >
void websocketpp::connection< config >::write_frame ( )
Todo
unit tests

This method locks the m_write_lock mutex

Definition at line 1802 of file connection_impl.hpp.

1802 {
1803 //m_alog.write(log::alevel::devel,"connection write_frame");
1804
1805 {
1806 scoped_lock_type lock(m_write_lock);
1807
1808 // Check the write flag. If true, there is an outstanding transport
1809 // write already. In this case we just return. The write handler will
1810 // start a new write if the write queue isn't empty. If false, we set
1811 // the write flag and proceed to initiate a transport write.
1812 if (m_write_flag) {
1813 return;
1814 }
1815
1816 // pull off all the messages that are ready to write.
1817 // stop if we get a message marked terminal
1818 message_ptr next_message = write_pop();
1819 while (next_message) {
1820 m_current_msgs.push_back(next_message);
1821 if (!next_message->get_terminal()) {
1822 next_message = write_pop();
1823 } else {
1824 next_message = message_ptr();
1825 }
1826 }
1827
1828 if (m_current_msgs.empty()) {
1829 // there was nothing to send
1830 return;
1831 } else {
1832 // At this point we own the next messages to be sent and are
1833 // responsible for holding the write flag until they are
1834 // successfully sent or there is some error
1835 m_write_flag = true;
1836 }
1837 }
1838
1839 typename std::vector<message_ptr>::iterator it;
1840 for (it = m_current_msgs.begin(); it != m_current_msgs.end(); ++it) {
1841 std::string const & header = (*it)->get_header();
1842 std::string const & payload = (*it)->get_payload();
1843
1844 m_send_buffer.push_back(transport::buffer(header.c_str(),header.size()));
1845 m_send_buffer.push_back(transport::buffer(payload.c_str(),payload.size()));
1846 }
1847
1848 // Print detailed send stats if those log levels are enabled
1849 if (m_alog.static_test(log::alevel::frame_header)) {
1850 if (m_alog.dynamic_test(log::alevel::frame_header)) {
1851 std::stringstream general,header,payload;
1852
1853 general << "Dispatching write containing " << m_current_msgs.size()
1854 <<" message(s) containing ";
1855 header << "Header Bytes: \n";
1856 payload << "Payload Bytes: \n";
1857
1858 size_t hbytes = 0;
1859 size_t pbytes = 0;
1860
1861 for (size_t i = 0; i < m_current_msgs.size(); i++) {
1862 hbytes += m_current_msgs[i]->get_header().size();
1863 pbytes += m_current_msgs[i]->get_payload().size();
1864
1865
1866 header << "[" << i << "] ("
1867 << m_current_msgs[i]->get_header().size() << ") "
1868 << utility::to_hex(m_current_msgs[i]->get_header()) << "\n";
1869
1870 if (m_alog.static_test(log::alevel::frame_payload)) {
1871 if (m_alog.dynamic_test(log::alevel::frame_payload)) {
1872 payload << "[" << i << "] ("
1873 << m_current_msgs[i]->get_payload().size() << ") ["<<m_current_msgs[i]->get_opcode()<<"] "
1874 << (m_current_msgs[i]->get_opcode() == frame::opcode::text ?
1875 m_current_msgs[i]->get_payload() :
1876 utility::to_hex(m_current_msgs[i]->get_payload())
1877 )
1878 << "\n";
1879 }
1880 }
1881 }
1882
1883 general << hbytes << " header bytes and " << pbytes << " payload bytes";
1884
1885 m_alog.write(log::alevel::frame_header,general.str());
1886 m_alog.write(log::alevel::frame_header,header.str());
1887 m_alog.write(log::alevel::frame_payload,payload.str());
1888 }
1889 }
1890
1891 transport_con_type::async_write(
1892 m_send_buffer,
1893 m_write_frame_handler
1894 );
1895}
message_type::ptr message_ptr
static level const frame_payload
One line per frame, includes the full message payload (warning: chatty)
Definition levels.hpp:129
static level const frame_header
One line per frame, includes the full frame header.
Definition levels.hpp:127
Here is the call graph for this function:

The documentation for this class was generated from the following files: