2023-03-22 21:48:14 +01:00
|
|
|
#pragma once
|
|
|
|
|
2023-03-22 23:05:18 +01:00
|
|
|
#include <hex.hpp>
|
|
|
|
|
2023-03-22 21:48:14 +01:00
|
|
|
#include <future>
|
2023-03-22 23:05:18 +01:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2023-03-22 21:48:14 +01:00
|
|
|
|
|
|
|
#include <wolv/io/file.hpp>
|
|
|
|
#include <wolv/utils/guards.hpp>
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
#if defined(OS_WEB)
|
|
|
|
#include <emscripten/fetch.h>
|
2023-11-30 14:40:07 +01:00
|
|
|
|
|
|
|
using curl_off_t = long;
|
|
|
|
#else
|
|
|
|
#include <curl/curl.h>
|
2023-10-04 12:00:32 +02:00
|
|
|
#endif
|
|
|
|
|
2023-11-30 10:22:15 +01:00
|
|
|
typedef void CURL;
|
|
|
|
|
2023-03-22 21:48:14 +01:00
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
class HttpRequest {
|
|
|
|
public:
|
|
|
|
|
|
|
|
class ResultBase {
|
|
|
|
public:
|
2023-03-23 11:23:07 +01:00
|
|
|
ResultBase() = default;
|
|
|
|
explicit ResultBase(u32 statusCode) : m_statusCode(statusCode), m_valid(true) { }
|
2023-03-22 21:48:14 +01:00
|
|
|
|
|
|
|
[[nodiscard]] u32 getStatusCode() const {
|
|
|
|
return this->m_statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool isSuccess() const {
|
|
|
|
return this->getStatusCode() == 200;
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
[[nodiscard]] bool isValid() const {
|
|
|
|
return this->m_valid;
|
|
|
|
}
|
|
|
|
|
2023-03-22 21:48:14 +01:00
|
|
|
private:
|
2023-03-23 11:23:07 +01:00
|
|
|
u32 m_statusCode = 0;
|
|
|
|
bool m_valid = false;
|
2023-03-22 21:48:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class Result : public ResultBase {
|
|
|
|
public:
|
2023-03-23 11:23:07 +01:00
|
|
|
Result() = default;
|
2023-03-22 21:48:14 +01:00
|
|
|
Result(u32 statusCode, T data) : ResultBase(statusCode), m_data(std::move(data)) { }
|
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
const T& getData() const {
|
|
|
|
return this->m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T m_data;
|
|
|
|
};
|
|
|
|
|
2023-05-20 13:10:24 +02:00
|
|
|
HttpRequest(std::string method, std::string url);
|
|
|
|
~HttpRequest();
|
2023-03-22 21:48:14 +01:00
|
|
|
|
2023-03-22 23:05:18 +01:00
|
|
|
HttpRequest(const HttpRequest&) = delete;
|
|
|
|
HttpRequest& operator=(const HttpRequest&) = delete;
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
HttpRequest(HttpRequest &&other) noexcept;
|
2023-03-22 23:05:18 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
HttpRequest& operator=(HttpRequest &&other) noexcept;
|
2023-03-22 23:05:18 +01:00
|
|
|
|
2023-11-10 21:59:20 +01:00
|
|
|
static void setProxyState(bool enabled);
|
|
|
|
static void setProxyUrl(std::string proxy);
|
2023-03-23 11:23:07 +01:00
|
|
|
|
|
|
|
void setMethod(std::string method) {
|
|
|
|
this->m_method = std::move(method);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setUrl(std::string url) {
|
|
|
|
this->m_url = std::move(url);
|
|
|
|
}
|
|
|
|
|
2023-03-22 21:48:14 +01:00
|
|
|
void addHeader(std::string key, std::string value) {
|
|
|
|
this->m_headers[std::move(key)] = std::move(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBody(std::string body) {
|
|
|
|
this->m_body = std::move(body);
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
void setTimeout(u32 timeout) {
|
|
|
|
this->m_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getProgress() const {
|
|
|
|
return this->m_progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cancel() {
|
|
|
|
this->m_canceled = true;
|
|
|
|
}
|
|
|
|
|
2023-03-22 23:05:18 +01:00
|
|
|
template<typename T = std::string>
|
2023-10-04 12:00:32 +02:00
|
|
|
std::future<Result<T>> downloadFile(const std::fs::path &path);
|
2023-03-22 21:48:14 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
std::future<Result<std::vector<u8>>> downloadFile();
|
2023-03-22 21:48:14 +01:00
|
|
|
|
2023-03-22 23:05:18 +01:00
|
|
|
template<typename T = std::string>
|
2023-10-04 12:00:32 +02:00
|
|
|
std::future<Result<T>> uploadFile(const std::fs::path &path, const std::string &mimeName = "filename");
|
2023-03-22 21:48:14 +01:00
|
|
|
|
2023-03-22 23:05:18 +01:00
|
|
|
template<typename T = std::string>
|
2023-10-04 12:00:32 +02:00
|
|
|
std::future<Result<T>> uploadFile(std::vector<u8> data, const std::string &mimeName = "filename", const std::fs::path &fileName = "data.bin");
|
2023-03-22 21:48:14 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
template<typename T = std::string>
|
|
|
|
std::future<Result<T>> execute();
|
2023-03-23 11:23:07 +01:00
|
|
|
|
2023-11-17 13:13:17 +01:00
|
|
|
static std::string urlEncode(const std::string &input);
|
2023-03-23 11:23:07 +01:00
|
|
|
|
2023-11-17 13:13:17 +01:00
|
|
|
static std::string urlDecode(const std::string &input);
|
2023-03-23 11:23:07 +01:00
|
|
|
|
2023-03-22 21:48:14 +01:00
|
|
|
protected:
|
2023-03-22 23:05:18 +01:00
|
|
|
void setDefaultConfig();
|
|
|
|
|
2023-03-22 21:48:14 +01:00
|
|
|
template<typename T>
|
2023-10-04 12:00:32 +02:00
|
|
|
Result<T> executeImpl(std::vector<u8> &data);
|
2023-03-22 21:48:14 +01:00
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
static size_t writeToVector(void *contents, size_t size, size_t nmemb, void *userdata);
|
|
|
|
static size_t writeToFile(void *contents, size_t size, size_t nmemb, void *userdata);
|
|
|
|
static int progressCallback(void *contents, curl_off_t dlTotal, curl_off_t dlNow, curl_off_t ulTotal, curl_off_t ulNow);
|
2023-03-22 21:48:14 +01:00
|
|
|
|
2023-07-26 13:50:51 +02:00
|
|
|
private:
|
|
|
|
static void checkProxyErrors();
|
|
|
|
|
2023-03-22 21:48:14 +01:00
|
|
|
private:
|
2023-10-04 12:00:32 +02:00
|
|
|
#if defined(OS_WEB)
|
|
|
|
emscripten_fetch_attr_t m_attr;
|
|
|
|
#else
|
2023-03-22 21:48:14 +01:00
|
|
|
CURL *m_curl;
|
2023-10-04 12:00:32 +02:00
|
|
|
#endif
|
2023-03-22 21:48:14 +01:00
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
std::mutex m_transmissionMutex;
|
|
|
|
|
2023-03-22 21:48:14 +01:00
|
|
|
std::string m_method;
|
|
|
|
std::string m_url;
|
|
|
|
std::string m_body;
|
2023-10-04 12:00:32 +02:00
|
|
|
std::promise<std::vector<u8>> m_promise;
|
2023-03-22 21:48:14 +01:00
|
|
|
std::map<std::string, std::string> m_headers;
|
2023-03-23 11:23:07 +01:00
|
|
|
u32 m_timeout = 1000;
|
|
|
|
|
2023-03-23 13:32:35 +01:00
|
|
|
std::atomic<float> m_progress = 0.0F;
|
|
|
|
std::atomic<bool> m_canceled = false;
|
2023-03-22 21:48:14 +01:00
|
|
|
};
|
2023-10-04 12:00:32 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 21:48:14 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
#if defined(OS_WEB)
|
|
|
|
#include <hex/helpers/http_requests_emscripten.hpp>
|
|
|
|
#else
|
|
|
|
#include <hex/helpers/http_requests_native.hpp>
|
|
|
|
#endif
|