Skip to content

Commit

Permalink
[py] Check the values in NO_PROXY and set the poolmanager accordingly
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AutomatedTester committed Oct 22, 2021
1 parent 96ee96a commit db37a9f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
23 changes: 17 additions & 6 deletions py/selenium/webdriver/remote/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@

from base64 import b64encode

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

# Env var NO_PROXY will override this part of the code
_no_proxy = os.environ.get('no_proxy', os.environ.get('NO_PROXY'))
if _no_proxy is not None:
ignore_proxy = _no_proxy
if _no_proxy:
for npu in _no_proxy.split(','):
npu = npu.strip()
if npu == "*":
ignore_proxy = True
break
n_url = parse.urlparse(npu)
remote_add = parse.urlparse(self._url)
if n_url.netloc:
if remote_add.netloc == n_url.netloc:
ignore_proxy = True
break
else:
if n_url.path in remote_add.netloc:
ignore_proxy = True
break

self._proxy_url = self._get_proxy_url() if not ignore_proxy else None
if keep_alive:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ def test_get_connection_manager_with_proxy(mock_proxy_settings):
assert conn.proxy.port == 8080


def test_get_connection_manager_when_no_proxy_set(mock_no_proxy_settings):
remote_connection = RemoteConnection("https://remote")
@pytest.mark.parametrize("url",
["*", ".localhost", "localhost:80", "locahost", "127.0.0.1",
"LOCALHOST", "LOCALHOST:80", "http://localhost", "https://localhost",
"test.localhost", " localhost"])
def test_get_connection_manager_when_no_proxy_set(mock_no_proxy_settings, url):
remote_connection = RemoteConnection(url)
conn = remote_connection._get_connection_manager()
assert type(conn) == urllib3.PoolManager

Expand Down Expand Up @@ -156,5 +160,5 @@ def mock_no_proxy_settings(monkeypatch):
monkeypatch.setenv("HTTP_PROXY", http_proxy)
monkeypatch.setenv("https_proxy", https_proxy)
monkeypatch.setenv("http_proxy", http_proxy)
monkeypatch.setenv("no_proxy", "localhost")
monkeypatch.setenv("NO_PROXY", "localhost")
monkeypatch.setenv("no_proxy", "65.253.214.253,localhost,127.0.0.1,*zyz.xx")
monkeypatch.setenv("NO_PROXY", "65.253.214.253,localhost,127.0.0.1,*zyz.xx")

0 comments on commit db37a9f

Please sign in to comment.