Rewrite parsing of http_proxy environment variable.

The regex matches host, port, username and password.
This commit is contained in:
tastytea 2019-08-21 10:39:14 +02:00
parent 915c85e9e9
commit 41a580b32b
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 13 additions and 23 deletions

View File

@ -18,6 +18,7 @@
#include <functional> // std::bind #include <functional> // std::bind
#include <exception> #include <exception>
#include <thread> #include <thread>
#include <regex>
#include <Poco/Net/HTTPSClientSession.h> #include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/HTTPRequest.h> #include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h> #include <Poco/Net/HTTPResponse.h>
@ -29,13 +30,15 @@
#include <Poco/Net/SSLException.h> #include <Poco/Net/SSLException.h>
#include "debug.hpp" #include "debug.hpp"
#include "mastodon-cpp.hpp" #include "mastodon-cpp.hpp"
#include <string>
using namespace Mastodon; using namespace Mastodon;
using std::cerr; using std::cerr;
using std::istream; using std::istream;
using std::make_unique; using std::make_unique;
using std::move; using std::move;
using std::regex;
using std::regex_search;
using std::smatch;
using Poco::Net::HTTPSClientSession; using Poco::Net::HTTPSClientSession;
using Poco::Net::HTTPRequest; using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse; using Poco::Net::HTTPResponse;
@ -53,33 +56,20 @@ API::http::http(const API &api, const string &instance,
Poco::Net::initializeSSL(); Poco::Net::initializeSSL();
// FIXME: rewrite set_proxy() that it calls set_proxy() here. // FIXME: rewrite set_proxy() that it calls set_proxy() here.
// FIXME: Username and password for proxy.
try try
{ {
HTTPSClientSession::ProxyConfig proxy; HTTPSClientSession::ProxyConfig proxy;
string proxy_env = Environment::get("http_proxy"); string env_proxy = Environment::get("http_proxy");
size_t pos; regex re_proxy("^(?:https?://)?(?:([^:]+):?([^@]*)@)?" // user:password
"([^:]+):([[:digit:]]+/?)"); // host:port
smatch match;
// Only keep text between // and /. regex_search(env_proxy, match, re_proxy);
if ((pos = proxy_env.find("//")) != string::npos) proxy.host = match[3].str();
{ proxy.port = std::stoi(match[4].str());
proxy_env = proxy_env.substr(pos + 2); proxy.username = match[1].str();
} proxy.password = match[2].str();
if ((pos = proxy_env.find('/')) != string::npos)
{
proxy_env = proxy_env.substr(0, pos);
}
if ((pos = proxy_env.find(':')) != string::npos)
{
proxy.host = proxy_env.substr(0, pos);
proxy.port = std::stoi(proxy_env.substr(pos + 1));
}
else
{
proxy.host = proxy_env;
}
HTTPSClientSession::setGlobalProxyConfig(proxy); HTTPSClientSession::setGlobalProxyConfig(proxy);
} }