Path: blob/trunk/py/selenium/webdriver/remote/client_config.py
3991 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import base6418import os19import socket20from enum import Enum21from urllib import parse2223import certifi2425from selenium.webdriver.common.proxy import Proxy, ProxyType262728class AuthType(Enum):29BASIC = "Basic"30BEARER = "Bearer"31X_API_KEY = "X-API-Key"323334class _ClientConfigDescriptor:35def __init__(self, name):36self.name = name3738def __get__(self, obj, cls):39return obj.__dict__[self.name]4041def __set__(self, obj, value) -> None:42obj.__dict__[self.name] = value434445class ClientConfig:46remote_server_addr = _ClientConfigDescriptor("_remote_server_addr")47"""Gets and Sets Remote Server."""48keep_alive = _ClientConfigDescriptor("_keep_alive")49"""Gets and Sets Keep Alive value."""50proxy = _ClientConfigDescriptor("_proxy")51"""Gets and Sets the proxy used for communicating with the driver/server."""52ignore_certificates = _ClientConfigDescriptor("_ignore_certificates")53"""Gets and Sets the ignore certificate check value."""54init_args_for_pool_manager = _ClientConfigDescriptor("_init_args_for_pool_manager")55"""Gets and Sets the ignore certificate check."""56timeout = _ClientConfigDescriptor("_timeout")57"""Gets and Sets the timeout (in seconds) used for communicating with the driver/server."""58ca_certs = _ClientConfigDescriptor("_ca_certs")59"""Gets and Sets the path to bundle of CA certificates."""60username = _ClientConfigDescriptor("_username")61"""Gets and Sets the username used for basic authentication to the remote."""62password = _ClientConfigDescriptor("_password")63"""Gets and Sets the password used for basic authentication to the remote."""64auth_type = _ClientConfigDescriptor("_auth_type")65"""Gets and Sets the type of authentication to the remote server."""66token = _ClientConfigDescriptor("_token")67"""Gets and Sets the token used for authentication to the remote server."""68user_agent = _ClientConfigDescriptor("_user_agent")69"""Gets and Sets user agent to be added to the request headers."""70extra_headers = _ClientConfigDescriptor("_extra_headers")71"""Gets and Sets extra headers to be added to the request."""72websocket_timeout = _ClientConfigDescriptor("_websocket_timeout")73"""Gets and Sets the WebSocket response wait timeout (in seconds) used for communicating with the browser."""74websocket_interval = _ClientConfigDescriptor("_websocket_interval")75"""Gets and Sets the WebSocket response wait interval (in seconds) used for communicating with the browser."""7677def __init__(78self,79remote_server_addr: str,80keep_alive: bool | None = True,81proxy: Proxy | None = Proxy(raw={"proxyType": ProxyType.SYSTEM}),82ignore_certificates: bool | None = False,83init_args_for_pool_manager: dict | None = None,84timeout: int | None = None,85ca_certs: str | None = None,86username: str | None = None,87password: str | None = None,88auth_type: AuthType | None = AuthType.BASIC,89token: str | None = None,90user_agent: str | None = None,91extra_headers: dict | None = None,92websocket_timeout: float | None = 30.0,93websocket_interval: float | None = 0.1,94) -> None:95self.remote_server_addr = remote_server_addr96self.keep_alive = keep_alive97self.proxy = proxy98self.ignore_certificates = ignore_certificates99self.init_args_for_pool_manager = init_args_for_pool_manager or {}100self.timeout = socket.getdefaulttimeout() if timeout is None else timeout101self.username = username102self.password = password103self.auth_type = auth_type104self.token = token105self.user_agent = user_agent106self.extra_headers = extra_headers107self.websocket_timeout = websocket_timeout108self.websocket_interval = websocket_interval109110self.ca_certs = (111(os.getenv("REQUESTS_CA_BUNDLE") if "REQUESTS_CA_BUNDLE" in os.environ else certifi.where())112if ca_certs is None113else ca_certs114)115116def reset_timeout(self) -> None:117"""Resets the timeout to the default value of socket."""118self._timeout = socket.getdefaulttimeout()119120def get_proxy_url(self) -> str | None:121"""Returns the proxy URL to use for the connection."""122proxy_type = self.proxy.proxy_type123remote_add = parse.urlparse(self.remote_server_addr)124if proxy_type is ProxyType.DIRECT:125return None126if proxy_type is ProxyType.SYSTEM:127_no_proxy = os.environ.get("no_proxy", os.environ.get("NO_PROXY"))128if _no_proxy:129for entry in map(str.strip, _no_proxy.split(",")):130if entry == "*":131return None132n_url = parse.urlparse(entry)133if n_url.netloc and remote_add.netloc == n_url.netloc:134return None135if n_url.path in remote_add.netloc:136return None137return os.environ.get(138"https_proxy" if self.remote_server_addr.startswith("https://") else "http_proxy",139os.environ.get("HTTPS_PROXY" if self.remote_server_addr.startswith("https://") else "HTTP_PROXY"),140)141if proxy_type is ProxyType.MANUAL:142return self.proxy.sslProxy if self.remote_server_addr.startswith("https://") else self.proxy.http_proxy143return None144145def get_auth_header(self) -> dict | None:146"""Returns the authorization to add to the request headers."""147if self.auth_type is AuthType.BASIC and self.username and self.password:148credentials = f"{self.username}:{self.password}"149encoded_credentials = base64.b64encode(credentials.encode("utf-8")).decode("utf-8")150return {"Authorization": f"{AuthType.BASIC.value} {encoded_credentials}"}151if self.auth_type is AuthType.BEARER and self.token:152return {"Authorization": f"{AuthType.BEARER.value} {self.token}"}153if self.auth_type is AuthType.X_API_KEY and self.token:154return {f"{AuthType.X_API_KEY.value}": f"{self.token}"}155return None156157158