Skip to content

Commit 781acfa

Browse files
committed
added nonProxyHosts to ProxyConfig to allow bypassing proxy for certain hosts
1 parent b576a66 commit 781acfa

6 files changed

Lines changed: 63 additions & 16 deletions

File tree

Net/include/Poco/Net/HTTPClientSession.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// HTTPClientSession.h
33
//
4-
// $Id: //poco/1.4/Net/include/Poco/Net/HTTPClientSession.h#5 $
4+
// $Id: //poco/1.4/Net/include/Poco/Net/HTTPClientSession.h#7 $
55
//
66
// Library: Net
77
// Package: HTTPClient
@@ -72,10 +72,18 @@ class Net_API HTTPClientSession: public HTTPSession
7272
{
7373
}
7474

75-
std::string host; /// Proxy server host name or IP address.
76-
Poco::UInt16 port; /// Proxy server TCP port.
77-
std::string username; /// Proxy server username.
78-
std::string password; /// Proxy server password.
75+
std::string host;
76+
/// Proxy server host name or IP address.
77+
Poco::UInt16 port;
78+
/// Proxy server TCP port.
79+
std::string username;
80+
/// Proxy server username.
81+
std::string password;
82+
/// Proxy server password.
83+
std::string nonProxyHosts;
84+
/// A regular expression defining hosts for which the proxy should be bypassed,
85+
/// e.g. "localhost|127\.0\.0\.1|192\.168\.0\.\d+". Can also be an empty
86+
/// string to disable proxy bypassing.
7987
};
8088

8189
HTTPClientSession();
@@ -231,6 +239,10 @@ class Net_API HTTPClientSession: public HTTPSession
231239
virtual bool secure() const;
232240
/// Return true iff the session uses SSL or TLS,
233241
/// or false otherwise.
242+
243+
bool bypassProxy() const;
244+
/// Returns true if the proxy should be bypassed
245+
/// for the current host.
234246

235247
protected:
236248
enum

Net/src/HTTPClientSession.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// HTTPClientSession.cpp
33
//
4-
// $Id: //poco/1.4/Net/src/HTTPClientSession.cpp#11 $
4+
// $Id: //poco/1.4/Net/src/HTTPClientSession.cpp#14 $
55
//
66
// Library: Net
77
// Package: HTTPClient
@@ -25,7 +25,7 @@
2525
#include "Poco/Net/NetException.h"
2626
#include "Poco/NumberFormatter.h"
2727
#include "Poco/CountingStream.h"
28-
#include "Poco/Base64Encoder.h"
28+
#include "Poco/RegularExpression.h"
2929
#include <sstream>
3030

3131

@@ -195,7 +195,7 @@ std::ostream& HTTPClientSession::sendRequest(HTTPRequest& request)
195195
request.setKeepAlive(false);
196196
if (!request.has(HTTPRequest::HOST))
197197
request.setHost(_host, _port);
198-
if (!_proxyConfig.host.empty())
198+
if (!_proxyConfig.host.empty() && !bypassProxy())
199199
{
200200
request.setURI(proxyRequestPrefix() + request.getURI());
201201
proxyAuthenticate(request);
@@ -327,7 +327,7 @@ int HTTPClientSession::write(const char* buffer, std::streamsize length)
327327

328328
void HTTPClientSession::reconnect()
329329
{
330-
if (_proxyConfig.host.empty())
330+
if (_proxyConfig.host.empty() || bypassProxy())
331331
{
332332
SocketAddress addr(_host, _port);
333333
connect(addr);
@@ -405,4 +405,14 @@ void HTTPClientSession::proxyTunnel()
405405
}
406406

407407

408+
bool HTTPClientSession::bypassProxy() const
409+
{
410+
if (!_proxyConfig.nonProxyHosts.empty())
411+
{
412+
return RegularExpression::match(_host, _proxyConfig.nonProxyHosts, RegularExpression::RE_CASELESS | RegularExpression::RE_ANCHORED);
413+
}
414+
else return false;
415+
}
416+
417+
408418
} } // namespace Poco::Net

Net/testsuite/src/HTTPClientSessionTest.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// HTTPClientSessionTest.cpp
33
//
4-
// $Id: //poco/1.4/Net/testsuite/src/HTTPClientSessionTest.cpp#1 $
4+
// $Id: //poco/1.4/Net/testsuite/src/HTTPClientSessionTest.cpp#2 $
55
//
66
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
77
// and Contributors.
@@ -279,6 +279,27 @@ void HTTPClientSessionTest::testProxyAuth()
279279
}
280280

