Remove curl
This commit is contained in:
parent
95df3ba287
commit
5f8a8e9a2b
1
deps/curl
vendored
1
deps/curl
vendored
@ -1 +0,0 @@
|
||||
Subproject commit a8a4abb2ae97c9a42db0cb82c7f1c508b01d75f3
|
73
deps/premake/curl.lua
vendored
73
deps/premake/curl.lua
vendored
@ -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)
|
@ -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())
|
||||
{
|
||||
|
@ -1,112 +1,48 @@
|
||||
#include "http.hpp"
|
||||
#include "finally.hpp"
|
||||
#include <curl/curl.h>
|
||||
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
#include "nt.hpp"
|
||||
#include <atlcomcli.h>
|
||||
|
||||
namespace utils::http
|
||||
{
|
||||
namespace
|
||||
std::optional<std::string> get_data(const std::string& url)
|
||||
{
|
||||
struct progress_helper
|
||||
{
|
||||
const std::function<void(size_t, size_t)>* callback{};
|
||||
std::exception_ptr exception{};
|
||||
};
|
||||
CComPtr<IStream> 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<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)
|
||||
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<std::optional<std::string>> get_data_async(const std::string& url, const headers& headers)
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,6 @@
|
||||
|
||||
namespace utils::http
|
||||
{
|
||||
using headers = std::unordered_map<std::string, std::string>;
|
||||
|
||||
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 = {});
|
||||
std::optional<std::string> get_data(const std::string& url);
|
||||
std::future<std::optional<std::string>> get_data_async(const std::string& url);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user