|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
Implements WebSocket HTTP/1.1 upgrade handshake (RFC 6455). More...
#include <websocket_handshake.h>

Static Public Member Functions | |
| static auto | create_client_handshake (std::string_view host, std::string_view path, uint16_t port, const std::map< std::string, std::string > &extra_headers={}) -> std::string |
| Creates a WebSocket handshake request (client-side). | |
| static auto | validate_server_response (const std::string &response, const std::string &expected_key) -> ws_handshake_result |
| Validates a WebSocket handshake response (client-side). | |
| static auto | parse_client_request (const std::string &request) -> ws_handshake_result |
| Parses a WebSocket handshake request (server-side). | |
| static auto | create_server_response (const std::string &client_key) -> std::string |
| Creates a WebSocket handshake response (server-side). | |
| static auto | generate_websocket_key () -> std::string |
| Generates a random Sec-WebSocket-Key (client-side). | |
| static auto | calculate_accept_key (const std::string &client_key) -> std::string |
| Calculates Sec-WebSocket-Accept from client key. | |
Static Private Member Functions | |
| static auto | parse_headers (const std::string &http_message) -> std::map< std::string, std::string > |
| Parses HTTP headers from a request or response. | |
| static auto | extract_status_code (const std::string &response) -> int |
| Extracts the status code from an HTTP response. | |
| static auto | base64_encode (const std::vector< uint8_t > &data) -> std::string |
| Encodes binary data to Base64. | |
| static auto | sha1_hash (const std::string &data) -> std::vector< uint8_t > |
| Computes SHA-1 hash of input data. | |
Implements WebSocket HTTP/1.1 upgrade handshake (RFC 6455).
This class provides static methods for creating and validating WebSocket handshake requests and responses according to RFC 6455 Section 4.
Definition at line 36 of file websocket_handshake.h.
|
staticprivate |
Encodes binary data to Base64.
Standard Base64 encoding without padding removal.
| data | The binary data to encode |
Definition at line 52 of file websocket_handshake.cpp.
|
static |
Calculates Sec-WebSocket-Accept from client key.
Computes the accept value by concatenating the client key with the WebSocket GUID, hashing with SHA-1, and encoding as Base64.
| client_key | The Sec-WebSocket-Key from the client |
Definition at line 110 of file websocket_handshake.cpp.
|
static |
Creates a WebSocket handshake request (client-side).
Generates an HTTP/1.1 upgrade request with all required WebSocket headers including a randomly generated Sec-WebSocket-Key.
| host | The target host (for Host header) |
| path | The request path (default: "/") |
| port | The target port (for Host header if non-standard) |
| extra_headers | Additional headers to include in the request |
Definition at line 183 of file websocket_handshake.cpp.
Referenced by kcenon::network::internal::websocket_socket::async_handshake().

|
static |
Creates a WebSocket handshake response (server-side).
Generates an HTTP/1.1 101 Switching Protocols response with the computed Sec-WebSocket-Accept header.
| client_key | The Sec-WebSocket-Key from the client's request |
Definition at line 324 of file websocket_handshake.cpp.
Referenced by kcenon::network::internal::websocket_socket::async_accept().

|
staticprivate |
Extracts the status code from an HTTP response.
Parses the HTTP status line to extract the numeric status code.
| response | The complete HTTP response |
Definition at line 163 of file websocket_handshake.cpp.
|
static |
Generates a random Sec-WebSocket-Key (client-side).
Creates a Base64-encoded random 16-byte value for use in the client handshake request.
Definition at line 94 of file websocket_handshake.cpp.
|
static |
Parses a WebSocket handshake request (server-side).
Validates that the client's request is a valid WebSocket upgrade request with all required headers.
| request | The complete HTTP request from the client |
Definition at line 272 of file websocket_handshake.cpp.
References kcenon::network::internal::ws_handshake_result::error_message, kcenon::network::internal::ws_handshake_result::headers, and kcenon::network::internal::ws_handshake_result::success.
Referenced by kcenon::network::internal::websocket_socket::async_accept().

|
staticprivate |
Parses HTTP headers from a request or response.
Extracts headers from the HTTP header section. Header names are converted to lowercase for case-insensitive lookup.
| http_message | The complete HTTP message |
Definition at line 124 of file websocket_handshake.cpp.
|
staticprivate |
Computes SHA-1 hash of input data.
Uses OpenSSL to compute the SHA-1 hash.
| data | The input data to hash |
Definition at line 85 of file websocket_handshake.cpp.
|
static |
Validates a WebSocket handshake response (client-side).
Verifies that the server's response is a valid WebSocket upgrade response with the correct Sec-WebSocket-Accept value.
| response | The complete HTTP response from the server |
| expected_key | The Sec-WebSocket-Key that was sent in the request |
Definition at line 218 of file websocket_handshake.cpp.
References kcenon::network::internal::ws_handshake_result::error_message, kcenon::network::internal::ws_handshake_result::headers, and kcenon::network::internal::ws_handshake_result::success.
Referenced by kcenon::network::internal::websocket_socket::async_handshake().