281281

282+
void HTTPClientSessionTest::testBypassProxy()
283+
{
284+
HTTPClientSession::ProxyConfig proxyConfig;
285+
proxyConfig.host = "proxy.domain.com";
286+
proxyConfig.port = 80;
287+
proxyConfig.nonProxyHosts = "localhost|127\\.0\\.0\\.1";
288+
289+
HTTPClientSession s1("localhost", 80);
290+
s1.setProxyConfig(proxyConfig);
291+
assert (s1.bypassProxy());
292+
293+
HTTPClientSession s2("127.0.0.1", 80);
294+
s2.setProxyConfig(proxyConfig);
295+
assert (s2.bypassProxy());
296+
297+
HTTPClientSession s3("www.appinf.com", 80);
298+
s3.setProxyConfig(proxyConfig);
299+
assert (!s3.bypassProxy());
300+
}
301+
302+
282303
void HTTPClientSessionTest::setUp()
283304
{
284305
}
@@ -305,6 +326,7 @@ CppUnit::Test* HTTPClientSessionTest::suite()
305326
CppUnit_addTest(pSuite, HTTPClientSessionTest, testKeepAlive);
306327
CppUnit_addTest(pSuite, HTTPClientSessionTest, testProxy);
307328
CppUnit_addTest(pSuite, HTTPClientSessionTest, testProxyAuth);
329+
CppUnit_addTest(pSuite, HTTPClientSessionTest, testBypassProxy);
308330

309331
return pSuite;
310332
}

Net/testsuite/src/HTTPClientSessionTest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class HTTPClientSessionTest: public CppUnit::TestCase
3838
void testKeepAlive();
3939
void testProxy();
4040
void testProxyAuth();
41+
void testBypassProxy();
4142

4243
void setUp();
4344
void tearDown();

NetSSL_OpenSSL/src/HTTPSClientSession.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// HTTPSClientSession.cpp
33
//
4-
// $Id: //poco/1.4/NetSSL_OpenSSL/src/HTTPSClientSession.cpp#3 $
4+
// $Id: //poco/1.4/NetSSL_OpenSSL/src/HTTPSClientSession.cpp#4 $
55
//
66
// Library: NetSSL_OpenSSL
77
// Package: HTTPSClient
@@ -145,7 +145,7 @@ void HTTPSClientSession::proxyAuthenticate(HTTPRequest& request)
145145

146146
void HTTPSClientSession::connect(const SocketAddress& address)
147147
{
148-
if (getProxyHost().empty())
148+
if (getProxyHost().empty() || bypassProxy())
149149
{
150150
SecureStreamSocket sss(socket());
151151
if (_pContext->sessionCacheEnabled())
@@ -176,7 +176,8 @@ int HTTPSClientSession::read(char* buffer, std::streamsize length)
176176
try
177177
{
178178
return HTTPSession::read(buffer, length);
179-
} catch(SSLConnectionUnexpectedlyClosedException&)
179+
}
180+
catch(SSLConnectionUnexpectedlyClosedException&)
180181
{
181182
return 0;
182183
}

NetSSL_Win/src/HTTPSClientSession.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// HTTPSClientSession.cpp
33
//
4-
// $Id: //poco/1.4/NetSSL_Win/src/HTTPSClientSession.cpp#3 $
4+
// $Id: //poco/1.4/NetSSL_Win/src/HTTPSClientSession.cpp#4 $
55
//
66
// Library: NetSSL_Win
77
// Package: HTTPSClient
@@ -145,7 +145,7 @@ void HTTPSClientSession::proxyAuthenticate(HTTPRequest& request)
145145

146146
void HTTPSClientSession::connect(const SocketAddress& address)
147147
{
148-
if (getProxyHost().empty())
148+
if (getProxyHost().empty() || bypassProxy())
149149
{
150150
SecureStreamSocket sss(socket());
151151
if (_pContext->sessionCacheEnabled())
@@ -176,7 +176,8 @@ int HTTPSClientSession::read(char* buffer, std::streamsize length)
176176
try
177177
{
178178
return HTTPSession::read(buffer, length);
179-
} catch(SSLConnectionUnexpectedlyClosedException&)
179+
}
180+
catch(SSLConnectionUnexpectedlyClosedException&)
180181
{
181182
return 0;
182183
}

0 commit comments

Comments
 (0)