#3089: HTTPSessionFactory does not support HTTPClientSession::ProxyConfig

This commit is contained in:
Günter Obiltschnig
2021-06-15 09:45:01 +02:00
parent d1e9452414
commit 59ae91e5ba
6 changed files with 72 additions and 93 deletions

View File

@@ -19,6 +19,7 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Mutex.h"
#include "Poco/URI.h"
#include "Poco/SingletonHolder.h"
@@ -31,7 +32,6 @@ namespace Net {
class HTTPSessionInstantiator;
class HTTPClientSession;
class Net_API HTTPSessionFactory
@@ -52,6 +52,9 @@ public:
HTTPSessionFactory(const std::string& proxyHost, Poco::UInt16 proxyPort);
/// Creates the HTTPSessionFactory and sets the proxy host and port.
HTTPSessionFactory(const HTTPClientSession::ProxyConfig& proxyConfig);
/// Creates the HTTPSessionFactory and sets the proxy configuration.
~HTTPSessionFactory();
/// Destroys the HTTPSessionFactory.
@@ -78,22 +81,28 @@ public:
const std::string& proxyHost() const;
/// Returns the proxy host, if one has been set, or an empty string otherwise.
Poco::UInt16 proxyPort() const;
/// Returns the proxy port number, if one has been set, or zero otherwise.
void setProxy(const std::string& proxyHost, Poco::UInt16 proxyPort);
/// Sets the proxy host and port number.
void setProxyCredentials(const std::string& username, const std::string& password);
/// Sets the username and password for proxy authorization (Basic auth only).
const std::string& proxyUsername() const;
/// Returns the username for proxy authorization.
const std::string& proxyPassword() const;
/// Returns the password for proxy authorization.
void setProxyConfig(const HTTPClientSession::ProxyConfig& proxyConfig);
/// Sets the proxy configuration.
const HTTPClientSession::ProxyConfig& getProxyConfig() const;
/// Returns the proxy configuration.
static HTTPSessionFactory& defaultFactory();
/// Returns the default HTTPSessionFactory.
@@ -109,14 +118,11 @@ private:
HTTPSessionFactory(const HTTPSessionFactory&);
HTTPSessionFactory& operator = (const HTTPSessionFactory&);
typedef std::map<std::string, InstantiatorInfo> Instantiators;
Instantiators _instantiators;
std::string _proxyHost;
Poco::UInt16 _proxyPort;
std::string _proxyUsername;
std::string _proxyPassword;
HTTPClientSession::ProxyConfig _proxyConfig;
mutable Poco::FastMutex _mutex;
};
@@ -127,25 +133,31 @@ private:
//
inline const std::string& HTTPSessionFactory::proxyHost() const
{
return _proxyHost;
return _proxyConfig.host;
}
inline Poco::UInt16 HTTPSessionFactory::proxyPort() const
{
return _proxyPort;
return _proxyConfig.port;
}
inline const std::string& HTTPSessionFactory::proxyUsername() const
{
return _proxyUsername;
return _proxyConfig.username;
}
inline const std::string& HTTPSessionFactory::proxyPassword() const
{
return _proxyPassword;
return _proxyConfig.password;
}
inline const HTTPClientSession::ProxyConfig& HTTPSessionFactory::getProxyConfig() const
{
return _proxyConfig;
}

View File

