2023-01-26 19:54:38 +01:00
|
|
|
//===========================================================================//
|
|
|
|
//
|
|
|
|
// Purpose: A set of utilities to perform requests
|
|
|
|
//
|
|
|
|
//===========================================================================//
|
2023-01-30 00:04:11 +01:00
|
|
|
#include "tier1/cvar.h"
|
2023-04-06 23:50:48 +02:00
|
|
|
#include "tier2/curlutils.h"
|
2023-01-26 19:54:38 +01:00
|
|
|
|
2023-05-10 00:05:38 +02:00
|
|
|
ConVar* ssl_verify_peer = nullptr;
|
|
|
|
ConVar* curl_timeout = nullptr;
|
|
|
|
ConVar* curl_debug = nullptr;
|
|
|
|
|
2023-04-25 22:51:06 +02:00
|
|
|
size_t CURLWriteStringCallback(char* contents, const size_t size, const size_t nmemb, void* userp)
|
2023-01-26 19:54:38 +01:00
|
|
|
{
|
|
|
|
reinterpret_cast<string*>(userp)->append(contents, size * nmemb);
|
|
|
|
return size * nmemb;
|
|
|
|
}
|
|
|
|
|
2023-04-25 22:51:06 +02:00
|
|
|
CURL* CURLInitRequest(const char* remote, const char* request, string& outResponse, curl_slist*& slist)
|
2023-01-26 19:54:38 +01:00
|
|
|
{
|
|
|
|
std::function<void(const char*)> fnError = [&](const char* errorMsg)
|
|
|
|
{
|
2023-07-22 21:14:04 +02:00
|
|
|
Error(eDLL_T::COMMON, NO_ERROR, "CURL: %s\n", errorMsg);
|
2023-01-26 19:54:38 +01:00
|
|
|
curl_slist_free_all(slist);
|
|
|
|
};
|
|
|
|
|
|
|
|
slist = curl_slist_append(slist, "Content-Type: application/json");
|
|
|
|
if (!slist)
|
|
|
|
{
|
|
|
|
fnError("Slist init failed");
|
2023-02-09 22:56:13 +01:00
|
|
|
return nullptr;
|
2023-01-26 19:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CURL* curl = curl_easy_init();
|
|
|
|
if (!curl)
|
|
|
|
{
|
|
|
|
fnError("Easy init failed");
|
2023-02-09 22:56:13 +01:00
|
|
|
return nullptr;
|
2023-01-26 19:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
|
2023-04-25 22:51:06 +02:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, remote);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
|
2023-01-26 19:54:38 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
2023-03-15 21:33:16 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, curl_timeout->GetInt());
|
2023-01-26 19:54:38 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CURLWriteStringCallback);
|
2023-04-25 22:51:06 +02:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &outResponse);
|
2023-01-26 19:54:38 +01:00
|
|
|
|
2023-01-30 00:04:11 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, curl_debug->GetBool());
|
2023-02-09 22:56:13 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
|
2023-01-30 00:04:11 +01:00
|
|
|
|
|
|
|
if (!ssl_verify_peer->GetBool())
|
|
|
|
{
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
|
|
|
}
|
|
|
|
|
2023-01-26 19:54:38 +01:00
|
|
|
return curl;
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:56:13 +01:00
|
|
|
CURLcode CURLSubmitRequest(CURL* curl, curl_slist*& slist)
|
2023-01-26 19:54:38 +01:00
|
|
|
{
|
|
|
|
CURLcode res = curl_easy_perform(curl);
|
|
|
|
curl_slist_free_all(slist);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLINFO CURLRetrieveInfo(CURL* curl)
|
|
|
|
{
|
|
|
|
CURLINFO status;
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-06-02 00:05:23 +02:00
|
|
|
bool CURLHandleError(CURL* curl, const CURLcode res, string& outMessage, const bool logError)
|
2023-01-26 19:54:38 +01:00
|
|
|
{
|
|
|
|
if (res == CURLE_OK)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const char* curlError = curl_easy_strerror(res);
|
2023-06-02 00:05:23 +02:00
|
|
|
|
|
|
|
if (logError)
|
2023-07-22 21:14:04 +02:00
|
|
|
Error(eDLL_T::COMMON, NO_ERROR, "CURL: %s\n", curlError);
|
2023-01-26 19:54:38 +01:00
|
|
|
|
|
|
|
outMessage = curlError;
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-25 22:51:06 +02:00
|
|
|
void CURLFormatUrl(string& outUrl, const char* host, const char* api)
|
2023-01-26 19:54:38 +01:00
|
|
|
{
|
2023-04-25 22:51:06 +02:00
|
|
|
outUrl = Format("%s%s%s", "https://", host, api);
|
2023-04-08 10:50:32 +02:00
|
|
|
}
|