diff --git a/include/connection.hpp b/include/connection.hpp index fcb2a23..2469738 100644 --- a/include/connection.hpp +++ b/include/connection.hpp @@ -98,6 +98,13 @@ public: return get(endpoint, {}); } + /*! @copydoc CURLWrapper::set_proxy(string_view) + * + * Sets also the proxy for the Instance you used to initialize this + * Connection. + */ + void set_proxy(const string_view proxy); + private: Instance &_instance; const string_view _baseuri; diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp index ecc30e1..faf79a5 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -127,6 +127,18 @@ public: return _connection; } + /*! + * @brief Set the proxy to use. + * + * See [CURLOPT_PROXY(3)] + * (https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html). + * + * @param proxy Examples: "socks4a://127.0.0.1:9050", "http://[::1]:3128". + * + * @since 0.1.0 + */ + void set_proxy(const string_view proxy); + protected: /*! * @brief Make a HTTP request. diff --git a/src/connection.cpp b/src/connection.cpp index f471c4e..9bc43f0 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -42,4 +42,10 @@ answer_type Connection::get(const endpoint_variant &endpoint, return make_request(http_method::GET, uri, parameters); } +void Connection::set_proxy(const string_view proxy) +{ + CURLWrapper::set_proxy(proxy); + _instance.set_proxy(proxy); +} + } // namespace mastodonpp diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp index 7b0eb79..0b8b49c 100644 --- a/src/curl_wrapper.cpp +++ b/src/curl_wrapper.cpp @@ -63,6 +63,16 @@ CURLWrapper::~CURLWrapper() noexcept } } +void CURLWrapper::set_proxy(const string_view proxy) +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + CURLcode code = curl_easy_setopt(_connection, CURLOPT_PROXY, proxy); + if (code != CURLE_OK) + { + throw CURLException{code, "Failed to set proxy", _curl_buffer_error}; + } +} + answer_type CURLWrapper::make_request(const http_method &method, string uri, const parametermap ¶meters) {