Skip to content

Commit

Permalink
Raise SSLError during runtime if no SSL support at runtime, instead o…
Browse files Browse the repository at this point in the history
…f ImportError during install (fixes #41)
  • Loading branch information
shazow committed Jan 29, 2012
1 parent a62860b commit 074fac2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
20 changes: 20 additions & 0 deletions test/with_dummyserver/test_https.py
Expand Up @@ -14,6 +14,7 @@
log.setLevel(logging.NOTSET)
log.addHandler(logging.StreamHandler(sys.stdout))


class TestHTTPS(HTTPSDummyServerTestCase):
def setUp(self):
self._pool = HTTPSConnectionPool(self.host, self.port)
Expand Down Expand Up @@ -54,6 +55,25 @@ def test_verified(self):
except SSLError as e:
self.assertTrue("doesn't match" in str(e))

def test_no_ssl(self):
import urllib3.connectionpool
OriginalHTTPSConnection = urllib3.connectionpool.HTTPSConnection
OriginalSSL = urllib3.connectionpool.ssl

urllib3.connectionpool.HTTPSConnection = None
urllib3.connectionpool.ssl = None

self.assertRaises(SSLError, self._pool._new_conn)

self.assertRaises(SSLError,
lambda: self._pool.request('GET', '/specific_method',
fields={'method': 'GET'}))

# Undo
urllib3.HTTPSConnection = OriginalHTTPSConnection
urllib3.connectionpool.ssl = OriginalSSL



if __name__ == '__main__':
unittest.main()
22 changes: 18 additions & 4 deletions urllib3/connectionpool.py
Expand Up @@ -16,23 +16,33 @@
poll = False

try: # Python 3
from http.client import HTTPConnection, HTTPSConnection, HTTPException
from http.client import HTTPConnection, HTTPException
from http.client import HTTP_PORT, HTTPS_PORT
except ImportError:
from httplib import HTTPConnection, HTTPSConnection, HTTPException
from httplib import HTTPConnection, HTTPException
from httplib import HTTP_PORT, HTTPS_PORT

try: # Python 3
from queue import Queue, Empty, Full
except ImportError:
from Queue import Queue, Empty, Full


try: # Compiled with SSL?
HTTPSConnection = None
BaseSSLError = None
ssl = None

try: # Python 3
from http.client import HTTPSConnection
except ImportError:
from httplib import HTTPSConnection

import ssl
BaseSSLError = ssl.SSLError

except ImportError:
ssl = None
BaseSSLError = None
pass


from .packages.ssl_match_hostname import match_hostname, CertificateError
Expand Down Expand Up @@ -469,6 +479,10 @@ def _new_conn(self):
% (self.num_connections, self.host))

if not ssl:
if not HTTPSConnection:
raise SSLError("Can't connect to HTTPS URL because the SSL "
"module is not available.")

return HTTPSConnection(host=self.host, port=self.port)

connection = VerifiedHTTPSConnection(host=self.host, port=self.port)
Expand Down

0 comments on commit 074fac2

Please sign in to comment.