Remove curl

This commit is contained in:
momo5502 2022-06-16 19:33:06 +02:00
parent 95df3ba287
commit 5f8a8e9a2b
5 changed files with 25 additions and 177 deletions

1
deps/curl vendored

@ -1 +0,0 @@
Subproject commit a8a4abb2ae97c9a42db0cb82c7f1c508b01d75f3

73
deps/premake/curl.lua vendored
View File

@ -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)

View File

@ -50,21 +50,9 @@ namespace updater
return get_self_file() + ".old"; 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( const auto data = utils::http::get_data(BINARY_URL);
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);
}
});
if (!data) if (!data)
{ {
@ -115,7 +103,7 @@ namespace updater
progress_ui.set_line(1, "Downloading update..."); progress_ui.set_line(1, "Downloading update...");
progress_ui.show(true); progress_ui.show(true);
const auto update_data = download_update(progress_ui); const auto update_data = download_update();
if (progress_ui.is_cancelled()) if (progress_ui.is_cancelled())
{ {

View File

@ -1,112 +1,48 @@
#include "http.hpp" #include "http.hpp"
#include "finally.hpp" #include "nt.hpp"
#include <curl/curl.h> #include <atlcomcli.h>
#pragma comment(lib, "ws2_32.lib")
namespace utils::http namespace utils::http
{ {
namespace std::optional<std::string> get_data(const std::string& url)
{ {
struct progress_helper CComPtr<IStream> stream;
{
const std::function<void(size_t, size_t)>* callback{};
std::exception_ptr exception{};
};
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*/) if (FAILED(URLOpenBlockingStreamA(nullptr, url.data(), &stream, 0, nullptr)))
{
auto* helper = static_cast<progress_helper*>(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<std::string*>(userp);
const auto total_size = size * nmemb;
buffer->append(static_cast<char*>(contents), total_size);
return total_size;
}
}
std::optional<std::string> get_data(const std::string& url, const headers& headers, const std::function<void(size_t, size_t)>& callback)
{
curl_slist* header_list = nullptr;
auto* curl = curl_easy_init();
if (!curl)
{ {
return {}; return {};
} }
auto _ = finally([&]() char buffer[0x1000];
{ std::string result;
curl_slist_free_all(header_list);
curl_easy_cleanup(curl);
});
for(const auto& header : headers) HRESULT status{};
do
{ {
auto data = header.first + ": " + header.second; DWORD bytes_read = 0;
header_list = curl_slist_append(header_list, data.data()); status = stream->Read(buffer, sizeof(buffer), &bytes_read);
if (bytes_read > 0)
{
result.append(buffer, bytes_read);
} }
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);
// Due to CURLOPT_FAILONERROR, CURLE_OK will not be met when the server returns 400 or 500
if (curl_easy_perform(curl) == CURLE_OK)
{
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code >= 200)
{
return { std::move(buffer) };
} }
while (SUCCEEDED(status) && status != S_FALSE);
throw std::runtime_error("Bad status code " + std::to_string(http_code) + " met while trying to download file " + url); if (FAILED(status))
}
if (helper.exception)
{ {
std::rethrow_exception(helper.exception);
}
return {}; return {};
} }
std::future<std::optional<std::string>> get_data_async(const std::string& url, const headers& headers) return {result};
}
std::future<std::optional<std::string>> 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);
}); });
} }
} }

View File

@ -6,8 +6,6 @@
namespace utils::http namespace utils::http
{ {
using headers = std::unordered_map<std::string, std::string>; std::optional<std::string> get_data(const std::string& url);
std::future<std::optional<std::string>> get_data_async(const std::string& url);
std::optional<std::string> get_data(const std::string& url, const headers& headers = {}, const std::function<void(size_t, size_t)>& callback = {});
std::future<std::optional<std::string>> get_data_async(const std::string& url, const headers& headers = {});
} }