Use regex to extract proxy settings from environment variable.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
c7e36a7297
commit
c19f5681a2
37
src/http.cpp
37
src/http.cpp
|
@ -14,10 +14,10 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <regex>
|
||||||
#include <Poco/Net/HTTPClientSession.h>
|
#include <Poco/Net/HTTPClientSession.h>
|
||||||
#include <Poco/Net/HTTPSClientSession.h>
|
#include <Poco/Net/HTTPSClientSession.h>
|
||||||
#include <Poco/Net/HTTPRequest.h>
|
#include <Poco/Net/HTTPRequest.h>
|
||||||
|
@ -35,6 +35,9 @@ using std::endl;
|
||||||
using std::istream;
|
using std::istream;
|
||||||
using std::unique_ptr;
|
using std::unique_ptr;
|
||||||
using std::make_unique;
|
using std::make_unique;
|
||||||
|
using std::regex;
|
||||||
|
using std::regex_search;
|
||||||
|
using std::smatch;
|
||||||
using Poco::Net::HTTPClientSession;
|
using Poco::Net::HTTPClientSession;
|
||||||
using Poco::Net::HTTPSClientSession;
|
using Poco::Net::HTTPSClientSession;
|
||||||
using Poco::Net::HTTPRequest;
|
using Poco::Net::HTTPRequest;
|
||||||
|
@ -47,31 +50,21 @@ void set_proxy()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
HTTPClientSession::ProxyConfig proxy;
|
HTTPSClientSession::ProxyConfig proxyconfig;
|
||||||
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 /.
|
if (regex_search(env_proxy, match, re_proxy))
|
||||||
if ((pos = proxy_env.find("//")) != string::npos)
|
|
||||||
{
|
{
|
||||||
proxy_env = proxy_env.substr(pos + 2);
|
proxyconfig.host = match[3].str();
|
||||||
}
|
proxyconfig.port = std::stoi(match[4].str());
|
||||||
if ((pos = proxy_env.find('/')) != string::npos)
|
proxyconfig.username = match[1].str();
|
||||||
{
|
proxyconfig.password = match[2].str();
|
||||||
proxy_env = proxy_env.substr(0, pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((pos = proxy_env.find(':')) != string::npos)
|
HTTPSClientSession::setGlobalProxyConfig(proxyconfig);
|
||||||
{
|
|
||||||
proxy.host = proxy_env.substr(0, pos);
|
|
||||||
proxy.port = std::stoi(proxy_env.substr(pos + 1));
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
proxy.host = proxy_env;
|
|
||||||
}
|
|
||||||
|
|
||||||
HTTPClientSession::setGlobalProxyConfig(proxy);
|
|
||||||
}
|
}
|
||||||
catch (const std::exception &)
|
catch (const std::exception &)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user