1
0
This repository has been archived on 2025-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
siadrive/SiaDrive.Api/SiaCurl.cpp
Scott E. Graves 27cd8def9b Mounting changes
2017-03-02 00:16:18 -06:00

189 lines
4.8 KiB
C++

#include "stdafx.h"
#include "SiaCurl.h"
#include <curl/curl.h>
using namespace Sia::Api;
CSiaCurl::CSiaCurl()
{
SetHostConfig({ L"localhost", 9980, L""});
}
CSiaCurl::CSiaCurl(const SiaHostConfig& hostConfig)
{
SetHostConfig(hostConfig);
}
CSiaCurl::~CSiaCurl()
{
}
SiaCurlError CSiaCurl::CheckApiError(const json& result)
{
SiaCurlError ret = SiaCurlError::Success;
if (result.find("message") != result.end())
{
ret = SiaCurlError::UnknownFailure;
const std::string msg = result["message"].get<std::string>();
if ((msg.length() >= 3))
{
if ((msg.substr(0, 3) == "404"))
{
ret = SiaCurlError::HttpError;
}
}
}
return ret;
}
std::string CSiaCurl::ConstructPath(const String& relativePath) const
{
const std::string ret = "http://" + std::string(CW2A(GetHostConfig().HostName.c_str())) + ":" + std::to_string(GetHostConfig().HostPort) + std::string(CW2A(relativePath.c_str()));
return ret;
}
SiaCurlError CSiaCurl::CheckHttpError(const std::string& result)
{
if (result.length() && ((result.length() < 2) || (result[0] != '{')))
{
return SiaCurlError::HttpError;
}
return SiaCurlError::Success;
}
SiaCurlError CSiaCurl::ProcessResponse(const int& res, const int& httpCode, const std::string& result, json& response) const
{
SiaCurlError ret;
if ((res == CURLE_OK) && ((httpCode >= 200) && (httpCode <300)))
{
ret = CheckHttpError(result);
if (ApiSuccess(ret))
{
ret = (result.length() ? CheckApiError((response = json::parse(result.c_str()))) : SiaCurlError::Success);
}
}
else
{
if ((res == CURLE_COULDNT_RESOLVE_HOST) || (res == CURLE_COULDNT_CONNECT))
{
ret = SiaCurlError::NoResponse;
}
else if (httpCode)
{
ret = SiaCurlError::HttpError;
}
else
{
ret = SiaCurlError::UnknownFailure;
}
}
return ret;
}
SiaCurlError CSiaCurl::_Get(const String& path, json& response) const
{
CURL* curlHandle = curl_easy_init();
curl_easy_reset(curlHandle);
curl_easy_setopt(curlHandle, CURLOPT_USERAGENT, "Sia-Agent");
curl_easy_setopt(curlHandle, CURLOPT_URL, ConstructPath(path).c_str());
curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, static_cast<size_t(*)(char*, size_t, size_t, void *)>([](char *buffer, size_t size, size_t nitems, void *outstream) -> size_t
{
(*reinterpret_cast<std::string*>(outstream)) += std::string(reinterpret_cast<LPCSTR>(buffer), size * nitems);
return size * nitems;
}));
std::string result;
curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &result);
const CURLcode res = curl_easy_perform(curlHandle);
long httpCode = 0;
curl_easy_getinfo(curlHandle, CURLINFO_RESPONSE_CODE, &httpCode);
SiaCurlError ret = ProcessResponse(res, httpCode, result, response);
curl_easy_cleanup(curlHandle);
return ret;
}
bool CSiaCurl::CheckVersion(SiaCurlError& error) const
{
error = SiaCurlError::InvalidRequiredVersion;
if (GetHostConfig().RequiredVersion.length())
{
error = SiaCurlError::NoResponse;
const String serverVersion = GetServerVersion();
if (serverVersion.length())
{
error = (serverVersion == GetHostConfig().RequiredVersion) ? SiaCurlError::Success : SiaCurlError::ServerVersionMismatch;
}
}
return ApiSuccess(error);
}
String CSiaCurl::GetServerVersion() const
{
json response;
if (ApiSuccess(_Get(L"/daemon/version", response)))
{
return String(CA2W(response["version"].get<std::string>().c_str()));
}
return L"";
}
SiaCurlError CSiaCurl::Get(const String& path, json& response) const
{
SiaCurlError ret;
if (CheckVersion(ret))
{
ret = _Get(path, response);
}
return ret;
}
SiaCurlError CSiaCurl::Post(const String& path, const PostParameters& parameters, json& response) const
{
SiaCurlError ret;
if (CheckVersion(ret))
{
CURL* curlHandle = curl_easy_init();
curl_easy_reset(curlHandle);
curl_easy_setopt(curlHandle, CURLOPT_USERAGENT, "Sia-Agent");
curl_easy_setopt(curlHandle, CURLOPT_URL, ConstructPath(path).c_str());
curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, static_cast<size_t(*)(char*, size_t, size_t, void *)>([](char *buffer, size_t size, size_t nitems, void *outstream) -> size_t
{
(*reinterpret_cast<std::string*>(outstream)) += std::string(reinterpret_cast<LPCSTR>(buffer), size * nitems);
return size * nitems;
}));
std::string fields;
for (const auto& param : parameters)
{
if (fields.length())
{
fields += "&";
}
fields += (std::string(CW2A(param.first.c_str())) + "=" + std::string(CW2A(param.second.c_str())));
}
curl_easy_setopt(curlHandle, CURLOPT_POSTFIELDS, fields.c_str());
std::string result;
curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &result);
const CURLcode res = curl_easy_perform(curlHandle);
long httpCode = 0;
curl_easy_getinfo(curlHandle, CURLINFO_RESPONSE_CODE, &httpCode);
ret = ProcessResponse(res, httpCode, result, response);
curl_easy_cleanup(curlHandle);
}
return ret;
}