Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPS proxy support (Second attempt) #3691

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 32 additions & 39 deletions Net/include/Poco/Net/HTTPClientSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

#include "Poco/Net/Net.h"
#include "Poco/Net/HTTPSession.h"
#include "Poco/Net/HTTPSessionFactory.h"
#include "Poco/Net/HTTPBasicCredentials.h"
#include "Poco/Net/HTTPDigestCredentials.h"
#include "Poco/Net/HTTPNTLMCredentials.h"
#include "Poco/Net/ProxyConfig.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/SharedPtr.h"
#include <istream>
Expand All @@ -32,7 +34,6 @@
namespace Poco {
namespace Net {


class HTTPRequest;
class HTTPResponse;

Expand Down Expand Up @@ -65,40 +66,6 @@ class Net_API HTTPClientSession: public HTTPSession
/// set up a session through a proxy.
{
public:
enum ProxyAuthentication
{
PROXY_AUTH_NONE, /// No proxy authentication
PROXY_AUTH_HTTP_BASIC, /// HTTP Basic proxy authentication (default, if username and password are supplied)
PROXY_AUTH_HTTP_DIGEST, /// HTTP Digest proxy authentication
PROXY_AUTH_NTLM /// NTLMv2 proxy authentication
};

struct ProxyConfig
/// HTTP proxy server configuration.
{
ProxyConfig():
port(HTTP_PORT),
authMethod(PROXY_AUTH_HTTP_BASIC)
{
}

std::string host;
/// Proxy server host name or IP address.
Poco::UInt16 port;
/// Proxy server TCP port.
std::string username;
/// Proxy server username.
std::string password;
/// Proxy server password.
std::string nonProxyHosts;
/// A regular expression defining hosts for which the proxy should be bypassed,
/// e.g. "localhost|127\.0\.0\.1|192\.168\.0\.\d+". Can also be an empty
/// string to disable proxy bypassing.

ProxyAuthentication authMethod;
/// The authentication method to use - HTTP Basic or NTLM.
};

HTTPClientSession();
/// Creates an unconnected HTTPClientSession.

Expand Down Expand Up @@ -167,21 +134,33 @@ class Net_API HTTPClientSession: public HTTPSession
const SocketAddress& getSourceAddress6();
/// Returns the last IPV6 source address set with setSourceAddress

void setProxy(const std::string& host, Poco::UInt16 port = HTTPSession::HTTP_PORT);
/// Sets the proxy host name and port number.
void setProxy(const std::string& host, Poco::UInt16 port = HTTPSession::HTTP_PORT, const std::string& protocol = "http", bool tunnel = true);
/// Sets the proxy host name, port number, protocol (http or https) and tunnel behaviour.

void setProxyHost(const std::string& host);
/// Sets the host name of the proxy server.

void setProxyPort(Poco::UInt16 port);
/// Sets the port number of the proxy server.

void setProxyProtocol(const std::string& protocol);
/// Sets the proxy protocol (http or https).

void setProxyTunnel(bool tunnel);
/// If 'true' proxy will be used as tunnel.

const std::string& getProxyHost() const;
/// Returns the proxy host name.

Poco::UInt16 getProxyPort() const;
/// Returns the proxy port number.

const std::string& getProxyProtocol() const;
/// Returns the proxy protocol.

bool isProxyTunnel() const;
/// Returns 'true' if proxy is configured as tunnel.

void setProxyCredentials(const std::string& username, const std::string& password);
/// Sets the username and password for proxy authentication.
/// Only Basic authentication is supported.
Expand Down Expand Up @@ -363,6 +342,8 @@ class Net_API HTTPClientSession: public HTTPSession
/// Calls proxyConnect() and attaches the resulting StreamSocket
/// to the HTTPClientSession.

HTTPSessionFactory _proxySessionFactory;
/// Factory to create HTTPClientSession to proxy.
private:
using OStreamPtr = Poco::SharedPtr<std::ostream>;
using IStreamPtr = Poco::SharedPtr<std::istream>;
Expand Down Expand Up @@ -422,6 +403,18 @@ inline Poco::UInt16 HTTPClientSession::getProxyPort() const
}


inline const std::string& HTTPClientSession::getProxyProtocol() const
{
return _proxyConfig.protocol;
}


inline bool HTTPClientSession::isProxyTunnel() const
{
return _proxyConfig.tunnel;
}


inline const std::string& HTTPClientSession::getProxyUsername() const
{
return _proxyConfig.username;
Expand All @@ -434,13 +427,13 @@ inline const std::string& HTTPClientSession::getProxyPassword() const
}


inline const HTTPClientSession::ProxyConfig& HTTPClientSession::getProxyConfig() const
inline const ProxyConfig& HTTPClientSession::getProxyConfig() const
{
return _proxyConfig;
}


inline const HTTPClientSession::ProxyConfig& HTTPClientSession::getGlobalProxyConfig()
inline const ProxyConfig& HTTPClientSession::getGlobalProxyConfig()
{
return _globalProxyConfig;
}
Expand Down
14 changes: 7 additions & 7 deletions Net/include/Poco/Net/HTTPSessionFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


#include "Poco/Net/Net.h"
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/ProxyConfig.h"
#include "Poco/Mutex.h"
#include "Poco/URI.h"
#include "Poco/SingletonHolder.h"
Expand All @@ -30,7 +30,7 @@
namespace Poco {
namespace Net {


class HTTPClientSession;
class HTTPSessionInstantiator;


Expand All @@ -52,7 +52,7 @@ class Net_API HTTPSessionFactory
HTTPSessionFactory(const std::string& proxyHost, Poco::UInt16 proxyPort);
/// Creates the HTTPSessionFactory and sets the proxy host and port.

HTTPSessionFactory(const HTTPClientSession::ProxyConfig& proxyConfig);
HTTPSessionFactory(const ProxyConfig& proxyConfig);
/// Creates the HTTPSessionFactory and sets the proxy configuration.

~HTTPSessionFactory();
Expand Down Expand Up @@ -97,10 +97,10 @@ class Net_API HTTPSessionFactory
const std::string& proxyPassword() const;
/// Returns the password for proxy authorization.

void setProxyConfig(const HTTPClientSession::ProxyConfig& proxyConfig);
void setProxyConfig(const ProxyConfig& proxyConfig);
/// Sets the proxy configuration.

const HTTPClientSession::ProxyConfig& getProxyConfig() const;
const ProxyConfig& getProxyConfig() const;
/// Returns the proxy configuration.

static HTTPSessionFactory& defaultFactory();
Expand All @@ -122,7 +122,7 @@ class Net_API HTTPSessionFactory
typedef std::map<std::string, InstantiatorInfo> Instantiators;

Instantiators _instantiators;
HTTPClientSession::ProxyConfig _proxyConfig;
ProxyConfig _proxyConfig;

mutable Poco::FastMutex _mutex;
};
Expand Down Expand Up @@ -155,7 +155,7 @@ inline const std::string& HTTPSessionFactory::proxyPassword() const
}


inline const HTTPClientSession::ProxyConfig& HTTPSessionFactory::getProxyConfig() const
inline const ProxyConfig& HTTPSessionFactory::getProxyConfig() const
{
return _proxyConfig;
}
Expand Down
8 changes: 4 additions & 4 deletions Net/include/Poco/Net/HTTPSessionInstantiator.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ class Net_API HTTPSessionInstantiator
/// Unregisters the factory with the global HTTPSessionFactory.

protected:
void setProxyConfig(const HTTPClientSession::ProxyConfig& proxyConfig);
void setProxyConfig(const ProxyConfig& proxyConfig);
/// Sets the proxy configuration.

const HTTPClientSession::ProxyConfig& getProxyConfig() const;
const ProxyConfig& getProxyConfig() const;
/// Returns the proxy configuration.

private:
HTTPClientSession::ProxyConfig _proxyConfig;
ProxyConfig _proxyConfig;

friend class HTTPSessionFactory;
};
Expand All @@ -68,7 +68,7 @@ class Net_API HTTPSessionInstantiator
//
// inlines
//
inline const HTTPClientSession::ProxyConfig& HTTPSessionInstantiator::getProxyConfig() const
inline const ProxyConfig& HTTPSessionInstantiator::getProxyConfig() const
{
return _proxyConfig;
}
Expand Down
65 changes: 65 additions & 0 deletions Net/include/Poco/Net/ProxyConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// ProxyConfig.h
//
// Library: Net
// Package: HTTP
// Module: ProxyConfig
//
// Definition of the ProxyConfig class.
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//

#ifndef Net_ProxyConfig_INCLUDED
#define Net_ProxyConfig_INCLUDED

#include "Poco/Net/Net.h"
#include "Poco/Net/HTTPSession.h"

namespace Poco {
namespace Net {
enum ProxyAuthentication {
PROXY_AUTH_NONE, /// No proxy authentication
PROXY_AUTH_HTTP_BASIC, /// HTTP Basic proxy authentication (default, if username and password are supplied)
PROXY_AUTH_HTTP_DIGEST, /// HTTP Digest proxy authentication
PROXY_AUTH_NTLM /// NTLMv2 proxy authentication
};

struct ProxyConfig
/// HTTP proxy server configuration.
{
ProxyConfig() :
port(HTTPSession::HTTP_PORT),
protocol("http"),
tunnel(true),
authMethod(PROXY_AUTH_HTTP_BASIC) {
}

std::string host;
/// Proxy server host name or IP address.
Poco::UInt16 port;
/// Proxy server TCP port.
std::string protocol;
/// Protocol to use (http or https).
bool tunnel;
/// Use proxy as tunnel (establish 2-way communication through CONNECT request).
/// If tunnel option is 'false' request will be send directly to proxy without CONNECT request.
std::string username;
/// Proxy server username.
std::string password;
/// Proxy server password.
std::string nonProxyHosts;
/// A regular expression defining hosts for which the proxy should be bypassed,
/// e.g. "localhost|127\.0\.0\.1|192\.168\.0\.\d+". Can also be an empty
/// string to disable proxy bypassing.

ProxyAuthentication authMethod;
/// The authentication method to use - HTTP Basic or NTLM.
};

} } // namespace Poco::Net

#endif // Net_ProxyConfig_INCLUDED
Loading
Loading