Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
websocket_handshake.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include <cstdint>
8#include <map>
9#include <string>
10#include <string_view>
11#include <vector>
12
14{
23 {
24 bool success;
25 std::string error_message;
26 std::map<std::string, std::string> headers;
27 };
28
37 {
38 public:
51 static auto create_client_handshake(
52 std::string_view host, std::string_view path, uint16_t port,
53 const std::map<std::string, std::string>& extra_headers = {})
54 -> std::string;
55
66 static auto validate_server_response(const std::string& response,
67 const std::string& expected_key)
69
79 static auto parse_client_request(const std::string& request)
81
91 static auto create_server_response(const std::string& client_key)
92 -> std::string;
93
102 static auto generate_websocket_key() -> std::string;
103
113 static auto calculate_accept_key(const std::string& client_key)
114 -> std::string;
115
116 private:
126 static auto parse_headers(const std::string& http_message)
127 -> std::map<std::string, std::string>;
128
137 static auto extract_status_code(const std::string& response) -> int;
138
147 static auto base64_encode(const std::vector<uint8_t>& data)
148 -> std::string;
149
158 static auto sha1_hash(const std::string& data) -> std::vector<uint8_t>;
159 };
160
161} // namespace kcenon::network::internal
Implements WebSocket HTTP/1.1 upgrade handshake (RFC 6455).
static auto generate_websocket_key() -> std::string
Generates a random Sec-WebSocket-Key (client-side).
static auto base64_encode(const std::vector< uint8_t > &data) -> std::string
Encodes binary data to Base64.
static auto extract_status_code(const std::string &response) -> int
Extracts the status code from an HTTP response.
static auto create_server_response(const std::string &client_key) -> std::string
Creates a WebSocket handshake response (server-side).
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_headers(const std::string &http_message) -> std::map< std::string, std::string >
Parses HTTP headers from a request or response.
static auto sha1_hash(const std::string &data) -> std::vector< uint8_t >
Computes SHA-1 hash of input data.
static auto parse_client_request(const std::string &request) -> ws_handshake_result
Parses a WebSocket handshake request (server-side).
static auto calculate_accept_key(const std::string &client_key) -> std::string
Calculates Sec-WebSocket-Accept from client key.
Result of a WebSocket handshake operation.
bool success
Whether the handshake was successful.
std::map< std::string, std::string > headers
Parsed HTTP headers.
std::string error_message
Error message if success is false.