Kurlyk
Loading...
Searching...
No Matches
kurlyk::utils Namespace Reference

Classes

class  CaseInsensitiveEqual
 Functor for case-insensitive string comparison. More...
 
class  CaseInsensitiveHash
 Functor for generating case-insensitive hash values for strings. More...
 
class  ClientErrorCategory
 Custom std::error_category for reporting internal client errors not tied to specific protocols (e.g., CURL, HTTP). More...
 
class  CurlErrorCategory
 Represents a custom error category for CURL errors, enabling integration with std::error_code. More...
 
class  EventQueue
 A thread-safe event queue that supports blocking and non-blocking event retrieval. More...
 
class  HttpErrorCategory
 Custom error category that maps HTTP status codes (e.g., 404, 500) to human-readable error messages. More...
 
class  ThreadSafePrintStream
 Thread-safe stream class for printing to console from multiple threads. More...
 
class  WebSocketErrorCategory
 Error category class for WebSocketError enumeration. More...
 

Typedefs

using CaseInsensitiveMultimap = std::unordered_multimap<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual>
 A case-insensitive unordered multimap for storing HTTP headers.
 
using CaseInsensitiveCookieMultimap = std::unordered_multimap<std::string, Cookie, CaseInsensitiveHash, CaseInsensitiveEqual>
 A case-insensitive unordered multimap specifically for storing HTTP cookies.
 

Enumerations

enum class  ClientError {
  CancelledByUser = 1 , AbortedDuringDestruction , ClientNotInitialized , InvalidConfiguration ,
  NotConnected
}
 Defines errors related to the internal state or lifecycle of the HTTP/WebSocket client. More...
 
enum class  WebSocketError {
  ConnectionFailed = 1 , UnexpectedClose , ProtocolViolation , UnsupportedDataType ,
  InvalidCloseCode , CompressionError
}
 Represents protocol-level WebSocket errors. More...
 

Functions

bool case_insensitive_equal (const std::string &str1, const std::string &str2) noexcept
 Compares two strings case-insensitively.
 
const std::error_category & client_error_category ()
 Returns the singleton instance of the ClientErrorCategory.
 
std::error_code make_error_code (ClientError e)
 Creates a std::error_code from a ClientError value.
 
std::error_code make_error_code (CURLcode e)
 Creates an std::error_code from a CURLcode, allowing CURL errors to be used with the std::error_code API.
 
bool is_valid_email_id (const std::string &str)
 Validates an email address format.
 
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::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.
 
std::string to_cookie_string (const CaseInsensitiveMultimap &cookies)
 Converts a CaseInsensitiveMultimap to a string format suitable for HTTP Cookie headers.
 
std::string to_cookie_string (const Cookies &cookies)
 Converts a CaseInsensitiveMultimap to a string format suitable for HTTP Cookie headers.
 
Cookies parse_cookie (std::string cookie)
 Parses a cookie string into a Cookies object.
 
std::string remove_http_prefix (const std::string &url)
 Removes the first occurrence of "https://" or "http://" from the given URL.
 
std::error_code make_http_error (int status_code)
 Creates an std::error_code from an HTTP status code.
 
bool is_http_error (const std::error_code &ec)
 Checks whether the given error code belongs to the HTTP error category.
 
std::string get_exec_dir ()
 Retrieves the directory of the executable file.
 
std::string percent_encode (const std::string &value) noexcept
 Encodes a string using Percent Encoding according to RFC 3986.
 
std::string percent_decode (const std::string &value) noexcept
 Decodes a Percent-Encoded string.
 
std::string to_upper_case (std::string str)
 Converts a string to uppercase.
 
std::string to_lower_case (std::string str)
 Converts a string to lowercase.
 
std::string extract_protocol (const std::string &url)
 Extracts the protocol from a URL.
 
std::string remove_ws_prefix (const std::string &url)
 Removes the first occurrence of "wss://" or "ws://" from the given URL.
 
bool is_valid_scheme (const std::string &url, const std::string &scheme)
 Checks if a given URL starts with a specified scheme.
 
bool is_valid_domain (const std::string &domain)
 Validates if a domain name is correctly formatted.
 
bool is_valid_path (const std::string &path)
 Checks if a path is correctly formatted.
 
bool is_valid_query (const std::string &query)
 Validates if a query string is correctly formatted.
 
bool is_valid_url (const std::string &url, const std::vector< std::string > &protocol)
 Validates if a URL is correctly formatted.
 
std::string convert_user_agent_to_sec_ch_ua (const std::string &user_agent)
 Converts a User-Agent string to a sec-ch-ua header value.
 
const std::error_category & websocket_error_category ()
 Returns the singleton instance of the WebSocket error category.
 
std::error_code make_error_code (WebSocketError e)
 Constructs an std::error_code from a WebSocketError.
 

Typedef Documentation

◆ CaseInsensitiveCookieMultimap

A case-insensitive unordered multimap specifically for storing HTTP cookies.

Definition at line 55 of file CaseInsensitiveMultimap.hpp.

◆ CaseInsensitiveMultimap

using kurlyk::utils::CaseInsensitiveMultimap = std::unordered_multimap<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual>

