diff --git a/deps/curl b/deps/curl deleted file mode 160000 index a8a4abb2..00000000 --- a/deps/curl +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a8a4abb2ae97c9a42db0cb82c7f1c508b01d75f3 diff --git a/deps/premake/curl.lua b/deps/premake/curl.lua deleted file mode 100644 index 4377e319..00000000 --- a/deps/premake/curl.lua +++ /dev/null @@ -1,73 +0,0 @@ -curl = { - source = path.join(dependencies.basePath, "curl"), -} - -function curl.import() - links { "curl" } - - filter "toolset:msc*" - links { "Crypt32.lib" } - filter {} - - curl.includes() -end - -function curl.includes() -filter "toolset:msc*" - includedirs { - path.join(curl.source, "include"), - } - - defines { - "CURL_STRICTER", - "CURL_STATICLIB", - "CURL_DISABLE_LDAP", - } -filter {} -end - -function curl.project() - if not os.istarget("windows") then - return - end - - project "curl" - language "C" - - curl.includes() - - includedirs { - path.join(curl.source, "lib"), - } - - files { - path.join(curl.source, "lib/**.c"), - path.join(curl.source, "lib/**.h"), - } - - defines { - "BUILDING_LIBCURL", - } - - filter "toolset:msc*" - - defines { - "USE_SCHANNEL", - "USE_WINDOWS_SSPI", - "USE_THREADS_WIN32", - } - - filter "toolset:not msc*" - - defines { - "USE_GNUTLS", - "USE_THREADS_POSIX", - } - - filter {} - - warnings "Off" - kind "StaticLib" -end - -table.insert(dependencies, curl) diff --git a/src/client/component/updater.cpp b/src/client/component/updater.cpp index a188b83b..d92c768f 100644 --- a/src/client/component/updater.cpp +++ b/src/client/component/updater.cpp @@ -50,21 +50,9 @@ namespace updater return get_self_file() + ".old"; } - std::string download_update(utils::progress_ui& progress_ui) + std::string download_update() { - const auto data = utils::http::get_data( - BINARY_URL, {}, [&progress_ui](const size_t total, const size_t current) - { - if (progress_ui.is_cancelled()) - { - throw std::runtime_error("Cancelled"); - } - - if (total > 0) - { - progress_ui.set_progress(current, total); - } - }); + const auto data = utils::http::get_data(BINARY_URL); if (!data) { @@ -115,7 +103,7 @@ namespace updater progress_ui.set_line(1, "Downloading update..."); progress_ui.show(true); - const auto update_data = download_update(progress_ui); + const auto update_data = download_update(); if (progress_ui.is_cancelled()) { diff --git a/src/common/utils/http.cpp b/src/common/utils/http.cpp index 70ae26f7..3cb59991 100644 --- a/src/common/utils/http.cpp +++ b/src/common/utils/http.cpp @@ -1,112 +1,48 @@ #include "http.hpp" -#include "finally.hpp" -#include - -#pragma comment(lib, "ws2_32.lib") +#include "nt.hpp" +#include namespace utils::http { - namespace + std::optional get_data(const std::string& url) { - struct progress_helper - { - const std::function* callback{}; - std::exception_ptr exception{}; - }; + CComPtr stream; - int progress_callback(void *clientp, const curl_off_t dltotal, const curl_off_t dlnow, const curl_off_t /*ultotal*/, const curl_off_t /*ulnow*/) - { - auto* helper = static_cast(clientp); - - try - { - if (*helper->callback) - { - (*helper->callback)(dltotal, dlnow); - } - } - catch(...) - { - helper->exception = std::current_exception(); - return -1; - } - - return 0; - } - - size_t write_callback(void* contents, const size_t size, const size_t nmemb, void* userp) - { - auto* buffer = static_cast(userp); - - const auto total_size = size * nmemb; - buffer->append(static_cast(contents), total_size); - return total_size; - } - } - - std::optional get_data(const std::string& url, const headers& headers, const std::function& callback) - { - curl_slist* header_list = nullptr; - auto* curl = curl_easy_init(); - if (!curl) + if (FAILED(URLOpenBlockingStreamA(nullptr, url.data(), &stream, 0, nullptr))) { return {}; } - auto _ = finally([&]() - { - curl_slist_free_all(header_list); - curl_easy_cleanup(curl); - }); - - for(const auto& header : headers) - { - auto data = header.first + ": " + header.second; - header_list = curl_slist_append(header_list, data.data()); - } + char buffer[0x1000]; + std::string result; - std::string buffer{}; - progress_helper helper{}; - helper.callback = &callback; - - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list); - curl_easy_setopt(curl, CURLOPT_URL, url.data()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); - curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback); - curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &helper); - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true); - curl_easy_setopt(curl, CURLOPT_USERAGENT, "boiii/1.0"); - curl_easy_setopt(curl, CURLOPT_FAILONERROR, true); + HRESULT status{}; - // Due to CURLOPT_FAILONERROR, CURLE_OK will not be met when the server returns 400 or 500 - if (curl_easy_perform(curl) == CURLE_OK) + do { - long http_code = 0; - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); + DWORD bytes_read = 0; + status = stream->Read(buffer, sizeof(buffer), &bytes_read); - if (http_code >= 200) + if (bytes_read > 0) { - return { std::move(buffer) }; + result.append(buffer, bytes_read); } - - throw std::runtime_error("Bad status code " + std::to_string(http_code) + " met while trying to download file " + url); } + while (SUCCEEDED(status) && status != S_FALSE); - if (helper.exception) + if (FAILED(status)) { - std::rethrow_exception(helper.exception); + return {}; } - return {}; + return {result}; } - std::future> get_data_async(const std::string& url, const headers& headers) + std::future> get_data_async(const std::string& url) { - return std::async(std::launch::async, [url, headers]() + return std::async(std::launch::async, [url]() { - return get_data(url, headers); + return get_data(url); }); } } diff --git a/src/common/utils/http.hpp b/src/common/utils/http.hpp index be9cf088..65628a9f 100644 --- a/src/common/utils/http.hpp +++ b/src/common/utils/http.hpp @@ -6,8 +6,6 @@ namespace utils::http { - using headers = std::unordered_map; - - std::optional get_data(const std::string& url, const headers& headers = {}, const std::function& callback = {}); - std::future> get_data_async(const std::string& url, const headers& headers = {}); + std::optional get_data(const std::string& url); + std::future> get_data_async(const std::string& url); }