summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Petrov <andrey.petrov@shazow.net>2012-06-30 16:03:30 -0700
committerAndrey Petrov <andrey.petrov@shazow.net>2012-06-30 16:03:30 -0700
commitd92d85c05d8630fbc09b6d0509ad9856d4fa957b (patch)
tree71b5e2f81f8d36da9bcf196901ea7f5ff249e25c
parentce4c03f4bd10d7bdae478b8a8cdc00c27d1e3d88 (diff)
downloadurllib3-new-connections.tar.gz
Messing around with new conneciton structure, factoring out httplib.new-connections
-rw-r--r--urllib3/connection/__init__.py0
-rw-r--r--urllib3/connection/_httplib.py96
-rw-r--r--urllib3/connection/base.py20
3 files changed, 116 insertions, 0 deletions
diff --git a/urllib3/connection/__init__.py b/urllib3/connection/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/urllib3/connection/__init__.py
diff --git a/urllib3/connection/_httplib.py b/urllib3/connection/_httplib.py
new file mode 100644
index 00000000..6c853b92
--- /dev/null
+++ b/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)
diff --git a/urllib3/connection/base.py b/urllib3/connection/base.py
new file mode 100644
index 00000000..79149eb6
--- /dev/null
+++ b/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'])