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;
20 std::string url;
21 std::string method = "GET";
22 std::string content;
23 std::string user_agent;
24 std::string accept_encoding;
25 std::string cookie_file;
26 std::string cookie;
27 std::string cert_file;
28 std::string key_file;
29 std::string ca_file;
30 std::string ca_path;
31 std::string proxy_server;
32 std::string proxy_auth;
34 bool proxy_tunnel = true;
35 std::string interface_name;
36 bool use_interface = false;
37
38 bool follow_location = true;
39 long max_redirects = 10;
40 bool auto_referer = false;
41 bool head_only = false;
42
43 long timeout = 30;
44 long connect_timeout = 10;
47 std::set<long> valid_statuses = {200};
48 long retry_attempts = 0;
49 long retry_delay_ms = 0;
50
51 bool clear_cookie_file = false;
52
53 // Debug parameters
54 bool verbose = false;
55 bool debug_header = false;
56
61 void set_url(
62 const std::string& host,
63 const std::string& path,
64 const std::string& query = std::string()) {
65 url = host;
66 if (!path.empty() && path[0] != '/') {
67 url += "/";
68 }
69 url += path;
70 if (!query.empty()) {
71 if (query[0] != '?') url += "?";
72 url += query;
73 }
74 }
75
80 void set_url(
81 const std::string& host,
82 const std::string& path,
83 const QueryParams& query) {
84 const std::string query_str = utils::to_query_string(query, "?");
85 set_url(host, path, query_str);
86 }
87
91 void set_url(const std::string& url, const QueryParams& query) {
92 const std::string args_str = utils::to_query_string(query, "?");
93 this->url = url;
94 if (!query.empty()) {
95 this->url += args_str;
96 }
97 }
98
105 bool identity = false,
106 bool deflate = false,
107 bool gzip = false,
108 bool brotli = false) {
109 std::string encodings;
110 if (identity) encodings += "identity";
111 if (deflate) encodings += (encodings.empty() ? "" : ",") + std::string("deflate");
112 if (gzip) encodings += (encodings.empty() ? "" : ",") + std::string("gzip");
113 if (brotli) encodings += (encodings.empty() ? "" : ",") + std::string("br");
114
115 accept_encoding = std::move(encodings);
116 }
117
120 void set_accept_language(const std::string& value) {
121 headers.emplace("Accept-Language", value);
122 }
123
126 void set_content_type(const std::string& value) {
127 headers.emplace("Content-Type", value);
128 }
129
132 void set_origin(const std::string& value) {
133 headers.emplace("Origin", value);
134 }
135
138 void set_referer(const std::string& value) {
139 headers.emplace("Referer", value);
140 }
141
146 const std::string& ip,
147 int port) {
148 proxy_server = ip + ":" + std::to_string(port);
149 }
150
156 const std::string& ip,
157 int port,
158 ProxyType type) {
159 proxy_server = ip + ":" + std::to_string(port);
160 proxy_type = type;
161 }
162
170 const std::string& ip,
171 int port,
172 const std::string& username,
173 const std::string& password,
175 set_proxy(ip, port);
176 set_proxy_auth(username, password);
177 proxy_type = type;
178 }
179
182 void set_proxy_server(const std::string& server) {
183 proxy_server = server;
184 }
185
188 void set_proxy_auth(const std::string& auth) {
189 proxy_auth = auth;
190 }
191
195 proxy_type = type;
196 }
197
202 const std::string& username,
203 const std::string& password) {
204 proxy_auth = username + ":" + password;
205 }
206
211 this->retry_attempts = retry_attempts;
212 this->retry_delay_ms = retry_delay_ms;
213 }
214
217 void add_valid_status(long status) {
218 valid_statuses.insert(status);
219 }
220
223 void set_valid_statuses(const std::set<long>& statuses) {
224 valid_statuses = statuses;
225 }
226
229 valid_statuses.clear();
230 }
231
234 void set_user_agent(const std::string& user_agent) {
235 this->user_agent = user_agent;
236 }
237
240 void set_cookie(const std::string& cookie) {
241 this->cookie = cookie;
242 }
243
246 void set_cert_file(const std::string& cert_file) {
247 this->cert_file = cert_file;
248 }
249
252 void set_ca_file(const std::string& ca_file) {
253 this->ca_file = ca_file;
254 }
255
258 void set_timeout(long timeout) {
259 this->timeout = timeout;
260 }
261
265 this->connect_timeout = connect_timeout;
266 }
267
270 void set_verbose(bool verbose) {
271 this->verbose = verbose;
272 }
273
277 this->debug_header = debug_header;
278 }
279
280 }; // HttpRequest
281
283 using HttpRequestPtr = std::unique_ptr<HttpRequest>;
284
285}; // namespace kurlyk
286
287#endif // _KURLYK_HTTP_REQUEST_HPP_INCLUDED
Represents an HTTP request.
bool proxy_tunnel
Enable proxy tunneling.
std::string user_agent
User-Agent header.
long specific_rate_limit_id
ID for specific rate limiting.
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, sends the request without a 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 host, path, and optional query parameters as a dictionary.
void set_valid_statuses(const std::set< long > &statuses)
Sets the valid HTTP status codes, replacing any existing values.
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.
long general_rate_limit_id
ID for general rate limiting.
uint64_t request_id
Unique identifier for the request (default is 0).
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_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.
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 host, 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.
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
A unique pointer to an HttpRequest object for memory management.
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.