~ubuntu-branches/ubuntu/wily/python-urllib3/wily

« back to all changes in this revision

Viewing changes to .pc/05_do-not-use-embedded-ssl-match-hostname.patch/urllib3/connection.py

  • Committer: Package Import Robot
  • Author(s): Daniele Tricoli
  • Date: 2014-07-07 16:09:06 UTC
  • mfrom: (4.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140707160906-hv2d4zrlg8u7rjby
Tags: 1.8.3-1
* New upstream release (Closes: #754090)
* debian/patches/01_do-not-use-embedded-python-six.patch
  - Refresh
* debian/patches/04_relax_nosetests_options.patch
  - Refresh

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
import socket
9
9
from socket import timeout as SocketTimeout
10
10
 
11
 
try: # Python 3
 
11
try:  # Python 3
12
12
    from http.client import HTTPConnection as _HTTPConnection, HTTPException
13
13
except ImportError:
14
14
    from httplib import HTTPConnection as _HTTPConnection, HTTPException
15
15
 
 
16
 
16
17
class DummyConnection(object):
17
18
    "Used to detect a failed ConnectionCls import."
18
19
    pass
19
20
 
20
 
try: # Compiled with SSL?
 
21
 
 
22
try:  # Compiled with SSL?
 
23
    HTTPSConnection = DummyConnection
 
24
    import ssl
 
25
    BaseSSLError = ssl.SSLError
 
26
except (ImportError, AttributeError):  # Platform-specific: No SSL.
21
27
    ssl = None
22
 
    HTTPSConnection = DummyConnection
23
28
 
24
29
    class BaseSSLError(BaseException):
25
30
        pass
26
31
 
27
 
    try: # Python 3
28
 
        from http.client import HTTPSConnection as _HTTPSConnection
29
 
    except ImportError:
30
 
        from httplib import HTTPSConnection as _HTTPSConnection
31
 
 
32
 
    import ssl
33
 
    BaseSSLError = ssl.SSLError
34
 
 
35
 
except (ImportError, AttributeError): # Platform-specific: No SSL.
36
 
    pass
37
32
 
38
33
from .exceptions import (
39
34
    ConnectTimeoutError,
58
53
    """
59
54
    Based on httplib.HTTPConnection but provides an extra constructor
60
55
    backwards-compatibility layer between older and newer Pythons.
 
56
 
 
57
    Additional keyword parameters are used to configure attributes of the connection.
 
58
    Accepted parameters include:
 
59
 
 
60
      - ``strict``: See the documentation on :class:`urllib3.connectionpool.HTTPConnectionPool`
 
61
      - ``source_address``: Set the source address for the current connection.
 
62
 
 
63
        .. note:: This is ignored for Python 2.6. It is only applied for 2.7 and 3.x
 
64
 
 
65
      - ``socket_options``: Set specific options on the underlying socket. If not specified, then
 
66
        defaults are loaded from ``HTTPConnection.default_socket_options`` which includes disabling
 
67
        Nagle's algorithm (sets TCP_NODELAY to 1) unless the connection is behind a proxy.
 
68
 
 
69
        For example, if you wish to enable TCP Keep Alive in addition to the defaults,
 
70
        you might pass::
 
71
 
 
72
            HTTPConnection.default_socket_options + [
 
73
                (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
 
74
            ]
 
75
 
 
76
        Or you may want to disable the defaults by passing an empty list (e.g., ``[]``).
61
77
    """
62
78
 
63
79
    default_port = port_by_scheme['http']
64
80
 
65
 
    # By default, disable Nagle's Algorithm.
66
 
    tcp_nodelay = 1
 
81
    #: Disable Nagle's algorithm by default.
 
82
    #: ``[(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]``
 
83
    default_socket_options = [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]
67
84
 
68
85
    def __init__(self, *args, **kw):
69
86
        if six.PY3:  # Python 3
74
91
        # Pre-set source_address in case we have an older Python like 2.6.
75
92
        self.source_address = kw.get('source_address')
76
93
 
 
94
        #: The socket options provided by the user. If no options are
 
95
        #: provided, we use the default options.
 
96
        self.socket_options = kw.pop('socket_options', self.default_socket_options)
 
97
 
77
98
        # Superclass also sets self.source_address in Python 2.7+.
78
 
        _HTTPConnection.__init__(self, *args, **kw)  
 
99
        _HTTPConnection.__init__(self, *args, **kw)
79
100
 
80
101
    def _new_conn(self):
81
102
        """ Establish a socket connection and set nodelay settings on it.
82
103
 
83
 
        :return: a new socket connection
 
104
        :return: New socket connection.
84
105
        """
85
106
        extra_args = []
86
107
        if self.source_address:  # Python 2.7+
87
108
            extra_args.append(self.source_address)
88
109
 
89
 
        conn = socket.create_connection(
90
 
            (self.host, self.port), self.timeout, *extra_args)
91
 
        conn.setsockopt(
92
 
            socket.IPPROTO_TCP, socket.TCP_NODELAY, self.tcp_nodelay)
 
110
        try:
 
111
            conn = socket.create_connection(
 
112
                (self.host, self.port), self.timeout, *extra_args)
 
113
 
 
114
        except SocketTimeout:
 
115
            raise ConnectTimeoutError(
 
116
                self, "Connection to %s timed out. (connect timeout=%s)" %
 
117
                (self.host, self.timeout))
 
118
 
 
119
        # Set options on the socket.
 
120
        self._set_options_on(conn)
93
121
 
94
122
        return conn
95
123
 
96
124
    def _prepare_conn(self, conn):
97
125
        self.sock = conn
98
 
        if self._tunnel_host:
 
126
        # the _tunnel_host attribute was added in python 2.6.3 (via
 
127
        # http://hg.python.org/cpython/rev/0f57b30a152f) so pythons 2.6(0-2) do
 
128
        # not have them.
 
129
        if getattr(self, '_tunnel_host', None):
99
130
            # TODO: Fix tunnel so it doesn't depend on self.sock state.
100
131
            self._tunnel()
 
132
            # Mark this connection as not reusable
 
133
            self.auto_open = 0
 
134
 
 
135
    def _set_options_on(self, conn):
 
136
        # Disable all socket options if the user passes ``socket_options=None``
 
137
        if self.socket_options is None:
 
138
            return
 
139
 
 
140
        for opt in self.socket_options:
 
141
            conn.setsockopt(*opt)
101
142
 
102
143
    def connect(self):
103
144
        conn = self._new_conn()
134
175
    cert_reqs = None
135
176
    ca_certs = None
136
177
    ssl_version = None
137
 
    conn_kw = {}
138
178
 
139
179
    def set_cert(self, key_file=None, cert_file=None,
140
180
                 cert_reqs=None, ca_certs=None,
149
189
 
150
190
    def connect(self):
151
191
        # Add certificate verification
152
 
 
153
 
        try:
154
 
            sock = socket.create_connection(
155
 
                address=(self.host, self.port), timeout=self.timeout,
156
 
                **self.conn_kw)
157
 
        except SocketTimeout:
158
 
            raise ConnectTimeoutError(
159
 
                self, "Connection to %s timed out. (connect timeout=%s)" %
160
 
                (self.host, self.timeout))
161
 
 
162
 
        sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY,
163
 
                        self.tcp_nodelay)
 
192
        conn = self._new_conn()
164
193
 
165
194
        resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs)
166
195
        resolved_ssl_version = resolve_ssl_version(self.ssl_version)
167
196
 
168
 
        # the _tunnel_host attribute was added in python 2.6.3 (via
169
 
        # http://hg.python.org/cpython/rev/0f57b30a152f) so pythons 2.6(0-2) do
170
 
        # not have them.
 
197
        hostname = self.host
171
198
        if getattr(self, '_tunnel_host', None):
172
 
            self.sock = sock
 
199
            # _tunnel_host was added in Python 2.6.3
 
200
            # (See: http://hg.python.org/cpython/rev/0f57b30a152f)
 
201
 
 
202
            self.sock = conn
173
203
            # Calls self._set_hostport(), so self.host is
174
204
            # self._tunnel_host below.
175
205
            self._tunnel()
 
206
            # Mark this connection as not reusable
 
207
            self.auto_open = 0
 
208
 
 
209
            # Override the host with the one we're requesting data from.
 
210
            hostname = self._tunnel_host
176
211
 
177
212
        # Wrap socket using verification with the root certs in
178
213
        # trusted_root_certs
179
 
        self.sock = ssl_wrap_socket(sock, self.key_file, self.cert_file,
 
214
        self.sock = ssl_wrap_socket(conn, self.key_file, self.cert_file,
180
215
                                    cert_reqs=resolved_cert_reqs,
181
216
                                    ca_certs=self.ca_certs,
182
 
                                    server_hostname=self.host,
 
217
                                    server_hostname=hostname,
183
218
                                    ssl_version=resolved_ssl_version)
184
219
 
185
220
        if resolved_cert_reqs != ssl.CERT_NONE:
188
223
                                   self.assert_fingerprint)
189
224
            elif self.assert_hostname is not False:
190
225
                match_hostname(self.sock.getpeercert(),
191
 
                               self.assert_hostname or self.host)
 
226
                               self.assert_hostname or hostname)
192
227
 
193
228
 
194
229
if ssl: