2023-10-04 12:00:32 +02:00
|
|
|
#if !defined(OS_WEB)
|
|
|
|
|
|
|
|
#include <hex/helpers/http_requests.hpp>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
namespace {
|
2023-11-10 21:59:20 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
std::string s_proxyUrl;
|
2023-11-10 21:59:20 +01:00
|
|
|
bool s_proxyState;
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HttpRequest::HttpRequest(std::string method, std::string url) : m_method(std::move(method)), m_url(std::move(url)) {
|
|
|
|
AT_FIRST_TIME {
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
};
|
|
|
|
|
|
|
|
AT_FINAL_CLEANUP {
|
|
|
|
curl_global_cleanup();
|
|
|
|
};
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_curl = curl_easy_init();
|
2023-10-04 12:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HttpRequest::~HttpRequest() {
|
2023-12-19 13:10:25 +01:00
|
|
|
curl_easy_cleanup(m_curl);
|
2023-10-04 12:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HttpRequest::HttpRequest(HttpRequest &&other) noexcept {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_curl = other.m_curl;
|
2023-10-04 12:00:32 +02:00
|
|
|
other.m_curl = nullptr;
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_method = std::move(other.m_method);
|
|
|
|
m_url = std::move(other.m_url);
|
|
|
|
m_headers = std::move(other.m_headers);
|
|
|
|
m_body = std::move(other.m_body);
|
2023-10-04 12:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HttpRequest& HttpRequest::operator=(HttpRequest &&other) noexcept {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_curl = other.m_curl;
|
2023-10-04 12:00:32 +02:00
|
|
|
other.m_curl = nullptr;
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_method = std::move(other.m_method);
|
|
|
|
m_url = std::move(other.m_url);
|
|
|
|
m_headers = std::move(other.m_headers);
|
|
|
|
m_body = std::move(other.m_body);
|
2023-10-04 12:00:32 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpRequest::setDefaultConfig() {
|
2023-12-19 13:10:25 +01:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_USERAGENT, "ImHex/1.0");
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_DEFAULT_PROTOCOL, "https");
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYPEER, 1L);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYHOST, 2L);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_TIMEOUT_MS, 0L);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT_MS, m_timeout);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, 1L);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, this);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progressCallback);
|
2023-11-10 21:59:20 +01:00
|
|
|
|
|
|
|
if (s_proxyState)
|
2023-12-19 13:10:25 +01:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_PROXY, s_proxyUrl.c_str());
|
2023-10-04 12:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::future<HttpRequest::Result<std::vector<u8>>> HttpRequest::downloadFile() {
|
|
|
|
return std::async(std::launch::async, [this] {
|
|
|
|
std::vector<u8> response;
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeToVector);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &response);
|
2023-10-04 12:00:32 +02:00
|
|
|
|
|
|
|
return this->executeImpl<std::vector<u8>>(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-10 21:59:20 +01:00
|
|
|
void HttpRequest::setProxyUrl(std::string proxy) {
|
2023-10-04 12:00:32 +02:00
|
|
|
s_proxyUrl = std::move(proxy);
|
|
|
|
}
|
|
|
|
|
2023-12-27 16:33:49 +01:00
|
|
|
void HttpRequest::setProxyState(bool enabled) {
|
|
|
|
s_proxyState = enabled;
|
2023-11-10 21:59:20 +01:00
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
void HttpRequest::checkProxyErrors() {
|
2023-11-10 21:59:20 +01:00
|
|
|
if (s_proxyState && !s_proxyUrl.empty()){
|
2023-10-04 12:00:32 +02:00
|
|
|
log::info("A custom proxy '{0}' is in use. Is it working correctly?", s_proxyUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int HttpRequest::progressCallback(void *contents, curl_off_t dlTotal, curl_off_t dlNow, curl_off_t ulTotal, curl_off_t ulNow) {
|
|
|
|
auto &request = *static_cast<HttpRequest *>(contents);
|
|
|
|
|
|
|
|
if (dlTotal > 0)
|
|
|
|
request.m_progress = float(dlNow) / dlTotal;
|
|
|
|
else if (ulTotal > 0)
|
|
|
|
request.m_progress = float(ulNow) / ulTotal;
|
|
|
|
else
|
|
|
|
request.m_progress = 0.0F;
|
|
|
|
|
|
|
|
return request.m_canceled ? CURLE_ABORTED_BY_CALLBACK : CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|