33 #ifndef NETWORK_CURL_HPP_ 34 #define NETWORK_CURL_HPP_ 38 #include "../Data/Compression/Gzip.hpp" 39 #include "../Helper/FileSystem.hpp" 40 #include "../Helper/Utf8.hpp" 41 #include "../Main/Exception.hpp" 42 #include "../Struct/NetworkSettings.hpp" 43 #include "../Wrapper/Curl.hpp" 44 #include "../Wrapper/CurlList.hpp" 46 #ifndef CRAWLSERVPP_TESTING 48 #include "../Helper/Portability/curl.h" 52 #include "FakeCurl/FakeCurl.hpp" 67 #include <string_view> 174 using IsRunningCallback = std::function<bool()>;
181 std::string_view cookieDirectory,
186 virtual ~Curl() =
default;
193 const Config& globalConfig,
195 std::queue<std::string>& warningsTo
199 void setHeaders(
const std::vector<std::string>& customHeaders);
209 std::string_view url,
211 std::string& contentTo,
212 const std::vector<std::uint32_t>& errors
216 [[nodiscard]] CURLcode
getCurlCode()
const noexcept;
224 std::uint64_t sleepForMilliseconds,
225 const IsRunningCallback& isRunningCallback
232 [[nodiscard]] std::string
escape(
233 const std::string& stringToEscape,
237 const std::string& escapedString,
240 [[nodiscard]] std::string
escapeUrl(std::string_view urlToEscape);
291 static int header(
char * data, std::size_t size, std::size_t nitems,
void * thisPtr);
298 static int writer(
char * data, std::size_t size, std::size_t nmemb,
void * thisPtr);
304 const std::string_view cookieDir;
305 CURLcode curlCode{CURLE_OK};
307 std::string contentType;
308 std::uint32_t responseCode{};
309 std::uint32_t xTsHeaderValue{};
310 bool limitedSettings{
false};
312 std::string tmpCookies;
313 std::string oldCookies;
316 unsigned int version{};
332 template<
typename T> std::enable_if_t<std::is_integral_v<T>>
333 setOption(CURLoption option, T numericValue) {
335 this->setOption(option, static_cast<long>(numericValue));
338 void setOption(CURLoption option,
const std::string& stringValue);
340 void setOption(CURLoption option,
long longValue);
341 void setOption(CURLoption option,
CurlList& list);
342 void setOption(CURLoption option,
void * pointer);
344 [[nodiscard]]
bool hasFeature(
int feature)
const noexcept;
349 [[nodiscard]] std::string preparePost(std::string_view url, std::string& postFieldsTo);
350 [[nodiscard]] std::string prepareGet(std::string_view url);
351 void checkResult(
const std::array<char, CURL_ERROR_SIZE>& errorBuffer);
352 void checkResponseCode(
const std::vector<std::uint32_t>& errors);
353 void retrieveContentType();
354 void processContentType();
355 void checkCompression();
356 void repairEncoding();
382 std::string_view cookieDirectory,
384 ) : cookieDir(cookieDirectory),
385 networkSettings(setNetworkSettings) {
387 if(!(this->curl.
valid())) {
392 const auto * versionInfo = curl_version_info(CURLVERSION_NOW);
394 this->features = versionInfo->features;
395 this->version = versionInfo->version_num;
403 this->setOption(CURLOPT_NOSIGNAL, 1L);
407 this->curlCode = curl_easy_setopt(
409 CURLOPT_HEADERFUNCTION,
417 this->curlCode = curl_easy_setopt(
419 CURLOPT_WRITEFUNCTION,
426 this->setOption(CURLOPT_WRITEDATA,
this);
427 this->setOption(CURLOPT_HEADERDATA,
this);
458 const Config& globalConfig,
460 std::queue<std::string>& warningsTo
463 if(!(this->curl.
valid())) {
470 CURLOPT_IGNORE_CONTENT_LENGTH,
476 std::string loadCookiesFrom;
477 std::string saveCookiesTo;
480 loadCookiesFrom.reserve(
481 this->cookieDir.length()
486 loadCookiesFrom = this->cookieDir;
493 std::string exceptionString{
"Cookie file '"};
495 exceptionString += loadCookiesFrom;
496 exceptionString +=
"is not in directory '";
497 exceptionString += this->cookieDir;
498 exceptionString +=
"'";
505 saveCookiesTo.reserve(
506 this->cookieDir.length()
511 saveCookiesTo = this->cookieDir;
518 std::string exceptionString{
"Cookie file '"};
520 exceptionString += saveCookiesTo;
521 exceptionString +=
"is not in directory '";
522 exceptionString += this->cookieDir;
523 exceptionString +=
"'";
529 this->setOption(CURLOPT_COOKIEFILE, loadCookiesFrom);
531 if(!saveCookiesTo.empty()) {
532 this->setOption(CURLOPT_COOKIEJAR, saveCookiesTo);
537 this->setOption(CURLOPT_COOKIESESSION,
true);
552 "DNS-over-HTTPS currently not supported," 553 " 'network.dns.doh' ignored." 565 this->setOption(CURLOPT_RESOLVE, this->dnsResolves);
569 std::string serverList;
572 serverList += dnsServer +
",";
575 serverList.pop_back();
577 this->setOption(CURLOPT_DNS_SERVERS, serverList);
586 "DNS shuffling currently not supported," 587 " 'network.dns.shuffle' ignored." 599 std::string encodingList;
606 encodingList +=
"br,";
610 "brotli encoding currently not supported," 611 " 'network.encoding.br' ignored." 618 encodingList +=
"deflate,";
622 "deflate encoding currently not supported" 623 " (the libcurl library misses libz support)," 624 " 'network.encoding.deflate' ignored." 631 encodingList +=
"gzip,";
635 "deflate encoding currently not supported" 636 " (the libcurl library misses libz support)," 637 " 'network.encoding.gzip' ignored." 643 encodingList +=
"identity,";
651 encodingList +=
"zstd,";
655 "Zstandard encoding currently not supported," 656 " 'network.encoding.zstd' ignored." 661 encodingList.pop_back();
663 this->setOption(CURLOPT_ACCEPT_ENCODING, encodingList);
666 this->setOption(CURLOPT_ACCEPT_ENCODING,
nullptr);
676 this->setOption(CURLOPT_HTTPHEADER, this->headers);
682 this->setOption(CURLOPT_HTTP200ALIASES, this->http200Aliases);
688 this->setOption(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
693 this->setOption(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
698 this->setOption(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
711 "HTTP/2 currently not supported," 712 " 'network.http.version' ignored." 724 CURLOPT_HTTP_VERSION,
730 "HTTP/2 ONLY currently not supported," 731 " 'network.http.version' ignored." 746 "HTTP/2 OVER TLS ONLY currently not supported," 747 " 'network.http.version' ignored." 762 "HTTP/3 ONLY currently not supported," 763 " 'network.http.version' ignored." 771 "Enum value for HTTP version not recognized," 772 " 'network.http.version' ignored." 788 this->setOption(CURLOPT_PROXY, this->networkSettings.
defaultProxy);
802 this->setOption(CURLOPT_PROXYHEADER, this->proxyHeaders);
811 "Pre-Proxy currently not supported," 812 " ' proxy.pre' ignored." 837 "Proxy TLS authentication currently not supported," 838 " 'proxy.tls.srp.user' and 'proxy.tls.srp.password' ignored." 853 this->setOption(CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
861 redirectPost |= CURL_REDIR_POST_301;
866 redirectPost |= CURL_REDIR_POST_302;
871 redirectPost |= CURL_REDIR_POST_303;
874 this->setOption(CURLOPT_POSTREDIR, redirectPost);
902 "SSL verification of proxy host and peer currently not supported," 903 " 'ssl.verify.proxy.host' and 'ssl.verify.proxy.peer' ignored." 912 this->setOption(CURLOPT_SSL_VERIFYHOST,
false);
913 this->setOption(CURLOPT_SSL_VERIFYPEER,
false);
914 this->setOption(CURLOPT_SSL_VERIFYSTATUS,
false);
923 "TCP Fast Open currently not supported," 924 " 'tcp.fast.open' ignored." 948 "Happy Eyeballs Configuration currently not supported," 949 " 'network.timeout.happyeyeballs' ignored." 959 this->setOption(CURLOPT_TLSAUTH_TYPE,
"SRP");
971 this->config = &globalConfig;
972 this->limitedSettings = limited;
989 this->setOption(CURLOPT_COOKIELIST,
"Set-Cookie:" + cookie);
1022 if(cookies.empty()) {
1024 this->setOption(CURLOPT_COOKIE,
nullptr);
1027 this->setOption(CURLOPT_COOKIE, cookies);
1030 this->oldCookies = this->tmpCookies;
1031 this->tmpCookies = cookies;
1052 this->tmpHeaders.
clear();
1054 if(customHeaders.empty()) {
1056 this->setOption(CURLOPT_HTTPHEADER, this->headers);
1060 this->tmpHeaders.
append(this->headers);
1061 this->tmpHeaders.
append(customHeaders);
1063 this->setOption(CURLOPT_HTTPHEADER, this->tmpHeaders);
1083 this->setOption(CURLOPT_VERBOSE, isVerbose);
1098 if(this->oldCookies.empty()) {
1099 this->setOption(CURLOPT_COOKIE,
nullptr);
1101 this->tmpCookies.clear();
1104 this->setOption(CURLOPT_COOKIE, this->oldCookies);
1106 this->tmpCookies = this->oldCookies;
1108 this->oldCookies.clear();
1122 this->tmpHeaders.
clear();
1125 this->setOption(CURLOPT_HTTPHEADER, this->headers);
1172 std::string_view url,
1174 std::string& contentTo,
1175 const std::vector<std::uint32_t>& errors
1177 this->clearContent();
1183 std::string postFields;
1185 const auto escapedUrl{
1186 usePost ? this->preparePost(url, postFields) : this->prepareGet(url)
1190 this->setOption(CURLOPT_URL, escapedUrl);
1193 std::array<char, CURL_ERROR_SIZE> errorBuffer{};
1195 errorBuffer.at(0) = 0;
1197 this->setOption(CURLOPT_ERRORBUFFER, errorBuffer.data());
1201 this->curlCode = curl_easy_perform(this->curl.
get());
1203 catch(
const std::exception& e) {
1205 this->setOption(CURLOPT_ERRORBUFFER,
nullptr);
1211 this->checkResult(errorBuffer);
1212 this->checkResponseCode(errors);
1213 this->retrieveContentType();
1214 this->processContentType();
1215 this->checkCompression();
1216 this->repairEncoding();
1218 contentTo.swap(this->content);
1226 return this->responseCode;
1235 return this->contentType;
1248 return this->curlCode;
1268 std::vector<std::uint32_t>(
1275 return "N/A (" + std::string(e.
view()) +
")";
1313 std::uint64_t sleepForMilliseconds,
1314 const IsRunningCallback& isRunningCallback
1317 this->dnsResolves.
clear();
1318 this->headers.
clear();
1319 this->tmpHeaders.
clear();
1320 this->http200Aliases.
clear();
1321 this->proxyHeaders.
clear();
1327 const auto sleepTill{
1329 + std::chrono::milliseconds(sleepForMilliseconds)
1343 this->setOption(CURLOPT_NOSIGNAL,
true);
1347 this->curlCode = curl_easy_setopt(
1349 CURLOPT_HEADERFUNCTION,
1357 this->curlCode = curl_easy_setopt(
1359 CURLOPT_WRITEFUNCTION,
1366 this->setOption(CURLOPT_WRITEDATA,
this);
1367 this->setOption(CURLOPT_HEADERDATA,
this);
1370 if(this->config !=
nullptr) {
1371 std::queue<std::string> logDump;
1375 this->limitedSettings,
1406 inline std::string
Curl::escape(
const std::string& stringToEscape,
bool usePlusForSpace) {
1407 if(stringToEscape.empty()) {
1411 if(!(this->curl.
valid())) {
1414 " libcurl library has not been initialized" 1422 stringToEscape.c_str(),
1423 stringToEscape.length()
1428 if(usePlusForSpace) {
1434 if(pos < result.length()) {
1435 result = result.substr(0, pos)
1467 inline std::string
Curl::unescape(
const std::string& escapedString,
bool usePlusForSpace) {
1468 if(escapedString.empty()) {
1472 if(!(this->curl.
valid())) {
1475 " libcurl library had not been initialized" 1483 escapedString.c_str(),
1484 escapedString.length(),
1490 if(usePlusForSpace) {
1494 pos = result.find(
'+', pos);
1496 if(pos < result.length()) {
1497 result.replace(pos, 1,
" ");
1535 urlToEscape.find_first_not_of(
1537 ) == std::string::npos
1542 if(!(this->curl.
valid())) {
1545 " libcurl library has not been initialized" 1552 while(pos < urlToEscape.length()) {
1555 if(end == std::string::npos) {
1556 end = urlToEscape.length();
1559 const std::string part(urlToEscape, pos, end - pos);
1570 if(end < urlToEscape.length()) {
1571 result += urlToEscape.at(end);
1604 if(curlString !=
nullptr) {
1605 std::string newString{curlString};
1607 curl_free(curlString);
1612 return std::string();
1631 inline int Curl::header(
char * data, std::size_t size, std::size_t nitems,
void * thisPtr) {
1632 if(thisPtr ==
nullptr) {
1661 std::stringstream stringStream;
1663 for(std::size_t n{xTsHeaderNameLen}; n < size; ++n) {
1664 stringStream << data[n];
1667 stringStream >> this->xTsHeaderValue;
1671 return static_cast<int>(size);
1690 inline int Curl::writer(
char * data, std::size_t size, std::size_t nmemb,
void * thisPtr) {
1691 if(thisPtr ==
nullptr) {
1708 this->content.append(data, size);
1710 return static_cast<int>(size);
1718 inline void Curl::setOption(CURLoption option,
const std::string& stringValue) {
1720 this->curlCode = curl_easy_setopt(
1731 inline void Curl::setOption(CURLoption option,
long longValue) {
1733 this->curlCode = curl_easy_setopt(
1743 inline void Curl::setOption(CURLoption option,
CurlList& list) {
1745 this->curlCode = curl_easy_setopt(
1755 inline void Curl::setOption(CURLoption option,
void * pointer) {
1757 this->curlCode = curl_easy_setopt(
1767 inline bool Curl::hasFeature(
int feature)
const noexcept {
1768 return (this->features & feature) == feature;
1772 inline void Curl::checkCode() {
1773 if(this->curlCode != CURLE_OK) {
1779 inline void Curl::clearContent() {
1780 this->content.clear();
1781 this->contentType.clear();
1783 this->responseCode = 0;
1784 this->xTsHeaderValue = 0;
1788 inline std::string Curl::preparePost(std::string_view url, std::string& postFieldsTo) {
1789 const auto delim{url.find(
'?')};
1790 const bool noFields{delim == std::string::npos};
1795 this->setOption(CURLOPT_POST,
true);
1801 this->setOption(CURLOPT_POSTFIELDSIZE, 0);
1808 postFieldsTo = url.substr(delim + 1);
1811 this->setOption(CURLOPT_POSTFIELDSIZE, postFieldsTo.size());
1812 this->setOption(CURLOPT_POSTFIELDS, postFieldsTo);
1816 return this->
escapeUrl(url.substr(0, delim));
1820 inline std::string Curl::prepareGet(std::string_view url) {
1823 this->setOption(CURLOPT_POST,
false);
1833 inline void Curl::checkResult(
const std::array<char, CURL_ERROR_SIZE>& errorBuffer) {
1835 const auto result{this->curlCode};
1837 if(result == CURLE_OK) {
1839 this->setOption(CURLOPT_ERRORBUFFER,
nullptr);
1844 std::string curlError{curl_easy_strerror(result)};
1846 if(errorBuffer.at(0) > 0) {
1848 curlError += errorBuffer.data();
1853 this->setOption(CURLOPT_ERRORBUFFER,
nullptr);
1856 this->curlCode = result;
1862 inline void Curl::checkResponseCode(
const std::vector<std::uint32_t>& errors) {
1865 long responseCodeL{};
1868 this->curlCode = curl_easy_getinfo(
1870 CURLINFO_RESPONSE_CODE,
1878 || responseCodeL > std::numeric_limits<std::uint32_t>::max()
1883 this->responseCode =
static_cast<std::uint32_t
>(responseCodeL);
1893 this->xTsHeaderValue == 0
1894 || this->responseCode != this->xTsHeaderValue
1897 std::string exceptionString{
"HTTP error "};
1899 exceptionString += std::to_string(this->responseCode);
1906 inline void Curl::retrieveContentType() {
1908 char * cString{
nullptr};
1911 this->curlCode = curl_easy_getinfo(
1913 CURLINFO_CONTENT_TYPE,
1919 if(cString ==
nullptr) {
1920 this->contentType = std::string{};
1924 this->contentType = cString;
1928 inline void Curl::processContentType() {
1930 this->contentType.begin(),
1931 this->contentType.end(),
1932 this->contentType.begin(),
1934 return std::tolower(c);
1938 this->contentType.erase(
1940 this->contentType.begin(),
1941 this->contentType.end(),
1943 return std::isspace(c);
1946 this->contentType.end()
1951 inline void Curl::checkCompression() {
1955 && this->contentType.find(
"gzip") != std::string::npos
1958 if(static_cast<unsigned char>(this->content[byte]) !=
gzipMagicNumber[byte]) {
1969 inline void Curl::repairEncoding() {
1970 std::string repairedContent;
1972 if(this->contentType.find(
"charset=iso-8859-1") != std::string::npos) {
1977 this->content.swap(repairedContent);
constexpr auto getPublicIpFrom
URL to retrieve the IP of the server from.
Definition: Curl.hpp:135
curl_slist * get() noexcept
Gets a pointer to the underlying list.
Definition: CurlList.hpp:146
#define CURL_VERSION_BROTLI
Definition: curl.h:113
bool encodingIdentity
Specifies whether to (also) request non-compressed encoding for requested content.
Definition: Config.hpp:283
void clear() noexcept
Clears the underlying libcurl handle.
Definition: Curl.hpp:255
std::vector< std::string > headers
Custom HTTP headers to be sent with every request.
Definition: Config.hpp:301
std::vector< std::string > http200Aliases
Aliases that will be treated like HTTP/1.0 200 OK.
Definition: Config.hpp:304
bool cookiesSession
Specifies whether to ignore obsolete session cookies.
Definition: Config.hpp:187
constexpr std::uint16_t httpVersion2
Attempt to use HTTP/2, fall back to HTTP/1.1.
Definition: Config.hpp:66
constexpr auto versionDnsShuffle
libcurl version needed for DNS shuffling, i.e. 7.60.0.
Definition: Curl.hpp:96
constexpr auto versionZstd
libcurl version needed for zstd encoding, i.e. 7.72.0.
Definition: Curl.hpp:102
std::uint64_t speedUpLimit
Maximum upload speed in bytes per second.
Definition: Config.hpp:499
#define CURLOPT_PRE_PROXY
Definition: curl.h:72
#define CURLOPT_DOH_URL
Definition: curl.h:85
std::string proxyAuth
Authentification for the proxy server used.
Definition: Config.hpp:367
bool sslVerifyProxyHost
Specifies whether to verify that the SSL certificate is for the proxy server it is known as...
Definition: Config.hpp:520
std::string tlsSrpPassword
User name used for TLS-SRP authentification.
Definition: Config.hpp:615
std::uint16_t httpVersion
HTTP version(s) to be used.
Definition: Config.hpp:313
std::string protocol
The protocol to be used for HTTP requests.
Definition: Config.hpp:637
#define CURL_VERSION_SSL
Definition: curl.h:97
#define CURLOPT_PROXY_TLSAUTH_PASSWORD
Definition: curl.h:70
constexpr auto xTsHeaderNameLen
Length of the X-ts header name, in bytes.
Definition: Curl.hpp:144
std::string proxy
Proxy server used.
Definition: Config.hpp:356
std::uint64_t speedDownLimit
Maximum download speed in bytes per second.
Definition: Config.hpp:476
constexpr auto versionHttp3Only
libcurl version needed for HTTP/3 ONLY support, i.e. 7.66.0.
Definition: Curl.hpp:114
std::int64_t dnsCacheTimeOut
The lifetime of DNS cache entries.
Definition: Config.hpp:203
constexpr std::uint16_t httpVersion3Only
Use HTTP/3 only.
Definition: Config.hpp:78
void setConfigGlobal(const Config &globalConfig, bool limited, std::queue< std::string > &warningsTo)
Sets the network options for the connection according to the given configuration. ...
Definition: Curl.hpp:457
#define CURL_VERSION_LIBZ
Definition: curl.h:101
constexpr std::uint16_t httpVersion2Tls
Attempt to use HTTP/2 over TLS, fall back to HTTP/1.1.
Definition: Config.hpp:72
CURLcode getCurlCode() const noexcept
Gets the libcurl return code received from the last API call.
Definition: Curl.hpp:1247
std::string localInterface
Interface to be used for outgoing traffic.
Definition: Config.hpp:319
bool dnsShuffle
Specifies whether to shuffle addresses when a host name returns more than one.
Definition: Config.hpp:235
RAII wrapper for lists used by the libcurl API.
Definition: CurlList.hpp:75
bool contains(std::string_view pathToDir, std::string_view pathToCheck)
Checks whether the given path is located inside the given directory.
Definition: FileSystem.hpp:270
std::string dnsDoH
The URL of a custom DNS-over-HTTPS (DoH) server.
Definition: Config.hpp:209
#define CURL_HTTP_VERSION_3
Definition: curl.h:89
static int writer(char *data, std::size_t size, std::size_t nmemb, void *thisPtr)
Static writer function to handle incoming network data.
Definition: Curl.hpp:1690
#define CURL_VERSION_ZSTD
Definition: curl.h:121
std::uint16_t localPort
Port to be used for outgoing traffic.
Definition: Config.hpp:327
Network settings containing the default proxy as well as host, port, and password of the TOR control ...
Definition: NetworkSettings.hpp:49
struct crawlservpp::Network::Config::Entries networkConfig
Configuration for networking.
void setVerbose(bool isVerbose)
Forces libcurl into or out of verbose mode.
Definition: Curl.hpp:1082
std::string tlsSrpUser
Password used for TLS-SRP authentification.
Definition: Config.hpp:606
constexpr auto checkEveryMilliseconds
The number of milliseconds to sleep before re-checking the status of the application.
Definition: Curl.hpp:87
std::string referer
The HTTP Referer header to be set.
Definition: Config.hpp:440
void unsetHeaders()
Unsets custom HTTP headers previously set.
Definition: Curl.hpp:1120
constexpr auto encodedSpaceLength
Length of a URL encoded space.
Definition: Curl.hpp:84
#define CURLOPT_PROXY_SSL_VERIFYPEER
Definition: curl.h:67
void append(const CurlList &other)
Appends another list to the list.
Definition: CurlList.hpp:230
#define CURL_HET_DEFAULT
Definition: curl.h:77
#define CURL_VERSION_TLSAUTH_SRP
Definition: curl.h:105
Abstract class containing the network-specific configuration for threads.
Definition: Config.hpp:121
bool contentLengthIgnore
Specifies whether the Content-Length header in HTTP responses will be ignored.
Definition: Config.hpp:140
void init()
Initializes the underlying libcurl handle.
Definition: Curl.hpp:230
bool encodingZstd
Specifies whether to request Zstandard encoding for requested content.
Definition: Config.hpp:298
constexpr auto xTsHeaderName
Name of the X-ts header.
Definition: Curl.hpp:141
bool tcpNagle
Specifies whether the TCP's Nagle algorithm is enabled on this connection.
Definition: Config.hpp:568
constexpr auto authTypeTlsSrp
libcurl authentification type for the proxy TLS-SRP authentification.
Definition: Curl.hpp:123
bool sslVerifyHost
Specifies whether to verify that the SSL certificate is for the server it is known as...
Definition: Config.hpp:506
bool sslVerifyPeer
Specifies whether to verify the authenticity of the server's SSL certificate.
Definition: Config.hpp:513
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
constexpr auto versionProxySslVerify
libcurl version needed for SSL verification of proxy host and peer, i.e. 7.52.0.
Definition: Curl.hpp:126
bool redirect
Specifies whether to follow HTTP Location headers for automatic redirects.
Definition: Config.hpp:405
std::string iso88591ToUtf8(std::string_view strIn)
Converts a string from ISO-8859-1 to UTF-8.
Definition: Utf8.hpp:139
constexpr std::uint16_t httpVersion2Only
Use non-TLS HTTP/2, even if HTTPS is not available.
Definition: Config.hpp:69
std::uint64_t speedLowLimit
Low speed limit in bytes per second.
Definition: Config.hpp:484
std::string proxyPre
Pre-proxy server to be used.
Definition: Config.hpp:379
bool valid() const noexcept
Checks whether the underlying libcurl handle is valid.
Definition: Curl.hpp:214
bool tcpFastOpen
Specifies whether TCP Fast Open will be enabled.
Definition: Config.hpp:545
std::uint16_t localPortRange
Number of ports to be tried for outgoing traffic.
Definition: Config.hpp:340
std::uint64_t timeOutRequest
The maximum amount of time a request is allowed to take, in seconds.
Definition: Config.hpp:597
#define CURL_HTTP_VERSION_2TLS
Definition: curl.h:58
int writerInClass(char *data, std::size_t size)
In-class writer function to handle incoming network data.
Definition: Curl.hpp:1707
int headerInClass(char *data, std::size_t size)
In-class header function to handle incoming header data.
Definition: Curl.hpp:1648
constexpr auto versionHttp2
libcurl version needed for HTTP/2 support, i.e. 7.33.0.
Definition: Curl.hpp:105
constexpr std::uint16_t httpVersion11
Use HTTP/1.1 only.
Definition: Config.hpp:63
std::string escapeUrl(std::string_view urlToEscape)
URL encodes the given string while leaving reserved characters (; / ? : @ = & #) intact.
Definition: Curl.hpp:1533
#define CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS
Definition: curl.h:76
std::string getContentType() const noexcept
Gets the content type of the HTTP reply received last.
Definition: Curl.hpp:1234
bool cookies
Specifies whether the internal cookie engine will be enabled.
Definition: Config.hpp:155
constexpr auto versionPreProxy
libcurl version needed for pre-proxy support, i.e. 7.52.0.
Definition: Curl.hpp:117
std::string now()
Formats the current date/time as string in the format YYYY-MM-DD HH:MM:SS.
Definition: DateTime.hpp:1045
void setCookies(const std::string &cookies)
Sets custom cookies.
Definition: Curl.hpp:1021
bool encodingGZip
Specifies whether to request gzip encoding for requested content.
Definition: Config.hpp:271
constexpr std::array getPublicIpErrors
Errors when retrieving the IP of the server.
Definition: Curl.hpp:138
constexpr std::uint16_t httpVersion1
Use HTTP/1 only.
Definition: Config.hpp:60
constexpr auto reservedCharacters
Reserved characters to be ignored when escaping a URL.
Definition: Curl.hpp:90
RAII wrapper for handles of the libcurl API.
Definition: Curl.hpp:70
constexpr auto versionTcpFastOpen
libcurl version needed for TCP Fast Open support, i.e. 7.49.0.
Definition: Curl.hpp:129
std::uint64_t speedLowTime
Number of seconds before a timeout occurs while the transfer speed is below the low speed limit...
Definition: Config.hpp:493
#define CURLOPT_TCP_FASTOPEN
Definition: curl.h:62
CURL * get() noexcept
Gets a pointer to the underlying libcurl handle.
Definition: Curl.hpp:193
bool repairUtf8(std::string_view strIn, std::string &strOut)
Replaces invalid UTF-8 characters in the given string and returns whether invalid characters occured...
Definition: Utf8.hpp:294
constexpr auto versionHttp2Tls
libcurl version needed for HTTP/2 OVER TLS ONLY support, i.e. 7.47.0.
Definition: Curl.hpp:111
std::string cookiesLoad
The file from which cookies will be read.
Definition: Config.hpp:164
constexpr auto versionDoH
libcurl version needed for DNS-over-HTTPS support, i.e. 7.62.0.
Definition: Curl.hpp:93
void unsetCookies()
Unsets custom cookies previously set.
Definition: Curl.hpp:1097
bool sslVerifyProxyPeer
Specifies whether to verify the authenticity of the proxy's SSL certificate.
Definition: Config.hpp:527
constexpr auto versionBrotli
libcurl version needed for Brotli encoding, i.e. 7.57.0.
Definition: Curl.hpp:99
bool verbose
Specifies whether libtidy should produce verbose output.
Definition: Config.hpp:630
bool redirectPost303
Specifies whether to NOT convert POST to GET requests when following 303 redirects.
Definition: Config.hpp:431
bool refererAutomatic
Specifies whether to send an updated HTTP Referer header when automatically redirected.
Definition: Config.hpp:446
bool redirectPost301
Specifies whether to NOT convert POST to GET requests when following 301 redirects.
Definition: Config.hpp:419
std::string decompress(const std::string &compressedContent)
Decompresses gzip-compressed content.
Definition: Gzip.hpp:111
void resetConnection(std::uint64_t sleepForMilliseconds, const IsRunningCallback &isRunningCallback)
Resets the connection.
Definition: Curl.hpp:1312
bool sslVerifyStatus
Specifies whether to verify the status of the server's SSL certificate.
Definition: Config.hpp:542
constexpr auto versionHttp2Only
libcurl version needed for HTTP/2 ONLY support, i.e. 7.49.0.
Definition: Curl.hpp:108
Class for libcurl exceptions.
Definition: Curl.hpp:260
#define CURLOPT_PROXY_TLSAUTH_USERNAME
Definition: curl.h:69
void setConfigCurrent(const Config ¤tConfig)
Sets temporary network options for the connection according to the given configuration.
Definition: Curl.hpp:986
constexpr std::array gzipMagicNumber
GZIP magic number.
Definition: Curl.hpp:147
std::string getPublicIp()
Uses the connection to determine its public IP address.
Definition: Curl.hpp:1260
#define CURL_VERSION_HTTP3
Definition: curl.h:117
std::string escape(const std::string &stringToEscape, bool usePlusForSpace)
URL encodes the given string.
Definition: Curl.hpp:1406
bool encodingDeflate
Specifies whether to request DEFLATE encoding for requested content.
Definition: Config.hpp:259
char getPathSeparator()
Gets the preferred separator for file paths in the current operating system.
Definition: FileSystem.hpp:187
std::string proxyTlsSrpPassword
TSL-SRP password for the proxy server used.
Definition: Config.hpp:387
#define CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
Definition: curl.h:63
constexpr auto versionProxyTlsAuth
libcurl version needed for proxy TLS authentification, i.e. 7.52.0.
Definition: Curl.hpp:120
bool proxyTunnelling
Specifies whether to enable proxy tunnelling.
Definition: Config.hpp:399
std::string unescape(const std::string &escapedString, bool usePlusForSpace)
URL decodes the given string.
Definition: Curl.hpp:1467
std::string_view view() const noexcept
Gets the description of the exception as a view to the underlying string.
Definition: Exception.hpp:158
bool redirectPost302
Specifies whether to NOT convert POST to GET requests when following 302 redirects.
Definition: Config.hpp:425
Provides an interface to the libcurl library for sending and receiving data over the network...
Definition: Curl.hpp:168
std::vector< std::string > proxyHeaders
Custom HTTP headers to be sent to the proxy server.
Definition: Config.hpp:373
#define CURLOPT_DNS_SHUFFLE_ADDRESSES
Definition: curl.h:81
static int header(char *data, std::size_t size, std::size_t nitems, void *thisPtr)
Static header function to handle incoming header data.
Definition: Curl.hpp:1631
std::uint64_t tcpKeepAliveIdle
The delay that will be waited before sending keep-alive probes, in seconds.
Definition: Config.hpp:554
void setHeaders(const std::vector< std::string > &customHeaders)
Sets custom HTTP headers.
Definition: Curl.hpp:1050
bool encodingTransfer
Specifies whether to request HTTP Transfer Encoding.
Definition: Config.hpp:286
std::string userAgent
Custom HTTP User-Agent header to be sent with all HTTP requests.
Definition: Config.hpp:621
static std::string curlStringToString(char *curlString)
Copies the given libcurl string into a std::string and releases its memory.
Definition: Curl.hpp:1603
Curl & operator=(Curl &)=delete
Deleted copy assignment operator.
std::vector< std::string > dnsResolves
DNS name resolves to be overwritten.
Definition: Config.hpp:229
Curl(std::string_view cookieDirectory, const NetworkSettings &setNetworkSettings)
Constructor setting the cookie directory and the network options.
Definition: Curl.hpp:381
std::string defaultProxy
The host name and the port of the default proxy to be used.
Definition: NetworkSettings.hpp:54
void getContent(std::string_view url, bool usePost, std::string &contentTo, const std::vector< std::uint32_t > &errors)
Uses the connection to get content by sending a HTTP request to the specified URL.
Definition: Curl.hpp:1171
virtual ~Curl()=default
Default destructor.
constexpr auto versionHappyEyeballs
libcurl version needed for the Happy Eyeballs algorithm, i.e. 7.59.0.
Definition: Curl.hpp:132
std::uint64_t tcpKeepAliveInterval
The interval time between keep-alive probes to sent, in seconds.
Definition: Config.hpp:560
#define CURL_HTTP_VERSION_2_0
Definition: curl.h:54
std::uint16_t timeOutHappyEyeballs
Number of milliseconds to try to connect only via IPv6 using the Happy Eyeballs algorithm.
Definition: Config.hpp:588
constexpr std::uint16_t httpVersionAny
Use any available HTTP version.
Definition: Config.hpp:57
std::string cookiesSet
Custom HTTP Cookie header independent from the internal cookie engine.
Definition: Config.hpp:197
std::uint16_t connectionsMax
The maximum number of parallel connections.
Definition: Config.hpp:137
std::string proxyTlsSrpUser
TSL-SRP user for the proxy server used.
Definition: Config.hpp:396
bool tcpKeepAlive
Specifies whether TCP keep-alive probing will be enabled.
Definition: Config.hpp:548
std::string dnsInterface
The interface that DNS name resolves should be bound to.
Definition: Config.hpp:215
bool noReUse
Specifies whether to prevent connections from re-using previous ones.
Definition: Config.hpp:348
std::uint64_t timeOut
The maximum amount of time a connection is allowed to take, in seconds.
Definition: Config.hpp:577
std::vector< std::string > dnsServers
DNS servers to be preffered.
Definition: Config.hpp:232
std::uint32_t getResponseCode() const noexcept
Gets the response code of the HTTP reply received last.
Definition: Curl.hpp:1225
#define CURLOPT_PROXY_TLSAUTH_TYPE
Definition: curl.h:71
std::uint64_t redirectMax
The maximum number of automatic redirects.
Definition: Config.hpp:413
bool encodingBr
Specifies whether to request brotli encoding for requested content.
Definition: Config.hpp:247
std::vector< std::string > cookiesOverwrite
Cookies to be overwritten.
Definition: Config.hpp:171
#define CURLOPT_PROXY_SSL_VERIFYHOST
Definition: curl.h:68
Namespace for networking classes.
Definition: Config.hpp:45
constexpr auto encodedSpace
URL encoding of a space.
Definition: Curl.hpp:81
std::string cookiesSave
The file to which cookies will be saved.
Definition: Config.hpp:180
void clear() noexcept
Resets the list and frees its memory.
Definition: CurlList.hpp:297
#define CURL_VERSION_HTTP2
Definition: curl.h:109