|
from __future__ import annotations |
|
|
|
import datetime |
|
import logging |
|
import os |
|
import re |
|
import socket |
|
import sys |
|
import typing |
|
import warnings |
|
from http.client import HTTPConnection as _HTTPConnection |
|
from http.client import HTTPException as HTTPException |
|
from http.client import ResponseNotReady |
|
from socket import timeout as SocketTimeout |
|
|
|
if typing.TYPE_CHECKING: |
|
from typing import Literal |
|
|
|
from .response import HTTPResponse |
|
from .util.ssl_ import _TYPE_PEER_CERT_RET_DICT |
|
from .util.ssltransport import SSLTransport |
|
|
|
from ._collections import HTTPHeaderDict |
|
from .util.response import assert_header_parsing |
|
from .util.timeout import _DEFAULT_TIMEOUT, _TYPE_TIMEOUT, Timeout |
|
from .util.util import to_str |
|
from .util.wait import wait_for_read |
|
|
|
try: |
|
import ssl |
|
|
|
BaseSSLError = ssl.SSLError |
|
except (ImportError, AttributeError): |
|
ssl = None |
|
|
|
class BaseSSLError(BaseException): |
|
pass |
|
|
|
|
|
from ._base_connection import _TYPE_BODY |
|
from ._base_connection import ProxyConfig as ProxyConfig |
|
from ._base_connection import _ResponseOptions as _ResponseOptions |
|
from ._version import __version__ |
|
from .exceptions import ( |
|
ConnectTimeoutError, |
|
HeaderParsingError, |
|
NameResolutionError, |
|
NewConnectionError, |
|
ProxyError, |
|
SystemTimeWarning, |
|
) |
|
from .util import SKIP_HEADER, SKIPPABLE_HEADERS, connection, ssl_ |
|
from .util.request import body_to_chunks |
|
from .util.ssl_ import assert_fingerprint as _assert_fingerprint |
|
from .util.ssl_ import ( |
|
create_urllib3_context, |
|
is_ipaddress, |
|
resolve_cert_reqs, |
|
resolve_ssl_version, |
|
ssl_wrap_socket, |
|
) |
|
from .util.ssl_match_hostname import CertificateError, match_hostname |
|
from .util.url import Url |
|
|
|
|
|
ConnectionError = ConnectionError |
|
BrokenPipeError = BrokenPipeError |
|
|
|
|
|
log = logging.getLogger(__name__) |
|
|
|
port_by_scheme = {"http": 80, "https": 443} |
|
|
|
|
|
|
|
RECENT_DATE = datetime.date(2023, 6, 1) |
|
|
|
_CONTAINS_CONTROL_CHAR_RE = re.compile(r"[^-!#$%&'*+.^_`|~0-9a-zA-Z]") |
|
|
|
_HAS_SYS_AUDIT = hasattr(sys, "audit") |
|
|
|
|
|
class HTTPConnection(_HTTPConnection): |
|
""" |
|
Based on :class:`http.client.HTTPConnection` but provides an extra constructor |
|
backwards-compatibility layer between older and newer Pythons. |
|
|
|
Additional keyword parameters are used to configure attributes of the connection. |
|
Accepted parameters include: |
|
|
|
- ``source_address``: Set the source address for the current connection. |
|
- ``socket_options``: Set specific options on the underlying socket. If not specified, then |
|
defaults are loaded from ``HTTPConnection.default_socket_options`` which includes disabling |
|
Nagle's algorithm (sets TCP_NODELAY to 1) unless the connection is behind a proxy. |
|
|
|
For example, if you wish to enable TCP Keep Alive in addition to the defaults, |
|
you might pass: |
|
|
|
.. code-block:: python |
|
|
|
HTTPConnection.default_socket_options + [ |
|
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1), |
|
] |
|
|
|
Or you may want to disable the defaults by passing an empty list (e.g., ``[]``). |
|
""" |
|
|
|
default_port: typing.ClassVar[int] = port_by_scheme["http"] |
|
|
|
|
|
|
|
default_socket_options: typing.ClassVar[connection._TYPE_SOCKET_OPTIONS] = [ |
|
(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
|
] |
|
|
|
|
|
is_verified: bool = False |
|
|
|
|
|
|
|
proxy_is_verified: bool | None = None |
|
|
|
blocksize: int |
|
source_address: tuple[str, int] | None |
|
socket_options: connection._TYPE_SOCKET_OPTIONS | None |
|
|
|
_has_connected_to_proxy: bool |
|
_response_options: _ResponseOptions | None |
|
_tunnel_host: str | None |
|
_tunnel_port: int | None |
|
_tunnel_scheme: str | None |
|
|
|
def __init__( |
|
self, |
|
host: str, |
|
port: int | None = None, |
|
*, |
|
timeout: _TYPE_TIMEOUT = _DEFAULT_TIMEOUT, |
|
source_address: tuple[str, int] | None = None, |
|
blocksize: int = 16384, |
|
socket_options: None |
|
| (connection._TYPE_SOCKET_OPTIONS) = default_socket_options, |
|
proxy: Url | None = None, |
|
proxy_config: ProxyConfig | None = None, |
|
) -> None: |
|
super().__init__( |
|
host=host, |
|
port=port, |
|
timeout=Timeout.resolve_default_timeout(timeout), |
|
source_address=source_address, |
|
blocksize=blocksize, |
|
) |
|
self.socket_options = socket_options |
|
self.proxy = proxy |
|
self.proxy_config = proxy_config |
|
|
|
self._has_connected_to_proxy = False |
|
self._response_options = None |
|
self._tunnel_host: str | None = None |
|
self._tunnel_port: int | None = None |
|
self._tunnel_scheme: str | None = None |
|
|
|
@property |
|
def host(self) -> str: |
|
""" |
|
Getter method to remove any trailing dots that indicate the hostname is an FQDN. |
|
|
|
In general, SSL certificates don't include the trailing dot indicating a |
|
fully-qualified domain name, and thus, they don't validate properly when |
|
checked against a domain name that includes the dot. In addition, some |
|
servers may not expect to receive the trailing dot when provided. |
|
|
|
However, the hostname with trailing dot is critical to DNS resolution; doing a |
|
lookup with the trailing dot will properly only resolve the appropriate FQDN, |
|
whereas a lookup without a trailing dot will search the system's search domain |
|
list. Thus, it's important to keep the original host around for use only in |
|
those cases where it's appropriate (i.e., when doing DNS lookup to establish the |
|
actual TCP connection across which we're going to send HTTP requests). |
|
""" |
|
return self._dns_host.rstrip(".") |
|
|
|
@host.setter |
|
def host(self, value: str) -> None: |
|
""" |
|
Setter for the `host` property. |
|
|
|
We assume that only urllib3 uses the _dns_host attribute; httplib itself |
|
only uses `host`, and it seems reasonable that other libraries follow suit. |
|
""" |
|
self._dns_host = value |
|
|
|
def _new_conn(self) -> socket.socket: |
|
"""Establish a socket connection and set nodelay settings on it. |
|
|
|
:return: New socket connection. |
|
""" |
|
try: |
|
sock = connection.create_connection( |
|
(self._dns_host, self.port), |
|
self.timeout, |
|
source_address=self.source_address, |
|
socket_options=self.socket_options, |
|
) |
|
except socket.gaierror as e: |
|
raise NameResolutionError(self.host, self, e) from e |
|
except SocketTimeout as e: |
|
raise ConnectTimeoutError( |
|
self, |
|
f"Connection to {self.host} timed out. (connect timeout={self.timeout})", |
|
) from e |
|
|
|
except OSError as e: |
|
raise NewConnectionError( |
|
self, f"Failed to establish a new connection: {e}" |
|
) from e |
|
|
|
|
|
if _HAS_SYS_AUDIT: |
|
sys.audit("http.client.connect", self, self.host, self.port) |
|
|
|
return sock |
|
|
|
def set_tunnel( |
|
self, |
|
host: str, |
|
port: int | None = None, |
|
headers: typing.Mapping[str, str] | None = None, |
|
scheme: str = "http", |
|
) -> None: |
|
if scheme not in ("http", "https"): |
|
raise ValueError( |
|
f"Invalid proxy scheme for tunneling: {scheme!r}, must be either 'http' or 'https'" |
|
) |
|
super().set_tunnel(host, port=port, headers=headers) |
|
self._tunnel_scheme = scheme |
|
|
|
def connect(self) -> None: |
|
self.sock = self._new_conn() |
|
if self._tunnel_host: |
|
|
|
self._has_connected_to_proxy = True |
|
|
|
|
|
self._tunnel() |
|
|
|
|
|
|
|
|
|
self._has_connected_to_proxy = bool(self.proxy) |
|
|
|
if self._has_connected_to_proxy: |
|
self.proxy_is_verified = False |
|
|
|
@property |
|
def is_closed(self) -> bool: |
|
return self.sock is None |
|
|
|
@property |
|
def is_connected(self) -> bool: |
|
if self.sock is None: |
|
return False |
|
return not wait_for_read(self.sock, timeout=0.0) |
|
|
|
@property |
|
def has_connected_to_proxy(self) -> bool: |
|
return self._has_connected_to_proxy |
|
|
|
@property |
|
def proxy_is_forwarding(self) -> bool: |
|
""" |
|
Return True if a forwarding proxy is configured, else return False |
|
""" |
|
return bool(self.proxy) and self._tunnel_host is None |
|
|
|
def close(self) -> None: |
|
try: |
|
super().close() |
|
finally: |
|
|
|
|
|
self.sock = None |
|
self.is_verified = False |
|
self.proxy_is_verified = None |
|
self._has_connected_to_proxy = False |
|
self._response_options = None |
|
self._tunnel_host = None |
|
self._tunnel_port = None |
|
self._tunnel_scheme = None |
|
|
|
def putrequest( |
|
self, |
|
method: str, |
|
url: str, |
|
skip_host: bool = False, |
|
skip_accept_encoding: bool = False, |
|
) -> None: |
|
"""""" |
|
|
|
|
|
match = _CONTAINS_CONTROL_CHAR_RE.search(method) |
|
if match: |
|
raise ValueError( |
|
f"Method cannot contain non-token characters {method!r} (found at least {match.group()!r})" |
|
) |
|
|
|
return super().putrequest( |
|
method, url, skip_host=skip_host, skip_accept_encoding=skip_accept_encoding |
|
) |
|
|
|
def putheader(self, header: str, *values: str) -> None: |
|
"""""" |
|
if not any(isinstance(v, str) and v == SKIP_HEADER for v in values): |
|
super().putheader(header, *values) |
|
elif to_str(header.lower()) not in SKIPPABLE_HEADERS: |
|
skippable_headers = "', '".join( |
|
[str.title(header) for header in sorted(SKIPPABLE_HEADERS)] |
|
) |
|
raise ValueError( |
|
f"urllib3.util.SKIP_HEADER only supports '{skippable_headers}'" |
|
) |
|
|
|
|
|
|
|
def request( |
|
self, |
|
method: str, |
|
url: str, |
|
body: _TYPE_BODY | None = None, |
|
headers: typing.Mapping[str, str] | None = None, |
|
*, |
|
chunked: bool = False, |
|
preload_content: bool = True, |
|
decode_content: bool = True, |
|
enforce_content_length: bool = True, |
|
) -> None: |
|
|
|
|
|
if self.sock is not None: |
|
self.sock.settimeout(self.timeout) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self._response_options = _ResponseOptions( |
|
request_method=method, |
|
request_url=url, |
|
preload_content=preload_content, |
|
decode_content=decode_content, |
|
enforce_content_length=enforce_content_length, |
|
) |
|
|
|
if headers is None: |
|
headers = {} |
|
header_keys = frozenset(to_str(k.lower()) for k in headers) |
|
skip_accept_encoding = "accept-encoding" in header_keys |
|
skip_host = "host" in header_keys |
|
self.putrequest( |
|
method, url, skip_accept_encoding=skip_accept_encoding, skip_host=skip_host |
|
) |
|
|
|
|
|
|
|
chunks_and_cl = body_to_chunks(body, method=method, blocksize=self.blocksize) |
|
chunks = chunks_and_cl.chunks |
|
content_length = chunks_and_cl.content_length |
|
|
|
|
|
if chunked: |
|
if "transfer-encoding" not in header_keys: |
|
self.putheader("Transfer-Encoding", "chunked") |
|
else: |
|
|
|
|
|
|
|
if "content-length" in header_keys: |
|
chunked = False |
|
elif "transfer-encoding" in header_keys: |
|
chunked = True |
|
|
|
|
|
else: |
|
chunked = False |
|
if content_length is None: |
|
if chunks is not None: |
|
chunked = True |
|
self.putheader("Transfer-Encoding", "chunked") |
|
else: |
|
self.putheader("Content-Length", str(content_length)) |
|
|
|
|
|
if "user-agent" not in header_keys: |
|
self.putheader("User-Agent", _get_default_user_agent()) |
|
for header, value in headers.items(): |
|
self.putheader(header, value) |
|
self.endheaders() |
|
|
|
|
|
if chunks is not None: |
|
for chunk in chunks: |
|
|
|
|
|
if not chunk: |
|
continue |
|
if isinstance(chunk, str): |
|
chunk = chunk.encode("utf-8") |
|
if chunked: |
|
self.send(b"%x\r\n%b\r\n" % (len(chunk), chunk)) |
|
else: |
|
self.send(chunk) |
|
|
|
|
|
|
|
if chunked: |
|
self.send(b"0\r\n\r\n") |
|
|
|
def request_chunked( |
|
self, |
|
method: str, |
|
url: str, |
|
body: _TYPE_BODY | None = None, |
|
headers: typing.Mapping[str, str] | None = None, |
|
) -> None: |
|
""" |
|
Alternative to the common request method, which sends the |
|
body with chunked encoding and not as one block |
|
""" |
|
warnings.warn( |
|
"HTTPConnection.request_chunked() is deprecated and will be removed " |
|
"in urllib3 v2.1.0. Instead use HTTPConnection.request(..., chunked=True).", |
|
category=DeprecationWarning, |
|
stacklevel=2, |
|
) |
|
self.request(method, url, body=body, headers=headers, chunked=True) |
|
|
|
def getresponse( |
|
self, |
|
) -> HTTPResponse: |
|
""" |
|
Get the response from the server. |
|
|
|
If the HTTPConnection is in the correct state, returns an instance of HTTPResponse or of whatever object is returned by the response_class variable. |
|
|
|
If a request has not been sent or if a previous response has not be handled, ResponseNotReady is raised. If the HTTP response indicates that the connection should be closed, then it will be closed before the response is returned. When the connection is closed, the underlying socket is closed. |
|
""" |
|
|
|
if self._response_options is None: |
|
raise ResponseNotReady() |
|
|
|
|
|
resp_options = self._response_options |
|
self._response_options = None |
|
|
|
|
|
|
|
self.sock.settimeout(self.timeout) |
|
|
|
|
|
from .response import HTTPResponse |
|
|
|
|
|
httplib_response = super().getresponse() |
|
|
|
try: |
|
assert_header_parsing(httplib_response.msg) |
|
except (HeaderParsingError, TypeError) as hpe: |
|
log.warning( |
|
"Failed to parse headers (url=%s): %s", |
|
_url_from_connection(self, resp_options.request_url), |
|
hpe, |
|
exc_info=True, |
|
) |
|
|
|
headers = HTTPHeaderDict(httplib_response.msg.items()) |
|
|
|
response = HTTPResponse( |
|
body=httplib_response, |
|
headers=headers, |
|
status=httplib_response.status, |
|
version=httplib_response.version, |
|
reason=httplib_response.reason, |
|
preload_content=resp_options.preload_content, |
|
decode_content=resp_options.decode_content, |
|
original_response=httplib_response, |
|
enforce_content_length=resp_options.enforce_content_length, |
|
request_method=resp_options.request_method, |
|
request_url=resp_options.request_url, |
|
) |
|
return response |
|
|
|
|
|
class HTTPSConnection(HTTPConnection): |
|
""" |
|
Many of the parameters to this constructor are passed to the underlying SSL |
|
socket by means of :py:func:`urllib3.util.ssl_wrap_socket`. |
|
""" |
|
|
|
default_port = port_by_scheme["https"] |
|
|
|
cert_reqs: int | str | None = None |
|
ca_certs: str | None = None |
|
ca_cert_dir: str | None = None |
|
ca_cert_data: None | str | bytes = None |
|
ssl_version: int | str | None = None |
|
ssl_minimum_version: int | None = None |
|
ssl_maximum_version: int | None = None |
|
assert_fingerprint: str | None = None |
|
|
|
def __init__( |
|
self, |
|
host: str, |
|
port: int | None = None, |
|
*, |
|
timeout: _TYPE_TIMEOUT = _DEFAULT_TIMEOUT, |
|
source_address: tuple[str, int] | None = None, |
|
blocksize: int = 16384, |
|
socket_options: None |
|
| (connection._TYPE_SOCKET_OPTIONS) = HTTPConnection.default_socket_options, |
|
proxy: Url | None = None, |
|
proxy_config: ProxyConfig | None = None, |
|
cert_reqs: int | str | None = None, |
|
assert_hostname: None | str | Literal[False] = None, |
|
assert_fingerprint: str | None = None, |
|
server_hostname: str | None = None, |
|
ssl_context: ssl.SSLContext | None = None, |
|
ca_certs: str | None = None, |
|
ca_cert_dir: str | None = None, |
|
ca_cert_data: None | str | bytes = None, |
|
ssl_minimum_version: int | None = None, |
|
ssl_maximum_version: int | None = None, |
|
ssl_version: int | str | None = None, |
|
cert_file: str | None = None, |
|
key_file: str | None = None, |
|
key_password: str | None = None, |
|
) -> None: |
|
super().__init__( |
|
host, |
|
port=port, |
|
timeout=timeout, |
|
source_address=source_address, |
|
blocksize=blocksize, |
|
socket_options=socket_options, |
|
proxy=proxy, |
|
proxy_config=proxy_config, |
|
) |
|
|
|
self.key_file = key_file |
|
self.cert_file = cert_file |
|
self.key_password = key_password |
|
self.ssl_context = ssl_context |
|
self.server_hostname = server_hostname |
|
self.assert_hostname = assert_hostname |
|
self.assert_fingerprint = assert_fingerprint |
|
self.ssl_version = ssl_version |
|
self.ssl_minimum_version = ssl_minimum_version |
|
self.ssl_maximum_version = ssl_maximum_version |
|
self.ca_certs = ca_certs and os.path.expanduser(ca_certs) |
|
self.ca_cert_dir = ca_cert_dir and os.path.expanduser(ca_cert_dir) |
|
self.ca_cert_data = ca_cert_data |
|
|
|
|
|
if cert_reqs is None: |
|
if self.ssl_context is not None: |
|
cert_reqs = self.ssl_context.verify_mode |
|
else: |
|
cert_reqs = resolve_cert_reqs(None) |
|
self.cert_reqs = cert_reqs |
|
|
|
def set_cert( |
|
self, |
|
key_file: str | None = None, |
|
cert_file: str | None = None, |
|
cert_reqs: int | str | None = None, |
|
key_password: str | None = None, |
|
ca_certs: str | None = None, |
|
assert_hostname: None | str | Literal[False] = None, |
|
assert_fingerprint: str | None = None, |
|
ca_cert_dir: str | None = None, |
|
ca_cert_data: None | str | bytes = None, |
|
) -> None: |
|
""" |
|
This method should only be called once, before the connection is used. |
|
""" |
|
warnings.warn( |
|
"HTTPSConnection.set_cert() is deprecated and will be removed " |
|
"in urllib3 v2.1.0. Instead provide the parameters to the " |
|
"HTTPSConnection constructor.", |
|
category=DeprecationWarning, |
|
stacklevel=2, |
|
) |
|
|
|
|
|
|
|
if cert_reqs is None: |
|
if self.ssl_context is not None: |
|
cert_reqs = self.ssl_context.verify_mode |
|
else: |
|
cert_reqs = resolve_cert_reqs(None) |
|
|
|
self.key_file = key_file |
|
self.cert_file = cert_file |
|
self.cert_reqs = cert_reqs |
|
self.key_password = key_password |
|
self.assert_hostname = assert_hostname |
|
self.assert_fingerprint = assert_fingerprint |
|
self.ca_certs = ca_certs and os.path.expanduser(ca_certs) |
|
self.ca_cert_dir = ca_cert_dir and os.path.expanduser(ca_cert_dir) |
|
self.ca_cert_data = ca_cert_data |
|
|
|
def connect(self) -> None: |
|
sock: socket.socket | ssl.SSLSocket |
|
self.sock = sock = self._new_conn() |
|
server_hostname: str = self.host |
|
tls_in_tls = False |
|
|
|
|
|
if self._tunnel_host is not None: |
|
|
|
if self._tunnel_scheme == "https": |
|
|
|
self.sock = sock = self._connect_tls_proxy(self.host, sock) |
|
tls_in_tls = True |
|
elif self._tunnel_scheme == "http": |
|
self.proxy_is_verified = False |
|
|
|
|
|
self._has_connected_to_proxy = True |
|
|
|
self._tunnel() |
|
|
|
server_hostname = self._tunnel_host |
|
|
|
if self.server_hostname is not None: |
|
server_hostname = self.server_hostname |
|
|
|
is_time_off = datetime.date.today() < RECENT_DATE |
|
if is_time_off: |
|
warnings.warn( |
|
( |
|
f"System time is way off (before {RECENT_DATE}). This will probably " |
|
"lead to SSL verification errors" |
|
), |
|
SystemTimeWarning, |
|
) |
|
|
|
|
|
server_hostname_rm_dot = server_hostname.rstrip(".") |
|
|
|
sock_and_verified = _ssl_wrap_socket_and_match_hostname( |
|
sock=sock, |
|
cert_reqs=self.cert_reqs, |
|
ssl_version=self.ssl_version, |
|
ssl_minimum_version=self.ssl_minimum_version, |
|
ssl_maximum_version=self.ssl_maximum_version, |
|
ca_certs=self.ca_certs, |
|
ca_cert_dir=self.ca_cert_dir, |
|
ca_cert_data=self.ca_cert_data, |
|
cert_file=self.cert_file, |
|
key_file=self.key_file, |
|
key_password=self.key_password, |
|
server_hostname=server_hostname_rm_dot, |
|
ssl_context=self.ssl_context, |
|
tls_in_tls=tls_in_tls, |
|
assert_hostname=self.assert_hostname, |
|
assert_fingerprint=self.assert_fingerprint, |
|
) |
|
self.sock = sock_and_verified.socket |
|
|
|
|
|
|
|
|
|
|
|
if self.proxy_is_forwarding: |
|
self.is_verified = False |
|
else: |
|
self.is_verified = sock_and_verified.is_verified |
|
|
|
|
|
|
|
|
|
self._has_connected_to_proxy = bool(self.proxy) |
|
|
|
|
|
|
|
if self._has_connected_to_proxy and self.proxy_is_verified is None: |
|
self.proxy_is_verified = sock_and_verified.is_verified |
|
|
|
def _connect_tls_proxy(self, hostname: str, sock: socket.socket) -> ssl.SSLSocket: |
|
""" |
|
Establish a TLS connection to the proxy using the provided SSL context. |
|
""" |
|
|
|
proxy_config = typing.cast(ProxyConfig, self.proxy_config) |
|
ssl_context = proxy_config.ssl_context |
|
sock_and_verified = _ssl_wrap_socket_and_match_hostname( |
|
sock, |
|
cert_reqs=self.cert_reqs, |
|
ssl_version=self.ssl_version, |
|
ssl_minimum_version=self.ssl_minimum_version, |
|
ssl_maximum_version=self.ssl_maximum_version, |
|
ca_certs=self.ca_certs, |
|
ca_cert_dir=self.ca_cert_dir, |
|
ca_cert_data=self.ca_cert_data, |
|
server_hostname=hostname, |
|
ssl_context=ssl_context, |
|
assert_hostname=proxy_config.assert_hostname, |
|
assert_fingerprint=proxy_config.assert_fingerprint, |
|
|
|
cert_file=None, |
|
key_file=None, |
|
key_password=None, |
|
tls_in_tls=False, |
|
) |
|
self.proxy_is_verified = sock_and_verified.is_verified |
|
return sock_and_verified.socket |
|
|
|
|
|
class _WrappedAndVerifiedSocket(typing.NamedTuple): |
|
""" |
|
Wrapped socket and whether the connection is |
|
verified after the TLS handshake |
|
""" |
|
|
|
socket: ssl.SSLSocket | SSLTransport |
|
is_verified: bool |
|
|
|
|
|
def _ssl_wrap_socket_and_match_hostname( |
|
sock: socket.socket, |
|
*, |
|
cert_reqs: None | str | int, |
|
ssl_version: None | str | int, |
|
ssl_minimum_version: int | None, |
|
ssl_maximum_version: int | None, |
|
cert_file: str | None, |
|
key_file: str | None, |
|
key_password: str | None, |
|
ca_certs: str | None, |
|
ca_cert_dir: str | None, |
|
ca_cert_data: None | str | bytes, |
|
assert_hostname: None | str | Literal[False], |
|
assert_fingerprint: str | None, |
|
server_hostname: str | None, |
|
ssl_context: ssl.SSLContext | None, |
|
tls_in_tls: bool = False, |
|
) -> _WrappedAndVerifiedSocket: |
|
"""Logic for constructing an SSLContext from all TLS parameters, passing |
|
that down into ssl_wrap_socket, and then doing certificate verification |
|
either via hostname or fingerprint. This function exists to guarantee |
|
that both proxies and targets have the same behavior when connecting via TLS. |
|
""" |
|
default_ssl_context = False |
|
if ssl_context is None: |
|
default_ssl_context = True |
|
context = create_urllib3_context( |
|
ssl_version=resolve_ssl_version(ssl_version), |
|
ssl_minimum_version=ssl_minimum_version, |
|
ssl_maximum_version=ssl_maximum_version, |
|
cert_reqs=resolve_cert_reqs(cert_reqs), |
|
) |
|
else: |
|
context = ssl_context |
|
|
|
context.verify_mode = resolve_cert_reqs(cert_reqs) |
|
|
|
|
|
if ( |
|
|
|
assert_fingerprint |
|
or assert_hostname |
|
|
|
or assert_hostname is False |
|
|
|
|
|
or ssl_.IS_PYOPENSSL |
|
or not ssl_.HAS_NEVER_CHECK_COMMON_NAME |
|
): |
|
context.check_hostname = False |
|
|
|
|
|
|
|
|
|
if ( |
|
not ca_certs |
|
and not ca_cert_dir |
|
and not ca_cert_data |
|
and default_ssl_context |
|
and hasattr(context, "load_default_certs") |
|
): |
|
context.load_default_certs() |
|
|
|
|
|
|
|
|
|
if server_hostname is not None: |
|
normalized = server_hostname.strip("[]") |
|
if "%" in normalized: |
|
normalized = normalized[: normalized.rfind("%")] |
|
if is_ipaddress(normalized): |
|
server_hostname = normalized |
|
|
|
ssl_sock = ssl_wrap_socket( |
|
sock=sock, |
|
keyfile=key_file, |
|
certfile=cert_file, |
|
key_password=key_password, |
|
ca_certs=ca_certs, |
|
ca_cert_dir=ca_cert_dir, |
|
ca_cert_data=ca_cert_data, |
|
server_hostname=server_hostname, |
|
ssl_context=context, |
|
tls_in_tls=tls_in_tls, |
|
) |
|
|
|
try: |
|
if assert_fingerprint: |
|
_assert_fingerprint( |
|
ssl_sock.getpeercert(binary_form=True), assert_fingerprint |
|
) |
|
elif ( |
|
context.verify_mode != ssl.CERT_NONE |
|
and not context.check_hostname |
|
and assert_hostname is not False |
|
): |
|
cert: _TYPE_PEER_CERT_RET_DICT = ssl_sock.getpeercert() |
|
|
|
|
|
|
|
|
|
if default_ssl_context: |
|
hostname_checks_common_name = False |
|
else: |
|
hostname_checks_common_name = ( |
|
getattr(context, "hostname_checks_common_name", False) or False |
|
) |
|
|
|
_match_hostname( |
|
cert, |
|
assert_hostname or server_hostname, |
|
hostname_checks_common_name, |
|
) |
|
|
|
return _WrappedAndVerifiedSocket( |
|
socket=ssl_sock, |
|
is_verified=context.verify_mode == ssl.CERT_REQUIRED |
|
or bool(assert_fingerprint), |
|
) |
|
except BaseException: |
|
ssl_sock.close() |
|
raise |
|
|
|
|
|
def _match_hostname( |
|
cert: _TYPE_PEER_CERT_RET_DICT | None, |
|
asserted_hostname: str, |
|
hostname_checks_common_name: bool = False, |
|
) -> None: |
|
|
|
|
|
|
|
stripped_hostname = asserted_hostname.strip("[]") |
|
if is_ipaddress(stripped_hostname): |
|
asserted_hostname = stripped_hostname |
|
|
|
try: |
|
match_hostname(cert, asserted_hostname, hostname_checks_common_name) |
|
except CertificateError as e: |
|
log.warning( |
|
"Certificate did not match expected hostname: %s. Certificate: %s", |
|
asserted_hostname, |
|
cert, |
|
) |
|
|
|
|
|
e._peer_cert = cert |
|
raise |
|
|
|
|
|
def _wrap_proxy_error(err: Exception, proxy_scheme: str | None) -> ProxyError: |
|
|
|
|
|
|
|
error_normalized = " ".join(re.split("[^a-z]", str(err).lower())) |
|
is_likely_http_proxy = ( |
|
"wrong version number" in error_normalized |
|
or "unknown protocol" in error_normalized |
|
or "record layer failure" in error_normalized |
|
) |
|
http_proxy_warning = ( |
|
". Your proxy appears to only use HTTP and not HTTPS, " |
|
"try changing your proxy URL to be HTTP. See: " |
|
"https://urllib3.readthedocs.io/en/latest/advanced-usage.html" |
|
"#https-proxy-error-http-proxy" |
|
) |
|
new_err = ProxyError( |
|
f"Unable to connect to proxy" |
|
f"{http_proxy_warning if is_likely_http_proxy and proxy_scheme == 'https' else ''}", |
|
err, |
|
) |
|
new_err.__cause__ = err |
|
return new_err |
|
|
|
|
|
def _get_default_user_agent() -> str: |
|
return f"python-urllib3/{__version__}" |
|
|
|
|
|
class DummyConnection: |
|
"""Used to detect a failed ConnectionCls import.""" |
|
|
|
|
|
if not ssl: |
|
HTTPSConnection = DummyConnection |
|
|
|
|
|
VerifiedHTTPSConnection = HTTPSConnection |
|
|
|
|
|
def _url_from_connection( |
|
conn: HTTPConnection | HTTPSConnection, path: str | None = None |
|
) -> str: |
|
"""Returns the URL from a given connection. This is mainly used for testing and logging.""" |
|
|
|
scheme = "https" if isinstance(conn, HTTPSConnection) else "http" |
|
|
|
return Url(scheme=scheme, host=conn.host, port=conn.port, path=path).url |
|
|