Skip to content

Commit 0c67a10

Browse files
SNOW-896926 adjust vendored urllib3 and requests (#2505)
Co-authored-by: Patryk Czajka <patryk.czajka@snowflake.com>
1 parent 78955fb commit 0c67a10

File tree

11 files changed

+63
-41
lines changed

11 files changed

+63
-41
lines changed

src/snowflake/connector/session_manager.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .vendored import requests
1515
from .vendored.requests import Response, Session
1616
from .vendored.requests.adapters import BaseAdapter, HTTPAdapter
17-
from .vendored.requests.exceptions import InvalidProxyURL
17+
from .vendored.requests.exceptions import InvalidProxyURL, InvalidURL
1818
from .vendored.requests.utils import prepend_scheme_if_needed, select_proxy
1919
from .vendored.urllib3 import PoolManager, Retry
2020
from .vendored.urllib3.poolmanager import ProxyManager
@@ -23,7 +23,6 @@
2323
if TYPE_CHECKING:
2424
from .vendored.urllib3.connectionpool import HTTPConnectionPool, HTTPSConnectionPool
2525

26-
2726
logger = logging.getLogger(__name__)
2827
REQUESTS_RETRY = 1 # requests library builtin retry
2928

@@ -60,19 +59,25 @@ def wrapper(self, *args, **kwargs):
6059
class ProxySupportAdapter(HTTPAdapter):
6160
"""This Adapter creates proper headers for Proxy CONNECT messages."""
6261

63-
def get_connection(
64-
self, url: str, proxies: dict | None = None
62+
def get_connection_with_tls_context(
63+
self, request, verify, proxies=None, cert=None
6564
) -> HTTPConnectionPool | HTTPSConnectionPool:
66-
proxy = select_proxy(url, proxies)
67-
parsed_url = urlparse(url)
68-
65+
proxy = select_proxy(request.url, proxies)
66+
try:
67+
host_params, pool_kwargs = self.build_connection_pool_key_attributes(
68+
request,
69+
verify,
70+
cert,
71+
)
72+
except ValueError as e:
73+
raise InvalidURL(e, request=request)
6974
if proxy:
7075
proxy = prepend_scheme_if_needed(proxy, "http")
7176
proxy_url = parse_url(proxy)
7277
if not proxy_url.host:
7378
raise InvalidProxyURL(
74-
"Please check proxy URL. It is malformed"
75-
" and could be missing the host."
79+
"Please check proxy URL. It is malformed "
80+
"and could be missing the host."
7681
)
7782
proxy_manager = self.proxy_manager_for(proxy)
7883

@@ -85,17 +90,22 @@ def get_connection(
8590
# Note: netloc also keeps user-info (user:pass@host) if present in URL. The driver never sends
8691
# URLs with embedded credentials, so we leave them unhandled — for full support
8792
# we’d need to manually concatenate hostname with optional port and IPv6 brackets.
93+
parsed_url = urlparse(request.url)
8894
proxy_manager.proxy_headers["Host"] = parsed_url.netloc
8995
else:
9096
logger.debug(
9197
f"Unable to set 'Host' to proxy manager of type {type(proxy_manager)} as"
9298
f" it does not have attribute 'proxy_headers'."
9399
)
94-
conn = proxy_manager.connection_from_url(url)
100+
101+
conn = proxy_manager.connection_from_host(
102+
**host_params, pool_kwargs=pool_kwargs
103+
)
95104
else:
96105
# Only scheme should be lower case
97-
url = parsed_url.geturl()
98-
conn = self.poolmanager.connection_from_url(url)
106+
conn = self.poolmanager.connection_from_host(
107+
**host_params, pool_kwargs=pool_kwargs
108+
)
99109

100110
return conn
101111

src/snowflake/connector/vendored/requests/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
import warnings
4242

43-
import urllib3
43+
from .. import urllib3
4444

4545
from .exceptions import RequestsDependencyWarning
4646

@@ -128,7 +128,7 @@ def _check_cryptography(cryptography_version):
128128
ssl = None
129129

130130
if not getattr(ssl, "HAS_SNI", False):
131-
from urllib3.contrib import pyopenssl
131+
from ..urllib3.contrib import pyopenssl
132132

133133
pyopenssl.inject_into_urllib3()
134134

@@ -140,7 +140,7 @@ def _check_cryptography(cryptography_version):
140140
pass
141141

142142
# urllib3's DependencyWarnings should be silenced.
143-
from urllib3.exceptions import DependencyWarning
143+
from ..urllib3.exceptions import DependencyWarning
144144

145145
warnings.simplefilter("ignore", DependencyWarning)
146146

src/snowflake/connector/vendored/requests/adapters.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
import typing
1212
import warnings
1313

14-
from urllib3.exceptions import ClosedPoolError, ConnectTimeoutError
15-
from urllib3.exceptions import HTTPError as _HTTPError
16-
from urllib3.exceptions import InvalidHeader as _InvalidHeader
17-
from urllib3.exceptions import (
14+
from ..urllib3.exceptions import ClosedPoolError, ConnectTimeoutError
15+
from ..urllib3.exceptions import HTTPError as _HTTPError
16+
from ..urllib3.exceptions import InvalidHeader as _InvalidHeader
17+
from ..urllib3.exceptions import (
1818
LocationValueError,
1919
MaxRetryError,
2020
NewConnectionError,
2121
ProtocolError,
2222
)
23-
from urllib3.exceptions import ProxyError as _ProxyError
24-
from urllib3.exceptions import ReadTimeoutError, ResponseError
25-
from urllib3.exceptions import SSLError as _SSLError
26-
from urllib3.poolmanager import PoolManager, proxy_from_url
27-
from urllib3.util import Timeout as TimeoutSauce
28-
from urllib3.util import parse_url
29-
from urllib3.util.retry import Retry
23+
from ..urllib3.exceptions import ProxyError as _ProxyError
24+
from ..urllib3.exceptions import ReadTimeoutError, ResponseError
25+
from ..urllib3.exceptions import SSLError as _SSLError
26+
from ..urllib3.poolmanager import PoolManager, proxy_from_url
27+
from ..urllib3.util import Timeout as TimeoutSauce
28+
from ..urllib3.util import parse_url
29+
from ..urllib3.util.retry import Retry
3030

3131
from .auth import _basic_auth_str
3232
from .compat import basestring, urlparse
@@ -56,7 +56,7 @@
5656
)
5757

5858
try:
59-
from urllib3.contrib.socks import SOCKSProxyManager
59+
from ..urllib3.contrib.socks import SOCKSProxyManager
6060
except ImportError:
6161

6262
def SOCKSProxyManager(*args, **kwargs):

src/snowflake/connector/vendored/requests/compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# -------
1414
# urllib3
1515
# -------
16-
from urllib3 import __version__ as urllib3_version
16+
from ..urllib3 import __version__ as urllib3_version
1717

1818
# Detect which major version of urllib3 is being used.
1919
try:

src/snowflake/connector/vendored/requests/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
This module contains the set of Requests' exceptions.
66
"""
7-
from urllib3.exceptions import HTTPError as BaseHTTPError
7+
from ..urllib3.exceptions import HTTPError as BaseHTTPError
88

99
from .compat import JSONDecodeError as CompatJSONDecodeError
1010

src/snowflake/connector/vendored/requests/help.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77

88
import idna
9-
import urllib3
9+
from .. import urllib3
1010

1111
from . import __version__ as requests_version
1212

@@ -21,7 +21,7 @@
2121
chardet = None
2222

2323
try:
24-
from urllib3.contrib import pyopenssl
24+
from ..urllib3.contrib import pyopenssl
2525
except ImportError:
2626
pyopenssl = None
2727
OpenSSL = None

src/snowflake/connector/vendored/requests/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
import encodings.idna # noqa: F401
1414
from io import UnsupportedOperation
1515

16-
from urllib3.exceptions import (
16+
from ..urllib3.exceptions import (
1717
DecodeError,
1818
LocationParseError,
1919
ProtocolError,
2020
ReadTimeoutError,
2121
SSLError,
2222
)
23-
from urllib3.fields import RequestField
24-
from urllib3.filepost import encode_multipart_formdata
25-
from urllib3.util import parse_url
23+
from ..urllib3.fields import RequestField
24+
from ..urllib3.filepost import encode_multipart_formdata
25+
from ..urllib3.util import parse_url
2626

2727
from ._internal_utils import to_native_string, unicode_is_ascii
2828
from .auth import HTTPBasicAuth

src/snowflake/connector/vendored/requests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import zipfile
2020
from collections import OrderedDict
2121

22-
from urllib3.util import make_headers, parse_url
22+
from ..urllib3.util import make_headers, parse_url
2323

2424
from . import certs
2525
from .__version__ import __version__

src/snowflake/connector/vendored/urllib3/connection.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,22 @@ def connect(self) -> None:
338338
if self._has_connected_to_proxy:
339339
self.proxy_is_verified = False
340340

341+
# See issue for more context: https://github.com/urllib3/urllib3/issues/1878
342+
# the maintainers know that this issue can be resolve using the change below but
343+
# they have not merged this change because they need to root-cause it. See
344+
# comment: https://github.com/urllib3/urllib3/issues/1878#issuecomment-641548977
345+
# adding the fix in our vendored code so our users get unblocked
346+
def _is_closed_patch_for_invalid_socket_descriptor(self):
347+
if getattr(self.sock, "fileno", lambda _: None)() == -1:
348+
return True
349+
341350
@property
342351
def is_closed(self) -> bool:
343-
return self.sock is None
352+
return self.sock is None or self._is_closed_patch_for_invalid_socket_descriptor()
344353

345354
@property
346355
def is_connected(self) -> bool:
347-
if self.sock is None:
356+
if self.sock is None or self._is_closed_patch_for_invalid_socket_descriptor():
348357
return False
349358
return not wait_for_read(self.sock, timeout=0.0)
350359

src/snowflake/connector/vendored/urllib3/contrib/emscripten/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
import urllib3.connection
3+
import snowflake.connector.vendored.urllib3.connection
44

55
from ...connectionpool import HTTPConnectionPool, HTTPSConnectionPool
66
from .connection import EmscriptenHTTPConnection, EmscriptenHTTPSConnection
@@ -12,5 +12,5 @@ def inject_into_urllib3() -> None:
1212
# if it isn't ignored
1313
HTTPConnectionPool.ConnectionCls = EmscriptenHTTPConnection
1414
HTTPSConnectionPool.ConnectionCls = EmscriptenHTTPSConnection
15-
urllib3.connection.HTTPConnection = EmscriptenHTTPConnection # type: ignore[misc,assignment]
16-
urllib3.connection.HTTPSConnection = EmscriptenHTTPSConnection # type: ignore[misc,assignment]
15+
snowflake.connector.vendored.urllib3.connection.HTTPConnection = EmscriptenHTTPConnection # type: ignore[misc,assignment]
16+
snowflake.connector.vendored.urllib3.connection.HTTPSConnection = EmscriptenHTTPSConnection # type: ignore[misc,assignment]

0 commit comments

Comments
 (0)