diff --git a/plugins/libimhex/include/hex/helpers/net.hpp b/plugins/libimhex/include/hex/helpers/net.hpp index 796c39fc3..991e61618 100644 --- a/plugins/libimhex/include/hex/helpers/net.hpp +++ b/plugins/libimhex/include/hex/helpers/net.hpp @@ -10,8 +10,10 @@ #include #include +#include -#include +using CURL = void; +struct curl_slist; namespace hex { @@ -31,25 +33,14 @@ namespace hex { Net(); ~Net(); - std::future> getString(const std::string &url); - std::future> getJson(const std::string &url); + std::future> getString(const std::string &url, u32 timeout = 2000); + std::future> getJson(const std::string &url, u32 timeout = 2000); - std::future> uploadFile(const std::string &url, const std::filesystem::path &filePath); - std::future> downloadFile(const std::string &url, const std::filesystem::path &filePath); + std::future> uploadFile(const std::string &url, const std::filesystem::path &filePath, u32 timeout = 2000); + std::future> 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 &extraHeaders = { }, const std::string &body = { }); + void setCommonSettings(std::string &response, const std::string &url, u32 timeout = 2000, const std::map &extraHeaders = { }, const std::string &body = { }); std::optional 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; diff --git a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp index fc8e8e4b6..1b3de4c84 100644 --- a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp +++ b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp @@ -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> params, std::vector body) : m_name(std::move(name)), m_params(std::move(params)), m_body(std::move(body)) { diff --git a/plugins/libimhex/source/helpers/net.cpp b/plugins/libimhex/source/helpers/net.cpp index 51009188d..ef883292c 100644 --- a/plugins/libimhex/source/helpers/net.cpp +++ b/plugins/libimhex/source/helpers/net.cpp @@ -7,8 +7,8 @@ #include #include -#include +#include #include #include @@ -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 &extraHeaders, const std::string &body) { + void Net::setCommonSettings(std::string &response, const std::string &url, u32 timeout, const std::map &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> Net::getString(const std::string &url) { + std::future> 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> Net::getJson(const std::string &url) { + std::future> 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> Net::uploadFile(const std::string &url, const std::filesystem::path &filePath) { + std::future> 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> Net::downloadFile(const std::string &url, const std::filesystem::path &filePath) { + std::future> 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 { 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 { }; + } + } \ No newline at end of file diff --git a/source/init/tasks.cpp b/source/init/tasks.cpp index 9912839d4..561bc31a8 100644 --- a/source/init/tasks.cpp +++ b/source/init/tasks.cpp @@ -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;