Skip to content

Commit db37a9f

Browse files
[py] Check the values in NO_PROXY and set the poolmanager accordingly
If people are setting no_proxy for certain values to get around the need to have a proxy for localhost, mostly, then we should set the poolmanager to proxymanager if not in no_proxy or poolmanager if it is. Fixes #9925 Fixes #9967
1 parent 96ee96a commit db37a9f

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

py/selenium/webdriver/remote/remote_connection.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626

2727
from base64 import b64encode
2828

29-
try:
30-
from urllib import parse
31-
except ImportError: # above is available in py3+, below is py2.7
32-
import urlparse as parse
29+
from urllib import parse
3330
from selenium import __version__
3431
from .command import Command
3532
from .errorhandler import ErrorCode
@@ -153,8 +150,22 @@ def __init__(self, remote_server_addr, keep_alive=False, resolve_ip=None, ignore
153150

154151
# Env var NO_PROXY will override this part of the code
155152
_no_proxy = os.environ.get('no_proxy', os.environ.get('NO_PROXY'))
156-
if _no_proxy is not None:
157-
ignore_proxy = _no_proxy
153+
if _no_proxy:
154+
for npu in _no_proxy.split(','):
155+
npu = npu.strip()
156+
if npu == "*":
157+
ignore_proxy = True
158+
break
159+
n_url = parse.urlparse(npu)
160+
remote_add = parse.urlparse(self._url)
161+
if n_url.netloc:
162+
if remote_add.netloc == n_url.netloc:
163+
ignore_proxy = True
164+
break
165+
else:
166+
if n_url.path in remote_add.netloc:
167+
ignore_proxy = True
168+
break
158169

159170
self._proxy_url = self._get_proxy_url() if not ignore_proxy else None
160171
if keep_alive:

py/test/unit/selenium/webdriver/remote/remote_connection_tests.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ def test_get_connection_manager_with_proxy(mock_proxy_settings):
104104
assert conn.proxy.port == 8080
105105

106106

107-
def test_get_connection_manager_when_no_proxy_set(mock_no_proxy_settings):
108-
remote_connection = RemoteConnection("https://remote")
107+
@pytest.mark.parametrize("url",
108+
["*", ".localhost", "localhost:80", "locahost", "127.0.0.1",
109+
"LOCALHOST", "LOCALHOST:80", "http://localhost", "https://localhost",
110+
"test.localhost", " localhost"])
111+
def test_get_connection_manager_when_no_proxy_set(mock_no_proxy_settings, url):
112+
remote_connection = RemoteConnection(url)
109113
conn = remote_connection._get_connection_manager()
110114
assert type(conn) == urllib3.PoolManager
111115

@@ -156,5 +160,5 @@ def mock_no_proxy_settings(monkeypatch):
156160
monkeypatch.setenv("HTTP_PROXY", http_proxy)
157161
monkeypatch.setenv("https_proxy", https_proxy)
158162
monkeypatch.setenv("http_proxy", http_proxy)
159-
monkeypatch.setenv("no_proxy", "localhost")
160-
monkeypatch.setenv("NO_PROXY", "localhost")
163+
monkeypatch.setenv("no_proxy", "65.253.214.253,localhost,127.0.0.1,*zyz.xx")
164+
monkeypatch.setenv("NO_PROXY", "65.253.214.253,localhost,127.0.0.1,*zyz.xx")

0 commit comments

Comments
 (0)