1
0
mirror of synced 2024-12-04 20:17:57 +01:00
ImHex/plugins/libimhex/include/hex/helpers/net.hpp

67 lines
1.7 KiB
C++
Raw Normal View History

#pragma once
#include <hex.hpp>
2021-08-29 22:15:18 +02:00
#include <cstring>
#include <future>
#include <optional>
2021-08-28 00:45:59 +02:00
#include <string>
#include <filesystem>
#include <atomic>
#include <nlohmann/json_fwd.hpp>
2021-08-28 00:45:59 +02:00
#include <curl/curl.h>
namespace hex {
template<typename T>
struct Response {
2021-08-28 00:45:59 +02:00
s32 code;
T body;
};
class Net {
public:
Net();
~Net();
2021-08-28 00:45:59 +02:00
std::future<Response<std::string>> getString(const std::string &url);
std::future<Response<nlohmann::json>> getJson(const std::string &url);
std::future<Response<std::string>> uploadFile(const std::string &url, const std::filesystem::path &filePath);
[[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 { };
}
2021-08-28 00:45:59 +02:00
[[nodiscard]]
float getProgress() const { return this->m_progress; }
2021-08-28 00:45:59 +02:00
void cancel() { this->m_shouldCancel = true; }
private:
void setCommonSettings(std::string &response, const std::string &url, const std::map<std::string, std::string> &extraHeaders = { }, const std::string &body = { });
std::optional<s32> 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;
2021-08-28 00:45:59 +02:00
struct curl_slist *m_headers = nullptr;
std::mutex m_transmissionActive;
float m_progress = 0.0F;
bool m_shouldCancel = false;
};
}