Moved set_proxy() to API::http().
continuous-integration/drone/push Build is failing Details

API::set_proxy() calls API::http::set_proxy(). Deleted API::get_proxy().
This commit is contained in:
tastytea 2019-08-25 04:50:08 +02:00
parent acbcb6224e
commit d2a4d835de
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 43 additions and 60 deletions

View File

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

View File

@ -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 &params,

View File

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