From c19f5681a222fce5d1fa5b793e84e761df959101 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 25 Aug 2019 04:22:32 +0200 Subject: [PATCH 1/2] Use regex to extract proxy settings from environment variable. --- src/http.cpp | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/http.cpp b/src/http.cpp index a022b5e..628ed34 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -14,10 +14,10 @@ * along with this program. If not, see . */ -#include #include #include #include +#include #include #include #include @@ -35,6 +35,9 @@ using std::endl; using std::istream; using std::unique_ptr; using std::make_unique; +using std::regex; +using std::regex_search; +using std::smatch; using Poco::Net::HTTPClientSession; using Poco::Net::HTTPSClientSession; using Poco::Net::HTTPRequest; @@ -47,31 +50,21 @@ void set_proxy() { try { - HTTPClientSession::ProxyConfig proxy; - string proxy_env = Environment::get("http_proxy"); - size_t pos; + HTTPSClientSession::ProxyConfig proxyconfig; + string env_proxy = Environment::get("http_proxy"); + regex re_proxy("^(?:https?://)?(?:([^:]+):?([^@]*)@)?" // user:password + "([^:]+):([[:digit:]]+/?)"); // host:port + smatch match; - // Only keep text between // and /. - if ((pos = proxy_env.find("//")) != string::npos) + if (regex_search(env_proxy, match, re_proxy)) { - proxy_env = proxy_env.substr(pos + 2); - } - if ((pos = proxy_env.find('/')) != string::npos) - { - proxy_env = proxy_env.substr(0, pos); - } + proxyconfig.host = match[3].str(); + proxyconfig.port = std::stoi(match[4].str()); + proxyconfig.username = match[1].str(); + proxyconfig.password = match[2].str(); - if ((pos = proxy_env.find(':')) != string::npos) - { - proxy.host = proxy_env.substr(0, pos); - proxy.port = std::stoi(proxy_env.substr(pos + 1)); + HTTPSClientSession::setGlobalProxyConfig(proxyconfig); } - else - { - proxy.host = proxy_env; - } - - HTTPClientSession::setGlobalProxyConfig(proxy); } catch (const std::exception &) { From d9d28dd707aa6249b5a288c3e8b95b57fa60014a Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 25 Aug 2019 05:05:36 +0200 Subject: [PATCH 2/2] Percent-decode proxy username and password. --- src/http.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/http.cpp b/src/http.cpp index 628ed34..71cbdc1 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -58,10 +58,14 @@ void set_proxy() if (regex_search(env_proxy, match, re_proxy)) { + string username, password; + Poco::URI::decode(match[1].str(), username); + Poco::URI::decode(match[2].str(), password); + proxyconfig.host = match[3].str(); proxyconfig.port = std::stoi(match[4].str()); - proxyconfig.username = match[1].str(); - proxyconfig.password = match[2].str(); + proxyconfig.username = username; + proxyconfig.password = password; HTTPSClientSession::setGlobalProxyConfig(proxyconfig); }