adapters.py 18 KB

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