From acbcb6224ec1cf91b5e76398e6a2ec2a4f9acf5c Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Aug 2019 11:06:41 +0200 Subject: [PATCH] Mastodon::http inherits proxy config from parent. Ff set_proxy() is called, _http.inherit_proxy() is called. --- src/http.cpp | 55 +++++++++++++++++++++++++++++++++++++++----- src/mastodon-cpp.cpp | 2 ++ src/mastodon-cpp.hpp | 11 +++++++-- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/http.cpp b/src/http.cpp index e375447..e4e58f3 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -59,19 +59,19 @@ API::http::http(const API &api, const string &instance, try { - HTTPSClientSession::ProxyConfig proxy; + HTTPSClientSession::ProxyConfig proxyconfig; string env_proxy = Environment::get("http_proxy"); regex re_proxy("^(?:https?://)?(?:([^:]+):?([^@]*)@)?" // user:password "([^:]+):([[:digit:]]+/?)"); // host:port smatch match; regex_search(env_proxy, match, re_proxy); - proxy.host = match[3].str(); - proxy.port = std::stoi(match[4].str()); - proxy.username = match[1].str(); - proxy.password = match[2].str(); + proxyconfig.host = match[3].str(); + proxyconfig.port = std::stoi(match[4].str()); + proxyconfig.username = match[1].str(); + proxyconfig.password = match[2].str(); - HTTPSClientSession::setGlobalProxyConfig(proxy); + HTTPSClientSession::setGlobalProxyConfig(proxyconfig); } catch (const std::exception &) { @@ -85,6 +85,49 @@ API::http::~http() Poco::Net::uninitializeSSL(); } +void API::http::inherit_proxy() +{ + // TODO: Test proxy. + string proxy, userpw; + parent.get_proxy(proxy, userpw); + + size_t pos = proxy.find(':'); + if (pos == string::npos) + { + return; + } + + try + { + HTTPSClientSession::ProxyConfig proxyconfig; + proxyconfig.host = proxy.substr(0, pos); + proxyconfig.port = std::stoi(proxy.substr(pos + 1)); + + if (!userpw.empty()) + { + pos = userpw.find(':'); + if (pos == string::npos) + { + proxyconfig.username = userpw; + } + else + { + proxyconfig.username = userpw.substr(0, pos); + proxyconfig.password = std::stoi(userpw.substr(pos + 1)); + } + } + + HTTPSClientSession::setGlobalProxyConfig(proxyconfig); + } + catch (const std::exception &e) + { + if (parent.exceptions()) + { + std::rethrow_exception(std::current_exception()); + } + } +} + return_call API::http::request(const http_method &meth, const string &path) { HTMLForm form; diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index d3fd927..1b0e4c3 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -297,6 +297,8 @@ void API::set_proxy(const string &proxy, const string &userpw) { _proxy = proxy; _proxy_userpw = userpw; + + _http.inherit_proxy(); } void API::get_proxy(string &proxy, string &userpw) const diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index dc506c3..c051a11 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -142,6 +142,13 @@ namespace Mastodon */ std::mutex &get_mutex(); + /*! + * @brief Inherit proxy from parent. Do not call this. + * + * @since 0.110.0 + */ + void inherit_proxy(); + private: const API &parent; const string _instance; @@ -441,8 +448,8 @@ namespace Mastodon * Since mastodon-cpp is built on libcurl, it respects the same * proxy environment variables. See `man curl`. * - * @param proxy See `man 3 CURLOPT_PROXY` - * @param userpw See `man 3 CURLOPT_PROXYUSERPWD` (optional) + * @param proxy host:port + * @param userpw username[:password] (optional) * * @since 0.15.0 */