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 std::size_t max_send_queue_size = 0;
31 bool reconnect = true;
32 bool verify_cert = true;
33
43
44 std::vector<RateLimitData> rate_limits;
45
50 void set_url(const std::string& host, const std::string& path, const std::string& query = "") {
51 url = host;
52 if (!path.empty() && path[0] != '/') {
53 url += "/";
54 }
55 url += path;
56 if (!query.empty()) {
57 url += (query[0] == '?' ? "" : "?") + query;
58 }
59 }
60
64 void set_url(const std::string& url, const QueryParams& query) {
65 this->url = url + (query.empty() ? "" : utils::to_query_string(query, "?"));
66 }
67
73 void set_accept_encoding(bool identity = false, bool deflate = false, bool gzip = false, bool brotli = false) {
74 std::string encodings;
75 if (identity) encodings += "identity";
76 if (deflate) encodings += (encodings.empty() ? "" : ",") + std::string("deflate");
77 if (gzip) encodings += (encodings.empty() ? "" : ",") + std::string("gzip");
78 if (brotli) encodings += (encodings.empty() ? "" : ",") + std::string("br");
79 accept_encoding = std::move(encodings);
80 }
81
85 void set_proxy(const std::string& ip, int port) {
86 proxy_server = ip + ":" + std::to_string(port);
87 }
88
93 void set_proxy(const std::string& ip, int port, ProxyType type) {
94 proxy_server = ip + ":" + std::to_string(port);
95 proxy_type = type;
96 }
97
100 void set_proxy_server(const std::string& server) {
101 proxy_server = server;
102 }
103
106 void set_proxy_auth(const std::string& auth) {
107 proxy_auth = auth;
108 }
109
113 proxy_type = type;
114 }
115
123 const std::string& ip,
124 int port,
125 const std::string& username,
126 const std::string& password,
128 set_proxy(ip, port);
129 set_proxy_auth(username, password);
130 proxy_type = type;
131 }
132
136 void set_proxy_auth(const std::string& username, const std::string& password) {
137 proxy_auth = username + ":" + password;
138 }
139
145 this->reconnect = reconnect;
146 this->reconnect_attempts = reconnect_attempts;
147 this->reconnect_delay = reconnect_delay;
148 }
149
152 void set_user_agent(const std::string& user_agent) {
153 this->user_agent = user_agent;
154 }
155
158 void set_accept_language(const std::string& accept_language) {
159 this->headers.emplace("Accept-Language", accept_language);
160 }
161
164 void set_cookie(const std::string& cookie) {
165 this->cookie = cookie;
166 }
167
171 this->idle_timeout = idle_timeout;
172 }
173
177 this->request_timeout = request_timeout;
178 }
179
183 this->max_send_queue_size = max_send_queue_size;
184 }
185
188 void set_ca_file(const std::string& ca_file) {
189 this->ca_file = ca_file;
190 }
191
195 void set_ca_file(bool verify_cert, const std::string& ca_file) {
196 this->verify_cert = verify_cert;
197 this->ca_file = ca_file;
198 }
199
203 this->verify_cert = verify_cert;
204 }
205
215 long add_rate_limit(long requests_per_period, long period_ms) {
216 rate_limits.emplace_back(requests_per_period, period_ms);
217 return rate_limits.size() - 1;
218 }
219
223 long add_rate_limit_rpm(long requests_per_minute) {
224 long period_ms = 60000; // 1 minute in milliseconds
225 return add_rate_limit(requests_per_minute, period_ms);
226 }
227
231 long add_rate_limit_rps(long requests_per_second) {
232 long period_ms = 1000; // 1 second in milliseconds
233 return add_rate_limit(requests_per_second, period_ms);
234 }
235 };
236
237} // namespace kurlyk
238
239#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.
std::size_t max_send_queue_size
Maximum number of queued outbound send operations, or zero if unbounded.
void set_max_send_queue_size(std::size_t max_send_queue_size)
Sets the maximum number of outbound send operations queued for this client.
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.