Use cURL so http requests don't get cached
This commit is contained in:
parent
5865f427de
commit
1775a13378
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -43,3 +43,6 @@
|
|||||||
[submodule "deps/imgui"]
|
[submodule "deps/imgui"]
|
||||||
path = deps/imgui
|
path = deps/imgui
|
||||||
url = https://github.com/fedddddd/imgui.git
|
url = https://github.com/fedddddd/imgui.git
|
||||||
|
[submodule "deps/curl"]
|
||||||
|
path = deps/curl
|
||||||
|
url = https://github.com/curl/curl.git
|
||||||
|
1
deps/curl
vendored
Submodule
1
deps/curl
vendored
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 7e30252ec34eb99c44b5bb8428e716b73ccbff4b
|
73
deps/premake/curl.lua
vendored
Normal file
73
deps/premake/curl.lua
vendored
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
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)
|
@ -1,48 +1,100 @@
|
|||||||
#include "http.hpp"
|
#include "http.hpp"
|
||||||
#include "nt.hpp"
|
#include <curl/curl.h>
|
||||||
#include <atlcomcli.h>
|
#include <gsl/gsl>
|
||||||
|
|
||||||
|
#pragma comment(lib, "ws2_32.lib")
|
||||||
|
|
||||||
namespace utils::http
|
namespace utils::http
|
||||||
{
|
{
|
||||||
std::optional<std::string> get_data(const std::string& url)
|
namespace
|
||||||
{
|
{
|
||||||
CComPtr<IStream> stream;
|
struct progress_helper
|
||||||
|
{
|
||||||
|
const std::function<void(size_t)>* callback{};
|
||||||
|
std::exception_ptr exception{};
|
||||||
|
};
|
||||||
|
|
||||||
if (FAILED(URLOpenBlockingStreamA(nullptr, url.data(), &stream, 0, nullptr)))
|
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)(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)>& callback)
|
||||||
|
{
|
||||||
|
curl_slist* header_list = nullptr;
|
||||||
|
auto* curl = curl_easy_init();
|
||||||
|
if (!curl)
|
||||||
{
|
{
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
char buffer[0x1000];
|
auto _ = gsl::finally([&]()
|
||||||
std::string result;
|
|
||||||
|
|
||||||
HRESULT status{};
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
{
|
||||||
DWORD bytes_read = 0;
|
curl_slist_free_all(header_list);
|
||||||
status = stream->Read(buffer, sizeof(buffer), &bytes_read);
|
curl_easy_cleanup(curl);
|
||||||
|
});
|
||||||
|
|
||||||
if (bytes_read > 0)
|
for(const auto& header : headers)
|
||||||
{
|
{
|
||||||
result.append(buffer, bytes_read);
|
auto data = header.first + ": " + header.second;
|
||||||
|
header_list = curl_slist_append(header_list, data.data());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
while (SUCCEEDED(status) && status != S_FALSE);
|
|
||||||
|
|
||||||
if (FAILED(status))
|
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);
|
||||||
|
|
||||||
|
if (curl_easy_perform(curl) == CURLE_OK)
|
||||||
{
|
{
|
||||||
|
return {std::move(buffer)};
|
||||||
|
}
|
||||||
|
|
||||||
|
if(helper.exception)
|
||||||
|
{
|
||||||
|
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]()
|
return std::async(std::launch::async, [url, headers]()
|
||||||
{
|
{
|
||||||
return get_data(url);
|
return get_data(url, headers);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
namespace utils::http
|
namespace utils::http
|
||||||
{
|
{
|
||||||
std::optional<std::string> get_data(const std::string& url);
|
using headers = std::unordered_map<std::string, std::string>;
|
||||||
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)>& callback = {});
|
||||||
|
std::future<std::optional<std::string>> get_data_async(const std::string& url, const headers& headers = {});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user