A case-insensitive unordered multimap for storing HTTP headers.

Definition at line 52 of file CaseInsensitiveMultimap.hpp.

Enumeration Type Documentation

◆ ClientError

enum class kurlyk::utils::ClientError
strong

Defines errors related to the internal state or lifecycle of the HTTP/WebSocket client.

Enumerator
CancelledByUser 

Request was cancelled explicitly by the user via cancel().

AbortedDuringDestruction 

Request handler was destroyed before completion, causing the request to abort.

ClientNotInitialized 

Operation attempted before client was properly initialized.

InvalidConfiguration 

Provided configuration is incomplete or invalid.

NotConnected 

Operation requires an active connection but none exists.

Definition at line 12 of file ClientErrorCategory.hpp.

◆ WebSocketError

enum class kurlyk::utils::WebSocketError
strong

Represents protocol-level WebSocket errors.

Enumerator
ConnectionFailed 

WebSocket connection could not be established.

UnexpectedClose 

Connection was closed unexpectedly (e.g., code 1006).

ProtocolViolation 

Protocol violation occurred during message exchange.

UnsupportedDataType 

Received an unsupported data type.

InvalidCloseCode 

Server sent an invalid close code.

CompressionError 

Error occurred during compression/decompression.

Definition at line 12 of file WebSocketErrorCategory.hpp.

Function Documentation

◆ case_insensitive_equal()

bool kurlyk::utils::case_insensitive_equal ( const std::string & str1,
const std::string & str2 )
inlinenoexcept

Compares two strings case-insensitively.

Parameters
str1First string to compare.
str2Second string to compare.
Returns
true if strings are equal ignoring case, otherwise false.

Definition at line 14 of file CaseInsensitiveMultimap.hpp.

◆ client_error_category()

const std::error_category & kurlyk::utils::client_error_category ( )
inline

Returns the singleton instance of the ClientErrorCategory.

Returns
Reference to the client error category.

Definition at line 48 of file ClientErrorCategory.hpp.

◆ convert_user_agent_to_sec_ch_ua()

std::string kurlyk::utils::convert_user_agent_to_sec_ch_ua ( const std::string & user_agent)

Converts a User-Agent string to a sec-ch-ua header value.

Parameters
user_agentThe User-Agent string.
Returns
The generated sec-ch-ua header value.

Definition at line 13 of file user_agent_utils.hpp.

◆ extract_protocol()

std::string kurlyk::utils::extract_protocol ( const std::string & url)
inline

Extracts the protocol from a URL.

Parameters
urlThe URL string.
Returns
The protocol scheme as a string. Returns an empty string if no protocol is found.

Definition at line 13 of file url_utils.hpp.

◆ get_exec_dir()

std::string kurlyk::utils::get_exec_dir ( )

Retrieves the directory of the executable file.

Returns
A string containing the directory path of the executable.

Definition at line 16 of file path_utils.hpp.

◆ is_http_error()

bool kurlyk::utils::is_http_error ( const std::error_code & ec)
inline

Checks whether the given error code belongs to the HTTP error category.

Parameters
ecThe error code to check.
Returns
True if the error code belongs to the "http" category.

Definition at line 46 of file HttpErrorCategory.hpp.

◆ is_valid_domain()

bool kurlyk::utils::is_valid_domain ( const std::string & domain)

Validates if a domain name is correctly formatted.

Parameters
domainThe domain string to validate.
Returns
True if the domain is valid, otherwise false.

Definition at line 62 of file url_utils.hpp.

◆ is_valid_email_id()

bool kurlyk::utils::is_valid_email_id ( const std::string & str)

Validates an email address format.

Parameters
strThe email address to validate.
Returns
True if the email address format is valid, false otherwise.

Definition at line 13 of file email_utils.hpp.

◆ is_valid_path()

bool kurlyk::utils::is_valid_path ( const std::string & path)

Checks if a path is correctly formatted.

Parameters
pathThe path string to validate.
Returns
True if the path is valid, otherwise false.

Definition at line 89 of file url_utils.hpp.

◆ is_valid_query()

bool kurlyk::utils::is_valid_query ( const std::string & query)

Validates if a query string is correctly formatted.

Parameters
queryThe query string to validate.
Returns
True if the query string is valid, otherwise false.

Definition at line 102 of file url_utils.hpp.

◆ is_valid_scheme()

bool kurlyk::utils::is_valid_scheme ( const std::string & url,
const std::string & scheme )

Checks if a given URL starts with a specified scheme.

Parameters
urlThe URL string to check.
schemeThe scheme (e.g., "http") to check.
Returns
True if the URL starts with the specified scheme, otherwise false.

Definition at line 55 of file url_utils.hpp.

◆ is_valid_url()

bool kurlyk::utils::is_valid_url ( const std::string & url,
const std::vector< std::string > & protocol )

Validates if a URL is correctly formatted.

Parameters
urlThe URL string to validate.
protocolA vector of valid protocol schemes.
Returns
True if the URL is valid, otherwise false.

Definition at line 126 of file url_utils.hpp.

