23e448f42e91b29f27433fb4ee1e87de3ee88f27
[sdc/sdc-distribution-client.git] /
1 # -*- coding: utf-8 -*-
2
3 """
4 requests.adapters
5 ~~~~~~~~~~~~~~~~~
6
7 This module contains the transport adapters that Requests uses to define
8 and maintain connections.
9 """
10
11 import os.path
12 import socket
13
14 from .models import Response
15 from .packages.urllib3.poolmanager import PoolManager, proxy_from_url
16 from .packages.urllib3.response import HTTPResponse
17 from .packages.urllib3.util import Timeout as TimeoutSauce
18 from .packages.urllib3.util.retry import Retry
19 from .compat import urlparse, basestring
20 from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,
21                     prepend_scheme_if_needed, get_auth_from_url, urldefragauth,
22                     select_proxy, to_native_string)
23 from .structures import CaseInsensitiveDict
24 from .packages.urllib3.exceptions import ClosedPoolError
25 from .packages.urllib3.exceptions import ConnectTimeoutError
26 from .packages.urllib3.exceptions import HTTPError as _HTTPError
27 from .packages.urllib3.exceptions import MaxRetryError
28 from .packages.urllib3.exceptions import NewConnectionError
29 from .packages.urllib3.exceptions import ProxyError as _ProxyError
30 from .packages.urllib3.exceptions import ProtocolError
31 from .packages.urllib3.exceptions import ReadTimeoutError
32 from .packages.urllib3.exceptions import SSLError as _SSLError
33 from .packages.urllib3.exceptions import ResponseError
34 from .cookies import extract_cookies_to_jar
35 from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,
36                          ProxyError, RetryError, InvalidSchema)
37 from .auth import _basic_auth_str
38
39 try:
40     from .packages.urllib3.contrib.socks import SOCKSProxyManager
41 except ImportError:
42     def SOCKSProxyManager(*args, **kwargs):
43         raise InvalidSchema("Missing dependencies for SOCKS support.")
44
45 DEFAULT_POOLBLOCK = False
46 DEFAULT_POOLSIZE = 10
47 DEFAULT_RETRIES = 0
48 DEFAULT_POOL_TIMEOUT = None
49
50
51 class BaseAdapter(object):
52     """The Base Transport Adapter"""
53
54     def __init__(self):
55         super(BaseAdapter, self).__init__()
56
57     def send(self):
58         raise NotImplementedError
59
60     def close(self):
61         raise NotImplementedError
62
63
64 class HTTPAdapter(BaseAdapter):
65     """The built-in HTTP Adapter for urllib3.
66
67     Provides a general-case interface for Requests sessions to contact HTTP and
68     HTTPS urls by implementing the Transport Adapter interface. This class will
69     usually be created by the :class:`Session <Session>` class under the
70     covers.
71
72     :param pool_connections: The number of urllib3 connection pools to cache.
73     :param pool_maxsize: The maximum number of connections to save in the pool.
74     :param max_retries: The maximum number of retries each connection
75         should attempt. Note, this applies only to failed DNS lookups, socket
76         connections and connection timeouts, never to requests where data has
77         made it to the server. By default, Requests does not retry failed
78         connections. If you need granular control over the conditions under
79         which we retry a request, import urllib3's ``Retry`` class and pass
80         that instead.
81     :param pool_block: Whether the connection pool should block for connections.
82
83     Usage::
84
85       >>> import requests
86       >>> s = requests.Session()
87       >>> a = requests.adapters.HTTPAdapter(max_retries=3)
88       >>> s.mount('http://', a)
89     """
90     __attrs__ = ['max_retries', 'config', '_pool_connections', '_pool_maxsize',
91                  '_pool_block']
92
93     def __init__(self, pool_connections=DEFAULT_POOLSIZE,
94                  pool_maxsize=DEFAULT_POOLSIZE, max_retries=DEFAULT_RETRIES,
95                  pool_block=DEFAULT_POOLBLOCK):
96         if max_retries == DEFAULT_RETRIES:
97             self.max_retries = Retry(0, read=False)
98         else:
99             self.max_retries = Retry.from_int(max_retries)
100         self.config = {}
101         self.proxy_manager = {}
102
103         super(HTTPAdapter, self).__init__()
104
105         self._pool_connections = pool_connections
106         self._pool_maxsize = pool_maxsize
107         self._pool_block = pool_block
108
109         self.init_poolmanager(pool_connections, pool_maxsize, block=pool_block)
110
111     def __getstate__(self):
112         return dict((attr, getattr(self, attr, None)) for attr in
113                     self.__attrs__)
114
115     def __setstate__(self, state):
116         # Can't handle by adding 'proxy_manager' to self.__attrs__ because
117         # self.poolmanager uses a lambda function, which isn't pickleable.
118         self.proxy_manager = {}
119         self.config = {}
120
121         for attr, value in state.items():
122             setattr(self, attr, value)
123
124         self.init_poolmanager(self._pool_connections, self._pool_maxsize,
125                               block=self._pool_block)
126
127     def init_poolmanager(self, connections, maxsize, block=DEFAULT_POOLBLOCK, **pool_kwargs):
128         """Initializes a urllib3 PoolManager.
129
130         This method should not be called from user code, and is only
131         exposed for use when subclassing the
132         :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
133
134         :param connections: The number of urllib3 connection pools to cache.
135         :param maxsize: The maximum number of connections to save in the pool.
136         :param block: Block when no free connections are available.
137         :param pool_kwargs: Extra keyword arguments used to initialize the Pool Manager.
138         """
139         # save these values for pickling
140         self._pool_connections = connections
141         self._pool_maxsize = maxsize
142         self._pool_block = block
143
144         self.poolmanager = PoolManager(num_pools=connections, maxsize=maxsize,
145                                        block=block, strict=True, **pool_kwargs)
146
147     def proxy_manager_for(self, proxy, **proxy_kwargs):
148         """Return urllib3 ProxyManager for the given proxy.
149
150         This method should not be called from user code, and is only
151         exposed for use when subclassing the
152         :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
153
154         :param proxy: The proxy to return a urllib3 ProxyManager for.
155         :param proxy_kwargs: Extra keyword arguments used to configure the Proxy Manager.
156         :returns: ProxyManager
157         """
158         if proxy in self.proxy_manager:
159             manager = self.proxy_manager[proxy]
160         elif proxy.lower().startswith('socks'):
161             username, password = get_auth_from_url(proxy)
162             manager = self.proxy_manager[proxy] = SOCKSProxyManager(
163                 proxy,
164                 username=username,
165                 password=password,
166                 num_pools=self._pool_connections,
167                 maxsize=self._pool_maxsize,
168                 block=self._pool_block,
169                 **proxy_kwargs
170             )
171         else:
172             proxy_headers = self.proxy_headers(proxy)
173             manager = self.proxy_manager[proxy] = proxy_from_url(
174                 proxy,
175                 proxy_headers=proxy_headers,
176                 num_pools=self._pool_connections,
177                 maxsize=self._pool_maxsize,
178                 block=self._pool_block,
179                 **proxy_kwargs)
180
181         return manager
182
183     def cert_verify(self, conn, url, verify, cert):
184         """Verify a SSL certificate. This method should not be called from user
185         code, and is only exposed for use when subclassing the
186         :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
187
188         :param conn: The urllib3 connection object associated with the cert.
189         :param url: The requested URL.
190         :param verify: Whether we should actually verify the certificate.
191         :param cert: The SSL certificate to verify.
192         """
193         if url.lower().startswith('https') and verify:
194
195             cert_loc = None
196
197             # Allow self-specified cert location.
198             if verify is not True:
199                 cert_loc = verify
200
201             if not cert_loc:
202                 cert_loc = DEFAULT_CA_BUNDLE_PATH
203
204             if not cert_loc:
205                 raise Exception("Could not find a suitable SSL CA certificate bundle.")
206
207             conn.cert_reqs = 'CERT_REQUIRED'
208
209             if not os.path.isdir(cert_loc):
210                 conn.ca_certs = cert_loc
211             else:
212                 conn.ca_cert_dir = cert_loc
213         else:
214             conn.cert_reqs = 'CERT_NONE'
215             conn.ca_certs = None
216             conn.ca_cert_dir = None
217
218         if cert:
219             if not isinstance(cert, basestring):
220                 conn.cert_file = cert[0]
221                 conn.key_file = cert[1]
222             else:
223                 conn.cert_file = cert
224
225     def build_response(self, req, resp):
226         """Builds a :class:`Response <requests.Response>` object from a urllib3
227         response. This should not be called from user code, and is only exposed
228         for use when subclassing the
229         :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`
230
231         :param req: The :class:`PreparedRequest <PreparedRequest>` used to generate the response.
232         :param resp: The urllib3 response object.
233         """
234         response = Response()
235
236         # Fallback to None if there's no status_code, for whatever reason.
237         response.status_code = getattr(resp, 'status', None)
238
239         # Make headers case-insensitive.
240         response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))
241
242         # Set encoding.
243         response.encoding = get_encoding_from_headers(response.headers)
244         response.raw = resp
245         response.reason = response.raw.reason
246
247         if isinstance(req.url, bytes):
248             response.url = req.url.decode('utf-8')
249         else:
250             response.url = req.url
251
252         # Add new cookies from the server.
253         extract_cookies_to_jar(response.cookies, req, resp)
254
255         # Give the Response some context.
256         response.request = req
257         response.connection = self
258
259         return response
260
261     def get_connection(self, url, proxies=None):
262         """Returns a urllib3 connection for the given URL. This should not be
263         called from user code, and is only exposed for use when subclassing the
264         :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
265
266         :param url: The URL to connect to.
267         :param proxies: (optional) A Requests-style dictionary of proxies used on this request.
268         """
269         proxy = select_proxy(url, proxies)
270
271         if proxy:
272             proxy = prepend_scheme_if_needed(proxy, 'http')
273             proxy_manager = self.proxy_manager_for(proxy)
274             conn = proxy_manager.connection_from_url(url)
275         else:
276             # Only scheme should be lower case
277             parsed = urlparse(url)
278             url = parsed.geturl()
279             conn = self.poolmanager.connection_from_url(url)
280
281         return conn
282
283     def close(self):
284         """Disposes of any internal state.
285
286         Currently, this closes the PoolManager and any active ProxyManager,
287         which closes any pooled connections.
288         """
289         self.poolmanager.clear()
290         for proxy in self.proxy_manager.values():
291             proxy.clear()
292
293     def request_url(self, request, proxies):
294         """Obtain the url to use when making the final request.
295
296         If the message is being sent through a HTTP proxy, the full URL has to
297         be used. Otherwise, we should only use the path portion of the URL.
298
299         This should not be called from user code, and is only exposed for use
300         when subclassing the
301         :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
302
303         :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
304         :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs.
305         """
306         proxy = select_proxy(request.url, proxies)
307         scheme = urlparse(request.url).scheme
308
309         is_proxied_http_request = (proxy and scheme != 'https')
310         using_socks_proxy = False
311         if proxy:
312             proxy_scheme = urlparse(proxy).scheme.lower()
313             using_socks_proxy = proxy_scheme.startswith('socks')
314
315         url = request.path_url
316         if is_proxied_http_request and not using_socks_proxy:
317             url = urldefragauth(request.url)
318
319         return url
320
321     def add_headers(self, request, **kwargs):
322         """Add any headers needed by the connection. As of v2.0 this does
323         nothing by default, but is left for overriding by users that subclass
324         the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
325
326         This should not be called from user code, and is only exposed for use
327         when subclassing the
328         :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
329
330         :param request: The :class:`PreparedRequest <PreparedRequest>` to add headers to.
331         :param kwargs: The keyword arguments from the call to send().
332         """
333         pass
334
335     def proxy_headers(self, proxy):
336         """Returns a dictionary of the headers to add to any request sent
337         through a proxy. This works with urllib3 magic to ensure that they are
338         correctly sent to the proxy, rather than in a tunnelled request if
339         CONNECT is being used.
340
341         This should not be called from user code, and is only exposed for use
342         when subclassing the
343         :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
344
345         :param proxies: The url of the proxy being used for this request.
346         """
347         headers = {}
348         username, password = get_auth_from_url(proxy)
349
350         if username and password:
351             headers['Proxy-Authorization'] = _basic_auth_str(username,
352                                                              password)
353
354         return headers
355
356     def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
357         """Sends PreparedRequest object. Returns Response object.
358
359         :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
360         :param stream: (optional) Whether to stream the request content.
361         :param timeout: (optional) How long to wait for the server to send
362             data before giving up, as a float, or a :ref:`(connect timeout,
363             read timeout) <timeouts>` tuple.
364         :type timeout: float or tuple
365         :param verify: (optional) Whether to verify SSL certificates.
366         :param cert: (optional) Any user-provided SSL certificate to be trusted.
367         :param proxies: (optional) The proxies dictionary to apply to the request.
368         """
369
370         conn = self.get_connection(request.url, proxies)
371
372         self.cert_verify(conn, request.url, verify, cert)
373         url = self.request_url(request, proxies)
374         self.add_headers(request)
375
376         chunked = not (request.body is None or 'Content-Length' in request.headers)
377
378         if isinstance(timeout, tuple):
379             try:
380                 connect, read = timeout
381                 timeout = TimeoutSauce(connect=connect, read=read)
382             except ValueError as e:
383                 # this may raise a string formatting error.
384                 err = ("Invalid timeout {0}. Pass a (connect, read) "
385                        "timeout tuple, or a single float to set "
386                        "both timeouts to the same value".format(timeout))
387                 raise ValueError(err)
388         else:
389             timeout = TimeoutSauce(connect=timeout, read=timeout)
390
391         try:
392             if not chunked:
393                 resp = conn.urlopen(
394                     method=request.method,
395                     url=url,
396                     body=request.body,
397                     headers=request.headers,
398                     redirect=False,
399                     assert_same_host=False,
400                     preload_content=False,
401                     decode_content=False,
402                     retries=self.max_retries,
403                     timeout=timeout
404                 )
405
406             # Send the request.
407             else:
408                 if hasattr(conn, 'proxy_pool'):
409                     conn = conn.proxy_pool
410
411                 low_conn = conn._get_conn(timeout=DEFAULT_POOL_TIMEOUT)
412
413                 try:
414                     low_conn.putrequest(request.method,
415                                         url,
416                                         skip_accept_encoding=True)
417
418                     for header, value in request.headers.items():
419                         low_conn.putheader(header, value)
420
421                     low_conn.endheaders()
422
423                     for i in request.body:
424                         low_conn.send(hex(len(i))[2:].encode('utf-8'))
425                         low_conn.send(b'\r\n')
426                         low_conn.send(i)
427                         low_conn.send(b'\r\n')
428                     low_conn.send(b'0\r\n\r\n')
429
430                     # Receive the response from the server
431                     try:
432                         # For Python 2.7+ versions, use buffering of HTTP
433                         # responses
434                         r = low_conn.getresponse(buffering=True)
435                     except TypeError:
436                         # For compatibility with Python 2.6 versions and back
437                         r = low_conn.getresponse()
438
439                     resp = HTTPResponse.from_httplib(
440                         r,
441                         pool=conn,
442                         connection=low_conn,
443                         preload_content=False,
444                         decode_content=False
445                     )
446                 except:
447                     # If we hit any problems here, clean up the connection.
448                     # Then, reraise so that we can handle the actual exception.
449                     low_conn.close()
450                     raise
451
452         except (ProtocolError, socket.error) as err:
453             raise ConnectionError(err, request=request)
454
455         except MaxRetryError as e:
456             if isinstance(e.reason, ConnectTimeoutError):
457                 # TODO: Remove this in 3.0.0: see #2811
458                 if not isinstance(e.reason, NewConnectionError):
459                     raise ConnectTimeout(e, request=request)
460
461             if isinstance(e.reason, ResponseError):
462                 raise RetryError(e, request=request)
463
464             if isinstance(e.reason, _ProxyError):
465                 raise ProxyError(e, request=request)
466
467             raise ConnectionError(e, request=request)
468
469         except ClosedPoolError as e:
470             raise ConnectionError(e, request=request)
471
472         except _ProxyError as e:
473             raise ProxyError(e)
474
475         except (_SSLError, _HTTPError) as e:
476             if isinstance(e, _SSLError):
477                 raise SSLError(e, request=request)
478             elif isinstance(e, ReadTimeoutError):
479                 raise ReadTimeout(e, request=request)
480             else:
481                 raise
482
483         return self.build_response(request, resp)