1
0
mirror of synced 2024-11-24 15:50:16 +01:00

sys: Added timeout setting to curl code. Make splash screen not hang as long

This commit is contained in:
WerWolv 2021-12-17 09:52:58 +01:00
parent b082a28cc4
commit bc53109a1f
4 changed files with 36 additions and 35 deletions

View File

@ -10,8 +10,10 @@
#include <atomic>
#include <nlohmann/json_fwd.hpp>
#include <curl/system.h>
#include <curl/curl.h>
using CURL = void;
struct curl_slist;
namespace hex {
@ -31,25 +33,14 @@ namespace hex {
Net();
~Net();
std::future<Response<std::string>> getString(const std::string &url);
std::future<Response<nlohmann::json>> getJson(const std::string &url);
std::future<Response<std::string>> getString(const std::string &url, u32 timeout = 2000);
std::future<Response<nlohmann::json>> getJson(const std::string &url, u32 timeout = 2000);
std::future<Response<std::string>> uploadFile(const std::string &url, const std::filesystem::path &filePath);
std::future<Response<void>> downloadFile(const std::string &url, const std::filesystem::path &filePath);
std::future<Response<std::string>> uploadFile(const std::string &url, const std::filesystem::path &filePath, u32 timeout = 2000);
std::future<Response<void>> downloadFile(const std::string &url, const std::filesystem::path &filePath, u32 timeout = 2000);
[[nodiscard]]
std::string encode(const std::string &input) {
auto escapedString = curl_easy_escape(this->m_ctx, input.c_str(), std::strlen(input.c_str()));
if (escapedString != nullptr) {
std::string output = escapedString;
curl_free(escapedString);
return output;
}
return { };
}
std::string encode(const std::string &input);
[[nodiscard]]
float getProgress() const { return this->m_progress; }
@ -57,13 +48,13 @@ namespace hex {
void cancel() { this->m_shouldCancel = true; }
private:
void setCommonSettings(std::string &response, const std::string &url, const std::map<std::string, std::string> &extraHeaders = { }, const std::string &body = { });
void setCommonSettings(std::string &response, const std::string &url, u32 timeout = 2000, const std::map<std::string, std::string> &extraHeaders = { }, const std::string &body = { });
std::optional<s32> execute();
friend int progressCallback(void *contents, curl_off_t dlTotal, curl_off_t dlNow, curl_off_t ulTotal, curl_off_t ulNow);
private:
CURL *m_ctx;
struct curl_slist *m_headers = nullptr;
curl_slist *m_headers = nullptr;
std::mutex m_transmissionActive;
float m_progress = 0.0F;

View File

@ -1970,7 +1970,6 @@ namespace hex::pl {
class ASTNodeAssignment : public ASTNode {
public:
// TODO: Implement this
ASTNodeAssignment(std::string lvalueName, ASTNode *rvalue) : m_lvalueName(std::move(lvalueName)), m_rvalue(rvalue) {
}
@ -2012,7 +2011,6 @@ namespace hex::pl {
class ASTNodeReturnStatement : public ASTNode {
public:
// TODO: Implement this
explicit ASTNodeReturnStatement(ASTNode *rvalue) : m_rvalue(rvalue) {
}
@ -2052,7 +2050,6 @@ namespace hex::pl {
class ASTNodeFunctionDefinition : public ASTNode {
public:
// TODO: Implement this
ASTNodeFunctionDefinition(std::string name, std::vector<std::pair<std::string, ASTNode*>> params, std::vector<ASTNode*> body)
: m_name(std::move(name)), m_params(std::move(params)), m_body(std::move(body)) {

View File

@ -7,8 +7,8 @@
#include <cstdio>
#include <mbedtls/ssl.h>
#include <mbedtls/error.h>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <hex/resources.hpp>
@ -74,7 +74,7 @@ namespace hex {
return net.m_shouldCancel ? CURLE_ABORTED_BY_CALLBACK : CURLE_OK;
}
void Net::setCommonSettings(std::string &response, const std::string &url, const std::map<std::string, std::string> &extraHeaders, const std::string &body) {
void Net::setCommonSettings(std::string &response, const std::string &url, u32 timeout, const std::map<std::string, std::string> &extraHeaders, const std::string &body) {
this->m_headers = curl_slist_append(this->m_headers, "Cache-Control: no-cache");
if (!extraHeaders.empty())
@ -105,7 +105,7 @@ namespace hex {
curl_easy_setopt(this->m_ctx, CURLOPT_SSL_CTX_FUNCTION, sslCtxFunction);
curl_easy_setopt(this->m_ctx, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(this->m_ctx, CURLOPT_TIMEOUT_MS, 0L);
curl_easy_setopt(this->m_ctx, CURLOPT_CONNECTTIMEOUT_MS, 2000L);
curl_easy_setopt(this->m_ctx, CURLOPT_CONNECTTIMEOUT_MS, timeout);
curl_easy_setopt(this->m_ctx, CURLOPT_XFERINFODATA, this);
curl_easy_setopt(this->m_ctx, CURLOPT_XFERINFOFUNCTION, progressCallback);
curl_easy_setopt(this->m_ctx, CURLOPT_NOSIGNAL, 1L);
@ -129,7 +129,7 @@ namespace hex {
return responseCode;
}
std::future<Response<std::string>> Net::getString(const std::string &url) {
std::future<Response<std::string>> Net::getString(const std::string &url, u32 timeout) {
this->m_transmissionActive.lock();
return std::async(std::launch::async, [=, this]{
@ -138,7 +138,7 @@ namespace hex {
ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); };
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET");
setCommonSettings(response, url);
setCommonSettings(response, url, timeout);
auto responseCode = execute();
@ -146,7 +146,7 @@ namespace hex {
});
}
std::future<Response<nlohmann::json>> Net::getJson(const std::string &url) {
std::future<Response<nlohmann::json>> Net::getJson(const std::string &url, u32 timeout) {
this->m_transmissionActive.lock();
return std::async(std::launch::async, [=, this]{
@ -155,7 +155,7 @@ namespace hex {
ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); };
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET");
setCommonSettings(response, url, {});
setCommonSettings(response, url, timeout);
auto responseCode = execute();
@ -163,7 +163,7 @@ namespace hex {
});
}
std::future<Response<std::string>> Net::uploadFile(const std::string &url, const std::filesystem::path &filePath) {
std::future<Response<std::string>> Net::uploadFile(const std::string &url, const std::filesystem::path &filePath, u32 timeout) {
this->m_transmissionActive.lock();
return std::async(std::launch::async, [=, this] {
@ -196,7 +196,7 @@ namespace hex {
curl_mime_filename(part, fileName.c_str());
curl_mime_name(part, "file");
setCommonSettings(response, url);
setCommonSettings(response, url, timeout);
curl_easy_setopt(this->m_ctx, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "POST");
@ -206,7 +206,7 @@ namespace hex {
});
}
std::future<Response<void>> Net::downloadFile(const std::string &url, const std::filesystem::path &filePath) {
std::future<Response<void>> Net::downloadFile(const std::string &url, const std::filesystem::path &filePath, u32 timeout) {
this->m_transmissionActive.lock();
return std::async(std::launch::async, [=, this]{
@ -218,7 +218,7 @@ namespace hex {
if (!file.isValid())
return Response<void> { 400 };
setCommonSettings(response, url, {});
setCommonSettings(response, url, timeout);
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(this->m_ctx, CURLOPT_WRITEFUNCTION, writeToFile);
curl_easy_setopt(this->m_ctx, CURLOPT_WRITEDATA, file.getHandle());
@ -228,4 +228,17 @@ namespace hex {
});
}
std::string Net::encode(const std::string &input) {
auto escapedString = curl_easy_escape(this->m_ctx, input.c_str(), std::strlen(input.c_str()));
if (escapedString != nullptr) {
std::string output = escapedString;
curl_free(escapedString);
return output;
}
return { };
}
}

View File

@ -25,7 +25,7 @@ namespace hex::init {
static bool checkForUpdates() {
hex::Net net;
auto releases = net.getJson(GitHubApiURL + "/releases/latest"s).get();
auto releases = net.getJson(GitHubApiURL + "/releases/latest"s, 200).get();
if (releases.code != 200)
return false;
@ -45,7 +45,7 @@ namespace hex::init {
static bool downloadInformation() {
hex::Net net;
auto tip = net.getString(ImHexApiURL + "/tip"s).get();
auto tip = net.getString(ImHexApiURL + "/tip"s, 200).get();
if (tip.code != 200)
return false;