Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(468)

Side by Side Diff: third_party/requests/packages/urllib3/connectionpool.py

Issue 25004007: Update 'requests' lib to v2.0. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/swarm_client
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 # urllib3/connectionpool.py 1 # urllib3/connectionpool.py
2 # Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt) 2 # Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
3 # 3 #
4 # This module is part of urllib3 and is released under 4 # This module is part of urllib3 and is released under
5 # the MIT License: http://www.opensource.org/licenses/mit-license.php 5 # the MIT License: http://www.opensource.org/licenses/mit-license.php
6 6
7 import errno
7 import logging 8 import logging
8 import socket
9 import errno
10 9
11 from socket import error as SocketError, timeout as SocketTimeout 10 from socket import error as SocketError, timeout as SocketTimeout
12 from .util import resolve_cert_reqs, resolve_ssl_version, assert_fingerprint 11 import socket
13 12
14 try: # Python 3 13 try: # Python 3
15 from http.client import HTTPConnection, HTTPException 14 from http.client import HTTPConnection, HTTPException
16 from http.client import HTTP_PORT, HTTPS_PORT 15 from http.client import HTTP_PORT, HTTPS_PORT
17 except ImportError: 16 except ImportError:
18 from httplib import HTTPConnection, HTTPException 17 from httplib import HTTPConnection, HTTPException
19 from httplib import HTTP_PORT, HTTPS_PORT 18 from httplib import HTTP_PORT, HTTPS_PORT
20 19
21 try: # Python 3 20 try: # Python 3
22 from queue import LifoQueue, Empty, Full 21 from queue import LifoQueue, Empty, Full
23 except ImportError: 22 except ImportError:
24 from Queue import LifoQueue, Empty, Full 23 from Queue import LifoQueue, Empty, Full
24 import Queue as _ # Platform-specific: Windows
25 25
26 26
27 try: # Compiled with SSL? 27 try: # Compiled with SSL?
28 HTTPSConnection = object 28 HTTPSConnection = object
29 BaseSSLError = None 29
30 class BaseSSLError(BaseException):
31 pass
32
30 ssl = None 33 ssl = None
31 34
32 try: # Python 3 35 try: # Python 3
33 from http.client import HTTPSConnection 36 from http.client import HTTPSConnection
34 except ImportError: 37 except ImportError:
35 from httplib import HTTPSConnection 38 from httplib import HTTPSConnection
36 39
37 import ssl 40 import ssl
38 BaseSSLError = ssl.SSLError 41 BaseSSLError = ssl.SSLError
39 42
40 except (ImportError, AttributeError): # Platform-specific: No SSL. 43 except (ImportError, AttributeError): # Platform-specific: No SSL.
41 pass 44 pass
42 45
43 46
44 from .request import RequestMethods
45 from .response import HTTPResponse
46 from .util import get_host, is_connection_dropped, ssl_wrap_socket
47 from .exceptions import ( 47 from .exceptions import (
48 ClosedPoolError, 48 ClosedPoolError,
49 ConnectTimeoutError,
49 EmptyPoolError, 50 EmptyPoolError,
50 HostChangedError, 51 HostChangedError,
51 MaxRetryError, 52 MaxRetryError,
52 SSLError, 53 SSLError,
53 TimeoutError, 54 ReadTimeoutError,
55 ProxyError,
54 ) 56 )
55 57 from .packages.ssl_match_hostname import CertificateError, match_hostname
56 from .packages.ssl_match_hostname import match_hostname, CertificateError
57 from .packages import six 58 from .packages import six
58 59 from .request import RequestMethods
60 from .response import HTTPResponse
61 from .util import (
62 assert_fingerprint,
63 get_host,
64 is_connection_dropped,
65 resolve_cert_reqs,
66 resolve_ssl_version,
67 ssl_wrap_socket,
68 Timeout,
69 )
59 70
60 xrange = six.moves.xrange 71 xrange = six.moves.xrange
61 72
62 log = logging.getLogger(__name__) 73 log = logging.getLogger(__name__)
63 74
64 _Default = object() 75 _Default = object()
65 76
66 port_by_scheme = { 77 port_by_scheme = {
67 'http': HTTP_PORT, 78 'http': HTTP_PORT,
68 'https': HTTPS_PORT, 79 'https': HTTPS_PORT,
(...skipping 17 matching lines...) Expand all
86 97
87 self.key_file = key_file 98 self.key_file = key_file
88 self.cert_file = cert_file 99 self.cert_file = cert_file
89 self.cert_reqs = cert_reqs 100 self.cert_reqs = cert_reqs
90 self.ca_certs = ca_certs 101 self.ca_certs = ca_certs
91 self.assert_hostname = assert_hostname 102 self.assert_hostname = assert_hostname
92 self.assert_fingerprint = assert_fingerprint 103 self.assert_fingerprint = assert_fingerprint
93 104
94 def connect(self): 105 def connect(self):
95 # Add certificate verification 106 # Add certificate verification
96 sock = socket.create_connection((self.host, self.port), self.timeout) 107 try:
108 sock = socket.create_connection(
109 address=(self.host, self.port),
110 timeout=self.timeout)
111 except SocketTimeout:
112 raise ConnectTimeoutError(
113 self, "Connection to %s timed out. (connect timeout=%s)" %
114 (self.host, self.timeout))
97 115
98 resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs) 116 resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs)
99 resolved_ssl_version = resolve_ssl_version(self.ssl_version) 117 resolved_ssl_version = resolve_ssl_version(self.ssl_version)
100 118
119 if self._tunnel_host:
120 self.sock = sock
121 # Calls self._set_hostport(), so self.host is
122 # self._tunnel_host below.
123 self._tunnel()
124
101 # Wrap socket using verification with the root certs in 125 # Wrap socket using verification with the root certs in
102 # trusted_root_certs 126 # trusted_root_certs
103 self.sock = ssl_wrap_socket(sock, self.key_file, self.cert_file, 127 self.sock = ssl_wrap_socket(sock, self.key_file, self.cert_file,
104 cert_reqs=resolved_cert_reqs, 128 cert_reqs=resolved_cert_reqs,
105 ca_certs=self.ca_certs, 129 ca_certs=self.ca_certs,
106 server_hostname=self.host, 130 server_hostname=self.host,
107 ssl_version=resolved_ssl_version) 131 ssl_version=resolved_ssl_version)
108 132
109 if resolved_cert_reqs != ssl.CERT_NONE: 133 if resolved_cert_reqs != ssl.CERT_NONE:
110 if self.assert_fingerprint: 134 if self.assert_fingerprint:
111 assert_fingerprint(self.sock.getpeercert(binary_form=True), 135 assert_fingerprint(self.sock.getpeercert(binary_form=True),
112 self.assert_fingerprint) 136 self.assert_fingerprint)
113 else: 137 elif self.assert_hostname is not False:
114 match_hostname(self.sock.getpeercert(), 138 match_hostname(self.sock.getpeercert(),
115 self.assert_hostname or self.host) 139 self.assert_hostname or self.host)
116 140
141
117 ## Pool objects 142 ## Pool objects
118 143
119 class ConnectionPool(object): 144 class ConnectionPool(object):
120 """ 145 """
121 Base class for all connection pools, such as 146 Base class for all connection pools, such as
122 :class:`.HTTPConnectionPool` and :class:`.HTTPSConnectionPool`. 147 :class:`.HTTPConnectionPool` and :class:`.HTTPSConnectionPool`.
123 """ 148 """
124 149
125 scheme = None 150 scheme = None
126 QueueCls = LifoQueue 151 QueueCls = LifoQueue
127 152
128 def __init__(self, host, port=None): 153 def __init__(self, host, port=None):
154 # httplib doesn't like it when we include brackets in ipv6 addresses
155 host = host.strip('[]')
156
129 self.host = host 157 self.host = host
130 self.port = port 158 self.port = port
131 159
132 def __str__(self): 160 def __str__(self):
133 return '%s(host=%r, port=%r)' % (type(self).__name__, 161 return '%s(host=%r, port=%r)' % (type(self).__name__,
134 self.host, self.port) 162 self.host, self.port)
135 163
164 # This is taken from http://hg.python.org/cpython/file/7aaba721ebc0/Lib/socket.p y#l252
165 _blocking_errnos = set([errno.EAGAIN, errno.EWOULDBLOCK])
136 166
137 class HTTPConnectionPool(ConnectionPool, RequestMethods): 167 class HTTPConnectionPool(ConnectionPool, RequestMethods):
138 """ 168 """
139 Thread-safe connection pool for one host. 169 Thread-safe connection pool for one host.
140 170
141 :param host: 171 :param host:
142 Host used for this HTTP Connection (e.g. "localhost"), passed into 172 Host used for this HTTP Connection (e.g. "localhost"), passed into
143 :class:`httplib.HTTPConnection`. 173 :class:`httplib.HTTPConnection`.
144 174
145 :param port: 175 :param port:
146 Port used for this HTTP Connection (None is equivalent to 80), passed 176 Port used for this HTTP Connection (None is equivalent to 80), passed
147 into :class:`httplib.HTTPConnection`. 177 into :class:`httplib.HTTPConnection`.
148 178
149 :param strict: 179 :param strict:
150 Causes BadStatusLine to be raised if the status line can't be parsed 180 Causes BadStatusLine to be raised if the status line can't be parsed
151 as a valid HTTP/1.0 or 1.1 status line, passed into 181 as a valid HTTP/1.0 or 1.1 status line, passed into
152 :class:`httplib.HTTPConnection`. 182 :class:`httplib.HTTPConnection`.
153 183
184 .. note::
185 Only works in Python 2. This parameter is ignored in Python 3.
186
154 :param timeout: 187 :param timeout:
155 Socket timeout for each individual connection, can be a float. None 188 Socket timeout in seconds for each individual connection. This can
156 disables timeout. 189 be a float or integer, which sets the timeout for the HTTP request,
190 or an instance of :class:`urllib3.util.Timeout` which gives you more
191 fine-grained control over request timeouts. After the constructor has
192 been parsed, this is always a `urllib3.util.Timeout` object.
157 193
158 :param maxsize: 194 :param maxsize:
159 Number of connections to save that can be reused. More than 1 is useful 195 Number of connections to save that can be reused. More than 1 is useful
160 in multithreaded situations. If ``block`` is set to false, more 196 in multithreaded situations. If ``block`` is set to false, more
161 connections will be created but they will not be saved once they've 197 connections will be created but they will not be saved once they've
162 been used. 198 been used.
163 199
164 :param block: 200 :param block:
165 If set to True, no more than ``maxsize`` connections will be used at 201 If set to True, no more than ``maxsize`` connections will be used at
166 a time. When no free connections are available, the call will block 202 a time. When no free connections are available, the call will block
167 until a connection has been released. This is a useful side effect for 203 until a connection has been released. This is a useful side effect for
168 particular multithreaded situations where one does not want to use more 204 particular multithreaded situations where one does not want to use more
169 than maxsize connections per host to prevent flooding. 205 than maxsize connections per host to prevent flooding.
170 206
171 :param headers: 207 :param headers:
172 Headers to include with all requests, unless other headers are given 208 Headers to include with all requests, unless other headers are given
173 explicitly. 209 explicitly.
210
211 :param _proxy:
212 Parsed proxy URL, should not be used directly, instead, see
213 :class:`urllib3.connectionpool.ProxyManager`"
214
215 :param _proxy_headers:
216 A dictionary with proxy headers, should not be used directly,
217 instead, see :class:`urllib3.connectionpool.ProxyManager`"
174 """ 218 """
175 219
176 scheme = 'http' 220 scheme = 'http'
177 221
178 def __init__(self, host, port=None, strict=False, timeout=None, maxsize=1, 222 def __init__(self, host, port=None, strict=False,
179 block=False, headers=None): 223 timeout=Timeout.DEFAULT_TIMEOUT, maxsize=1, block=False,
224 headers=None, _proxy=None, _proxy_headers=None):
180 ConnectionPool.__init__(self, host, port) 225 ConnectionPool.__init__(self, host, port)
181 RequestMethods.__init__(self, headers) 226 RequestMethods.__init__(self, headers)
182 227
183 self.strict = strict 228 self.strict = strict
229
230 # This is for backwards compatibility and can be removed once a timeout
231 # can only be set to a Timeout object
232 if not isinstance(timeout, Timeout):
233 timeout = Timeout.from_float(timeout)
234
184 self.timeout = timeout 235 self.timeout = timeout
236
185 self.pool = self.QueueCls(maxsize) 237 self.pool = self.QueueCls(maxsize)
186 self.block = block 238 self.block = block
187 239
240 self.proxy = _proxy
241 self.proxy_headers = _proxy_headers or {}
242
188 # Fill the queue up so that doing get() on it will block properly 243 # Fill the queue up so that doing get() on it will block properly
189 for _ in xrange(maxsize): 244 for _ in xrange(maxsize):
190 self.pool.put(None) 245 self.pool.put(None)
191 246
192 # These are mostly for testing and debugging purposes. 247 # These are mostly for testing and debugging purposes.
193 self.num_connections = 0 248 self.num_connections = 0
194 self.num_requests = 0 249 self.num_requests = 0
195 250
196 def _new_conn(self): 251 def _new_conn(self):
197 """ 252 """
198 Return a fresh :class:`httplib.HTTPConnection`. 253 Return a fresh :class:`httplib.HTTPConnection`.
199 """ 254 """
200 self.num_connections += 1 255 self.num_connections += 1
201 log.info("Starting new HTTP connection (%d): %s" % 256 log.info("Starting new HTTP connection (%d): %s" %
202 (self.num_connections, self.host)) 257 (self.num_connections, self.host))
203 return HTTPConnection(host=self.host, 258 extra_params = {}
204 port=self.port, 259 if not six.PY3: # Python 2
205 strict=self.strict) 260 extra_params['strict'] = self.strict
261
262 return HTTPConnection(host=self.host, port=self.port,
263 timeout=self.timeout.connect_timeout,
264 **extra_params)
265
206 266
207 def _get_conn(self, timeout=None): 267 def _get_conn(self, timeout=None):
208 """ 268 """
209 Get a connection. Will return a pooled connection if one is available. 269 Get a connection. Will return a pooled connection if one is available.
210 270
211 If no connections are available and :prop:`.block` is ``False``, then a 271 If no connections are available and :prop:`.block` is ``False``, then a
212 fresh connection is returned. 272 fresh connection is returned.
213 273
214 :param timeout: 274 :param timeout:
215 Seconds to wait before giving up and raising 275 Seconds to wait before giving up and raising
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 return # Everything is dandy, done. 316 return # Everything is dandy, done.
257 except AttributeError: 317 except AttributeError:
258 # self.pool is None. 318 # self.pool is None.
259 pass 319 pass
260 except Full: 320 except Full:
261 # This should never happen if self.block == True 321 # This should never happen if self.block == True
262 log.warning("HttpConnectionPool is full, discarding connection: %s" 322 log.warning("HttpConnectionPool is full, discarding connection: %s"
263 % self.host) 323 % self.host)
264 324
265 # Connection never got put back into the pool, close it. 325 # Connection never got put back into the pool, close it.
266 conn.close() 326 if conn:
327 conn.close()
328
329 def _get_timeout(self, timeout):
330 """ Helper that always returns a :class:`urllib3.util.Timeout` """
331 if timeout is _Default:
332 return self.timeout.clone()
333
334 if isinstance(timeout, Timeout):
335 return timeout.clone()
336 else:
337 # User passed us an int/float. This is for backwards compatibility,
338 # can be removed later
339 return Timeout.from_float(timeout)
267 340
268 def _make_request(self, conn, method, url, timeout=_Default, 341 def _make_request(self, conn, method, url, timeout=_Default,
269 **httplib_request_kw): 342 **httplib_request_kw):
270 """ 343 """
271 Perform a request on a given httplib connection object taken from our 344 Perform a request on a given httplib connection object taken from our
272 pool. 345 pool.
346
347 :param conn:
348 a connection from one of our connection pools
349
350 :param timeout:
351 Socket timeout in seconds for the request. This can be a
352 float or integer, which will set the same timeout value for
353 the socket connect and the socket read, or an instance of
354 :class:`urllib3.util.Timeout`, which gives you more fine-grained
355 control over your timeouts.
273 """ 356 """
274 self.num_requests += 1 357 self.num_requests += 1
275 358
276 if timeout is _Default: 359 timeout_obj = self._get_timeout(timeout)
277 timeout = self.timeout
278 360
279 conn.timeout = timeout # This only does anything in Py26+ 361 try:
280 conn.request(method, url, **httplib_request_kw) 362 timeout_obj.start_connect()
363 conn.timeout = timeout_obj.connect_timeout
364 # conn.request() calls httplib.*.request, not the method in
365 # request.py. It also calls makefile (recv) on the socket
366 conn.request(method, url, **httplib_request_kw)
367 except SocketTimeout:
368 raise ConnectTimeoutError(
369 self, "Connection to %s timed out. (connect timeout=%s)" %
370 (self.host, timeout_obj.connect_timeout))
281 371
282 # Set timeout 372 # Reset the timeout for the recv() on the socket
283 sock = getattr(conn, 'sock', False) # AppEngine doesn't have sock attr. 373 read_timeout = timeout_obj.read_timeout
284 if sock: 374 log.debug("Setting read timeout to %s" % read_timeout)
285 sock.settimeout(timeout) 375 # App Engine doesn't have a sock attr
376 if hasattr(conn, 'sock') and \
377 read_timeout is not None and \
378 read_timeout is not Timeout.DEFAULT_TIMEOUT:
379 # In Python 3 socket.py will catch EAGAIN and return None when you
380 # try and read into the file pointer created by http.client, which
381 # instead raises a BadStatusLine exception. Instead of catching
382 # the exception and assuming all BadStatusLine exceptions are read
383 # timeouts, check for a zero timeout before making the request.
384 if read_timeout == 0:
385 raise ReadTimeoutError(
386 self, url,
387 "Read timed out. (read timeout=%s)" % read_timeout)
388 conn.sock.settimeout(read_timeout)
286 389
287 try: # Python 2.7+, use buffering of HTTP responses 390 # Receive the response from the server
288 httplib_response = conn.getresponse(buffering=True) 391 try:
289 except TypeError: # Python 2.6 and older 392 try: # Python 2.7+, use buffering of HTTP responses
290 httplib_response = conn.getresponse() 393 httplib_response = conn.getresponse(buffering=True)
394 except TypeError: # Python 2.6 and older
395 httplib_response = conn.getresponse()
396 except SocketTimeout:
397 raise ReadTimeoutError(
398 self, url, "Read timed out. (read timeout=%s)" % read_timeout)
399
400 except SocketError as e: # Platform-specific: Python 2
401 # See the above comment about EAGAIN in Python 3. In Python 2 we
402 # have to specifically catch it and throw the timeout error
403 if e.errno in _blocking_errnos:
404 raise ReadTimeoutError(
405 self, url,
406 "Read timed out. (read timeout=%s)" % read_timeout)
407 raise
408
291 409
292 # AppEngine doesn't have a version attr. 410 # AppEngine doesn't have a version attr.
293 http_version = getattr(conn, '_http_vsn_str', 'HTTP/?') 411 http_version = getattr(conn, '_http_vsn_str', 'HTTP/?')
294 log.debug("\"%s %s %s\" %s %s" % (method, url, http_version, 412 log.debug("\"%s %s %s\" %s %s" % (method, url, http_version,
295 httplib_response.status, 413 httplib_response.status,
296 httplib_response.length)) 414 httplib_response.length))
297 return httplib_response 415 return httplib_response
298 416
299 def close(self): 417 def close(self):
300 """ 418 """
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 :param headers: 478 :param headers:
361 Dictionary of custom headers to send, such as User-Agent, 479 Dictionary of custom headers to send, such as User-Agent,
362 If-None-Match, etc. If None, pool headers are used. If provided, 480 If-None-Match, etc. If None, pool headers are used. If provided,
363 these headers completely replace any pool-specific headers. 481 these headers completely replace any pool-specific headers.
364 482
365 :param retries: 483 :param retries:
366 Number of retries to allow before raising a MaxRetryError exception. 484 Number of retries to allow before raising a MaxRetryError exception.
367 485
368 :param redirect: 486 :param redirect:
369 If True, automatically handle redirects (status codes 301, 302, 487 If True, automatically handle redirects (status codes 301, 302,
370 303, 307). Each redirect counts as a retry. 488 303, 307, 308). Each redirect counts as a retry.
371 489
372 :param assert_same_host: 490 :param assert_same_host:
373 If ``True``, will make sure that the host of the pool requests is 491 If ``True``, will make sure that the host of the pool requests is
374 consistent else will raise HostChangedError. When False, you can 492 consistent else will raise HostChangedError. When False, you can
375 use the pool on an HTTP proxy and request foreign hosts. 493 use the pool on an HTTP proxy and request foreign hosts.
376 494
377 :param timeout: 495 :param timeout:
378 If specified, overrides the default timeout for this one request. 496 If specified, overrides the default timeout for this one
497 request. It may be a float (in seconds) or an instance of
498 :class:`urllib3.util.Timeout`.
379 499
380 :param pool_timeout: 500 :param pool_timeout:
381 If set and the pool is set to block=True, then this method will 501 If set and the pool is set to block=True, then this method will
382 block for ``pool_timeout`` seconds and raise EmptyPoolError if no 502 block for ``pool_timeout`` seconds and raise EmptyPoolError if no
383 connection is available within the time period. 503 connection is available within the time period.
384 504
385 :param release_conn: 505 :param release_conn:
386 If False, then the urlopen call will not release the connection 506 If False, then the urlopen call will not release the connection
387 back into the pool once a response is received (but will release if 507 back into the pool once a response is received (but will release if
388 you read the entire contents of the response such as when 508 you read the entire contents of the response such as when
389 `preload_content=True`). This is useful if you're not preloading 509 `preload_content=True`). This is useful if you're not preloading
390 the response's content immediately. You will need to call 510 the response's content immediately. You will need to call
391 ``r.release_conn()`` on the response ``r`` to return the connection 511 ``r.release_conn()`` on the response ``r`` to return the connection
392 back into the pool. If None, it takes the value of 512 back into the pool. If None, it takes the value of
393 ``response_kw.get('preload_content', True)``. 513 ``response_kw.get('preload_content', True)``.
394 514
395 :param \**response_kw: 515 :param \**response_kw:
396 Additional parameters are passed to 516 Additional parameters are passed to
397 :meth:`urllib3.response.HTTPResponse.from_httplib` 517 :meth:`urllib3.response.HTTPResponse.from_httplib`
398 """ 518 """
399 if headers is None: 519 if headers is None:
400 headers = self.headers 520 headers = self.headers
401 521
402 if retries < 0: 522 if retries < 0:
403 raise MaxRetryError(self, url) 523 raise MaxRetryError(self, url)
404 524
405 if timeout is _Default:
406 timeout = self.timeout
407
408 if release_conn is None: 525 if release_conn is None:
409 release_conn = response_kw.get('preload_content', True) 526 release_conn = response_kw.get('preload_content', True)
410 527
411 # Check host 528 # Check host
412 if assert_same_host and not self.is_same_host(url): 529 if assert_same_host and not self.is_same_host(url):
413 host = "%s://%s" % (self.scheme, self.host)
414 if self.port:
415 host = "%s:%d" % (host, self.port)
416
417 raise HostChangedError(self, url, retries - 1) 530 raise HostChangedError(self, url, retries - 1)
418 531
419 conn = None 532 conn = None
420 533
421 try: 534 try:
422 # Request a connection from the queue 535 # Request a connection from the queue
423 conn = self._get_conn(timeout=pool_timeout) 536 conn = self._get_conn(timeout=pool_timeout)
424 537
425 # Make the request on the httplib connection object 538 # Make the request on the httplib connection object
426 httplib_response = self._make_request(conn, method, url, 539 httplib_response = self._make_request(conn, method, url,
(...skipping 10 matching lines...) Expand all
437 response = HTTPResponse.from_httplib(httplib_response, 550 response = HTTPResponse.from_httplib(httplib_response,
438 pool=self, 551 pool=self,
439 connection=response_conn, 552 connection=response_conn,
440 **response_kw) 553 **response_kw)
441 554
442 # else: 555 # else:
443 # The connection will be put back into the pool when 556 # The connection will be put back into the pool when
444 # ``response.release_conn()`` is called (implicitly by 557 # ``response.release_conn()`` is called (implicitly by
445 # ``response.read()``) 558 # ``response.read()``)
446 559
447 except Empty as e: 560 except Empty:
448 # Timed out by queue 561 # Timed out by queue
449 raise TimeoutError(self, url, 562 raise ReadTimeoutError(
450 "Request timed out. (pool_timeout=%s)" % 563 self, url, "Read timed out, no pool connections are available.")
451 pool_timeout)
452 564
453 except SocketTimeout as e: 565 except SocketTimeout:
454 # Timed out by socket 566 # Timed out by socket
455 raise TimeoutError(self, url, 567 raise ReadTimeoutError(self, url, "Read timed out.")
456 "Request timed out. (timeout=%s)" %
457 timeout)
458 568
459 except BaseSSLError as e: 569 except BaseSSLError as e:
460 # SSL certificate error 570 # SSL certificate error
571 if 'timed out' in str(e) or \
572 'did not complete (read)' in str(e): # Platform-specific: Python 2.6
573 raise ReadTimeoutError(self, url, "Read timed out.")
461 raise SSLError(e) 574 raise SSLError(e)
462 575
463 except CertificateError as e: 576 except CertificateError as e:
464 # Name mismatch 577 # Name mismatch
465 raise SSLError(e) 578 raise SSLError(e)
466 579
467 except (HTTPException, SocketError) as e: 580 except (HTTPException, SocketError) as e:
581 if isinstance(e, SocketError) and self.proxy is not None:
582 raise ProxyError('Cannot connect to proxy. '
583 'Socket error: %s.' % e)
584
468 # Connection broken, discard. It will be replaced next _get_conn(). 585 # Connection broken, discard. It will be replaced next _get_conn().
469 conn = None 586 conn = None
470 # This is necessary so we can access e below 587 # This is necessary so we can access e below
471 err = e 588 err = e
472 589
473 if retries == 0: 590 if retries == 0:
474 raise MaxRetryError(self, url, e) 591 raise MaxRetryError(self, url, e)
475 592
476 finally: 593 finally:
477 if release_conn: 594 if release_conn:
(...skipping 28 matching lines...) Expand all
506 class HTTPSConnectionPool(HTTPConnectionPool): 623 class HTTPSConnectionPool(HTTPConnectionPool):
507 """ 624 """
508 Same as :class:`.HTTPConnectionPool`, but HTTPS. 625 Same as :class:`.HTTPConnectionPool`, but HTTPS.
509 626
510 When Python is compiled with the :mod:`ssl` module, then 627 When Python is compiled with the :mod:`ssl` module, then
511 :class:`.VerifiedHTTPSConnection` is used, which *can* verify certificates, 628 :class:`.VerifiedHTTPSConnection` is used, which *can* verify certificates,
512 instead of :class:`httplib.HTTPSConnection`. 629 instead of :class:`httplib.HTTPSConnection`.
513 630
514 :class:`.VerifiedHTTPSConnection` uses one of ``assert_fingerprint``, 631 :class:`.VerifiedHTTPSConnection` uses one of ``assert_fingerprint``,
515 ``assert_hostname`` and ``host`` in this order to verify connections. 632 ``assert_hostname`` and ``host`` in this order to verify connections.
633 If ``assert_hostname`` is False, no verification is done.
516 634
517 The ``key_file``, ``cert_file``, ``cert_reqs``, ``ca_certs`` and 635 The ``key_file``, ``cert_file``, ``cert_reqs``, ``ca_certs`` and
518 ``ssl_version`` are only used if :mod:`ssl` is available and are fed into 636 ``ssl_version`` are only used if :mod:`ssl` is available and are fed into
519 :meth:`urllib3.util.ssl_wrap_socket` to upgrade the connection socket 637 :meth:`urllib3.util.ssl_wrap_socket` to upgrade the connection socket
520 into an SSL socket. 638 into an SSL socket.
521 """ 639 """
522 640
523 scheme = 'https' 641 scheme = 'https'
524 642
525 def __init__(self, host, port=None, 643 def __init__(self, host, port=None,
526 strict=False, timeout=None, maxsize=1, 644 strict=False, timeout=None, maxsize=1,
527 block=False, headers=None, 645 block=False, headers=None,
646 _proxy=None, _proxy_headers=None,
528 key_file=None, cert_file=None, cert_reqs=None, 647 key_file=None, cert_file=None, cert_reqs=None,
529 ca_certs=None, ssl_version=None, 648 ca_certs=None, ssl_version=None,
530 assert_hostname=None, assert_fingerprint=None): 649 assert_hostname=None, assert_fingerprint=None):
531 650
532 HTTPConnectionPool.__init__(self, host, port, 651 HTTPConnectionPool.__init__(self, host, port, strict, timeout, maxsize,
533 strict, timeout, maxsize, 652 block, headers, _proxy, _proxy_headers)
534 block, headers)
535 self.key_file = key_file 653 self.key_file = key_file
536 self.cert_file = cert_file 654 self.cert_file = cert_file
537 self.cert_reqs = cert_reqs 655 self.cert_reqs = cert_reqs
538 self.ca_certs = ca_certs 656 self.ca_certs = ca_certs
539 self.ssl_version = ssl_version 657 self.ssl_version = ssl_version
540 self.assert_hostname = assert_hostname 658 self.assert_hostname = assert_hostname
541 self.assert_fingerprint = assert_fingerprint 659 self.assert_fingerprint = assert_fingerprint
542 660
661 def _prepare_conn(self, connection):
662 """
663 Prepare the ``connection`` for :meth:`urllib3.util.ssl_wrap_socket`
664 and establish the tunnel if proxy is used.
665 """
666
667 if isinstance(connection, VerifiedHTTPSConnection):
668 connection.set_cert(key_file=self.key_file,
669 cert_file=self.cert_file,
670 cert_reqs=self.cert_reqs,
671 ca_certs=self.ca_certs,
672 assert_hostname=self.assert_hostname,
673 assert_fingerprint=self.assert_fingerprint)
674 connection.ssl_version = self.ssl_version
675
676 if self.proxy is not None:
677 # Python 2.7+
678 try:
679 set_tunnel = connection.set_tunnel
680 except AttributeError: # Platform-specific: Python 2.6
681 set_tunnel = connection._set_tunnel
682 set_tunnel(self.host, self.port, self.proxy_headers)
683 # Establish tunnel connection early, because otherwise httplib
684 # would improperly set Host: header to proxy's IP:port.
685 connection.connect()
686
687 return connection
688
543 def _new_conn(self): 689 def _new_conn(self):
544 """ 690 """
545 Return a fresh :class:`httplib.HTTPSConnection`. 691 Return a fresh :class:`httplib.HTTPSConnection`.
546 """ 692 """
547 self.num_connections += 1 693 self.num_connections += 1
548 log.info("Starting new HTTPS connection (%d): %s" 694 log.info("Starting new HTTPS connection (%d): %s"
549 % (self.num_connections, self.host)) 695 % (self.num_connections, self.host))
550 696
697 actual_host = self.host
698 actual_port = self.port
699 if self.proxy is not None:
700 actual_host = self.proxy.host
701 actual_port = self.proxy.port
702
551 if not ssl: # Platform-specific: Python compiled without +ssl 703 if not ssl: # Platform-specific: Python compiled without +ssl
552 if not HTTPSConnection or HTTPSConnection is object: 704 if not HTTPSConnection or HTTPSConnection is object:
553 raise SSLError("Can't connect to HTTPS URL because the SSL " 705 raise SSLError("Can't connect to HTTPS URL because the SSL "
554 "module is not available.") 706 "module is not available.")
707 connection_class = HTTPSConnection
708 else:
709 connection_class = VerifiedHTTPSConnection
555 710
556 return HTTPSConnection(host=self.host, 711 extra_params = {}
557 port=self.port, 712 if not six.PY3: # Python 2
558 strict=self.strict) 713 extra_params['strict'] = self.strict
714 connection = connection_class(host=actual_host, port=actual_port,
715 timeout=self.timeout.connect_timeout,
716 **extra_params)
559 717
560 connection = VerifiedHTTPSConnection(host=self.host, 718 return self._prepare_conn(connection)
561 port=self.port,
562 strict=self.strict)
563 connection.set_cert(key_file=self.key_file, cert_file=self.cert_file,
564 cert_reqs=self.cert_reqs, ca_certs=self.ca_certs,
565 assert_hostname=self.assert_hostname,
566 assert_fingerprint=self.assert_fingerprint)
567
568 connection.ssl_version = self.ssl_version
569
570 return connection
571 719
572 720
573 def connection_from_url(url, **kw): 721 def connection_from_url(url, **kw):
574 """ 722 """
575 Given a url, return an :class:`.ConnectionPool` instance of its host. 723 Given a url, return an :class:`.ConnectionPool` instance of its host.
576 724
577 This is a shortcut for not having to parse out the scheme, host, and port 725 This is a shortcut for not having to parse out the scheme, host, and port
578 of the url before creating an :class:`.ConnectionPool` instance. 726 of the url before creating an :class:`.ConnectionPool` instance.
579 727
580 :param url: 728 :param url:
581 Absolute URL string that must include the scheme. Port is optional. 729 Absolute URL string that must include the scheme. Port is optional.
582 730
583 :param \**kw: 731 :param \**kw:
584 Passes additional parameters to the constructor of the appropriate 732 Passes additional parameters to the constructor of the appropriate
585 :class:`.ConnectionPool`. Useful for specifying things like 733 :class:`.ConnectionPool`. Useful for specifying things like
586 timeout, maxsize, headers, etc. 734 timeout, maxsize, headers, etc.
587 735
588 Example: :: 736 Example: ::
589 737
590 >>> conn = connection_from_url('http://google.com/') 738 >>> conn = connection_from_url('http://google.com/')
591 >>> r = conn.request('GET', '/') 739 >>> r = conn.request('GET', '/')
592 """ 740 """
593 scheme, host, port = get_host(url) 741 scheme, host, port = get_host(url)
594 if scheme == 'https': 742 if scheme == 'https':
595 return HTTPSConnectionPool(host, port=port, **kw) 743 return HTTPSConnectionPool(host, port=port, **kw)
596 else: 744 else:
597 return HTTPConnectionPool(host, port=port, **kw) 745 return HTTPConnectionPool(host, port=port, **kw)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698