You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

953 lines
33 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. requests.models
  4. ~~~~~~~~~~~~~~~
  5. This module contains the primary objects that power Requests.
  6. """
  7. import datetime
  8. import sys
  9. # Import encoding now, to avoid implicit import later.
  10. # Implicit import within threads may cause LookupError when standard library is in a ZIP,
  11. # such as in Embedded Python. See https://github.com/requests/requests/issues/3578.
  12. import encodings.idna
  13. from urllib3.fields import RequestField
  14. from urllib3.filepost import encode_multipart_formdata
  15. from urllib3.util import parse_url
  16. from urllib3.exceptions import (
  17. DecodeError, ReadTimeoutError, ProtocolError, LocationParseError)
  18. from io import UnsupportedOperation
  19. from .hooks import default_hooks
  20. from .structures import CaseInsensitiveDict
  21. from .auth import HTTPBasicAuth
  22. from .cookies import cookiejar_from_dict, get_cookie_header, _copy_cookie_jar
  23. from .exceptions import (
  24. HTTPError, MissingSchema, InvalidURL, ChunkedEncodingError,
  25. ContentDecodingError, ConnectionError, StreamConsumedError)
  26. from ._internal_utils import to_native_string, unicode_is_ascii
  27. from .utils import (
  28. guess_filename, get_auth_from_url, requote_uri,
  29. stream_decode_response_unicode, to_key_val_list, parse_header_links,
  30. iter_slices, guess_json_utf, super_len, check_header_validity)
  31. from .compat import (
  32. Callable, Mapping,
  33. cookielib, urlunparse, urlsplit, urlencode, str, bytes,
  34. is_py2, chardet, builtin_str, basestring)
  35. from .compat import json as complexjson
  36. from .status_codes import codes
  37. #: The set of HTTP status codes that indicate an automatically
  38. #: processable redirect.
  39. REDIRECT_STATI = (
  40. codes.moved, # 301
  41. codes.found, # 302
  42. codes.other, # 303
  43. codes.temporary_redirect, # 307
  44. codes.permanent_redirect, # 308
  45. )
  46. DEFAULT_REDIRECT_LIMIT = 30
  47. CONTENT_CHUNK_SIZE = 10 * 1024
  48. ITER_CHUNK_SIZE = 512
  49. class RequestEncodingMixin(object):
  50. @property
  51. def path_url(self):
  52. """Build the path URL to use."""
  53. url = []
  54. p = urlsplit(self.url)
  55. path = p.path
  56. if not path:
  57. path = '/'
  58. url.append(path)
  59. query = p.query
  60. if query:
  61. url.append('?')
  62. url.append(query)
  63. return ''.join(url)
  64. @staticmethod
  65. def _encode_params(data):
  66. """Encode parameters in a piece of data.
  67. Will successfully encode parameters when passed as a dict or a list of
  68. 2-tuples. Order is retained if data is a list of 2-tuples but arbitrary
  69. if parameters are supplied as a dict.
  70. """
  71. if isinstance(data, (str, bytes)):
  72. return data
  73. elif hasattr(data, 'read'):
  74. return data
  75. elif hasattr(data, '__iter__'):
  76. result = []
  77. for k, vs in to_key_val_list(data):
  78. if isinstance(vs, basestring) or not hasattr(vs, '__iter__'):
  79. vs = [vs]
  80. for v in vs:
  81. if v is not None:
  82. result.append(
  83. (k.encode('utf-8') if isinstance(k, str) else k,
  84. v.encode('utf-8') if isinstance(v, str) else v))
  85. return urlencode(result, doseq=True)
  86. else:
  87. return data
  88. @staticmethod
  89. def _encode_files(files, data):
  90. """Build the body for a multipart/form-data request.
  91. Will successfully encode files when passed as a dict or a list of
  92. tuples. Order is retained if data is a list of tuples but arbitrary
  93. if parameters are supplied as a dict.
  94. The tuples may be 2-tuples (filename, fileobj), 3-tuples (filename, fileobj, contentype)
  95. or 4-tuples (filename, fileobj, contentype, custom_headers).
  96. """
  97. if (not files):
  98. raise ValueError("Files must be provided.")
  99. elif isinstance(data, basestring):
  100. raise ValueError("Data must not be a string.")
  101. new_fields = []
  102. fields = to_key_val_list(data or {})
  103. files = to_key_val_list(files or {})
  104. for field, val in fields:
  105. if isinstance(val, basestring) or not hasattr(val, '__iter__'):
  106. val = [val]
  107. for v in val:
  108. if v is not None:
  109. # Don't call str() on bytestrings: in Py3 it all goes wrong.
  110. if not isinstance(v, bytes):
  111. v = str(v)
  112. new_fields.append(
  113. (field.decode('utf-8') if isinstance(field, bytes) else field,
  114. v.encode('utf-8') if isinstance(v, str) else v))
  115. for (k, v) in files:
  116. # support for explicit filename
  117. ft = None
  118. fh = None
  119. if isinstance(v, (tuple, list)):
  120. if len(v) == 2:
  121. fn, fp = v
  122. elif len(v) == 3:
  123. fn, fp, ft = v
  124. else:
  125. fn, fp, ft, fh = v
  126. else:
  127. fn = guess_filename(v) or k
  128. fp = v
  129. if isinstance(fp, (str, bytes, bytearray)):
  130. fdata = fp
  131. elif hasattr(fp, 'read'):
  132. fdata = fp.read()
  133. elif fp is None:
  134. continue
  135. else:
  136. fdata = fp
  137. rf = RequestField(name=k, data=fdata, filename=fn, headers=fh)
  138. rf.make_multipart(content_type=ft)
  139. new_fields.append(rf)
  140. body, content_type = encode_multipart_formdata(new_fields)
  141. return body, content_type
  142. class RequestHooksMixin(object):
  143. def register_hook(self, event, hook):
  144. """Properly register a hook."""
  145. if event not in self.hooks:
  146. raise ValueError('Unsupported event specified, with event name "%s"' % (event))
  147. if isinstance(hook, Callable):
  148. self.hooks[event].append(hook)
  149. elif hasattr(hook, '__iter__'):
  150. self.hooks[event].extend(h for h in hook if isinstance(h, Callable))
  151. def deregister_hook(self, event, hook):
  152. """Deregister a previously registered hook.
  153. Returns True if the hook existed, False if not.
  154. """
  155. try:
  156. self.hooks[event].remove(hook)
  157. return True
  158. except ValueError:
  159. return False
  160. class Request(RequestHooksMixin):
  161. """A user-created :class:`Request <Request>` object.
  162. Used to prepare a :class:`PreparedRequest <PreparedRequest>`, which is sent to the server.
  163. :param method: HTTP method to use.
  164. :param url: URL to send.
  165. :param headers: dictionary of headers to send.
  166. :param files: dictionary of {filename: fileobject} files to multipart upload.
  167. :param data: the body to attach to the request. If a dictionary or
  168. list of tuples ``[(key, value)]`` is provided, form-encoding will
  169. take place.
  170. :param json: json for the body to attach to the request (if files or data is not specified).
  171. :param params: URL parameters to append to the URL. If a dictionary or
  172. list of tuples ``[(key, value)]`` is provided, form-encoding will
  173. take place.
  174. :param auth: Auth handler or (user, pass) tuple.
  175. :param cookies: dictionary or CookieJar of cookies to attach to this request.
  176. :param hooks: dictionary of callback hooks, for internal usage.
  177. Usage::
  178. >>> import requests
  179. >>> req = requests.Request('GET', 'https://httpbin.org/get')
  180. >>> req.prepare()
  181. <PreparedRequest [GET]>
  182. """
  183. def __init__(self,
  184. method=None, url=None, headers=None, files=None, data=None,
  185. params=None, auth=None, cookies=None, hooks=None, json=None):
  186. # Default empty dicts for dict params.
  187. data = [] if data is None else data
  188. files = [] if files is None else files
  189. headers = {} if headers is None else headers
  190. params = {} if params is None else params
  191. hooks = {} if hooks is None else hooks
  192. self.hooks = default_hooks()
  193. for (k, v) in list(hooks.items()):
  194. self.register_hook(event=k, hook=v)
  195. self.method = method
  196. self.url = url
  197. self.headers = headers
  198. self.files = files
  199. self.data = data
  200. self.json = json
  201. self.params = params
  202. self.auth = auth
  203. self.cookies = cookies
  204. def __repr__(self):
  205. return '<Request [%s]>' % (self.method)
  206. def prepare(self):
  207. """Constructs a :class:`PreparedRequest <PreparedRequest>` for transmission and returns it."""
  208. p = PreparedRequest()
  209. p.prepare(
  210. method=self.method,
  211. url=self.url,
  212. headers=self.headers,
  213. files=self.files,
  214. data=self.data,
  215. json=self.json,
  216. params=self.params,
  217. auth=self.auth,
  218. cookies=self.cookies,
  219. hooks=self.hooks,
  220. )
  221. return p
  222. class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
  223. """The fully mutable :class:`PreparedRequest <PreparedRequest>` object,
  224. containing the exact bytes that will be sent to the server.
  225. Generated from either a :class:`Request <Request>` object or manually.
  226. Usage::
  227. >>> import requests
  228. >>> req = requests.Request('GET', 'https://httpbin.org/get')
  229. >>> r = req.prepare()
  230. <PreparedRequest [GET]>
  231. >>> s = requests.Session()
  232. >>> s.send(r)
  233. <Response [200]>
  234. """
  235. def __init__(self):
  236. #: HTTP verb to send to the server.
  237. self.method = None
  238. #: HTTP URL to send the request to.
  239. self.url = None
  240. #: dictionary of HTTP headers.
  241. self.headers = None
  242. # The `CookieJar` used to create the Cookie header will be stored here
  243. # after prepare_cookies is called
  244. self._cookies = None
  245. #: request body to send to the server.
  246. self.body = None
  247. #: dictionary of callback hooks, for internal usage.
  248. self.hooks = default_hooks()
  249. #: integer denoting starting position of a readable file-like body.
  250. self._body_position = None
  251. def prepare(self,
  252. method=None, url=None, headers=None, files=None, data=None,
  253. params=None, auth=None, cookies=None, hooks=None, json=None):
  254. """Prepares the entire request with the given parameters."""
  255. self.prepare_method(method)
  256. self.prepare_url(url, params)
  257. self.prepare_headers(headers)
  258. self.prepare_cookies(cookies)
  259. self.prepare_body(data, files, json)
  260. self.prepare_auth(auth, url)
  261. # Note that prepare_auth must be last to enable authentication schemes
  262. # such as OAuth to work on a fully prepared request.
  263. # This MUST go after prepare_auth. Authenticators could add a hook
  264. self.prepare_hooks(hooks)
  265. def __repr__(self):
  266. return '<PreparedRequest [%s]>' % (self.method)
  267. def copy(self):
  268. p = PreparedRequest()
  269. p.method = self.method
  270. p.url = self.url
  271. p.headers = self.headers.copy() if self.headers is not None else None
  272. p._cookies = _copy_cookie_jar(self._cookies)
  273. p.body = self.body
  274. p.hooks = self.hooks
  275. p._body_position = self._body_position
  276. return p
  277. def prepare_method(self, method):
  278. """Prepares the given HTTP method."""
  279. self.method = method
  280. if self.method is not None:
  281. self.method = to_native_string(self.method.upper())
  282. @staticmethod
  283. def _get_idna_encoded_host(host):
  284. import idna
  285. try:
  286. host = idna.encode(host, uts46=True).decode('utf-8')
  287. except idna.IDNAError:
  288. raise UnicodeError
  289. return host
  290. def prepare_url(self, url, params):
  291. """Prepares the given HTTP URL."""
  292. #: Accept objects that have string representations.
  293. #: We're unable to blindly call unicode/str functions
  294. #: as this will include the bytestring indicator (b'')
  295. #: on python 3.x.
  296. #: https://github.com/requests/requests/pull/2238
  297. if isinstance(url, bytes):
  298. url = url.decode('utf8')
  299. else:
  300. url = unicode(url) if is_py2 else str(url)
  301. # Remove leading whitespaces from url
  302. url = url.lstrip()
  303. # Don't do any URL preparation for non-HTTP schemes like `mailto`,
  304. # `data` etc to work around exceptions from `url_parse`, which
  305. # handles RFC 3986 only.
  306. if ':' in url and not url.lower().startswith('http'):
  307. self.url = url
  308. return
  309. # Support for unicode domain names and paths.
  310. try:
  311. scheme, auth, host, port, path, query, fragment = parse_url(url)
  312. except LocationParseError as e:
  313. raise InvalidURL(*e.args)
  314. if not scheme:
  315. error = ("Invalid URL {0!r}: No schema supplied. Perhaps you meant http://{0}?")
  316. error = error.format(to_native_string(url, 'utf8'))
  317. raise MissingSchema(error)
  318. if not host:
  319. raise InvalidURL("Invalid URL %r: No host supplied" % url)
  320. # In general, we want to try IDNA encoding the hostname if the string contains
  321. # non-ASCII characters. This allows users to automatically get the correct IDNA
  322. # behaviour. For strings containing only ASCII characters, we need to also verify
  323. # it doesn't start with a wildcard (*), before allowing the unencoded hostname.
  324. if not unicode_is_ascii(host):
  325. try:
  326. host = self._get_idna_encoded_host(host)
  327. except UnicodeError:
  328. raise InvalidURL('URL has an invalid label.')
  329. elif host.startswith(u'*'):
  330. raise InvalidURL('URL has an invalid label.')
  331. # Carefully reconstruct the network location
  332. netloc = auth or ''
  333. if netloc:
  334. netloc += '@'
  335. netloc += host
  336. if port:
  337. netloc += ':' + str(port)
  338. # Bare domains aren't valid URLs.
  339. if not path:
  340. path = '/'
  341. if is_py2:
  342. if isinstance(scheme, str):
  343. scheme = scheme.encode('utf-8')
  344. if isinstance(netloc, str):
  345. netloc = netloc.encode('utf-8')
  346. if isinstance(path, str):
  347. path = path.encode('utf-8')
  348. if isinstance(query, str):
  349. query = query.encode('utf-8')
  350. if isinstance(fragment, str):
  351. fragment = fragment.encode('utf-8')
  352. if isinstance(params, (str, bytes)):
  353. params = to_native_string(params)
  354. enc_params = self._encode_params(params)
  355. if enc_params:
  356. if query:
  357. query = '%s&%s' % (query, enc_params)
  358. else:
  359. query = enc_params
  360. url = requote_uri(urlunparse([scheme, netloc, path, None, query, fragment]))
  361. self.url = url
  362. def prepare_headers(self, headers):
  363. """Prepares the given HTTP headers."""
  364. self.headers = CaseInsensitiveDict()
  365. if headers:
  366. for header in headers.items():
  367. # Raise exception on invalid header value.
  368. check_header_validity(header)
  369. name, value = header
  370. self.headers[to_native_string(name)] = value
  371. def prepare_body(self, data, files, json=None):
  372. """Prepares the given HTTP body data."""
  373. # Check if file, fo, generator, iterator.
  374. # If not, run through normal process.
  375. # Nottin' on you.
  376. body = None
  377. content_type = None
  378. if not data and json is not None:
  379. # urllib3 requires a bytes-like body. Python 2's json.dumps
  380. # provides this natively, but Python 3 gives a Unicode string.
  381. content_type = 'application/json'
  382. body = complexjson.dumps(json)
  383. if not isinstance(body, bytes):
  384. body = body.encode('utf-8')
  385. is_stream = all([
  386. hasattr(data, '__iter__'),
  387. not isinstance(data, (basestring, list, tuple, Mapping))
  388. ])
  389. try:
  390. length = super_len(data)
  391. except (TypeError, AttributeError, UnsupportedOperation):
  392. length = None
  393. if is_stream:
  394. body = data
  395. if getattr(body, 'tell', None) is not None:
  396. # Record the current file position before reading.
  397. # This will allow us to rewind a file in the event
  398. # of a redirect.
  399. try:
  400. self._body_position = body.tell()
  401. except (IOError, OSError):
  402. # This differentiates from None, allowing us to catch
  403. # a failed `tell()` later when trying to rewind the body
  404. self._body_position = object()
  405. if files:
  406. raise NotImplementedError('Streamed bodies and files are mutually exclusive.')
  407. if length:
  408. self.headers['Content-Length'] = builtin_str(length)
  409. else:
  410. self.headers['Transfer-Encoding'] = 'chunked'
  411. else:
  412. # Multi-part file uploads.
  413. if files:
  414. (body, content_type) = self._encode_files(files, data)
  415. else:
  416. if data:
  417. body = self._encode_params(data)
  418. if isinstance(data, basestring) or hasattr(data, 'read'):
  419. content_type = None
  420. else:
  421. content_type = 'application/x-www-form-urlencoded'
  422. self.prepare_content_length(body)
  423. # Add content-type if it wasn't explicitly provided.
  424. if content_type and ('content-type' not in self.headers):
  425. self.headers['Content-Type'] = content_type
  426. self.body = body
  427. def prepare_content_length(self, body):
  428. """Prepare Content-Length header based on request method and body"""
  429. if body is not None:
  430. length = super_len(body)
  431. if length:
  432. # If length exists, set it. Otherwise, we fallback
  433. # to Transfer-Encoding: chunked.
  434. self.headers['Content-Length'] = builtin_str(length)
  435. elif self.method not in ('GET', 'HEAD') and self.headers.get('Content-Length') is None:
  436. # Set Content-Length to 0 for methods that can have a body
  437. # but don't provide one. (i.e. not GET or HEAD)
  438. self.headers['Content-Length'] = '0'
  439. def prepare_auth(self, auth, url=''):
  440. """Prepares the given HTTP auth data."""
  441. # If no Auth is explicitly provided, extract it from the URL first.
  442. if auth is None:
  443. url_auth = get_auth_from_url(self.url)
  444. auth = url_auth if any(url_auth) else None
  445. if auth:
  446. if isinstance(auth, tuple) and len(auth) == 2:
  447. # special-case basic HTTP auth
  448. auth = HTTPBasicAuth(*auth)
  449. # Allow auth to make its changes.
  450. r = auth(self)
  451. # Update self to reflect the auth changes.
  452. self.__dict__.update(r.__dict__)
  453. # Recompute Content-Length
  454. self.prepare_content_length(self.body)
  455. def prepare_cookies(self, cookies):
  456. """Prepares the given HTTP cookie data.
  457. This function eventually generates a ``Cookie`` header from the
  458. given cookies using cookielib. Due to cookielib's design, the header
  459. will not be regenerated if it already exists, meaning this function
  460. can only be called once for the life of the
  461. :class:`PreparedRequest <PreparedRequest>` object. Any subsequent calls
  462. to ``prepare_cookies`` will have no actual effect, unless the "Cookie"
  463. header is removed beforehand.
  464. """
  465. if isinstance(cookies, cookielib.CookieJar):
  466. self._cookies = cookies
  467. else:
  468. self._cookies = cookiejar_from_dict(cookies)
  469. cookie_header = get_cookie_header(self._cookies, self)
  470. if cookie_header is not None:
  471. self.headers['Cookie'] = cookie_header
  472. def prepare_hooks(self, hooks):
  473. """Prepares the given hooks."""
  474. # hooks can be passed as None to the prepare method and to this
  475. # method. To prevent iterating over None, simply use an empty list
  476. # if hooks is False-y
  477. hooks = hooks or []
  478. for event in hooks:
  479. self.register_hook(event, hooks[event])
  480. class Response(object):
  481. """The :class:`Response <Response>` object, which contains a
  482. server's response to an HTTP request.
  483. """
  484. __attrs__ = [
  485. '_content', 'status_code', 'headers', 'url', 'history',
  486. 'encoding', 'reason', 'cookies', 'elapsed', 'request'
  487. ]
  488. def __init__(self):
  489. self._content = False
  490. self._content_consumed = False
  491. self._next = None
  492. #: Integer Code of responded HTTP Status, e.g. 404 or 200.
  493. self.status_code = None
  494. #: Case-insensitive Dictionary of Response Headers.
  495. #: For example, ``headers['content-encoding']`` will return the
  496. #: value of a ``'Content-Encoding'`` response header.
  497. self.headers = CaseInsensitiveDict()
  498. #: File-like object representation of response (for advanced usage).
  499. #: Use of ``raw`` requires that ``stream=True`` be set on the request.
  500. # This requirement does not apply for use internally to Requests.
  501. self.raw = None
  502. #: Final URL location of Response.
  503. self.url = None
  504. #: Encoding to decode with when accessing r.text.
  505. self.encoding = None
  506. #: A list of :class:`Response <Response>` objects from
  507. #: the history of the Request. Any redirect responses will end
  508. #: up here. The list is sorted from the oldest to the most recent request.
  509. self.history = []
  510. #: Textual reason of responded HTTP Status, e.g. "Not Found" or "OK".
  511. self.reason = None
  512. #: A CookieJar of Cookies the server sent back.
  513. self.cookies = cookiejar_from_dict({})
  514. #: The amount of time elapsed between sending the request
  515. #: and the arrival of the response (as a timedelta).
  516. #: This property specifically measures the time taken between sending
  517. #: the first byte of the request and finishing parsing the headers. It
  518. #: is therefore unaffected by consuming the response content or the
  519. #: value of the ``stream`` keyword argument.
  520. self.elapsed = datetime.timedelta(0)
  521. #: The :class:`PreparedRequest <PreparedRequest>` object to which this
  522. #: is a response.
  523. self.request = None
  524. def __enter__(self):
  525. return self
  526. def __exit__(self, *args):
  527. self.close()
  528. def __getstate__(self):
  529. # Consume everything; accessing the content attribute makes
  530. # sure the content has been fully read.
  531. if not self._content_consumed:
  532. self.content
  533. return {attr: getattr(self, attr, None) for attr in self.__attrs__}
  534. def __setstate__(self, state):
  535. for name, value in state.items():
  536. setattr(self, name, value)
  537. # pickled objects do not have .raw
  538. setattr(self, '_content_consumed', True)
  539. setattr(self, 'raw', None)
  540. def __repr__(self):
  541. return '<Response [%s]>' % (self.status_code)
  542. def __bool__(self):
  543. """Returns True if :attr:`status_code` is less than 400.
  544. This attribute checks if the status code of the response is between
  545. 400 and 600 to see if there was a client error or a server error. If
  546. the status code, is between 200 and 400, this will return True. This
  547. is **not** a check to see if the response code is ``200 OK``.
  548. """
  549. return self.ok
  550. def __nonzero__(self):
  551. """Returns True if :attr:`status_code` is less than 400.
  552. This attribute checks if the status code of the response is between
  553. 400 and 600 to see if there was a client error or a server error. If
  554. the status code, is between 200 and 400, this will return True. This
  555. is **not** a check to see if the response code is ``200 OK``.
  556. """
  557. return self.ok
  558. def __iter__(self):
  559. """Allows you to use a response as an iterator."""
  560. return self.iter_content(128)
  561. @property
  562. def ok(self):
  563. """Returns True if :attr:`status_code` is less than 400, False if not.
  564. This attribute checks if the status code of the response is between
  565. 400 and 600 to see if there was a client error or a server error. If
  566. the status code is between 200 and 400, this will return True. This
  567. is **not** a check to see if the response code is ``200 OK``.
  568. """
  569. try:
  570. self.raise_for_status()
  571. except HTTPError:
  572. return False
  573. return True
  574. @property
  575. def is_redirect(self):
  576. """True if this Response is a well-formed HTTP redirect that could have
  577. been processed automatically (by :meth:`Session.resolve_redirects`).
  578. """
  579. return ('location' in self.headers and self.status_code in REDIRECT_STATI)
  580. @property
  581. def is_permanent_redirect(self):
  582. """True if this Response one of the permanent versions of redirect."""
  583. return ('location' in self.headers and self.status_code in (codes.moved_permanently, codes.permanent_redirect))
  584. @property
  585. def next(self):
  586. """Returns a PreparedRequest for the next request in a redirect chain, if there is one."""
  587. return self._next
  588. @property
  589. def apparent_encoding(self):
  590. """The apparent encoding, provided by the chardet library."""
  591. return chardet.detect(self.content)['encoding']
  592. def iter_content(self, chunk_size=1, decode_unicode=False):
  593. """Iterates over the response data. When stream=True is set on the
  594. request, this avoids reading the content at once into memory for
  595. large responses. The chunk size is the number of bytes it should
  596. read into memory. This is not necessarily the length of each item
  597. returned as decoding can take place.
  598. chunk_size must be of type int or None. A value of None will
  599. function differently depending on the value of `stream`.
  600. stream=True will read data as it arrives in whatever size the
  601. chunks are received. If stream=False, data is returned as
  602. a single chunk.
  603. If decode_unicode is True, content will be decoded using the best
  604. available encoding based on the response.
  605. """
  606. def generate():
  607. # Special case for urllib3.
  608. if hasattr(self.raw, 'stream'):
  609. try:
  610. for chunk in self.raw.stream(chunk_size, decode_content=True):
  611. yield chunk
  612. except ProtocolError as e:
  613. raise ChunkedEncodingError(e)
  614. except DecodeError as e:
  615. raise ContentDecodingError(e)
  616. except ReadTimeoutError as e:
  617. raise ConnectionError(e)
  618. else:
  619. # Standard file-like object.
  620. while True:
  621. chunk = self.raw.read(chunk_size)
  622. if not chunk:
  623. break
  624. yield chunk
  625. self._content_consumed = True
  626. if self._content_consumed and isinstance(self._content, bool):
  627. raise StreamConsumedError()
  628. elif chunk_size is not None and not isinstance(chunk_size, int):
  629. raise TypeError("chunk_size must be an int, it is instead a %s." % type(chunk_size))
  630. # simulate reading small chunks of the content
  631. reused_chunks = iter_slices(self._content, chunk_size)
  632. stream_chunks = generate()
  633. chunks = reused_chunks if self._content_consumed else stream_chunks
  634. if decode_unicode:
  635. chunks = stream_decode_response_unicode(chunks, self)
  636. return chunks
  637. def iter_lines(self, chunk_size=ITER_CHUNK_SIZE, decode_unicode=None, delimiter=None):
  638. """Iterates over the response data, one line at a time. When
  639. stream=True is set on the request, this avoids reading the
  640. content at once into memory for large responses.
  641. .. note:: This method is not reentrant safe.
  642. """
  643. pending = None
  644. for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):
  645. if pending is not None:
  646. chunk = pending + chunk
  647. if delimiter:
  648. lines = chunk.split(delimiter)
  649. else:
  650. lines = chunk.splitlines()
  651. if lines and lines[-1] and chunk and lines[-1][-1] == chunk[-1]:
  652. pending = lines.pop()
  653. else:
  654. pending = None
  655. for line in lines:
  656. yield line
  657. if pending is not None:
  658. yield pending
  659. @property
  660. def content(self):
  661. """Content of the response, in bytes."""
  662. if self._content is False:
  663. # Read the contents.
  664. if self._content_consumed:
  665. raise RuntimeError(
  666. 'The content for this response was already consumed')
  667. if self.status_code == 0 or self.raw is None:
  668. self._content = None
  669. else:
  670. self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  671. self._content_consumed = True
  672. # don't need to release the connection; that's been handled by urllib3
  673. # since we exhausted the data.
  674. return self._content
  675. @property
  676. def text(self):
  677. """Content of the response, in unicode.
  678. If Response.encoding is None, encoding will be guessed using
  679. ``chardet``.
  680. The encoding of the response content is determined based solely on HTTP
  681. headers, following RFC 2616 to the letter. If you can take advantage of
  682. non-HTTP knowledge to make a better guess at the encoding, you should
  683. set ``r.encoding`` appropriately before accessing this property.
  684. """
  685. # Try charset from content-type
  686. content = None
  687. encoding = self.encoding
  688. if not self.content:
  689. return str('')
  690. # Fallback to auto-detected encoding.
  691. if self.encoding is None:
  692. encoding = self.apparent_encoding
  693. # Decode unicode from given encoding.
  694. try:
  695. content = str(self.content, encoding, errors='replace')
  696. except (LookupError, TypeError):
  697. # A LookupError is raised if the encoding was not found which could
  698. # indicate a misspelling or similar mistake.
  699. #
  700. # A TypeError can be raised if encoding is None
  701. #
  702. # So we try blindly encoding.
  703. content = str(self.content, errors='replace')
  704. return content
  705. def json(self, **kwargs):
  706. r"""Returns the json-encoded content of a response, if any.
  707. :param \*\*kwargs: Optional arguments that ``json.loads`` takes.
  708. :raises ValueError: If the response body does not contain valid json.
  709. """
  710. if not self.encoding and self.content and len(self.content) > 3:
  711. # No encoding set. JSON RFC 4627 section 3 states we should expect
  712. # UTF-8, -16 or -32. Detect which one to use; If the detection or
  713. # decoding fails, fall back to `self.text` (using chardet to make
  714. # a best guess).
  715. encoding = guess_json_utf(self.content)
  716. if encoding is not None:
  717. try:
  718. return complexjson.loads(
  719. self.content.decode(encoding), **kwargs
  720. )
  721. except UnicodeDecodeError:
  722. # Wrong UTF codec detected; usually because it's not UTF-8
  723. # but some other 8-bit codec. This is an RFC violation,
  724. # and the server didn't bother to tell us what codec *was*
  725. # used.
  726. pass
  727. return complexjson.loads(self.text, **kwargs)
  728. @property
  729. def links(self):
  730. """Returns the parsed header links of the response, if any."""
  731. header = self.headers.get('link')
  732. # l = MultiDict()
  733. l = {}
  734. if header:
  735. links = parse_header_links(header)
  736. for link in links:
  737. key = link.get('rel') or link.get('url')
  738. l[key] = link
  739. return l
  740. def raise_for_status(self):
  741. """Raises stored :class:`HTTPError`, if one occurred."""
  742. http_error_msg = ''
  743. if isinstance(self.reason, bytes):
  744. # We attempt to decode utf-8 first because some servers
  745. # choose to localize their reason strings. If the string
  746. # isn't utf-8, we fall back to iso-8859-1 for all other
  747. # encodings. (See PR #3538)
  748. try:
  749. reason = self.reason.decode('utf-8')
  750. except UnicodeDecodeError:
  751. reason = self.reason.decode('iso-8859-1')
  752. else:
  753. reason = self.reason
  754. if 400 <= self.status_code < 500:
  755. http_error_msg = u'%s Client Error: %s for url: %s' % (self.status_code, reason, self.url)
  756. elif 500 <= self.status_code < 600:
  757. http_error_msg = u'%s Server Error: %s for url: %s' % (self.status_code, reason, self.url)
  758. if http_error_msg:
  759. raise HTTPError(http_error_msg, response=self)
  760. def close(self):
  761. """Releases the connection back to the pool. Once this method has been
  762. called the underlying ``raw`` object must not be accessed again.
  763. *Note: Should not normally need to be called explicitly.*
  764. """
  765. if not self._content_consumed:
  766. self.raw.close()
  767. release_conn = getattr(self.raw, 'release_conn', None)
  768. if release_conn is not None:
  769. release_conn()