Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
WebSocket.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <openssl/sha.h>
4#include <openssl/evp.h>
5
6namespace http {
7namespace websocket {
8
12class Handshake {
13public:
19 static std::string generate_accept_key(const std::string& client_key) {
20 const std::string magic_string = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
21 std::string combined = client_key + magic_string;
22
23 unsigned char hash[SHA_DIGEST_LENGTH];
24 SHA1(reinterpret_cast<const unsigned char*>(combined.c_str()), combined.length(), hash);
25
26 // Base64 encode using OpenSSL EVP
27 int expected_len = 4 * ((SHA_DIGEST_LENGTH + 2) / 3);
28 std::string base64_key(static_cast<size_t>(expected_len), '\0');
29
30 int encoded_len = EVP_EncodeBlock(
31 reinterpret_cast<unsigned char*>(&base64_key[0]),
32 hash,
33 SHA_DIGEST_LENGTH
34 );
35
36 base64_key.resize(static_cast<size_t>(encoded_len));
37 return base64_key;
38 }
39};
40
41} // namespace websocket
42} // namespace http
Utility class for WebSocket handshakes.
Definition WebSocket.hpp:12
static std::string generate_accept_key(const std::string &client_key)
Generates the Sec-WebSocket-Accept key for the WebSocket handshake.
Definition WebSocket.hpp:19
Definition Http2Session.hpp:21
Definition EventRouter.hpp:13