sessions.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. # -*- coding: utf-8 -*-
  2. """
  3. requests.session
  4. ~~~~~~~~~~~~~~~~
  5. This module provides a Session object to manage and persist settings across
  6. requests (cookies, auth, proxies).
  7. """
  8. import os
  9. # from collections import Mapping
  10. try:
  11. from collections import Mapping, MutableMapping
  12. except ImportError:
  13. from collections.abc import Mapping, MutableMapping
  14. from datetime import datetime
  15. from .auth import _basic_auth_str
  16. from .compat import cookielib, OrderedDict, urljoin, urlparse
  17. from .cookies import (
  18. cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)
  19. from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT
  20. from .hooks import default_hooks, dispatch_hook
  21. from .utils import to_key_val_list, default_headers, to_native_string
  22. from .exceptions import (
  23. TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError)
  24. from .packages.urllib3._collections import RecentlyUsedContainer
  25. from .structures import CaseInsensitiveDict
  26. from .adapters import HTTPAdapter
  27. from .utils import (
  28. requote_uri, get_environ_proxies, get_netrc_auth, should_bypass_proxies,
  29. get_auth_from_url
  30. )
  31. from .status_codes import codes
  32. # formerly defined here, reexposed here for backward compatibility
  33. from .models import REDIRECT_STATI
  34. REDIRECT_CACHE_SIZE = 1000
  35. def merge_setting(request_setting, session_setting, dict_class=OrderedDict):
  36. """
  37. Determines appropriate setting for a given request, taking into account the
  38. explicit setting on that request, and the setting in the session. If a
  39. setting is a dictionary, they will be merged together using `dict_class`
  40. """
  41. if session_setting is None:
  42. return request_setting
  43. if request_setting is None:
  44. return session_setting
  45. # Bypass if not a dictionary (e.g. verify)
  46. if not (
  47. isinstance(session_setting, Mapping) and
  48. isinstance(request_setting, Mapping)
  49. ):
  50. return request_setting
  51. merged_setting = dict_class(to_key_val_list(session_setting))
  52. merged_setting.update(to_key_val_list(request_setting))
  53. # Remove keys that are set to None.
  54. for (k, v) in request_setting.items():
  55. if v is None:
  56. del merged_setting[k]
  57. merged_setting = dict((k, v) for (k, v) in merged_setting.items() if v is not None)
  58. return merged_setting
  59. def merge_hooks(request_hooks, session_hooks, dict_class=OrderedDict):
  60. """
  61. Properly merges both requests and session hooks.
  62. This is necessary because when request_hooks == {'response': []}, the
  63. merge breaks Session hooks entirely.
  64. """
  65. if session_hooks is None or session_hooks.get('response') == []:
  66. return request_hooks
  67. if request_hooks is None or request_hooks.get('response') == []:
  68. return session_hooks
  69. return merge_setting(request_hooks, session_hooks, dict_class)
  70. class SessionRedirectMixin(object):
  71. def resolve_redirects(self, resp, req, stream=False, timeout=None,
  72. verify=True, cert=None, proxies=None, **adapter_kwargs):
  73. """Receives a Response. Returns a generator of Responses."""
  74. i = 0
  75. hist = [] # keep track of history
  76. while resp.is_redirect:
  77. prepared_request = req.copy()
  78. if i > 0:
  79. # Update history and keep track of redirects.
  80. hist.append(resp)
  81. new_hist = list(hist)
  82. resp.history = new_hist
  83. try:
  84. resp.content # Consume socket so it can be released
  85. except (ChunkedEncodingError, ContentDecodingError, RuntimeError):
  86. resp.raw.read(decode_content=False)
  87. if i >= self.max_redirects:
  88. raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects)
  89. # Release the connection back into the pool.
  90. resp.close()
  91. url = resp.headers['location']
  92. method = req.method
  93. # Handle redirection without scheme (see: RFC 1808 Section 4)
  94. if url.startswith('//'):
  95. parsed_rurl = urlparse(resp.url)
  96. url = '%s:%s' % (parsed_rurl.scheme, url)
  97. # The scheme should be lower case...
  98. parsed = urlparse(url)
  99. url = parsed.geturl()
  100. # Facilitate relative 'location' headers, as allowed by RFC 7231.
  101. # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
  102. # Compliant with RFC3986, we percent encode the url.
  103. if not parsed.netloc:
  104. url = urljoin(resp.url, requote_uri(url))
  105. else:
  106. url = requote_uri(url)
  107. prepared_request.url = to_native_string(url)
  108. # Cache the url, unless it redirects to itself.
  109. if resp.is_permanent_redirect and req.url != prepared_request.url:
  110. self.redirect_cache[req.url] = prepared_request.url
  111. # http://tools.ietf.org/html/rfc7231#section-6.4.4
  112. if (resp.status_code == codes.see_other and
  113. method != 'HEAD'):
  114. method = 'GET'
  115. # Do what the browsers do, despite standards...
  116. # First, turn 302s into GETs.
  117. if resp.status_code == codes.found and method != 'HEAD':
  118. method = 'GET'
  119. # Second, if a POST is responded to with a 301, turn it into a GET.
  120. # This bizarre behaviour is explained in Issue 1704.
  121. if resp.status_code == codes.moved and method == 'POST':
  122. method = 'GET'
  123. prepared_request.method = method
  124. # https://github.com/kennethreitz/requests/issues/1084
  125. if resp.status_code not in (codes.temporary_redirect, codes.permanent_redirect):
  126. if 'Content-Length' in prepared_request.headers:
  127. del prepared_request.headers['Content-Length']
  128. prepared_request.body = None
  129. headers = prepared_request.headers
  130. try:
  131. del headers['Cookie']
  132. except KeyError:
  133. pass
  134. # Extract any cookies sent on the response to the cookiejar
  135. # in the new request. Because we've mutated our copied prepared
  136. # request, use the old one that we haven't yet touched.
  137. extract_cookies_to_jar(prepared_request._cookies, req, resp.raw)
  138. prepared_request._cookies.update(self.cookies)
  139. prepared_request.prepare_cookies(prepared_request._cookies)
  140. # Rebuild auth and proxy information.
  141. proxies = self.rebuild_proxies(prepared_request, proxies)
  142. self.rebuild_auth(prepared_request, resp)
  143. # Override the original request.
  144. req = prepared_request
  145. resp = self.send(
  146. req,
  147. stream=stream,
  148. timeout=timeout,
  149. verify=verify,
  150. cert=cert,
  151. proxies=proxies,
  152. allow_redirects=False,
  153. **adapter_kwargs
  154. )
  155. extract_cookies_to_jar(self.cookies, prepared_request, resp.raw)
  156. i += 1
  157. yield resp
  158. def rebuild_auth(self, prepared_request, response):
  159. """
  160. When being redirected we may want to strip authentication from the
  161. request to avoid leaking credentials. This method intelligently removes
  162. and reapplies authentication where possible to avoid credential loss.
  163. """
  164. headers = prepared_request.headers
  165. url = prepared_request.url
  166. if 'Authorization' in headers:
  167. # If we get redirected to a new host, we should strip out any
  168. # authentication headers.
  169. original_parsed = urlparse(response.request.url)
  170. redirect_parsed = urlparse(url)
  171. if (original_parsed.hostname != redirect_parsed.hostname):
  172. del headers['Authorization']
  173. # .netrc might have more auth for us on our new host.
  174. new_auth = get_netrc_auth(url) if self.trust_env else None
  175. if new_auth is not None:
  176. prepared_request.prepare_auth(new_auth)
  177. return
  178. def rebuild_proxies(self, prepared_request, proxies):
  179. """
  180. This method re-evaluates the proxy configuration by considering the
  181. environment variables. If we are redirected to a URL covered by
  182. NO_PROXY, we strip the proxy configuration. Otherwise, we set missing
  183. proxy keys for this URL (in case they were stripped by a previous
  184. redirect).
  185. This method also replaces the Proxy-Authorization header where
  186. necessary.
  187. """
  188. headers = prepared_request.headers
  189. url = prepared_request.url
  190. scheme = urlparse(url).scheme
  191. new_proxies = proxies.copy() if proxies is not None else {}
  192. if self.trust_env and not should_bypass_proxies(url):
  193. environ_proxies = get_environ_proxies(url)
  194. proxy = environ_proxies.get(scheme)
  195. if proxy:
  196. new_proxies.setdefault(scheme, environ_proxies[scheme])
  197. if 'Proxy-Authorization' in headers:
  198. del headers['Proxy-Authorization']
  199. try:
  200. username, password = get_auth_from_url(new_proxies[scheme])
  201. except KeyError:
  202. username, password = None, None
  203. if username and password:
  204. headers['Proxy-Authorization'] = _basic_auth_str(username, password)
  205. return new_proxies
  206. class Session(SessionRedirectMixin):
  207. """A Requests session.
  208. Provides cookie persistence, connection-pooling, and configuration.
  209. Basic Usage::
  210. >>> import requests
  211. >>> s = requests.Session()
  212. >>> s.get('http://httpbin.org/get')
  213. 200
  214. """
  215. __attrs__ = [
  216. 'headers', 'cookies', 'auth', 'proxies', 'hooks', 'params', 'verify',
  217. 'cert', 'prefetch', 'adapters', 'stream', 'trust_env',
  218. 'max_redirects',
  219. ]
  220. def __init__(self):
  221. #: A case-insensitive dictionary of headers to be sent on each
  222. #: :class:`Request <Request>` sent from this
  223. #: :class:`Session <Session>`.
  224. self.headers = default_headers()
  225. #: Default Authentication tuple or object to attach to
  226. #: :class:`Request <Request>`.
  227. self.auth = None
  228. #: Dictionary mapping protocol to the URL of the proxy (e.g.
  229. #: {'http': 'foo.bar:3128'}) to be used on each
  230. #: :class:`Request <Request>`.
  231. self.proxies = {}
  232. #: Event-handling hooks.
  233. self.hooks = default_hooks()
  234. #: Dictionary of querystring data to attach to each
  235. #: :class:`Request <Request>`. The dictionary values may be lists for
  236. #: representing multivalued query parameters.
  237. self.params = {}
  238. #: Stream response content default.
  239. self.stream = False
  240. #: SSL Verification default.
  241. self.verify = True
  242. #: SSL certificate default.
  243. self.cert = None
  244. #: Maximum number of redirects allowed. If the request exceeds this
  245. #: limit, a :class:`TooManyRedirects` exception is raised.
  246. self.max_redirects = DEFAULT_REDIRECT_LIMIT
  247. #: Should we trust the environment?
  248. self.trust_env = True
  249. #: A CookieJar containing all currently outstanding cookies set on this
  250. #: session. By default it is a
  251. #: :class:`RequestsCookieJar <requests.cookies.RequestsCookieJar>`, but
  252. #: may be any other ``cookielib.CookieJar`` compatible object.
  253. self.cookies = cookiejar_from_dict({})
  254. # Default connection adapters.
  255. self.adapters = OrderedDict()
  256. self.mount('https://', HTTPAdapter())
  257. self.mount('http://', HTTPAdapter())
  258. # Only store 1000 redirects to prevent using infinite memory
  259. self.redirect_cache = RecentlyUsedContainer(REDIRECT_CACHE_SIZE)
  260. def __enter__(self):
  261. return self
  262. def __exit__(self, *args):
  263. self.close()
  264. def prepare_request(self, request):
  265. """Constructs a :class:`PreparedRequest <PreparedRequest>` for
  266. transmission and returns it. The :class:`PreparedRequest` has settings
  267. merged from the :class:`Request <Request>` instance and those of the
  268. :class:`Session`.
  269. :param request: :class:`Request` instance to prepare with this
  270. session's settings.
  271. """
  272. cookies = request.cookies or {}
  273. # Bootstrap CookieJar.
  274. if not isinstance(cookies, cookielib.CookieJar):
  275. cookies = cookiejar_from_dict(cookies)
  276. # Merge with session cookies
  277. merged_cookies = merge_cookies(
  278. merge_cookies(RequestsCookieJar(), self.cookies), cookies)
  279. # Set environment's basic authentication if not explicitly set.
  280. auth = request.auth
  281. if self.trust_env and not auth and not self.auth:
  282. auth = get_netrc_auth(request.url)
  283. p = PreparedRequest()
  284. p.prepare(
  285. method=request.method.upper(),
  286. url=request.url,
  287. files=request.files,
  288. data=request.data,
  289. json=request.json,
  290. headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict),
  291. params=merge_setting(request.params, self.params),
  292. auth=merge_setting(auth, self.auth),
  293. cookies=merged_cookies,
  294. hooks=merge_hooks(request.hooks, self.hooks),
  295. )
  296. return p
  297. def request(self, method, url,
  298. params=None,
  299. data=None,
  300. headers=None,
  301. cookies=None,
  302. files=None,
  303. auth=None,
  304. timeout=None,
  305. allow_redirects=True,
  306. proxies=None,
  307. hooks=None,
  308. stream=None,
  309. verify=None,
  310. cert=None,
  311. json=None):
  312. """Constructs a :class:`Request <Request>`, prepares it and sends it.
  313. Returns :class:`Response <Response>` object.
  314. :param method: method for the new :class:`Request` object.
  315. :param url: URL for the new :class:`Request` object.
  316. :param params: (optional) Dictionary or bytes to be sent in the query
  317. string for the :class:`Request`.
  318. :param data: (optional) Dictionary or bytes to send in the body of the
  319. :class:`Request`.
  320. :param json: (optional) json to send in the body of the
  321. :class:`Request`.
  322. :param headers: (optional) Dictionary of HTTP Headers to send with the
  323. :class:`Request`.
  324. :param cookies: (optional) Dict or CookieJar object to send with the
  325. :class:`Request`.
  326. :param files: (optional) Dictionary of ``'filename': file-like-objects``
  327. for multipart encoding upload.
  328. :param auth: (optional) Auth tuple or callable to enable
  329. Basic/Digest/Custom HTTP Auth.
  330. :param timeout: (optional) How long to wait for the server to send
  331. data before giving up, as a float, or a (`connect timeout, read
  332. timeout <user/advanced.html#timeouts>`_) tuple.
  333. :type timeout: float or tuple
  334. :param allow_redirects: (optional) Set to True by default.
  335. :type allow_redirects: bool
  336. :param proxies: (optional) Dictionary mapping protocol to the URL of
  337. the proxy.
  338. :param stream: (optional) whether to immediately download the response
  339. content. Defaults to ``False``.
  340. :param verify: (optional) if ``True``, the SSL cert will be verified.
  341. A CA_BUNDLE path can also be provided.
  342. :param cert: (optional) if String, path to ssl client cert file (.pem).
  343. If Tuple, ('cert', 'key') pair.
  344. """
  345. method = to_native_string(method)
  346. # Create the Request.
  347. req = Request(
  348. method = method.upper(),
  349. url = url,
  350. headers = headers,
  351. files = files,
  352. data = data or {},
  353. json = json,
  354. params = params or {},
  355. auth = auth,
  356. cookies = cookies,
  357. hooks = hooks,
  358. )
  359. prep = self.prepare_request(req)
  360. proxies = proxies or {}
  361. settings = self.merge_environment_settings(
  362. prep.url, proxies, stream, verify, cert
  363. )
  364. # Send the request.
  365. send_kwargs = {
  366. 'timeout': timeout,
  367. 'allow_redirects': allow_redirects,
  368. }
  369. send_kwargs.update(settings)
  370. resp = self.send(prep, **send_kwargs)
  371. return resp
  372. def get(self, url, **kwargs):
  373. """Sends a GET request. Returns :class:`Response` object.
  374. :param url: URL for the new :class:`Request` object.
  375. :param \*\*kwargs: Optional arguments that ``request`` takes.
  376. """
  377. kwargs.setdefault('allow_redirects', True)
  378. return self.request('GET', url, **kwargs)
  379. def options(self, url, **kwargs):
  380. """Sends a OPTIONS request. Returns :class:`Response` object.
  381. :param url: URL for the new :class:`Request` object.
  382. :param \*\*kwargs: Optional arguments that ``request`` takes.
  383. """
  384. kwargs.setdefault('allow_redirects', True)
  385. return self.request('OPTIONS', url, **kwargs)
  386. def head(self, url, **kwargs):
  387. """Sends a HEAD request. Returns :class:`Response` object.
  388. :param url: URL for the new :class:`Request` object.
  389. :param \*\*kwargs: Optional arguments that ``request`` takes.
  390. """
  391. kwargs.setdefault('allow_redirects', False)
  392. return self.request('HEAD', url, **kwargs)
  393. def post(self, url, data=None, json=None, **kwargs):
  394. """Sends a POST request. Returns :class:`Response` object.
  395. :param url: URL for the new :class:`Request` object.
  396. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
  397. :param json: (optional) json to send in the body of the :class:`Request`.
  398. :param \*\*kwargs: Optional arguments that ``request`` takes.
  399. """
  400. return self.request('POST', url, data=data, json=json, **kwargs)
  401. def put(self, url, data=None, **kwargs):
  402. """Sends a PUT request. Returns :class:`Response` object.
  403. :param url: URL for the new :class:`Request` object.
  404. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
  405. :param \*\*kwargs: Optional arguments that ``request`` takes.
  406. """
  407. return self.request('PUT', url, data=data, **kwargs)
  408. def patch(self, url, data=None, **kwargs):
  409. """Sends a PATCH request. Returns :class:`Response` object.
  410. :param url: URL for the new :class:`Request` object.
  411. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
  412. :param \*\*kwargs: Optional arguments that ``request`` takes.
  413. """
  414. return self.request('PATCH', url, data=data, **kwargs)
  415. def delete(self, url, **kwargs):
  416. """Sends a DELETE request. Returns :class:`Response` object.
  417. :param url: URL for the new :class:`Request` object.
  418. :param \*\*kwargs: Optional arguments that ``request`` takes.
  419. """
  420. return self.request('DELETE', url, **kwargs)
  421. def send(self, request, **kwargs):
  422. """Send a given PreparedRequest."""
  423. # Set defaults that the hooks can utilize to ensure they always have
  424. # the correct parameters to reproduce the previous request.
  425. kwargs.setdefault('stream', self.stream)
  426. kwargs.setdefault('verify', self.verify)
  427. kwargs.setdefault('cert', self.cert)
  428. kwargs.setdefault('proxies', self.proxies)
  429. # It's possible that users might accidentally send a Request object.
  430. # Guard against that specific failure case.
  431. if not isinstance(request, PreparedRequest):
  432. raise ValueError('You can only send PreparedRequests.')
  433. checked_urls = set()
  434. while request.url in self.redirect_cache:
  435. checked_urls.add(request.url)
  436. new_url = self.redirect_cache.get(request.url)
  437. if new_url in checked_urls:
  438. break
  439. request.url = new_url
  440. # Set up variables needed for resolve_redirects and dispatching of hooks
  441. allow_redirects = kwargs.pop('allow_redirects', True)
  442. stream = kwargs.get('stream')
  443. hooks = request.hooks
  444. # Get the appropriate adapter to use
  445. adapter = self.get_adapter(url=request.url)
  446. # Start time (approximately) of the request
  447. start = datetime.utcnow()
  448. # Send the request
  449. r = adapter.send(request, **kwargs)
  450. # Total elapsed time of the request (approximately)
  451. r.elapsed = datetime.utcnow() - start
  452. # Response manipulation hooks
  453. r = dispatch_hook('response', hooks, r, **kwargs)
  454. # Persist cookies
  455. if r.history:
  456. # If the hooks create history then we want those cookies too
  457. for resp in r.history:
  458. extract_cookies_to_jar(self.cookies, resp.request, resp.raw)
  459. extract_cookies_to_jar(self.cookies, request, r.raw)
  460. # Redirect resolving generator.
  461. gen = self.resolve_redirects(r, request, **kwargs)
  462. # Resolve redirects if allowed.
  463. history = [resp for resp in gen] if allow_redirects else []
  464. # Shuffle things around if there's history.
  465. if history:
  466. # Insert the first (original) request at the start
  467. history.insert(0, r)
  468. # Get the last request made
  469. r = history.pop()
  470. r.history = history
  471. if not stream:
  472. r.content
  473. return r
  474. def merge_environment_settings(self, url, proxies, stream, verify, cert):
  475. """Check the environment and merge it with some settings."""
  476. # Gather clues from the surrounding environment.
  477. if self.trust_env:
  478. # Set environment's proxies.
  479. env_proxies = get_environ_proxies(url) or {}
  480. for (k, v) in env_proxies.items():
  481. proxies.setdefault(k, v)
  482. # Look for requests environment configuration and be compatible
  483. # with cURL.
  484. if verify is True or verify is None:
  485. verify = (os.environ.get('REQUESTS_CA_BUNDLE') or
  486. os.environ.get('CURL_CA_BUNDLE'))
  487. # Merge all the kwargs.
  488. proxies = merge_setting(proxies, self.proxies)
  489. stream = merge_setting(stream, self.stream)
  490. verify = merge_setting(verify, self.verify)
  491. cert = merge_setting(cert, self.cert)
  492. return {'verify': verify, 'proxies': proxies, 'stream': stream,
  493. 'cert': cert}
  494. def get_adapter(self, url):
  495. """Returns the appropriate connnection adapter for the given URL."""
  496. for (prefix, adapter) in self.adapters.items():
  497. if url.lower().startswith(prefix):
  498. return adapter
  499. # Nothing matches :-/
  500. raise InvalidSchema("No connection adapters were found for '%s'" % url)
  501. def close(self):
  502. """Closes all adapters and as such the session"""
  503. for v in self.adapters.values():
  504. v.close()
  505. def mount(self, prefix, adapter):
  506. """Registers a connection adapter to a prefix.
  507. Adapters are sorted in descending order by key length."""
  508. self.adapters[prefix] = adapter
  509. keys_to_move = [k for k in self.adapters if len(k) < len(prefix)]
  510. for key in keys_to_move:
  511. self.adapters[key] = self.adapters.pop(key)
  512. def __getstate__(self):
  513. state = dict((attr, getattr(self, attr, None)) for attr in self.__attrs__)
  514. state['redirect_cache'] = dict(self.redirect_cache)
  515. return state
  516. def __setstate__(self, state):
  517. redirect_cache = state.pop('redirect_cache', {})
  518. for attr, value in state.items():
  519. setattr(self, attr, value)
  520. self.redirect_cache = RecentlyUsedContainer(REDIRECT_CACHE_SIZE)
  521. for redirect, to in redirect_cache.items():
  522. self.redirect_cache[redirect] = to
  523. def session():
  524. """Returns a :class:`Session` for context-management."""
  525. return Session()