Mastodon::http inherits proxy config from parent.
continuous-integration/drone/push Build is passing Details

Ff set_proxy() is called, _http.inherit_proxy() is called.
This commit is contained in:
tastytea 2019-08-21 11:06:41 +02:00
parent 188033a0c1
commit acbcb6224e
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 60 additions and 8 deletions

View File

@ -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;

View File

@ -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

View File

@ -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
*/