geode/loader/include/Geode/utils/web.hpp

135 lines
4.1 KiB
C++
Raw Normal View History

#pragma once
2024-06-07 05:20:39 -04:00
#include <Geode/loader/Loader.hpp> // another great circular dependency fix
#include <matjson.hpp>
2022-12-04 11:39:40 -05:00
#include "Result.hpp"
2024-06-07 05:20:39 -04:00
#include "Task.hpp"
2024-01-03 21:12:23 -05:00
#include <chrono>
2024-06-07 05:20:39 -04:00
#include <optional>
namespace geode::utils::web {
GEODE_DLL void openLinkInBrowser(std::string const& url);
2024-06-07 05:20:39 -04:00
// https://curl.se/libcurl/c/CURLOPT_HTTPAUTH.html
namespace http_auth {
constexpr static long BASIC = 0x0001;
constexpr static long DIGEST = 0x0002;
constexpr static long DIGEST_IE = 0x0004;
constexpr static long BEARER = 0x0008;
constexpr static long NEGOTIATE = 0x0010;
constexpr static long NTLM = 0x0020;
constexpr static long NTLM_WB = 0x0040;
constexpr static long ANY = 0x0080;
constexpr static long ANYSAFE = 0x0100;
constexpr static long ONLY = 0x0200;
constexpr static long AWS_SIGV4 = 0x0400;
}
2022-12-04 11:39:40 -05:00
2024-06-07 05:20:39 -04:00
// https://curl.se/libcurl/c/CURLOPT_PROXYTYPE.html
enum class ProxyType {
HTTP, // HTTP
HTTPS, // HTTPS
HTTPS2, // HTTPS (attempt to use HTTP/2)
SOCKS4, // Socks4
SOCKS4A, // Socks4 with hostname resolution
SOCKS5, // Socks5
SOCKS5H, // Socks5 with hostname resolution
};
2024-06-07 05:20:39 -04:00
struct ProxyOpts {
std::string address; // Proxy address/FQDN
std::optional<std::uint16_t> port; // Proxy port
ProxyType type = ProxyType::HTTP; // Proxy type
long auth = http_auth::BASIC; // HTTP proxy auth method
std::string username; // Proxy username
std::string password; // Proxy password
bool tunneling = false; // Enable HTTP tunneling
bool certVerification = true; // Enable HTTPS certificate verification
};
2024-06-07 05:20:39 -04:00
class WebRequest;
2024-06-07 05:20:39 -04:00
class GEODE_DLL WebResponse final {
private:
class Impl;
2024-06-07 05:20:39 -04:00
std::shared_ptr<Impl> m_impl;
2024-06-07 05:20:39 -04:00
friend class WebRequest;
public:
2024-06-07 05:20:39 -04:00
// Must be default-constructible for use in Promise
WebResponse();
bool ok() const;
int code() const;
Result<std::string> string() const;
Result<matjson::Value> json() const;
ByteVector data() const;
std::vector<std::string> headers() const;
std::optional<std::string> header(std::string_view name) const;
};
2024-06-07 05:20:39 -04:00
class GEODE_DLL WebProgress final {
2022-10-12 21:50:41 -04:00
private:
2024-01-03 20:17:27 -05:00
class Impl;
2024-06-07 05:20:39 -04:00
std::shared_ptr<Impl> m_impl;
2022-10-12 21:50:41 -04:00
2024-06-07 05:20:39 -04:00
friend class WebRequest;
2022-10-12 21:50:41 -04:00
public:
2024-06-07 05:20:39 -04:00
// Must be default-constructible for use in Promise
WebProgress();
2024-06-07 05:20:39 -04:00
size_t downloaded() const;
size_t downloadTotal() const;
std::optional<float> downloadProgress() const;
2024-06-07 05:20:39 -04:00
size_t uploaded() const;
size_t uploadTotal() const;
std::optional<float> uploadProgress() const;
2022-10-12 21:50:41 -04:00
};
2024-06-07 05:20:39 -04:00
using WebTask = Task<WebResponse, WebProgress>;
2024-06-07 05:20:39 -04:00
class GEODE_DLL WebRequest final {
private:
class Impl;
2022-10-30 14:59:20 -04:00
2024-06-07 05:20:39 -04:00
std::shared_ptr<Impl> m_impl;
public:
2024-06-07 05:20:39 -04:00
WebRequest();
~WebRequest();
2024-06-07 05:20:39 -04:00
WebTask send(std::string_view method, std::string_view url);
WebTask post(std::string_view url);
WebTask get(std::string_view url);
WebTask put(std::string_view url);
WebTask patch(std::string_view url);
2024-06-07 05:20:39 -04:00
WebRequest& header(std::string_view name, std::string_view value);
WebRequest& param(std::string_view name, std::string_view value);
2024-06-07 05:20:39 -04:00
template <std::integral T>
WebRequest& param(std::string_view name, T value) {
return this->param(name, std::to_string(value));
}
2024-06-07 05:20:39 -04:00
WebRequest& userAgent(std::string_view name);
2024-06-07 05:20:39 -04:00
WebRequest& timeout(std::chrono::seconds time);
2022-10-30 14:59:20 -04:00
2024-06-07 05:20:39 -04:00
WebRequest& certVerification(bool enabled);
WebRequest& CABundleContent(std::string_view content);
WebRequest& proxyOpts(ProxyOpts const& proxyOpts);
2022-10-13 09:36:36 -04:00
2024-06-07 05:20:39 -04:00
WebRequest& body(ByteVector raw);
WebRequest& bodyString(std::string_view str);
WebRequest& bodyJSON(matjson::Value const& json);
};
}