Kurlyk
Loading...
Searching...
No Matches
WebSocketConfig.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_WEBSOCKET_CONFIG_HPP_INCLUDED
3#define _KURLYK_WEBSOCKET_CONFIG_HPP_INCLUDED
4
7
8namespace kurlyk {
9
13 public:
15 std::string url;
16 std::string user_agent;
17 std::string accept_encoding;
18 std::string cookie;
19 std::vector<std::string> protocols;
20 std::string cert_file;
21 std::string key_file;
22 std::string ca_file;
23 std::string proxy_server;
24 std::string proxy_auth;
26 long request_timeout = 20;
27 long idle_timeout = 0;
28 long reconnect_delay = 5;
30 bool reconnect = true;
31 bool verify_cert = true;
32
42
43 std::vector<RateLimitData> rate_limits;
44
49 void set_url(const std::string& host, const std::string& path, const std::string& query = "") {
50 url = host;
51 if (!path.empty() && path[0] != '/') {
52 url += "/";
53 }
54 url += path;
55 if (!query.empty()) {
56 url += (query[0] == '?' ? "" : "?") + query;
57 }
58 }
59
63 void set_url(const std::string& url, const QueryParams& query) {
64 this->url = url + (query.empty() ? "" : utils::to_query_string(query, "?"));
65 }
66
72 void set_accept_encoding(bool identity = false, bool deflate = false, bool gzip = false, bool brotli = false) {
73 std::string encodings;
74 if (identity) encodings += "identity";
75 if (deflate) encodings += (encodings.empty() ? "" : ",") + std::string("deflate");
76 if (gzip) encodings += (encodings.empty() ? "" : ",") + std::string("gzip");
77 if (brotli) encodings += (encodings.empty() ? "" : ",") + std::string("br");
78 accept_encoding = std::move(encodings);
79 }
80
84 void set_proxy(const std::string& ip, int port) {
85 proxy_server = ip + ":" + std::to_string(port);
86 }
87
92 void set_proxy(const std::string& ip, int port, ProxyType type) {
93 proxy_server = ip + ":" + std::to_string(port);
94 proxy_type = type;
95 }
96
99 void set_proxy_server(const std::string& server) {
100 proxy_server = server;
101 }
102
105 void set_proxy_auth(const std::string& auth) {
106 proxy_auth = auth;
107 }
108
112 proxy_type = type;
113 }
114
122 const std::string& ip,
123 int port,
124 const std::string& username,
125 const std::string& password,
127 set_proxy(ip, port);
128 set_proxy_auth(username, password);
129 proxy_type = type;
130 }
131
135 void set_proxy_auth(const std::string& username, const std::string& password) {
136 proxy_auth = username + ":" + password;
137 }
138
144 this->reconnect = reconnect;
145 this->reconnect_attempts = reconnect_attempts;
146 this->reconnect_delay = reconnect_delay;
147 }
148
151 void set_user_agent(const std::string& user_agent) {
152 this->user_agent = user_agent;
153 }
154
157 void set_accept_language(const std::string& accept_language) {
158 this->headers.emplace("Accept-Language", accept_language);
159 }
160
163 void set_cookie(const std::string& cookie) {
164 this->cookie = cookie;
165 }
166
170 this->idle_timeout = idle_timeout;
171 }
172
176 this->request_timeout = request_timeout;
177 }
178
181 void set_ca_file(const std::string& ca_file) {
182 this->ca_file = ca_file;
183 }
184
188 void set_ca_file(bool verify_cert, const std::string& ca_file) {
189 this->verify_cert = verify_cert;
190 this->ca_file = ca_file;
191 }
192
196 this->verify_cert = verify_cert;
197 }
198
208 long add_rate_limit(long requests_per_period, long period_ms) {
209 rate_limits.emplace_back(requests_per_period, period_ms);
210 return rate_limits.size() - 1;
211 }
212
216 long add_rate_limit_rpm(long requests_per_minute) {
217 long period_ms = 60000; // 1 minute in milliseconds
218 return add_rate_limit(requests_per_minute, period_ms);
219 }
220
224 long add_rate_limit_rps(long requests_per_second) {
225 long period_ms = 1000; // 1 second in milliseconds
226 return add_rate_limit(requests_per_second, period_ms);
227 }
228 };
229
230} // namespace kurlyk
231
232#endif // _KURLYK_WEBSOCKET_CONFIG_HPP_INCLUDED
Configuration parameters for establishing and managing WebSocket connections.
void set_cookie(const std::string &cookie)
Sets the cookie data.
void set_verify_cert(bool verify_cert)
Sets whether to verify the server’s certificate.
ProxyType proxy_type
Proxy type (e.g., HTTP, SOCKS5).
std::string accept_encoding
Accept-Encoding header.
long add_rate_limit_rpm(long requests_per_minute)
Adds a rate limit based on Requests Per Minute (RPM).
void set_reconnect(bool reconnect, long reconnect_attempts=0, long reconnect_delay=0)
Configures reconnection behavior.
std::string proxy_server
Proxy address in <ip:port> format.
void set_proxy(const std::string &ip, int port)
Sets the proxy server address.
long idle_timeout
Maximum idle time for the WebSocket connection in seconds (0 means no timeout).
std::string user_agent
User-Agent header.
std::string ca_file
Path to the Root CA certificate file.
void set_proxy_type(ProxyType type)
Sets the proxy type.
long reconnect_attempts
Number of reconnection attempts (0 means infinite attempts).
std::string url
URL of the WebSocket server.
void set_proxy_auth(const std::string &auth)
Sets the proxy authentication credentials.
std::vector< RateLimitData > rate_limits
List of rate limits applied to WebSocket messages.
std::string cert_file
Path to the client certificate file.
Headers headers
HTTP headers included in the WebSocket connection request.
void set_url(const std::string &host, const std::string &path, const std::string &query="")
Sets the WebSocket server URL with optional query parameters.
long add_rate_limit_rps(long requests_per_second)
Adds a rate limit based on Requests Per Second (RPS).
void set_proxy(const std::string &ip, int port, ProxyType type)
Sets the proxy server address.
void set_accept_language(const std::string &accept_language)
Sets the Accept-Language header.
std::string cookie
Cookie data as a string.
bool verify_cert
If true, verifies the server’s certificate and hostname according to RFC 2818.
void set_idle_timeout(long idle_timeout)
Configures the idle timeout for the WebSocket connection.
void set_proxy(const std::string &ip, int port, const std::string &username, const std::string &password, ProxyType type=ProxyType::PROXY_HTTP)
Sets the proxy server address with authentication.
std::string proxy_auth
Proxy authentication in <username:password> format.
void set_ca_file(const std::string &ca_file)
Sets the path to the CA certificate file.
void set_request_timeout(long request_timeout)
Sets the timeout for WebSocket requests.
long reconnect_delay
Delay in seconds between reconnection attempts.
void set_user_agent(const std::string &user_agent)
Sets the User-Agent header.
bool reconnect
Enables automatic reconnection if true.
long add_rate_limit(long requests_per_period, long period_ms)
Adds a rate limit configuration to control the frequency of WebSocket messages.
long request_timeout
Timeout for WebSocket requests in seconds (0 means no timeout).
std::string key_file
Path to the private key file corresponding to the client certificate.
void set_ca_file(bool verify_cert, const std::string &ca_file)
Sets certificate verification and sets the CA certificate file.
void set_proxy_auth(const std::string &username, const std::string &password)
Configures proxy authentication credentials.
void set_proxy_server(const std::string &server)
Sets the proxy server address.
std::vector< std::string > protocols
List of subprotocols for the Sec-WebSocket-Protocol header.
void set_accept_encoding(bool identity=false, bool deflate=false, bool gzip=false, bool brotli=false)
Sets the Accept-Encoding header with specified encodings.
void set_url(const std::string &url, const QueryParams &query)
Sets the WebSocket server URL with specified query parameters.
std::string to_query_string(const QueryParams &query, const std::string &prefix=std::string()) noexcept
Converts a map of query parameters into a URL query string.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
ProxyType
Enumeration of supported proxy types compatible with libcurl.
Definition enums.hpp:12
@ PROXY_HTTP
HTTP proxy.
Definition enums.hpp:13
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
RateLimitData(long requests_per_period=0, long period_ms=0)
long period_ms
Time period in milliseconds for the request limit.
long requests_per_period
Maximum number of requests allowed per period.