adapters.py 21 KB

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