From d2a4d835de74760ea53ae36adeac21b2c07ebffa Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 25 Aug 2019 04:50:08 +0200 Subject: [PATCH] Moved set_proxy() to API::http(). API::set_proxy() calls API::http::set_proxy(). Deleted API::get_proxy(). --- src/http.cpp | 59 +++++++++++++++++++++++--------------------- src/mastodon-cpp.cpp | 15 ++--------- src/mastodon-cpp.hpp | 29 ++++++++-------------- 3 files changed, 43 insertions(+), 60 deletions(-) diff --git a/src/http.cpp b/src/http.cpp index e4e58f3..50bc4eb 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -59,19 +59,29 @@ API::http::http(const API &api, const string &instance, try { - HTTPSClientSession::ProxyConfig proxyconfig; string env_proxy = Environment::get("http_proxy"); - regex re_proxy("^(?:https?://)?(?:([^:]+):?([^@]*)@)?" // user:password - "([^:]+):([[:digit:]]+/?)"); // host:port - smatch match; + size_t pos; - regex_search(env_proxy, match, re_proxy); - proxyconfig.host = match[3].str(); - proxyconfig.port = std::stoi(match[4].str()); - proxyconfig.username = match[1].str(); - proxyconfig.password = match[2].str(); + // Only keep text between // and /. + if ((pos = env_proxy.find("//")) != string::npos) + { + env_proxy = env_proxy.substr(pos + 2); + } + if ((pos = env_proxy.find('/')) != string::npos) + { + env_proxy = env_proxy.substr(0, pos); + } - HTTPSClientSession::setGlobalProxyConfig(proxyconfig); + if ((pos = env_proxy.find('@')) != string::npos) + { + string hostport = env_proxy.substr(pos + 1); + string userpw = env_proxy.substr(0, pos); + parent.set_proxy(hostport, userpw); + } + else + { + parent.set_proxy(env_proxy); + } } catch (const std::exception &) { @@ -85,35 +95,28 @@ API::http::~http() Poco::Net::uninitializeSSL(); } -void API::http::inherit_proxy() +void API::http::set_proxy(const string &hostport, const string &userpw) { // 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)); + size_t pos = hostport.find(':'); + + proxyconfig.host = hostport.substr(0, pos); + if (pos != string::npos) + { + proxyconfig.port = std::stoi(hostport.substr(pos + 1)); + } if (!userpw.empty()) { pos = userpw.find(':'); - if (pos == string::npos) + proxyconfig.username = userpw.substr(0, pos); + if (pos != string::npos) { - proxyconfig.username = userpw; - } - else - { - proxyconfig.username = userpw.substr(0, pos); - proxyconfig.password = std::stoi(userpw.substr(pos + 1)); + proxyconfig.password = userpw.substr(pos + 1); } } diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index 1b0e4c3..332603c 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -36,8 +36,6 @@ API::API(const string &instance, const string &access_token) , _useragent(string("mastodon-cpp/") + global::version) , _http(*this, instance, access_token) , _exceptions(false) -, _proxy("") -, _proxy_userpw("") { bool fash = false; const std::regex re_gab("(?:\\.|^)gab\\.[^\\.]+$"); @@ -293,18 +291,9 @@ bool API::exceptions() const return _exceptions; } -void API::set_proxy(const string &proxy, const string &userpw) +void API::set_proxy(const string &hostport, const string &userpw) { - _proxy = proxy; - _proxy_userpw = userpw; - - _http.inherit_proxy(); -} - -void API::get_proxy(string &proxy, string &userpw) const -{ - proxy = _proxy; - userpw = _proxy_userpw; + _http.set_proxy(hostport, userpw); } const parameters API::delete_params(const parameters ¶ms, diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index c051a11..58c7b8a 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -143,11 +143,14 @@ namespace Mastodon std::mutex &get_mutex(); /*! - * @brief Inherit proxy from parent. Do not call this. + * @brief Set proxy. Do not call this directly. + * + * @param hostport host[:port] + * @param userpw user[:password] * * @since 0.110.0 */ - void inherit_proxy(); + void set_proxy(const string &hostport, const string &userpw); private: const API &parent; @@ -445,25 +448,15 @@ namespace Mastodon /*! * @brief Sets the proxy. * - * Since mastodon-cpp is built on libcurl, it respects the same - * proxy environment variables. See `man curl`. + * Both the username and the password will be URL decoded + * before use. * - * @param proxy host:port - * @param userpw username[:password] (optional) + * @param hostport host[:port] + * @param userpw username[:password] (optional) * * @since 0.15.0 */ - void set_proxy(const string &proxy, const string &userpw = ""); - - /*! - * @brief For internal use - * - * @param proxy URL - * @param userpw username:password - * - * @since 0.15.1 - */ - void get_proxy(string &proxy, string &userpw) const; + void set_proxy(const string &hostport, const string &userpw = ""); /*! * @brief Make a GET request that doesn't require parameters. @@ -652,8 +645,6 @@ namespace Mastodon string _useragent; http _http; bool _exceptions; - string _proxy; - string _proxy_userpw; /*! * @brief Converts map of parameters into a string.