2#ifndef _KURLYK_HTTP_REQUEST_HANDLER_HPP_INCLUDED
3#define _KURLYK_HTTP_REQUEST_HANDLER_HPP_INCLUDED
20# if __cplusplus >= 201402L
47 size_t total_size = size * nmemb;
48 auto* buffer =
static_cast<std::string*
>(userdata);
50 buffer->append(data, total_size);
57 size_t buffer_size = size * nitems;
58 auto* headers =
static_cast<Headers*
>(userdata);
59 std::string key, value;
62 headers->emplace(key, value);
76 if (message->data.result == CURLE_OPERATION_TIMEDOUT) {
81 message->data.result != CURLE_OK) {
85 if (message->data.result != CURLE_OK) {
100 if (!retry_attempts ||
101 valid_statuses.count(
m_response->status_code) ||
102 retry_attempt >= retry_attempts) {
153 m_curl = curl_easy_init();
156 curl_easy_setopt(
m_curl, CURLOPT_URL, request->url.c_str());
157 curl_easy_setopt(
m_curl, CURLOPT_NOSIGNAL, 1L);
158 curl_easy_setopt(
m_curl, CURLOPT_CUSTOMREQUEST, request->method.c_str());
159 if (request->head_only) {
160 curl_easy_setopt(
m_curl, CURLOPT_NOBODY, 1L);
162 curl_easy_setopt(
m_curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
172 curl_easy_setopt(
m_curl, CURLOPT_PRIVATE,
this);
176 curl_easy_getinfo(
m_curl, CURLINFO_NAMELOOKUP_TIME, &
m_response->namelookup_time);
177 curl_easy_getinfo(
m_curl, CURLINFO_CONNECT_TIME, &
m_response->connect_time);
178 curl_easy_getinfo(
m_curl, CURLINFO_APPCONNECT_TIME, &
m_response->appconnect_time);
179 curl_easy_getinfo(
m_curl, CURLINFO_PRETRANSFER_TIME, &
m_response->pretransfer_time);
180 curl_easy_getinfo(
m_curl, CURLINFO_STARTTRANSFER_TIME, &
m_response->starttransfer_time);
190 curl_easy_setopt(
m_curl, CURLOPT_SSLKEY, request.
key_file.c_str());
192 if (!request.
ca_file.empty()) {
193 curl_easy_setopt(
m_curl, CURLOPT_CAINFO, request.
ca_file.c_str());
197 if (!request.
ca_path.empty()) {
198 curl_easy_setopt(
m_curl, CURLOPT_CAPATH, request.
ca_path.c_str());
200 curl_easy_setopt(
m_curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_MAX_DEFAULT);
231 for (
const auto& header : request.
headers) {
232 std::string header_line = header.first +
": " + header.second;
248 curl_easy_setopt(
m_curl, CURLOPT_PROXYTYPE, to_curl_proxy_type(request.
proxy_type));
252 curl_easy_setopt(
m_curl, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
259 if (!request.
cookie.empty() && request.
headers.count(
"Cookie") == 0) {
260 curl_easy_setopt(
m_curl, CURLOPT_COOKIE, request.
cookie.c_str());
265 curl_easy_setopt(
m_curl, CURLOPT_COOKIELIST,
"ALL");
277 curl_easy_setopt(
m_curl, CURLOPT_POSTFIELDS, request.
content.c_str());
278 curl_easy_setopt(
m_curl, CURLOPT_POSTFIELDSIZE,
static_cast<long>(request.
content.size()));
void fill_response_timings()
void cancel()
Marks the request as cancelled.
~HttpRequestHandler()
Destructor for HttpRequestHandler, handling cleanup of CURL and headers.
CURL * m_curl
CURL handle for the request.
HttpRequestHandler(std::unique_ptr< HttpRequestContext > context)
Constructs an HttpRequestHandler with the specified request context.
void set_proxy_options(const HttpRequest &request)
Configures proxy options if set in the request.
static size_t write_http_response_body(char *data, size_t size, size_t nmemb, void *userdata)
Processes the body data received from the server and appends it to the response content.
const char * get_ca_file_path() const
Gets the full path to the CA certificate file.
void set_ssl_options(const HttpRequest &request)
Sets SSL options such as cert, key, and CA file.
std::unique_ptr< HttpRequestContext > m_request_context
Context for the current request.
void set_cookie_options(const HttpRequest &request)
Sets cookie options if cookies are specified in the request.
static size_t parse_http_response_header(char *buffer, size_t size, size_t nitems, void *userdata)
Parses and stores response headers in the Headers container.
void init_curl()
Initializes CURL options for the request, setting headers, method, SSL, timeouts, and other parameter...
bool m_callback_called
Indicates if the callback was called.
bool handle_curl_message(CURLMsg *message)
Processes a CURL message and determines if a callback should be invoked.
std::string m_ca_file
Cached CA file path.
uint64_t get_request_id()
Retrieves the unique ID of the HTTP request.
struct curl_slist * m_headers
CURL headers list.
char m_error_buffer[CURL_ERROR_SIZE]
Buffer for CURL error messages.
CURL * get_curl() noexcept
Retrieves the CURL handle associated with this request.
std::unique_ptr< HttpRequestContext > get_request_context()
Returns the unique pointer to the HttpRequestContext object.
void set_request_options(const HttpRequest &request)
Sets general options such as headers, cookies, and proxy.
void set_request_body(const HttpRequest &request)
Sets request body content for applicable HTTP methods.
void set_custom_headers(const HttpRequest &request)
Appends custom headers to the request if provided.
std::unique_ptr< HttpResponse > m_response
Response object.
Represents an HTTP request.
bool proxy_tunnel
Enable proxy tunneling.
std::string user_agent
User-Agent header.
std::string proxy_auth
Proxy authentication in <username:password> format.
long timeout
Request timeout in seconds.
bool head_only
If true, sends the request without a response body (HEAD-like behavior).
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).
bool use_interface
Enable the specified network interface.
std::string interface_name
Network interface name to use for the request.
std::string content
Data payload for the request.
std::string cookie_file
Path to the cookie file; if empty, cookies are not saved.
std::string proxy_server
Proxy address in <ip:port> format.
std::string cookie
Cookie data as a string.
ProxyType proxy_type
Proxy type (e.g., HTTP, SOCKS5).
Headers headers
HTTP request headers.
bool auto_referer
Automatically set Referer header.
std::string accept_encoding
Accept-Encoding header.
std::string ca_file
Path to the CA certificate file.
std::string key_file
Path to the private key for the client certificate.
bool clear_cookie_file
Flag to clear the cookie file at the start of the request.
long max_redirects
Maximum allowed redirects.
std::string ca_path
Path to a directory containing CA certificates.
std::string method
HTTP request method (e.g., "GET", "POST").
Represents the response received from an HTTP request, including headers, content,...
bool case_insensitive_equal(const std::string &str1, const std::string &str2) noexcept
Compares two strings case-insensitively.
@ AbortedDuringDestruction
Request handler was destroyed before completion, causing the request to abort.
@ CancelledByUser
Request was cancelled explicitly by the user via cancel().
void parse_http_header_pair(const char *buffer, const size_t &size, std::string &key, std::string &value)
Parses a header pair from a buffer.
std::error_code make_http_error(int status_code)
Creates an std::error_code from an HTTP status code.
std::error_code make_error_code(ClientError e)
Creates a std::error_code from a ClientError value.
std::string get_exec_dir()
Retrieves the directory of the executable file.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
Enables use of ClientError with std::error_code.