◆ make_error_code() [1/3]

std::error_code kurlyk::utils::make_error_code ( ClientError e)
inline

Creates a std::error_code from a ClientError value.

Parameters
eClientError enum value.
Returns
std::error_code representing the specified client error.

Definition at line 56 of file ClientErrorCategory.hpp.

◆ make_error_code() [2/3]

std::error_code kurlyk::utils::make_error_code ( CURLcode e)
inline

Creates an std::error_code from a CURLcode, allowing CURL errors to be used with the std::error_code API.

Parameters
eThe CURL error code.
Returns
An std::error_code corresponding to the specified CURL error.

Definition at line 31 of file CurlErrorCategory.hpp.

◆ make_error_code() [3/3]

std::error_code kurlyk::utils::make_error_code ( WebSocketError e)
inline

Constructs an std::error_code from a WebSocketError.

Definition at line 56 of file WebSocketErrorCategory.hpp.

◆ make_http_error()

std::error_code kurlyk::utils::make_http_error ( int status_code)
inline

Creates an std::error_code from an HTTP status code.

Parameters
status_codeThe HTTP status code (e.g., 404, 500).
Returns
Corresponding std::error_code bound to the HTTP category.

Definition at line 38 of file HttpErrorCategory.hpp.

◆ parse_cookie()

Cookies kurlyk::utils::parse_cookie ( std::string cookie)

Parses a cookie string into a Cookies object.

Parameters
cookieThe cookie string to parse.
Returns
A Cookies object containing parsed cookies.

Definition at line 138 of file http_parser.hpp.

◆ parse_http_header_pair()

void kurlyk::utils::parse_http_header_pair ( const char * buffer,
const size_t & size,
std::string & key,
std::string & value )
inline

Parses a header pair from a buffer.

Parameters
bufferThe buffer containing the header.
sizeThe size of the buffer.
keyOutput parameter for the header key.
valueOutput parameter for the header value.

Definition at line 15 of file http_parser.hpp.

◆ percent_decode()

std::string kurlyk::utils::percent_decode ( const std::string & value)
noexcept

Decodes a Percent-Encoded string.

Parameters
valueThe percent-encoded string to be decoded.
Returns
The decoded string.

Definition at line 39 of file percent_encoding.hpp.

◆ percent_encode()

std::string kurlyk::utils::percent_encode ( const std::string & value)
noexcept

Encodes a string using Percent Encoding according to RFC 3986.

Parameters
valueThe string to be encoded.
Returns
The percent-encoded string.

Definition at line 17 of file percent_encoding.hpp.

◆ remove_http_prefix()

std::string kurlyk::utils::remove_http_prefix ( const std::string & url)

Removes the first occurrence of "https://" or "http://" from the given URL.

Parameters
urlThe URL from which to remove the substring.
Returns
std::string The modified URL with the first occurrence of "https://" or "http://" removed.

Definition at line 13 of file http_utils.hpp.

◆ remove_ws_prefix()

std::string kurlyk::utils::remove_ws_prefix ( const std::string & url)

Removes the first occurrence of "wss://" or "ws://" from the given URL.

Parameters
urlThe URL from which to remove the substring.
Returns
std::string The modified URL with the first occurrence of "wss://" or "ws://" removed.

Definition at line 27 of file url_utils.hpp.

◆ to_cookie_string() [1/2]

std::string kurlyk::utils::to_cookie_string ( const CaseInsensitiveMultimap & cookies)

Converts a CaseInsensitiveMultimap to a string format suitable for HTTP Cookie headers.

Parameters
cookiesThe multimap containing key-value pairs.
Returns
A string formatted as a Cookie header.

Definition at line 98 of file http_parser.hpp.

◆ to_cookie_string() [2/2]

std::string kurlyk::utils::to_cookie_string ( const Cookies & cookies)

Converts a CaseInsensitiveMultimap to a string format suitable for HTTP Cookie headers.

Parameters
cookiesThe multimap containing key-value pairs.
Returns
A string formatted as a Cookie header.

Definition at line 114 of file http_parser.hpp.

◆ to_lower_case()

std::string kurlyk::utils::to_lower_case ( std::string str)
inline

Converts a string to lowercase.

Parameters
strInput string.
Returns
A new string converted to lowercase.

Definition at line 26 of file string_utils.hpp.

◆ to_query_string()

std::string kurlyk::utils::to_query_string ( const QueryParams & query,
const std::string & prefix = std::string() )
noexcept

Converts a map of query parameters into a URL query string.

Parameters
queryThe multimap containing query fields and values.
prefixOptional prefix for the query string.
Returns
The encoded query string.

Definition at line 76 of file http_parser.hpp.

◆ to_upper_case()

std::string kurlyk::utils::to_upper_case ( std::string str)
inline

Converts a string to uppercase.

Parameters
strInput string.
Returns
A new string converted to uppercase.

Definition at line 16 of file string_utils.hpp.

◆ websocket_error_category()

const std::error_category & kurlyk::utils::websocket_error_category ( )
inline

Returns the singleton instance of the WebSocket error category.

Definition at line 50 of file WebSocketErrorCategory.hpp.