summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThea Flowers <theaflowers@google.com>2018-10-05 16:11:48 -0700
committerSeth M. Larson <sethmichaellarson@gmail.com>2018-10-05 18:11:48 -0500
commit977c9ffc86df83e69d3350b46b32abe9f916b5d7 (patch)
tree480acae5791851aa06171af587de765f7690b357
parent6b3636f797840e6e01ed3c1ecfbcb6290069614a (diff)
downloadurllib3-977c9ffc86df83e69d3350b46b32abe9f916b5d7.tar.gz
Early-out ipv6 checks when running on App Engine (#1450)
-rw-r--r--src/urllib3/contrib/_appengine_environ.py30
-rw-r--r--src/urllib3/contrib/appengine.py30
-rw-r--r--src/urllib3/util/connection.py8
-rw-r--r--test/test_util.py7
4 files changed, 52 insertions, 23 deletions
diff --git a/src/urllib3/contrib/_appengine_environ.py b/src/urllib3/contrib/_appengine_environ.py
new file mode 100644
index 00000000..f3e00942
--- /dev/null
+++ b/src/urllib3/contrib/_appengine_environ.py
@@ -0,0 +1,30 @@
+"""
+This module provides means to detect the App Engine environment.
+"""
+
+import os
+
+
+def is_appengine():
+ return (is_local_appengine() or
+ is_prod_appengine() or
+ is_prod_appengine_mvms())
+
+
+def is_appengine_sandbox():
+ return is_appengine() and not is_prod_appengine_mvms()
+
+
+def is_local_appengine():
+ return ('APPENGINE_RUNTIME' in os.environ and
+ 'Development/' in os.environ['SERVER_SOFTWARE'])
+
+
+def is_prod_appengine():
+ return ('APPENGINE_RUNTIME' in os.environ and
+ 'Google App Engine/' in os.environ['SERVER_SOFTWARE'] and
+ not is_prod_appengine_mvms())
+
+
+def is_prod_appengine_mvms():
+ return os.environ.get('GAE_VM', False) == 'true'
diff --git a/src/urllib3/contrib/appengine.py b/src/urllib3/contrib/appengine.py
index b2951cf8..2952f114 100644
--- a/src/urllib3/contrib/appengine.py
+++ b/src/urllib3/contrib/appengine.py
@@ -41,7 +41,6 @@ urllib3 on Google App Engine:
from __future__ import absolute_import
import io
import logging
-import os
import warnings
from ..packages.six.moves.urllib.parse import urljoin
@@ -58,6 +57,7 @@ from ..request import RequestMethods
from ..response import HTTPResponse
from ..util.timeout import Timeout
from ..util.retry import Retry
+from . import _appengine_environ
try:
from google.appengine.api import urlfetch
@@ -280,26 +280,10 @@ class AppEngineManager(RequestMethods):
return retries
-def is_appengine():
- return (is_local_appengine() or
- is_prod_appengine() or
- is_prod_appengine_mvms())
+# Alias methods from _appengine_environ to maintain public API interface.
-
-def is_appengine_sandbox():
- return is_appengine() and not is_prod_appengine_mvms()
-
-
-def is_local_appengine():
- return ('APPENGINE_RUNTIME' in os.environ and
- 'Development/' in os.environ['SERVER_SOFTWARE'])
-
-
-def is_prod_appengine():
- return ('APPENGINE_RUNTIME' in os.environ and
- 'Google App Engine/' in os.environ['SERVER_SOFTWARE'] and
- not is_prod_appengine_mvms())
-
-
-def is_prod_appengine_mvms():
- return os.environ.get('GAE_VM', False) == 'true'
+is_appengine = _appengine_environ.is_appengine
+is_appengine_sandbox = _appengine_environ.is_appengine_sandbox
+is_local_appengine = _appengine_environ.is_local_appengine
+is_prod_appengine = _appengine_environ.is_prod_appengine
+is_prod_appengine_mvms = _appengine_environ.is_prod_appengine_mvms
diff --git a/src/urllib3/util/connection.py b/src/urllib3/util/connection.py
index 5cf488f4..5ad70b2f 100644
--- a/src/urllib3/util/connection.py
+++ b/src/urllib3/util/connection.py
@@ -1,6 +1,7 @@
from __future__ import absolute_import
import socket
from .wait import NoWayToWaitForSocketError, wait_for_read
+from ..contrib import _appengine_environ
def is_connection_dropped(conn): # Platform-specific
@@ -105,6 +106,13 @@ def _has_ipv6(host):
sock = None
has_ipv6 = False
+ # App Engine doesn't support IPV6 sockets and actually has a quota on the
+ # number of sockets that can be used, so just early out here instead of
+ # creating a socket needlessly.
+ # See https://github.com/urllib3/urllib3/issues/1446
+ if _appengine_environ.is_appengine_sandbox():
+ return False
+
if socket.has_ipv6:
# has_ipv6 returns true if cPython was compiled with IPv6 support.
# It does not tell us if the system has IPv6 support enabled. To
diff --git a/test/test_util.py b/test/test_util.py
index 73d9452a..a0c99a16 100644
--- a/test/test_util.py
+++ b/test/test_util.py
@@ -540,6 +540,13 @@ class TestUtil(object):
instance.bind.return_value = True
assert _has_ipv6('::1')
+ def test_has_ipv6_disabled_on_appengine(self):
+ gae_patch = patch(
+ 'urllib3.contrib._appengine_environ.is_appengine_sandbox',
+ return_value=True)
+ with gae_patch:
+ assert not _has_ipv6('::1')
+
def test_ip_family_ipv6_enabled(self):
with patch('urllib3.util.connection.HAS_IPV6', True):
assert allowed_gai_family() == socket.AF_UNSPEC