Skip to content

Commit

Permalink
[webkitpy] Don't require urllib3
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=271062
rdar://124694958

Reviewed by Elliott Williams.

* Tools/Scripts/webkitpy/__init__.py:
(create_connection): Only override default timeout behavior if urllib3
is available.

Canonical link: https://commits.webkit.org/276170@main
  • Loading branch information
JonWBedard committed Mar 15, 2024
1 parent ed5868a commit 8acea81
Showing 1 changed file with 68 additions and 66 deletions.
134 changes: 68 additions & 66 deletions Tools/Scripts/webkitpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,75 +141,77 @@
except Exception as e:
sys.stderr.write('Error while importing six: {}... trying to continue\n'.format(e))

import socket
import urllib3

HAS_CHECKED_IPV6 = False
FIRST_IPV6_TIMEOUT = 3


# Pulled from urllib3, https://github.com/urllib3/urllib3/blob/main/src/urllib3/util/connection.py
# Disable IPV6 if the first attempt to connect to an IPV6 address fails
def create_connection(
address,
timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
source_address=None,
socket_options=None,
):
global HAS_CHECKED_IPV6

host, port = address
if host.startswith("["):
host = host.strip("[]")
err = None
try:
import socket
import urllib3

family = urllib3.util.connection.allowed_gai_family()
HAS_CHECKED_IPV6 = False
FIRST_IPV6_TIMEOUT = 3

try:
host.encode("idna")
except UnicodeError:
return six.raise_from(
LocationParseError(u"'%s', label empty or too long" % host), None
)
# Pulled from urllib3, https://github.com/urllib3/urllib3/blob/main/src/urllib3/util/connection.py
# Disable IPV6 if the first attempt to connect to an IPV6 address fails
def create_connection(
address,
timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
source_address=None,
socket_options=None,
):
global HAS_CHECKED_IPV6

for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
host, port = address
if host.startswith("["):
host = host.strip("[]")
err = None

is_first_ipv6 = not HAS_CHECKED_IPV6 and af == socket.AF_INET6
if af == socket.AF_INET6 and not urllib3.util.connection.HAS_IPV6:
continue
family = urllib3.util.connection.allowed_gai_family()

sock = None
try:
sock = socket.socket(af, socktype, proto)

urllib3.util.connection._set_socket_options(sock, socket_options)

if is_first_ipv6 and (not timeout or timeout > FIRST_IPV6_TIMEOUT):
sock.settimeout(FIRST_IPV6_TIMEOUT)
elif timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
sock.settimeout(timeout)
if source_address:
sock.bind(source_address)
sock.connect(sa)
return sock

except socket.error as e:
err = e
if sock is not None:
sock.close()
sock = None
if is_first_ipv6:
sys.stderr.write('IPV6 request to {} has failed, disabling IPV6\n'.format(host))
urllib3.util.connection.HAS_IPV6 = False
finally:
if af == socket.AF_INET6:
HAS_CHECKED_IPV6 = True

if err is not None:
raise err

raise socket.error("getaddrinfo returns an empty list")


urllib3.util.connection.create_connection = create_connection
host.encode("idna")
except UnicodeError:
return six.raise_from(
LocationParseError(u"'%s', label empty or too long" % host), None
)

for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res

is_first_ipv6 = not HAS_CHECKED_IPV6 and af == socket.AF_INET6
if af == socket.AF_INET6 and not urllib3.util.connection.HAS_IPV6:
continue

sock = None
try:
sock = socket.socket(af, socktype, proto)

urllib3.util.connection._set_socket_options(sock, socket_options)

if is_first_ipv6 and (not timeout or timeout > FIRST_IPV6_TIMEOUT):
sock.settimeout(FIRST_IPV6_TIMEOUT)
elif timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
sock.settimeout(timeout)
if source_address:
sock.bind(source_address)
sock.connect(sa)
return sock

except socket.error as e:
err = e
if sock is not None:
sock.close()
sock = None
if is_first_ipv6:
sys.stderr.write('IPV6 request to {} has failed, disabling IPV6\n'.format(host))
urllib3.util.connection.HAS_IPV6 = False
finally:
if af == socket.AF_INET6:
HAS_CHECKED_IPV6 = True

if err is not None:
raise err

raise socket.error("getaddrinfo returns an empty list")

urllib3.util.connection.create_connection = create_connection

except ModuleNotFoundError:
pass

0 comments on commit 8acea81

Please sign in to comment.