C++ Actor Framework 1.0.0
Loading...
Searching...
No Matches
caf::net::web_socket::handshake Class Reference

Wraps state and algorithms for the WebSocket client handshake as defined in RFC 6455. More...

#include <handshake.hpp>

Public Types

using key_type = std::array<std::byte, 16>
 

Public Member Functions

 handshake (const handshake &)=default
 
 handshake (handshake &&) noexcept=default
 
handshakeoperator= (const handshake &)=default
 
handshakeoperator= (handshake &&) noexcept=default
 
const auto & key () const noexcept
 Returns the 16-byte key for the opening handshake.
 
void key (key_type new_key) noexcept
 Sets the 16-byte key for the opening handshake.
 
bool assign_key (std::string_view base64_key)
 Tries to assign a key from base64 input.
 
std::string response_key () const
 Returns the key for the response message, i.e., what the server puts into the HTTP field Sec-WebSocket-Accept.
 
const auto & fields () const noexcept
 Returns all configured header fields, except the key.
 
bool has_valid_key () const noexcept
 Checks whether at least one bit in the key is one.
 
bool has_mandatory_fields () const noexcept
 Checks whether all mandatory fields were provided by the user.
 
void randomize_key ()
 Fills the key with pseudo-random numbers generated by std::minstd_rand with a seed chosen from std::random_device.
 
void randomize_key (unsigned seed)
 Fills the key with pseudo-random numbers generated by std::minstd_rand, initialized with seed.
 
void endpoint (std::string value)
 Sets a value for the mandatory WebSocket endpoint, i.e., the Request-URI component of the GET method according to RFC 2616.
 
bool has_endpoint () const noexcept
 Checks whether the handshake has an endpoint defined.
 
void host (std::string value)
 Sets a value for the mandatory Host field.
 
bool has_host () const noexcept
 Checks whether the handshake has an host defined.
 
void origin (std::string value)
 Sets a value for the optional Origin field.
 
void protocols (std::string value)
 Sets a value for the optional Sec-WebSocket-Protocol field.
 
void extensions (std::string value)
 Sets a value for the optional Sec-WebSocket-Extensions field.
 
void write_http_1_request (byte_buffer &buf) const
 Writes the HTTP 1.1 request message to buf.
 
void write_http_1_response (byte_buffer &buf) const
 Writes the HTTP 1.1 response message to buf.
 
void write_response (http::lower_layer::server *down) const
 Writes the HTTP response message to down.
 
bool is_valid_http_1_response (std::string_view http_response) const
 Checks whether the http_response contains a HTTP 1.1 response to the generated HTTP GET request.
 

Static Public Attributes

static constexpr uint32_t max_http_size = 2048
 Maximum size for HTTP request and response messages.
 

Detailed Description

Wraps state and algorithms for the WebSocket client handshake as defined in RFC 6455.

Member Function Documentation

◆ endpoint()

void caf::net::web_socket::handshake::endpoint ( std::string value)

Sets a value for the mandatory WebSocket endpoint, i.e., the Request-URI component of the GET method according to RFC 2616.

Parameters
valueIdentifies the endpoint that should handle the request.

◆ extensions()

void caf::net::web_socket::handshake::extensions ( std::string value)

Sets a value for the optional Sec-WebSocket-Extensions field.

Parameters
valueA comma-separated list of values indicating which extensions the client would like to speak, ordered by preference.

◆ has_valid_key()

bool caf::net::web_socket::handshake::has_valid_key ( ) const
nodiscardnoexcept

Checks whether at least one bit in the key is one.

A default constructed header object fills the key with zeros.

◆ host()

void caf::net::web_socket::handshake::host ( std::string value)

Sets a value for the mandatory Host field.

Parameters
valueThe Internet host and port number of the resource being requested, as obtained from the original URI given by the user or referring resource.

◆ is_valid_http_1_response()

bool caf::net::web_socket::handshake::is_valid_http_1_response ( std::string_view http_response) const

Checks whether the http_response contains a HTTP 1.1 response to the generated HTTP GET request.

A valid response contains:

  • HTTP status code 101 (Switching Protocols).
  • An Upgrade field with the value websocket.
  • A Connection field with the value Upgrade.
  • A Sec-WebSocket-Accept field with the value response_key().

◆ origin()

void caf::net::web_socket::handshake::origin ( std::string value)

Sets a value for the optional Origin field.

Parameters
valueIndicates where the GET request originates from. Usually only sent by browser clients.

◆ protocols()

void caf::net::web_socket::handshake::protocols ( std::string value)

Sets a value for the optional Sec-WebSocket-Protocol field.

Parameters
valueA comma-separated list of values indicating which protocols the client would like to speak, ordered by preference.

◆ write_http_1_request()

void caf::net::web_socket::handshake::write_http_1_request ( byte_buffer & buf) const

Writes the HTTP 1.1 request message to buf.

Precondition
has_mandatory_fields()

◆ write_http_1_response()

void caf::net::web_socket::handshake::write_http_1_response ( byte_buffer & buf) const

Writes the HTTP 1.1 response message to buf.

Precondition
has_valid_key()

◆ write_response()

void caf::net::web_socket::handshake::write_response ( http::lower_layer::server * down) const

Writes the HTTP response message to down.

Precondition
has_valid_key()

Member Data Documentation

◆ max_http_size

uint32_t caf::net::web_socket::handshake::max_http_size = 2048
staticconstexpr

Maximum size for HTTP request and response messages.

A handshake should usually fit into 200-300 Bytes, so 2KB is more than enough.


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