Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Messing around with new conneciton structure, factoring out httplib.

  • Loading branch information...
commit d92d85c05d8630fbc09b6d0509ad9856d4fa957b 1 parent ce4c03f
@shazow authored
View
0  urllib3/connection/__init__.py
No changes.
View
96 urllib3/connection/_httplib.py
@@ -0,0 +1,96 @@
+# urllib3/connections/_httplib.py
+# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
+#
+# This module is part of urllib3 and is released under
+# the MIT License: http://www.opensource.org/licenses/mit-license.php
+
+
+import socket
+
+try: # Python 3
+ from http.client import HTTPConnection as _HTTPConnection
+except ImportError:
+ from httplib import HTTPConnection as _HTTPConnection
+
+try: # Compiled with SSL?
+ _HTTPSConnection = object
+ BaseSSLError = None
+ ssl = None
+
+ try: # Python 3
+ from http.client import HTTPSConnection as _HTTPSConnection
+ except ImportError:
+ from httplib import HTTPSConnection as _HTTPSConnection
+
+ import ssl
+ BaseSSLError = ssl.SSLError
+
+except (ImportError, AttributeError):
+ pass
+
+from .base import _Connection
+from ..packages.ssl_match_hostname import match_hostname
+
+
+
+## Connection objects (extension of httplib)
+
+class _VerifiedHTTPSConnection(_HTTPSConnection):
+ """
+ Based on httplib.HTTPSConnection but wraps the socket with
+ SSL certification.
+ """
+ cert_reqs = None
+ ca_certs = None
+
+ def set_cert(self, key_file=None, cert_file=None,
+ cert_reqs='CERT_NONE', ca_certs=None):
+ ssl_req_scheme = {
+ 'CERT_NONE': ssl.CERT_NONE,
+ 'CERT_OPTIONAL': ssl.CERT_OPTIONAL,
+ 'CERT_REQUIRED': ssl.CERT_REQUIRED
+ }
+
+ self.key_file = key_file
+ self.cert_file = cert_file
+ self.cert_reqs = ssl_req_scheme.get(cert_reqs) or ssl.CERT_NONE
+ self.ca_certs = ca_certs
+
+ def connect(self):
+ # Add certificate verification
+ sock = socket.create_connection((self.host, self.port), self.timeout)
+
+ # Wrap socket using verification with the root certs in
+ # trusted_root_certs
+ self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file,
+ cert_reqs=self.cert_reqs,
+ ca_certs=self.ca_certs)
+ if self.ca_certs:
+ match_hostname(self.sock.getpeercert(), self.host)
+
+
+def connection_from_url(url, **kw):
+ """
+ Given a url, return an :class:`.Connection` instance of its host.
+
+ This is a shortcut for not having to parse out the scheme, host, and port
+ of the url before creating an :class:`.ConnectionPool` instance.
+
+ :param url:
+ Absolute URL string that must include the scheme. Port is optional.
+
+ :param \**kw:
+ Passes additional parameters to the constructor of the appropriate
+ :class:`.ConnectionPool`. Useful for specifying things like
+ timeout, maxsize, headers, etc.
+
+ Example: ::
+
+ >>> conn = connection_from_url('http://google.com/')
+ >>> r = conn.request('GET', '/')
+ """
+ scheme, host, port = get_host(url)
+ if scheme == 'https':
+ return HTTPSConnectionPool(host, port=port, **kw)
+ else:
+ return HTTPConnectionPool(host, port=port, **kw)
View
20 urllib3/connection/base.py
@@ -0,0 +1,20 @@
+# urllib3/connection/base.py
+# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
+#
+# This module is part of urllib3 and is released under
+# the MIT License: http://www.opensource.org/licenses/mit-license.php
+
+
+from collections import namedtuple
+
+
+class Connection(object):
+ "Generic Connection base class."
+
+ def __init__(self, host, port):
+ self.host = host
+ self.port = port
+
+
+
+Cert = namedtuple('Cert', ['key_file', 'cert_file', 'cert_reqs', 'ca_certs'])
Please sign in to comment.
Something went wrong with that request. Please try again.