![]() |
Kurlyk
|
A client class for making HTTP requests to a specific host. More...
#include <HttpClient.hpp>
Public Member Functions | |
HttpClient () | |
Default constructor for HttpClient. | |
HttpClient (const std::string &host) | |
Constructs an HttpClient with the specified host. | |
HttpClient (const HttpClient &)=delete | |
void | operator= (const HttpClient &)=delete |
virtual | ~HttpClient () |
Destructor for HttpClient. | |
void | cancel_requests () |
Cancels the active request associated with this client and waits for its completion. | |
void | set_host (const std::string &host) |
Sets the host URL for the HTTP client. | |
void | set_headers (const kurlyk::Headers &headers) |
Sets the default headers for HTTP requests. | |
void | assign_rate_limit_id (long limit_id, RateLimitType type=RateLimitType::RL_GENERAL) |
Assigns an existing rate limit to the HTTP request. | |
void | set_rate_limit_id (long limit_id, RateLimitType type=RateLimitType::RL_GENERAL) |
Sets the rate limit ID for the HTTP request (alias for assign_rate_limit_id ). | |
void | set_rate_limit (long requests_per_period, long period_ms, RateLimitType type=RateLimitType::RL_GENERAL) |
Sets the rate limit for HTTP requests. | |
void | set_rate_limit_rpm (long requests_per_minute, RateLimitType type=RateLimitType::RL_GENERAL) |
Sets the rate limit based on requests per minute (RPM). | |
void | set_rate_limit_rps (long requests_per_second, RateLimitType type=RateLimitType::RL_GENERAL) |
Sets the rate limit based on requests per second (RPS). | |
void | set_accept_encoding (bool identity=false, bool deflate=false, bool gzip=false, bool brotli=false) |
Sets the Accept-Encoding header. | |
void | set_accept_encoding (const std::string &value) |
Sets a custom Accept-Encoding header value. | |
void | set_accept_language (const std::string &value) |
Sets the Accept-Language header value. | |
void | set_content_type (const std::string &value) |
Sets the Content-Type header value. | |
void | set_origin (const std::string &value) |
Sets the Origin header value. | |
void | set_referer (const std::string &value) |
Sets the Referer header value. | |
void | set_dnt (const bool value) |
Sets the Do Not Track (DNT) header value. | |
void | set_follow_location (bool value) |
Configures whether to follow redirects automatically. | |
void | set_auto_referer (bool value) |
Configures whether to automatically set the Referer header on redirects. | |
void | set_proxy_tunnel (bool value) |
Configures whether to use a tunneling proxy for HTTP requests. | |
void | set_head_only (bool value) |
Configures whether to send only the HTTP headers (HEAD request). | |
void | set_proxy (const std::string &ip, int port, ProxyType type=ProxyType::PROXY_HTTP) |
Sets the proxy server address. | |
void | set_proxy (const std::string &ip, const int port, const std::string &username, const std::string &password, ProxyType type=ProxyType::PROXY_HTTP) |
Sets the proxy server address with authentication details. | |
void | set_proxy_auth (const std::string &username, const std::string &password) |
Sets proxy authentication credentials. | |
void | set_proxy_server (const std::string &server) |
Sets the proxy server address. | |
void | set_proxy_auth (const std::string &auth) |
Sets the proxy authentication credentials. | |
void | set_proxy_type (ProxyType type) |
Sets the proxy type. | |
void | set_retry_attempts (long retry_attempts, long retry_delay_ms) |
Sets retry attempts and delay between retries for HTTP requests. | |
void | add_valid_status (long status) |
Adds a valid HTTP status code to the request. | |
void | set_valid_statuses (const std::set< long > &statuses) |
Replaces all valid HTTP status codes for the request. | |
void | clear_valid_statuses () |
Clears the set of valid HTTP status codes for the request. | |
void | set_user_agent (const std::string &user_agent) |
Sets the User-Agent header. | |
void | set_cookie (const std::string &cookie) |
Sets the cookie string for HTTP requests. | |
void | set_cert_file (const std::string &cert_file) |
Sets the client certificate file path. | |
void | set_ca_file (const std::string &ca_file) |
Sets the path to the CA certificate file. | |
void | set_timeout (long timeout) |
Sets the timeout duration for HTTP requests. | |
void | set_connect_timeout (long connect_timeout) |
Sets the connection timeout duration. | |
void | set_verbose (bool verbose) |
Enables or disables verbose output. | |
void | set_debug_header (bool debug_header) |
Enables or disables debug headers. | |
void | set_max_redirects (long max_redirects) |
Sets the maximum number of redirects for the client. | |
bool | request (const std::string &method, const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, HttpResponseCallback callback) |
Sends an HTTP request with the specified method, path, and parameters. | |
bool | request (const std::string &method, const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, long specific_rate_limit_id, HttpResponseCallback callback) |
Sends an HTTP request with the specified method, path, parameters, and specific rate limit ID. | |
bool | get (const std::string &path, const QueryParams &query, const Headers &headers, HttpResponseCallback callback) |
Sends a GET request. | |
bool | post (const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, HttpResponseCallback callback) |
Sends a POST request. | |
bool | get (const std::string &path, const QueryParams &query, const Headers &headers, long specific_rate_limit_id, HttpResponseCallback callback) |
Sends a GET request with a specific rate limit ID. | |
bool | post (const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, long specific_rate_limit_id, HttpResponseCallback callback) |
Sends a POST request with a specific rate limit ID. | |
std::future< HttpResponsePtr > | request (const std::string &method, const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content) |
Sends an HTTP request with a specified method, path, and parameters, and returns a future with the response. | |
std::future< HttpResponsePtr > | request (const std::string &method, const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, long specific_rate_limit_id) |
Sends an HTTP request with a specified method, path, specific rate limit ID and parameters, and returns a future with the response. | |
std::future< HttpResponsePtr > | get (const std::string &path, const QueryParams &query, const Headers &headers) |
Sends a GET request asynchronously and returns a future with the response. | |
std::future< HttpResponsePtr > | post (const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content) |
Sends a POST request asynchronously and returns a future with the response. | |
std::future< HttpResponsePtr > | get (const std::string &path, const QueryParams &query, const Headers &headers, long specific_rate_limit_id) |
Sends an asynchronous GET request with a specific rate limit ID and returns a future with the response. | |
std::future< HttpResponsePtr > | post (const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, long specific_rate_limit_id) |
Sends an asynchronous POST request with a specific rate limit ID and returns a future with the response. | |
Private Member Functions | |
bool | request (std::unique_ptr< HttpRequest > request_ptr, HttpResponseCallback callback) |
Adds the request to the request manager and notifies the worker to process it. | |
void | safe_submit_request (std::shared_ptr< std::promise< HttpResponsePtr > > promise, std::unique_ptr< HttpRequest > request_ptr, HttpResponseCallback callback) |
Submits a request and propagates any failure to the provided promise. | |
Static Private Member Functions | |
static void | safe_set_response (std::shared_ptr< std::promise< HttpResponsePtr > > promise, HttpResponsePtr response) |
Safely sets the response value on the given promise. | |
static void | ensure_initialized () |
Ensures that the network worker and request manager are initialized. | |
Private Attributes | |
HttpRequest | m_request |
The request object used for configuring and sending requests. | |
std::string | m_host |
The base host URL for the HTTP client. | |
bool | is_general_limit_owned = false |
Flag indicating if the client owns the general rate limit. | |
bool | is_specific_limit_owned = false |
Flag indicating if the client owns the specific rate limit. | |
A client class for making HTTP requests to a specific host.
This class provides methods to configure the client, including rate limiting, proxy settings, retry logic, and more.
Definition at line 13 of file HttpClient.hpp.
|
inline |
Default constructor for HttpClient.
Definition at line 17 of file HttpClient.hpp.
|
inline |
Constructs an HttpClient with the specified host.
host | The base host URL for the HTTP client. |
Definition at line 24 of file HttpClient.hpp.
|
delete |
|
inlinevirtual |
Destructor for HttpClient.
Definition at line 34 of file HttpClient.hpp.
|
inline |
Adds a valid HTTP status code to the request.
status | The HTTP status code to allow. |
Definition at line 303 of file HttpClient.hpp.
|
inline |
Assigns an existing rate limit to the HTTP request.
limit_id | The unique identifier of the rate limit to assign. |
type | Specifies the rate limit type (general or specific). |
Definition at line 88 of file HttpClient.hpp.
|
inline |
Cancels the active request associated with this client and waits for its completion.
Definition at line 47 of file HttpClient.hpp.
|
inline |
Clears the set of valid HTTP status codes for the request.
Definition at line 314 of file HttpClient.hpp.
|
inlinestaticprivate |
Ensures that the network worker and request manager are initialized.
Definition at line 710 of file HttpClient.hpp.
|
inline |
Sends a GET request asynchronously and returns a future with the response.
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
Definition at line 586 of file HttpClient.hpp.
|
inline |
Sends a GET request.
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
callback | The callback function to be called when the request is completed. |
Definition at line 442 of file HttpClient.hpp.
|
inline |
Sends an asynchronous GET request with a specific rate limit ID and returns a future with the response.
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
specific_rate_limit_id | The specific rate limit ID to be applied to this request. |
Definition at line 613 of file HttpClient.hpp.
|
inline |
Sends a GET request with a specific rate limit ID.
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
specific_rate_limit_id | The specific rate limit ID to be applied to this request. |
callback | The callback function to be called when the request is completed. |
Definition at line 473 of file HttpClient.hpp.
|
delete |
|
inline |
Sends a POST request asynchronously and returns a future with the response.
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
content | The request body content. |
Definition at line 599 of file HttpClient.hpp.
|
inline |
Sends a POST request.
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
content | The request body content. |
callback | The callback function to be called when the request is completed. |
Definition at line 457 of file HttpClient.hpp.
|
inline |
Sends an asynchronous POST request with a specific rate limit ID and returns a future with the response.
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
content | The request body content. |
specific_rate_limit_id | The specific rate limit ID to be applied to this request. |
Definition at line 628 of file HttpClient.hpp.
|
inline |
Sends a POST request with a specific rate limit ID.
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
content | The request body content. |
specific_rate_limit_id | The specific rate limit ID to be applied to this request. |
callback | The callback function to be called when the request is completed. |
Definition at line 490 of file HttpClient.hpp.
|
inline |
Sends an HTTP request with a specified method, path, and parameters, and returns a future with the response.
method | The HTTP method (e.g., "GET", "POST"). |
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
content | The request body content. |
Definition at line 507 of file HttpClient.hpp.
|
inline |
Sends an HTTP request with the specified method, path, and parameters.
method | The HTTP method (e.g., "GET", "POST"). |
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
content | The request body content. |
callback | The callback function to be called when the request is completed. |
Definition at line 380 of file HttpClient.hpp.
|
inline |
Sends an HTTP request with a specified method, path, specific rate limit ID and parameters, and returns a future with the response.
method | The HTTP method (e.g., "GET", "POST"). |
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
content | The request body content. |
specific_rate_limit_id | The specific rate limit ID to be applied to this request. |
Definition at line 544 of file HttpClient.hpp.
|
inline |
Sends an HTTP request with the specified method, path, parameters, and specific rate limit ID.
method | The HTTP method (e.g., "GET", "POST"). |
path | The URL path for the request. |
query | The query arguments. |
headers | The HTTP headers. |
content | The request body content. |
specific_rate_limit_id | The specific rate limit ID to be applied to this request. |
callback | The callback function to be called when the request is completed. |
Definition at line 408 of file HttpClient.hpp.
|
inlineprivate |
Adds the request to the request manager and notifies the worker to process it.
request_ptr | The HTTP request to be sent. |
callback | The callback function to be called when the request is completed. |
Definition at line 647 of file HttpClient.hpp.
|
inlinestaticprivate |
Safely sets the response value on the given promise.
promise | Promise that receives the HTTP response. |
response | Completed HTTP response to forward to the caller. |
Definition at line 658 of file HttpClient.hpp.
|
inlineprivate |
Submits a request and propagates any failure to the provided promise.
promise | Promise to signal upon success or failure. |
request_ptr | Prepared HTTP request to enqueue. |
callback | Callback executed when the request completes. |
Definition at line 681 of file HttpClient.hpp.
|
inline |
Sets the Accept-Encoding header.
identity | Enable identity encoding. |
deflate | Enable deflate encoding. |
gzip | Enable gzip encoding. |
brotli | Enable brotli encoding. |
Definition at line 172 of file HttpClient.hpp.
|
inline |
Sets a custom Accept-Encoding header value.
value | The custom value for the Accept-Encoding header. |
Definition at line 182 of file HttpClient.hpp.
|
inline |
Sets the Accept-Language header value.
value | The language value to be sent with the Accept-Language header. |
Definition at line 188 of file HttpClient.hpp.
|
inline |
Configures whether to automatically set the Referer header on redirects.
value | If true, enables automatically setting the Referer header during redirects. |
Definition at line 224 of file HttpClient.hpp.
|
inline |
Sets the path to the CA certificate file.
ca_file | Path to the CA certificate file. |
Definition at line 338 of file HttpClient.hpp.
|
inline |
Sets the client certificate file path.
cert_file | Path to the client certificate file. |
Definition at line 332 of file HttpClient.hpp.
|
inline |
Sets the connection timeout duration.
connect_timeout | Connection timeout in seconds. |
Definition at line 350 of file HttpClient.hpp.
|
inline |
Sets the Content-Type header value.
value | The MIME type for the Content-Type header. |
Definition at line 194 of file HttpClient.hpp.
|
inline |
Sets the cookie string for HTTP requests.
cookie | Cookie data as a single string. |
Definition at line 326 of file HttpClient.hpp.
|
inline |
Enables or disables debug headers.
debug_header | Enable (true) or disable (false) debug headers. |
Definition at line 362 of file HttpClient.hpp.
|
inline |
Sets the Do Not Track (DNT) header value.
value | If true, sets the DNT header to "1". |
Definition at line 212 of file HttpClient.hpp.
|
inline |
Configures whether to follow redirects automatically.
value | If true, enables following HTTP redirects. |
Definition at line 218 of file HttpClient.hpp.
|
inline |
Configures whether to send only the HTTP headers (HEAD request).
value | If true, the request will not download the response body (uses CURLOPT_NOBODY internally). Useful for measuring latency or checking resource availability without downloading content. |
Definition at line 237 of file HttpClient.hpp.
|
inline |
Sets the default headers for HTTP requests.
headers | The headers to be included with each request. |
Definition at line 81 of file HttpClient.hpp.
|
inline |
Sets the host URL for the HTTP client.
host | The base host URL for HTTP requests. |
Definition at line 75 of file HttpClient.hpp.
|
inline |
Sets the maximum number of redirects for the client.
max_redirects | The maximum number of redirects allowed. |
Definition at line 368 of file HttpClient.hpp.
|
inline |
Sets the Origin header value.
value | The origin to be sent with the Origin header. |
Definition at line 200 of file HttpClient.hpp.
|
inline |
Sets the proxy server address with authentication details.
ip | Proxy server IP address. |
port | Proxy server port. |
username | Proxy username. |
password | Proxy password. |
type | The type of proxy, defaulting to HTTP. |
Definition at line 258 of file HttpClient.hpp.
|
inline |
Sets the proxy server address.
ip | Proxy server IP address. |
port | Proxy server port. |
type | The type of proxy, defaulting to HTTP. |
Definition at line 245 of file HttpClient.hpp.
|
inline |
Sets the proxy authentication credentials.
auth | Proxy authentication in <username:password> format. |
Definition at line 284 of file HttpClient.hpp.
|
inline |
Sets proxy authentication credentials.
username | Proxy username. |
password | Proxy password. |
Definition at line 270 of file HttpClient.hpp.
|
inline |
Sets the proxy server address.
server | Proxy address in <ip:port> format. |
Definition at line 278 of file HttpClient.hpp.
|
inline |
Configures whether to use a tunneling proxy for HTTP requests.
value | If true, enables tunneling through the proxy server. Tunneling proxies are typically used for HTTPS requests to securely forward traffic. |
Definition at line 230 of file HttpClient.hpp.
|
inline |
|
inline |
Sets the rate limit for HTTP requests.
requests_per_period | The maximum number of requests allowed within the specified period. |
period_ms | The duration of the period in milliseconds. |
type | The type of rate limit (either general or specific). |
Definition at line 124 of file HttpClient.hpp.
|
inline |
Sets the rate limit ID for the HTTP request (alias for assign_rate_limit_id
).
limit_id | The unique identifier of the rate limit to assign. |
type | Specifies the rate limit type (general or specific). |
assign_rate_limit_id
. Definition at line 114 of file HttpClient.hpp.
|
inline |
Sets the rate limit based on requests per minute (RPM).
requests_per_minute | Maximum number of requests allowed per minute. |
type | The type of rate limit (either general or specific). |
Definition at line 150 of file HttpClient.hpp.
|
inline |
Sets the rate limit based on requests per second (RPS).
requests_per_second | Maximum number of requests allowed per second. |
type | The type of rate limit (either general or specific). |
Definition at line 160 of file HttpClient.hpp.
|
inline |
Sets the Referer header value.
value | The referer URL to be sent with the Referer header. |
Definition at line 206 of file HttpClient.hpp.
|
inline |
Sets retry attempts and delay between retries for HTTP requests.
retry_attempts | Number of retry attempts. |
retry_delay_ms | Delay in milliseconds between retry attempts. |
Definition at line 297 of file HttpClient.hpp.
|
inline |
Sets the timeout duration for HTTP requests.
timeout | Timeout duration in seconds. |
Definition at line 344 of file HttpClient.hpp.
|
inline |
Sets the User-Agent header.
user_agent | User-Agent string. |
Definition at line 320 of file HttpClient.hpp.
|
inline |
Replaces all valid HTTP status codes for the request.
statuses | The set of HTTP status codes to allow. |
Definition at line 309 of file HttpClient.hpp.
|
inline |
Enables or disables verbose output.
verbose | Enable (true) or disable (false) verbose output. |
Definition at line 356 of file HttpClient.hpp.
|
private |
Flag indicating if the client owns the general rate limit.
Definition at line 640 of file HttpClient.hpp.
|
private |
Flag indicating if the client owns the specific rate limit.
Definition at line 641 of file HttpClient.hpp.
|
private |
The base host URL for the HTTP client.
Definition at line 639 of file HttpClient.hpp.
|
private |
The request object used for configuring and sending requests.
Definition at line 638 of file HttpClient.hpp.