Kurlyk
Loading...
Searching...
No Matches
HttpRequest.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_HTTP_REQUEST_HPP_INCLUDED
3#define _KURLYK_HTTP_REQUEST_HPP_INCLUDED
4
7
8namespace kurlyk {
9
17 public:
18 uint64_t request_id = 0;
19 uint64_t group_id = 0;
21 std::string url;
22 std::string method = "GET";
23 std::string content;
24 std::string user_agent;
25 std::string accept_encoding;
26 std::string cookie_file;
27 std::string cookie;
28 std::string cert_file;
29 std::string key_file;
30 std::string ca_file;
31 std::string ca_path;
32 std::string proxy_server;
33 std::string proxy_auth;
35 bool proxy_tunnel = true;
36 std::string interface_name;
37 bool use_interface = false;
38
39 bool follow_location = true;
40 long max_redirects = 10;
41 bool auto_referer = false;
42 bool head_only = false;
43 bool streaming = false;
44
45 long timeout = 30;
46 long connect_timeout = 10;
51 std::set<long> valid_statuses = {200};
52 long retry_attempts = 0;
53 long retry_delay_ms = 0;
54
55 bool clear_cookie_file = false;
56
57 // Debug parameters
58 bool verbose = false;
59 bool debug_header = false;
60
65 void set_url(
66 const std::string& host,
67 const std::string& path,
68 const std::string& query = std::string()) {
69 url = host;
70 if (!path.empty() && path[0] != '/') {
71 url += "/";
72 }
73 url += path;
74 if (!query.empty()) {
75 if (query[0] != '?') url += "?";
76 url += query;
77 }
78 }
79
84 void set_url(
85 const std::string& host,
86 const std::string& path,
87 const QueryParams& query) {
88 const std::string query_str = utils::to_query_string(query, "?");
89 set_url(host, path, query_str);
90 }
91
95 void set_url(const std::string& url, const QueryParams& query) {
96 const std::string args_str = utils::to_query_string(query, "?");
97 this->url = url;
98 if (!query.empty()) {
99 this->url += args_str;
100 }
101 }
102
109 bool identity = false,
110 bool deflate = false,
111 bool gzip = false,
112 bool brotli = false) {
113 std::string encodings;
114 if (identity) encodings += "identity";
115 if (deflate) encodings += (encodings.empty() ? "" : ",") + std::string("deflate");
116 if (gzip) encodings += (encodings.empty() ? "" : ",") + std::string("gzip");
117 if (brotli) encodings += (encodings.empty() ? "" : ",") + std::string("br");
118
119 accept_encoding = std::move(encodings);
120 }
121
124 void set_accept_language(const std::string& value) {
125 headers.emplace("Accept-Language", value);
126 }
127
130 void set_content_type(const std::string& value) {
131 headers.emplace("Content-Type", value);
132 }
133
136 void set_origin(const std::string& value) {
137 headers.emplace("Origin", value);
138 }
139
142 void set_referer(const std::string& value) {
143 headers.emplace("Referer", value);
144 }
145
150 const std::string& ip,
151 int port) {
152 proxy_server = ip + ":" + std::to_string(port);
153 }
154
160 const std::string& ip,
161 int port,
162 ProxyType type) {
163 proxy_server = ip + ":" + std::to_string(port);
164 proxy_type = type;
165 }
166
174 const std::string& ip,
175 int port,
176 const std::string& username,
177 const std::string& password,
179 set_proxy(ip, port);
180 set_proxy_auth(username, password);
181 proxy_type = type;
182 }
183
186 void set_proxy_server(const std::string& server) {
187 proxy_server = server;
188 }
189
192 void set_proxy_auth(const std::string& auth) {
193 proxy_auth = auth;
194 }
195
199 proxy_type = type;
200 }
201
206 const std::string& username,
207 const std::string& password) {
208 proxy_auth = username + ":" + password;
209 }
210
215 this->retry_attempts = retry_attempts;
216 this->retry_delay_ms = retry_delay_ms;
217 }
218
221 void add_valid_status(long status) {
222 valid_statuses.insert(status);
223 }
224
227 void set_valid_statuses(const std::set<long>& statuses) {
228 valid_statuses = statuses;
229 }
230
233 valid_statuses.clear();
234 }
235
238 void set_user_agent(const std::string& user_agent) {
239 this->user_agent = user_agent;
240 }
241
244 void set_cookie(const std::string& cookie) {
245 this->cookie = cookie;
246 }
247
250 void set_cert_file(const std::string& cert_file) {
251 this->cert_file = cert_file;
252 }
253
256 void set_ca_file(const std::string& ca_file) {
257 this->ca_file = ca_file;
258 }
259
262 void set_timeout(long timeout) {
263 this->timeout = timeout;
264 }
265
269 this->connect_timeout = connect_timeout;
270 }
271
275 this->streaming = streaming;
276 }
277
280 void set_verbose(bool verbose) {
281 this->verbose = verbose;
282 }
283
287 this->debug_header = debug_header;
288 }
289
290 }; // HttpRequest
291
293 using HttpRequestPtr = std::unique_ptr<HttpRequest>;
294
295}; // namespace kurlyk
296
297#endif // _KURLYK_HTTP_REQUEST_HPP_INCLUDED
Represents an HTTP request configuration.
bool proxy_tunnel
Enable proxy tunneling.
std::string user_agent
User-Agent header.
std::string proxy_auth
Proxy authentication in <username:password> format.
void set_proxy_type(ProxyType type)
Sets the proxy type.
long timeout
Request timeout in seconds.
void set_verbose(bool verbose)
Enables or disables verbose mode.
bool head_only
If true, does not download the response body (HEAD-like behavior).
void set_cert_file(const std::string &cert_file)
Sets the client certificate file path.
void set_proxy_auth(const std::string &auth)
Sets the proxy authentication credentials.
bool follow_location
Automatically follow HTTP redirects.
bool debug_header
Include headers in debug output (CURLOPT_HEADER).
long connect_timeout
Connection timeout in seconds.
std::string cert_file
Path to the client certificate file.
bool verbose
Enable verbose output (CURLOPT_VERBOSE).
void set_retry_attempts(long retry_attempts, long retry_delay_ms)
Sets the number of retry attempts and delay before retrying.
void set_proxy_server(const std::string &server)
Sets the proxy server address.
bool use_interface
Enable the specified network interface.
void set_referer(const std::string &value)
Sets the Referer header value.
void set_ca_file(const std::string &ca_file)
Sets the path to the CA certificate file.
std::string url
Full request URL.
std::string interface_name
Network interface name to use for the request.
void set_timeout(long timeout)
Sets the request timeout.
std::string content
Data payload for the request.
std::string cookie_file
Path to the cookie file; if empty, cookies are not saved.
void set_proxy(const std::string &ip, int port, ProxyType type)
Sets the proxy server address.
void set_proxy_auth(const std::string &username, const std::string &password)
Sets proxy authentication credentials.
void set_url(const std::string &host, const std::string &path, const QueryParams &query)
Sets the request URL with base URL, path, and query parameters.
void set_valid_statuses(const std::set< long > &statuses)
Sets the valid HTTP status codes, replacing any existing values.
bool streaming
Enable intermediate callbacks for response body chunks.
std::string proxy_server
Proxy address in <ip:port> format.
std::string cookie
Cookie data as a string.
void set_cookie(const std::string &cookie)
Sets the cookie data.
uint64_t request_id
Unique ID of this concrete HTTP request.
long retry_delay_ms
Delay between retry attempts in milliseconds.
ProxyType proxy_type
Proxy type (e.g., HTTP, SOCKS5).
Headers headers
HTTP request headers.
void set_connect_timeout(long connect_timeout)
Sets the connection timeout.
void set_streaming(bool streaming)
Enables or disables intermediate callbacks for response body chunks.
void set_url(const std::string &url, const QueryParams &query)
Sets the request URL and appends optional query parameters.
void set_proxy(const std::string &ip, int port)
Sets the proxy server address.
bool auto_referer
Automatically set Referer header.
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 details.
void clear_valid_statuses()
Clears all valid status codes.
std::string general_rate_limit_key
Key used to separate general rate limit state; empty means default shared state.
std::string specific_rate_limit_key
Key used to separate specific rate limit state; empty means default shared state.
void set_content_type(const std::string &value)
Sets the Content-Type header value.
void set_accept_encoding(bool identity=false, bool deflate=false, bool gzip=false, bool brotli=false)
Sets the Accept-Encoding header with optional encoding types.
std::string accept_encoding
Accept-Encoding header.
void set_accept_language(const std::string &value)
Sets the Accept-Language header value.
std::string ca_file
Path to the CA certificate file.
long retry_attempts
Number of retry attempts in case of failure.
std::string key_file
Path to the private key for the client certificate.
void set_origin(const std::string &value)
Sets the Origin header value.
bool clear_cookie_file
Flag to clear the cookie file at the start of the request.
void set_url(const std::string &host, const std::string &path, const std::string &query=std::string())
Sets the request URL with base URL, path, and optional query parameters.
long max_redirects
Maximum allowed redirects.
std::string ca_path
Path to a directory containing CA certificates.
std::set< long > valid_statuses
Set of valid HTTP response status codes.
std::string method
HTTP request method (e.g., "GET", "POST").
void set_debug_header(bool debug_header)
Enables or disables debugging headers in output.
void set_user_agent(const std::string &user_agent)
Sets the User-Agent header.
HttpRateLimitHandlePtr general_rate_limit
General rate limit handle.
uint64_t group_id
ID shared by related requests, for example all requests created by one HttpClient.
HttpRateLimitHandlePtr specific_rate_limit
Specific rate limit handle.
void add_valid_status(long status)
Adds a single valid HTTP status code.
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
std::unique_ptr< HttpRequest > HttpRequestPtr
Owning pointer to an HTTP request.
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
std::shared_ptr< HttpRateLimitHandle > HttpRateLimitHandlePtr
Shared RAII handle for HTTP rate limits.
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.