116 lines
2.7 KiB
C++
116 lines
2.7 KiB
C++
#include "stdafx.h"
|
|
#include "CurlHelper.h"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
struct CurlBodyData
|
|
{
|
|
const char* bodyPtr;
|
|
size_t sizeLeft;
|
|
};
|
|
|
|
static size_t curlReadCallback(void* ptr, size_t size, size_t nmemb, void* userdata)
|
|
{
|
|
CurlBodyData* bodyData = static_cast<CurlBodyData*>(userdata);
|
|
size_t bufferSize = size * nmemb;
|
|
if(bufferSize < 1) return 0;
|
|
|
|
if (bodyData->sizeLeft == 0)
|
|
return 0;
|
|
|
|
//we only send one byte at a time
|
|
size_t copySize = 1;
|
|
memcpy(ptr, bodyData->bodyPtr, copySize);
|
|
bodyData->bodyPtr += copySize;
|
|
bodyData->sizeLeft -= copySize;
|
|
return copySize;
|
|
}
|
|
|
|
static size_t curlWriteCallback(void* ptr, size_t size, size_t nmemb, void* userdata)
|
|
{
|
|
std::string* output = static_cast<std::string*>(userdata);
|
|
size_t newLength = size * nmemb;
|
|
size_t oldLength = output->size();
|
|
|
|
output->resize(oldLength + newLength);
|
|
std::copy((char*)ptr, (char*)ptr + newLength, output->begin() + oldLength);
|
|
|
|
return newLength;
|
|
}
|
|
|
|
int32_t curlRequest(const std::string& url, const std::string& body, std::string& output, bool jsonBody)
|
|
{
|
|
CURL* curlCtx = curl_easy_init();
|
|
if (curlCtx == nullptr) return -1;
|
|
|
|
curl_easy_reset(curlCtx);
|
|
|
|
CurlBodyData bodyData;
|
|
bodyData.bodyPtr = body.c_str();
|
|
bodyData.sizeLeft = body.size();
|
|
|
|
curl_easy_setopt(curlCtx, CURLOPT_URL, url.c_str());
|
|
curl_easy_setopt(curlCtx, CURLOPT_SSL_VERIFYPEER, 0L);
|
|
curl_easy_setopt(curlCtx, CURLOPT_SSL_VERIFYHOST, 0L);
|
|
|
|
curl_easy_setopt(curlCtx, CURLOPT_NOPROXY, "*");
|
|
curl_easy_setopt(curlCtx, CURLOPT_TIMEOUT, 3L);
|
|
curl_easy_setopt(curlCtx, CURLOPT_NOSIGNAL, 1L);
|
|
|
|
curl_easy_setopt(curlCtx, CURLOPT_POST, 1L);
|
|
|
|
curl_easy_setopt(curlCtx, CURLOPT_READFUNCTION, curlReadCallback);
|
|
|
|
curl_easy_setopt(curlCtx, CURLOPT_READDATA, &bodyData);
|
|
|
|
curl_easy_setopt(curlCtx, CURLOPT_WRITEFUNCTION, curlWriteCallback);
|
|
curl_easy_setopt(curlCtx, CURLOPT_WRITEDATA, &output);
|
|
|
|
struct curl_slist* headers = nullptr;
|
|
if (jsonBody)
|
|
{
|
|
headers = curl_slist_append(headers, "Content-Type: application/json");
|
|
}
|
|
if(headers != nullptr)
|
|
{
|
|
curl_easy_setopt(curlCtx, CURLOPT_HTTPHEADER, headers);
|
|
}
|
|
|
|
curl_easy_setopt(curlCtx, CURLOPT_POSTFIELDSIZE, static_cast<long>(bodyData.sizeLeft));
|
|
|
|
CURLcode returnCode = curl_easy_perform(curlCtx);
|
|
if(returnCode != CURLE_OK)
|
|
{
|
|
curl_easy_cleanup(curlCtx);
|
|
if (headers != nullptr)
|
|
{
|
|
curl_slist_free_all(headers);
|
|
headers = nullptr;
|
|
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
long stateCode = 0;
|
|
curl_easy_getinfo(curlCtx, CURLINFO_RESPONSE_CODE, &stateCode);
|
|
|
|
if (headers != nullptr)
|
|
{
|
|
curl_slist_free_all(headers);
|
|
headers = nullptr;
|
|
}
|
|
curl_easy_cleanup(curlCtx);
|
|
|
|
if(stateCode != 200)
|
|
{
|
|
return static_cast<int32_t>(stateCode);
|
|
}
|
|
|
|
if(output.empty())
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
return returnCode;
|
|
} |