2#ifndef KURLYK_HEADER_KURLYK_TYPES_PROXY_CONFIG_HPP_INCLUDED
3#define KURLYK_HEADER_KURLYK_TYPES_PROXY_CONFIG_HPP_INCLUDED
15#if defined(KURLYK_JSON_SUPPORT) && KURLYK_JSON_SUPPORT
16#include <nlohmann/json.hpp>
59 const std::string& ip,
61 const std::string& username,
62 const std::string& password,
71 void set_proxy_auth(
const std::string& username,
const std::string& password) {
78 const std::string::size_type pos =
proxy_server.rfind(
':');
79 if (pos == std::string::npos || pos == 0)
return std::string();
86 const std::string::size_type pos =
proxy_server.rfind(
':');
87 if (pos == std::string::npos || pos + 1 >=
proxy_server.size())
return 0;
89 const int port = std::stoi(
proxy_server.substr(pos + 1));
90 return port > 0 && port <= 65535 ? port : 0;
99 const std::string::size_type pos =
proxy_auth.find(
':');
100 if (pos == std::string::npos)
return std::string();
107 const std::string::size_type pos =
proxy_auth.find(
':');
108 if (pos == std::string::npos || pos + 1 >=
proxy_auth.size())
return std::string();
119#if defined(KURLYK_JSON_SUPPORT) && KURLYK_JSON_SUPPORT
122 inline const char* proxy_type_name(
ProxyType type) {
135 inline ProxyType proxy_type_from_json(
const nlohmann::json& value) {
136 if (value.is_number_integer()) {
137 const int raw = value.get<
int>();
152 throw std::invalid_argument(
"Invalid ProxyType: " + name);
159 json = nlohmann::json{
160 {
"proxy_server", config.proxy_server},
161 {
"proxy_auth", config.proxy_auth},
162 {
"proxy_type", detail::proxy_type_name(config.proxy_type)},
168 inline void from_json(
const nlohmann::json&
json,
ProxyConfig& config) {
169 if (
json.contains(
"proxy_server")) config.proxy_server =
json.at(
"proxy_server").get<std::string>();
170 if (
json.contains(
"proxy_auth")) config.proxy_auth =
json.at(
"proxy_auth").get<std::string>();
171 if (
json.contains(
"proxy_type")) config.proxy_type = detail::proxy_type_from_json(
json.at(
"proxy_type"));
172 if (
json.contains(
"use")) config.use =
json.at(
"use").get<
bool>();
Defines enums used across the Kurlyk library, including proxy types, rate limits, and WebSocket event...
std::string to_upper_case(std::string str)
Converts a string to uppercase.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
ProxyType
Enumeration of supported proxy types compatible with libcurl.
@ PROXY_SOCKS4A
SOCKS4A proxy.
@ PROXY_SOCKS5_HOSTNAME
SOCKS5 proxy with hostname resolution.
@ PROXY_HTTPS
HTTPS proxy.
@ PROXY_HTTP_1_0
HTTP/1.0 proxy.
@ PROXY_SOCKS4
SOCKS4 proxy.
@ PROXY_SOCKS5
SOCKS5 proxy.
Enables use of ClientError with std::error_code.
Provides basic string manipulation utilities.
Stores proxy server, authentication, and transport settings.
ProxyType proxy_type
Proxy transport type.
void set_proxy(const std::string &ip, int port, const std::string &username, const std::string &password, ProxyType type=ProxyType::PROXY_HTTP)
Sets the proxy server and authentication values.
ProxyConfig(std::string server, std::string auth, ProxyType type=ProxyType::PROXY_HTTP)
Constructs a proxy configuration with server and authentication values.
std::string proxy_auth
Proxy authentication in username:password format.
void set_proxy(const std::string &ip, int port, ProxyType type=ProxyType::PROXY_HTTP)
Sets the proxy server address and transport type.
std::string proxy_server
Proxy address in host:port format.
bool is_valid() const
Checks whether the proxy server address is valid.
std::string get_username() const
Returns the proxy username.
ProxyConfig()
Constructs an empty HTTP proxy configuration.
int get_port() const
Returns the proxy port.
bool use
Indicates whether the proxy is enabled.
std::string get_password() const
Returns the proxy password.
void set_proxy_auth(const std::string &username, const std::string &password)
Sets proxy authentication credentials.
std::string get_ip() const
Returns the host part of the proxy server address.