Ensure we are utilizing the context created by HTTPSConnection, or falling back to ssl. Fixes #517

This commit is contained in:
Matt Martz 2018-05-24 09:37:39 -05:00
parent c7530bb143
commit 41e599f9c3
1 changed files with 17 additions and 12 deletions

View File

@ -36,7 +36,7 @@ except ImportError:
gzip = None gzip = None
GZIP_BASE = object GZIP_BASE = object
__version__ = '2.0.1' __version__ = '2.0.2a'
class FakeShutdownEvent(object): class FakeShutdownEvent(object):
@ -85,9 +85,9 @@ except ImportError:
HTTPErrorProcessor, OpenerDirector) HTTPErrorProcessor, OpenerDirector)
try: try:
from httplib import HTTPConnection from httplib import HTTPConnection, BadStatusLine
except ImportError: except ImportError:
from http.client import HTTPConnection from http.client import HTTPConnection, BadStatusLine
try: try:
from httplib import HTTPSConnection from httplib import HTTPSConnection
@ -266,10 +266,13 @@ try:
except AttributeError: except AttributeError:
CERT_ERROR = tuple() CERT_ERROR = tuple()
HTTP_ERRORS = ((HTTPError, URLError, socket.error, ssl.SSLError) + HTTP_ERRORS = (
CERT_ERROR) (HTTPError, URLError, socket.error, ssl.SSLError, BadStatusLine) +
CERT_ERROR
)
except ImportError: except ImportError:
HTTP_ERRORS = (HTTPError, URLError, socket.error) ssl = None
HTTP_ERRORS = (HTTPError, URLError, socket.error, BadStatusLine)
class SpeedtestException(Exception): class SpeedtestException(Exception):
@ -420,14 +423,12 @@ if HTTPSConnection:
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
source_address = kwargs.pop('source_address', None) source_address = kwargs.pop('source_address', None)
context = kwargs.pop('context', None)
timeout = kwargs.pop('timeout', 10) timeout = kwargs.pop('timeout', 10)
HTTPSConnection.__init__(self, *args, **kwargs) HTTPSConnection.__init__(self, *args, **kwargs)
self.source_address = source_address
self._context = context
self.timeout = timeout self.timeout = timeout
self.source_address = source_address
def connect(self): def connect(self):
"Connect to a host on a given (SSL) port." "Connect to a host on a given (SSL) port."
@ -435,9 +436,13 @@ if HTTPSConnection:
SpeedtestHTTPConnection.connect(self) SpeedtestHTTPConnection.connect(self)
kwargs = {} kwargs = {}
if ssl:
if hasattr(ssl, 'SSLContext'): if hasattr(ssl, 'SSLContext'):
kwargs['server_hostname'] = self.host kwargs['server_hostname'] = self.host
try:
self.sock = self._context.wrap_socket(self.sock, **kwargs) self.sock = self._context.wrap_socket(self.sock, **kwargs)
except AttributeError:
self.sock = ssl.wrap_socket(self.sock, **kwargs)
def _build_connection(connection, source_address, timeout, context=None): def _build_connection(connection, source_address, timeout, context=None):