@@ -19,7 +19,7 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/HTTPSession.h"
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/URI.h"
@@ -27,9 +27,6 @@ namespace Poco {
namespace Net {
class HTTPClientSession;
class Net_API HTTPSessionInstantiator
/// A factory for HTTPClientSession objects.
///
@@ -55,31 +52,15 @@ public:
/// Unregisters the factory with the global HTTPSessionFactory.
protected:
void setProxy(const std::string& host, Poco::UInt16 port);
/// Sets the proxy host and port.
/// Called by HTTPSessionFactory.
const std::string& proxyHost() const;
/// Returns the proxy post.
void setProxyConfig(const HTTPClientSession::ProxyConfig& proxyConfig);
/// Sets the proxy configuration.
Poco::UInt16 proxyPort() const;
/// Returns the proxy port.
void setProxyCredentials(const std::string& username, const std::string& password);
/// Sets the username and password for proxy authorization (Basic auth only).
const std::string& proxyUsername() const;
/// Returns the username for proxy authorization.
const std::string& proxyPassword() const;
/// Returns the password for proxy authorization.
const HTTPClientSession::ProxyConfig& getProxyConfig() const;
/// Returns the proxy configuration.
private:
std::string _proxyHost;
Poco::UInt16 _proxyPort;
std::string _proxyUsername;
std::string _proxyPassword;
HTTPClientSession::ProxyConfig _proxyConfig;
friend class HTTPSessionFactory;
};
@@ -87,27 +68,9 @@ private:
//
// inlines
//
inline const std::string& HTTPSessionInstantiator::proxyHost() const
inline const HTTPClientSession::ProxyConfig& HTTPSessionInstantiator::getProxyConfig() const
{
return _proxyHost;
}
inline Poco::UInt16 HTTPSessionInstantiator::proxyPort() const
{
return _proxyPort;
}
inline const std::string& HTTPSessionInstantiator::proxyUsername() const
{
return _proxyUsername;
}
inline const std::string& HTTPSessionInstantiator::proxyPassword() const
{
return _proxyPassword;
return _proxyConfig;
}

View File

@@ -27,15 +27,20 @@ namespace Poco {
namespace Net {
HTTPSessionFactory::HTTPSessionFactory():
_proxyPort(0)
HTTPSessionFactory::HTTPSessionFactory()
{
}
HTTPSessionFactory::HTTPSessionFactory(const std::string& proxyHost, Poco::UInt16 proxyPort):
_proxyHost(proxyHost),
_proxyPort(proxyPort)
HTTPSessionFactory::HTTPSessionFactory(const std::string& proxyHost, Poco::UInt16 proxyPort)
{
_proxyConfig.host = proxyHost;
_proxyConfig.port = proxyPort;
}
HTTPSessionFactory::HTTPSessionFactory(const HTTPClientSession::ProxyConfig& proxyConfig):
_proxyConfig(proxyConfig)
{
}
@@ -55,7 +60,7 @@ void HTTPSessionFactory::registerProtocol(const std::string& protocol, HTTPSessi
FastMutex::ScopedLock lock(_mutex);
std::pair<Instantiators::iterator, bool> tmp = _instantiators.insert(make_pair(protocol, InstantiatorInfo(pSessionInstantiator)));
if (!tmp.second)
if (!tmp.second)
{
++tmp.first->second.cnt;
delete pSessionInstantiator;
@@ -66,7 +71,7 @@ void HTTPSessionFactory::registerProtocol(const std::string& protocol, HTTPSessi
void HTTPSessionFactory::unregisterProtocol(const std::string& protocol)
{
FastMutex::ScopedLock lock(_mutex);
Instantiators::iterator it = _instantiators.find(protocol);
if (it != _instantiators.end())
{
@@ -84,7 +89,7 @@ void HTTPSessionFactory::unregisterProtocol(const std::string& protocol)
bool HTTPSessionFactory::supportsProtocol(const std::string& protocol)
{
FastMutex::ScopedLock lock(_mutex);
Instantiators::iterator it = _instantiators.find(protocol);
return it != _instantiators.end();
}
@@ -93,14 +98,13 @@ bool HTTPSessionFactory::supportsProtocol(const std::string& protocol)
HTTPClientSession* HTTPSessionFactory::createClientSession(const Poco::URI& uri)
{
FastMutex::ScopedLock lock(_mutex);
if (uri.isRelative()) throw Poco::UnknownURISchemeException("Relative URIs are not supported by HTTPSessionFactory.");
Instantiators::iterator it = _instantiators.find(uri.getScheme());
if (it != _instantiators.end())
{
it->second.pIn->setProxy(_proxyHost, _proxyPort);
it->second.pIn->setProxyCredentials(_proxyUsername, _proxyPassword);
it->second.pIn->setProxyConfig(_proxyConfig);
return it->second.pIn->createClientSession(uri);
}
else throw Poco::UnknownURISchemeException(uri.getScheme());
@@ -111,8 +115,8 @@ void HTTPSessionFactory::setProxy(const std::string& host, Poco::UInt16 port)
{
FastMutex::ScopedLock lock(_mutex);
_proxyHost = host;
_proxyPort = port;
_proxyConfig.host = host;
_proxyConfig.port = port;
}
@@ -120,8 +124,16 @@ void HTTPSessionFactory::setProxyCredentials(const std::string& username, const
{
FastMutex::ScopedLock lock(_mutex);
_proxyUsername = username;
_proxyPassword = password;
_proxyConfig.username = username;
_proxyConfig.password = password;
}
void HTTPSessionFactory::setProxyConfig(const HTTPClientSession::ProxyConfig& proxyConfig)
{
FastMutex::ScopedLock lock(_mutex);
_proxyConfig = proxyConfig;
}

View File

@@ -24,8 +24,7 @@ namespace Poco {
namespace Net {
HTTPSessionInstantiator::HTTPSessionInstantiator():
_proxyPort(0)
HTTPSessionInstantiator::HTTPSessionInstantiator()
{
}
@@ -39,10 +38,9 @@ HTTPClientSession* HTTPSessionInstantiator::createClientSession(const Poco::URI&
{
poco_assert (uri.getScheme() == "http");
HTTPClientSession* pSession = new HTTPClientSession(uri.getHost(), uri.getPort());
if (!proxyHost().empty())
if (!getProxyConfig().host.empty())
{
pSession->setProxy(proxyHost(), proxyPort());
pSession->setProxyCredentials(proxyUsername(), proxyPassword());
pSession->setProxyConfig(getProxyConfig());
}
return pSession;
}
@@ -60,18 +58,11 @@ void HTTPSessionInstantiator::unregisterInstantiator()
}
void HTTPSessionInstantiator::setProxy(const std::string& host, Poco::UInt16 port)
void HTTPSessionInstantiator::setProxyConfig(const HTTPClientSession::ProxyConfig& proxyConfig)
{
_proxyHost = host;
_proxyPort = port;
_proxyConfig = proxyConfig;
}
void HTTPSessionInstantiator::setProxyCredentials(const std::string& username, const std::string& password)
{
_proxyUsername = username;
_proxyPassword = password;
}
} } // namespace Poco::Net

View File

@@ -41,10 +41,9 @@ HTTPClientSession* HTTPSSessionInstantiator::createClientSession(const Poco::URI
{
poco_assert (uri.getScheme() == "https");
HTTPSClientSession* pSession = _pContext.isNull() ? new HTTPSClientSession(uri.getHost(), uri.getPort()) : new HTTPSClientSession(uri.getHost(), uri.getPort(), _pContext);
if (!proxyHost().empty())
if (!getProxyConfig().host.empty())
{
pSession->setProxy(proxyHost(), proxyPort());
pSession->setProxyCredentials(proxyUsername(), proxyPassword());
pSession->setProxyConfig(getProxyConfig());
}
return pSession;
}

View File

@@ -41,8 +41,10 @@ HTTPClientSession* HTTPSSessionInstantiator::createClientSession(const Poco::URI
{
poco_assert (uri.getScheme() == "https");
HTTPSClientSession* pSession = _pContext.isNull() ? new HTTPSClientSession(uri.getHost(), uri.getPort()) : new HTTPSClientSession(uri.getHost(), uri.getPort(), _pContext);
pSession->setProxy(proxyHost(), proxyPort());
pSession->setProxyCredentials(proxyUsername(), proxyPassword());
if (!getProxyConfig().host.empty())
{
pSession->setProxyConfig(getProxyConfig());
}
return pSession;
}