2020import platform
2121import socket
2222import string
23+ import warnings
2324from base64 import b64encode
2425from urllib import parse
2526
2627import certifi
2728import urllib3
2829
2930from selenium import __version__
30-
3131from . import utils
32+ from .client_config import ClientConfig
3233from .command import Command
3334from .errorhandler import ErrorCode
35+ from .. import Proxy
3436
3537LOGGER = logging .getLogger (__name__ )
3638
@@ -211,12 +213,6 @@ def get_remote_connection_headers(cls, parsed_url, keep_alive=False):
211213
212214 return headers
213215
214- def _get_proxy_url (self ):
215- if self ._url .startswith ("https://" ):
216- return os .environ .get ("https_proxy" , os .environ .get ("HTTPS_PROXY" ))
217- if self ._url .startswith ("http://" ):
218- return os .environ .get ("http_proxy" , os .environ .get ("HTTP_PROXY" ))
219-
220216 def _identify_http_proxy_auth (self ):
221217 url = self ._proxy_url
222218 url = url [url .find (":" ) + 3 :]
@@ -248,31 +244,42 @@ def _get_connection_manager(self):
248244
249245 return urllib3 .PoolManager (** pool_manager_init_args )
250246
251- def __init__ (self , remote_server_addr : str , keep_alive : bool = False , ignore_proxy : bool = False ):
252- self .keep_alive = keep_alive
253- self ._url = remote_server_addr
254-
255- # Env var NO_PROXY will override this part of the code
256- _no_proxy = os .environ .get ("no_proxy" , os .environ .get ("NO_PROXY" ))
257- if _no_proxy :
258- for npu in _no_proxy .split ("," ):
259- npu = npu .strip ()
260- if npu == "*" :
261- ignore_proxy = True
262- break
263- n_url = parse .urlparse (npu )
264- remote_add = parse .urlparse (self ._url )
265- if n_url .netloc :
266- if remote_add .netloc == n_url .netloc :
267- ignore_proxy = True
268- break
269- else :
270- if n_url .path in remote_add .netloc :
271- ignore_proxy = True
272- break
273-
274- self ._proxy_url = self ._get_proxy_url () if not ignore_proxy else None
275- if keep_alive :
247+ def __init__ (
248+ self ,
249+ remote_server_addr : str ,
250+ keep_alive : bool = True ,
251+ ignore_proxy : bool = False ,
252+ client_config : ClientConfig = None ,
253+ ):
254+ self ._client_config = client_config or ClientConfig ()
255+
256+ if remote_server_addr :
257+ warnings .warn (
258+ "setting keep_alive in RemoteConnection() is deprecated, " "set in ClientConfig instance insttead" ,
259+ DeprecationWarning ,
260+ stacklevel = 2 ,
261+ )
262+ self ._client_config .remote_server_addr = remote_server_addr
263+
264+ if not keep_alive :
265+ warnings .warn (
266+ "setting keep_alive in RemoteConnection() is deprecated, " "set in ClientConfig instance insttead" ,
267+ DeprecationWarning ,
268+ stacklevel = 2 ,
269+ )
270+ self ._client_config .keep_alive = keep_alive
271+
272+ if ignore_proxy :
273+ warnings .warn (
274+ "setting keep_alive in RemoteConnection() is deprecated, " "set in ClientConfig instance insttead" ,
275+ DeprecationWarning ,
276+ stacklevel = 2 ,
277+ )
278+ self ._client_config .proxy = Proxy (raw = {"proxyType" : "direct" })
279+
280+ self ._proxy_url = self ._client_config .get_proxy_url ()
281+
282+ if self ._client_config .keep_alive :
276283 self ._conn = self ._get_connection_manager ()
277284 self ._commands = remote_commands
278285
@@ -296,7 +303,7 @@ def execute(self, command, params):
296303 for word in substitute_params :
297304 del params [word ]
298305 data = utils .dump_json (params )
299- url = f"{ self ._url } { path } "
306+ url = f"{ self ._client_config . remote_server_addr } { path } "
300307 return self ._request (command_info [0 ], url , body = data )
301308
302309 def _request (self , method , url , body = None ):
@@ -312,12 +319,11 @@ def _request(self, method, url, body=None):
312319 """
313320 LOGGER .debug ("%s %s %s" , method , url , body )
314321 parsed_url = parse .urlparse (url )
315- headers = self .get_remote_connection_headers (parsed_url , self .keep_alive )
316- response = None
322+ headers = self .get_remote_connection_headers (parsed_url , self ._client_config .keep_alive )
317323 if body and method not in ("POST" , "PUT" ):
318324 body = None
319325
320- if self .keep_alive :
326+ if self ._client_config . keep_alive :
321327 response = self ._conn .request (method , url , body = body , headers = headers )
322328 statuscode = response .status
323329 else :
0 commit comments