snowflake-connector-python 3.16.0__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. snowflake/connector/__init__.py +96 -0
  2. snowflake/connector/_query_context_cache.py +276 -0
  3. snowflake/connector/_sql_util.py +48 -0
  4. snowflake/connector/_utils.py +70 -0
  5. snowflake/connector/arrow_context.py +203 -0
  6. snowflake/connector/auth/__init__.py +53 -0
  7. snowflake/connector/auth/_auth.py +586 -0
  8. snowflake/connector/auth/_http_server.py +220 -0
  9. snowflake/connector/auth/_oauth_base.py +400 -0
  10. snowflake/connector/auth/by_plugin.py +219 -0
  11. snowflake/connector/auth/default.py +36 -0
  12. snowflake/connector/auth/idtoken.py +72 -0
  13. snowflake/connector/auth/keypair.py +222 -0
  14. snowflake/connector/auth/no_auth.py +39 -0
  15. snowflake/connector/auth/oauth.py +49 -0
  16. snowflake/connector/auth/oauth_code.py +479 -0
  17. snowflake/connector/auth/oauth_credentials.py +66 -0
  18. snowflake/connector/auth/okta.py +335 -0
  19. snowflake/connector/auth/pat.py +39 -0
  20. snowflake/connector/auth/usrpwdmfa.py +69 -0
  21. snowflake/connector/auth/webbrowser.py +502 -0
  22. snowflake/connector/auth/workload_identity.py +94 -0
  23. snowflake/connector/azure_storage_client.py +276 -0
  24. snowflake/connector/backoff_policies.py +141 -0
  25. snowflake/connector/bind_upload_agent.py +89 -0
  26. snowflake/connector/cache.py +696 -0
  27. snowflake/connector/compat.py +127 -0
  28. snowflake/connector/config_manager.py +496 -0
  29. snowflake/connector/connection.py +2273 -0
  30. snowflake/connector/connection_diagnostic.py +776 -0
  31. snowflake/connector/constants.py +442 -0
  32. snowflake/connector/converter.py +785 -0
  33. snowflake/connector/converter_issue23517.py +87 -0
  34. snowflake/connector/converter_null.py +14 -0
  35. snowflake/connector/converter_snowsql.py +205 -0
  36. snowflake/connector/cursor.py +1951 -0
  37. snowflake/connector/dbapi.py +53 -0
  38. snowflake/connector/description.py +19 -0
  39. snowflake/connector/direct_file_operation_utils.py +88 -0
  40. snowflake/connector/encryption_util.py +220 -0
  41. snowflake/connector/errorcode.py +91 -0
  42. snowflake/connector/errors.py +615 -0
  43. snowflake/connector/externals_utils/__init__.py +0 -0
  44. snowflake/connector/externals_utils/externals_setup.py +27 -0
  45. snowflake/connector/feature.py +4 -0
  46. snowflake/connector/file_compression_type.py +118 -0
  47. snowflake/connector/file_lock.py +72 -0
  48. snowflake/connector/file_transfer_agent.py +1215 -0
  49. snowflake/connector/file_util.py +153 -0
  50. snowflake/connector/gcs_storage_client.py +474 -0
  51. snowflake/connector/gzip_decoder.py +85 -0
  52. snowflake/connector/local_storage_client.py +90 -0
  53. snowflake/connector/log_configuration.py +60 -0
  54. snowflake/connector/logging_utils/__init__.py +0 -0
  55. snowflake/connector/logging_utils/filters.py +72 -0
  56. snowflake/connector/nanoarrow_arrow_iterator.cp313-win_amd64.pyd +0 -0
  57. snowflake/connector/nanoarrow_cpp/ArrowIterator/ArrayConverter.cpp +60 -0
  58. snowflake/connector/nanoarrow_cpp/ArrowIterator/ArrayConverter.hpp +29 -0
  59. snowflake/connector/nanoarrow_cpp/ArrowIterator/BinaryConverter.cpp +19 -0
  60. snowflake/connector/nanoarrow_cpp/ArrowIterator/BinaryConverter.hpp +26 -0
  61. snowflake/connector/nanoarrow_cpp/ArrowIterator/BooleanConverter.cpp +21 -0
  62. snowflake/connector/nanoarrow_cpp/ArrowIterator/BooleanConverter.hpp +23 -0
  63. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowChunkIterator.cpp +557 -0
  64. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowChunkIterator.hpp +98 -0
  65. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowIterator.cpp +125 -0
  66. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowIterator.hpp +115 -0
  67. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowTableIterator.cpp +1005 -0
  68. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowTableIterator.hpp +127 -0
  69. snowflake/connector/nanoarrow_cpp/ArrowIterator/DateConverter.cpp +47 -0
  70. snowflake/connector/nanoarrow_cpp/ArrowIterator/DateConverter.hpp +46 -0
  71. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecFloatConverter.cpp +83 -0
  72. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecFloatConverter.hpp +35 -0
  73. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecimalConverter.cpp +97 -0
  74. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecimalConverter.hpp +72 -0
  75. snowflake/connector/nanoarrow_cpp/ArrowIterator/FixedSizeListConverter.cpp +73 -0
  76. snowflake/connector/nanoarrow_cpp/ArrowIterator/FixedSizeListConverter.hpp +28 -0
  77. snowflake/connector/nanoarrow_cpp/ArrowIterator/FloatConverter.cpp +30 -0
  78. snowflake/connector/nanoarrow_cpp/ArrowIterator/FloatConverter.hpp +35 -0
  79. snowflake/connector/nanoarrow_cpp/ArrowIterator/IColumnConverter.hpp +17 -0
  80. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntConverter.cpp +23 -0
  81. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntConverter.hpp +45 -0
  82. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntervalConverter.cpp +71 -0
  83. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntervalConverter.hpp +56 -0
  84. snowflake/connector/nanoarrow_cpp/ArrowIterator/LICENSE.txt +209 -0
  85. snowflake/connector/nanoarrow_cpp/ArrowIterator/MapConverter.cpp +75 -0
  86. snowflake/connector/nanoarrow_cpp/ArrowIterator/MapConverter.hpp +30 -0
  87. snowflake/connector/nanoarrow_cpp/ArrowIterator/ObjectConverter.cpp +46 -0
  88. snowflake/connector/nanoarrow_cpp/ArrowIterator/ObjectConverter.hpp +29 -0
  89. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Common.cpp +8 -0
  90. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Common.hpp +95 -0
  91. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Helpers.cpp +57 -0
  92. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Helpers.hpp +36 -0
  93. snowflake/connector/nanoarrow_cpp/ArrowIterator/SnowflakeType.cpp +34 -0
  94. snowflake/connector/nanoarrow_cpp/ArrowIterator/SnowflakeType.hpp +48 -0
  95. snowflake/connector/nanoarrow_cpp/ArrowIterator/StringConverter.cpp +19 -0
  96. snowflake/connector/nanoarrow_cpp/ArrowIterator/StringConverter.hpp +26 -0
  97. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeConverter.cpp +36 -0
  98. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeConverter.hpp +31 -0
  99. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeStampConverter.cpp +346 -0
  100. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeStampConverter.hpp +145 -0
  101. snowflake/connector/nanoarrow_cpp/ArrowIterator/Util/macros.hpp +14 -0
  102. snowflake/connector/nanoarrow_cpp/ArrowIterator/Util/time.cpp +65 -0
  103. snowflake/connector/nanoarrow_cpp/ArrowIterator/Util/time.hpp +68 -0
  104. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_accessors.h +101 -0
  105. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_alloc.h +127 -0
  106. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_assert.h +45 -0
  107. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_builder.h +1908 -0
  108. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_emitter.h +215 -0
  109. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_endian.h +125 -0
  110. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_epilogue.h +7 -0
  111. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_flatbuffers.h +55 -0
  112. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_identifier.h +148 -0
  113. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_iov.h +31 -0
  114. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_prologue.h +8 -0
  115. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_refmap.h +144 -0
  116. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_rtconfig.h +162 -0
  117. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_types.h +97 -0
  118. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_verifier.h +239 -0
  119. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/flatcc_portable.h +14 -0
  120. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/paligned_alloc.h +210 -0
  121. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pattributes.h +84 -0
  122. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pdiagnostic.h +84 -0
  123. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pdiagnostic_pop.h +20 -0
  124. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pdiagnostic_push.h +51 -0
  125. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pendian.h +206 -0
  126. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pendian_detect.h +118 -0
  127. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pinline.h +19 -0
  128. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pinttypes.h +52 -0
  129. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/portable.h +2 -0
  130. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/portable_basic.h +25 -0
  131. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pstatic_assert.h +67 -0
  132. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pstdalign.h +162 -0
  133. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pstdint.h +898 -0
  134. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/punaligned.h +190 -0
  135. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pversion.h +6 -0
  136. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pwarnings.h +52 -0
  137. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc.c +3204 -0
  138. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow.c +3217 -0
  139. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow.h +3618 -0
  140. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow.hpp +379 -0
  141. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_arrow_iterator.pyx +256 -0
  142. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_device.c +512 -0
  143. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_device.h +350 -0
  144. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_ipc.c +33273 -0
  145. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_ipc.h +397 -0
  146. snowflake/connector/nanoarrow_cpp/Logging/logging.cpp +100 -0
  147. snowflake/connector/nanoarrow_cpp/Logging/logging.hpp +49 -0
  148. snowflake/connector/network.py +1297 -0
  149. snowflake/connector/ocsp_asn1crypto.py +447 -0
  150. snowflake/connector/ocsp_snowflake.py +1883 -0
  151. snowflake/connector/options.py +131 -0
  152. snowflake/connector/pandas_tools.py +732 -0
  153. snowflake/connector/proxy.py +43 -0
  154. snowflake/connector/py.typed +0 -0
  155. snowflake/connector/result_batch.py +786 -0
  156. snowflake/connector/result_set.py +319 -0
  157. snowflake/connector/s3_storage_client.py +605 -0
  158. snowflake/connector/secret_detector.py +181 -0
  159. snowflake/connector/sf_dirs.py +64 -0
  160. snowflake/connector/sfbinaryformat.py +35 -0
  161. snowflake/connector/sfdatetime.py +345 -0
  162. snowflake/connector/snow_logging.py +144 -0
  163. snowflake/connector/sqlstate.py +9 -0
  164. snowflake/connector/ssd_internal_keys.py +30 -0
  165. snowflake/connector/ssl_wrap_socket.py +134 -0
  166. snowflake/connector/storage_client.py +478 -0
  167. snowflake/connector/telemetry.py +249 -0
  168. snowflake/connector/telemetry_oob.py +543 -0
  169. snowflake/connector/test_util.py +30 -0
  170. snowflake/connector/time_util.py +159 -0
  171. snowflake/connector/token_cache.py +402 -0
  172. snowflake/connector/tool/__init__.py +0 -0
  173. snowflake/connector/tool/dump_certs.py +57 -0
  174. snowflake/connector/tool/dump_ocsp_response.py +139 -0
  175. snowflake/connector/tool/dump_ocsp_response_cache.py +194 -0
  176. snowflake/connector/tool/probe_connection.py +69 -0
  177. snowflake/connector/url_util.py +49 -0
  178. snowflake/connector/util_text.py +301 -0
  179. snowflake/connector/vendored/__init__.py +3 -0
  180. snowflake/connector/vendored/requests/LICENSE +175 -0
  181. snowflake/connector/vendored/requests/__init__.py +169 -0
  182. snowflake/connector/vendored/requests/__version__.py +14 -0
  183. snowflake/connector/vendored/requests/_internal_utils.py +50 -0
  184. snowflake/connector/vendored/requests/adapters.py +537 -0
  185. snowflake/connector/vendored/requests/api.py +157 -0
  186. snowflake/connector/vendored/requests/auth.py +315 -0
  187. snowflake/connector/vendored/requests/certs.py +17 -0
  188. snowflake/connector/vendored/requests/compat.py +79 -0
  189. snowflake/connector/vendored/requests/cookies.py +561 -0
  190. snowflake/connector/vendored/requests/exceptions.py +140 -0
  191. snowflake/connector/vendored/requests/help.py +134 -0
  192. snowflake/connector/vendored/requests/hooks.py +33 -0
  193. snowflake/connector/vendored/requests/models.py +1033 -0
  194. snowflake/connector/vendored/requests/sessions.py +833 -0
  195. snowflake/connector/vendored/requests/status_codes.py +128 -0
  196. snowflake/connector/vendored/requests/structures.py +99 -0
  197. snowflake/connector/vendored/requests/utils.py +1093 -0
  198. snowflake/connector/vendored/urllib3/LICENSE.txt +21 -0
  199. snowflake/connector/vendored/urllib3/__init__.py +85 -0
  200. snowflake/connector/vendored/urllib3/_collections.py +355 -0
  201. snowflake/connector/vendored/urllib3/_version.py +2 -0
  202. snowflake/connector/vendored/urllib3/connection.py +572 -0
  203. snowflake/connector/vendored/urllib3/connectionpool.py +1137 -0
  204. snowflake/connector/vendored/urllib3/contrib/__init__.py +0 -0
  205. snowflake/connector/vendored/urllib3/contrib/_appengine_environ.py +36 -0
  206. snowflake/connector/vendored/urllib3/contrib/_securetransport/__init__.py +0 -0
  207. snowflake/connector/vendored/urllib3/contrib/_securetransport/bindings.py +519 -0
  208. snowflake/connector/vendored/urllib3/contrib/_securetransport/low_level.py +397 -0
  209. snowflake/connector/vendored/urllib3/contrib/appengine.py +314 -0
  210. snowflake/connector/vendored/urllib3/contrib/ntlmpool.py +130 -0
  211. snowflake/connector/vendored/urllib3/contrib/pyopenssl.py +509 -0
  212. snowflake/connector/vendored/urllib3/contrib/securetransport.py +920 -0
  213. snowflake/connector/vendored/urllib3/contrib/socks.py +216 -0
  214. snowflake/connector/vendored/urllib3/exceptions.py +323 -0
  215. snowflake/connector/vendored/urllib3/fields.py +274 -0
  216. snowflake/connector/vendored/urllib3/filepost.py +98 -0
  217. snowflake/connector/vendored/urllib3/packages/__init__.py +0 -0
  218. snowflake/connector/vendored/urllib3/packages/backports/__init__.py +0 -0
  219. snowflake/connector/vendored/urllib3/packages/backports/makefile.py +51 -0
  220. snowflake/connector/vendored/urllib3/packages/backports/weakref_finalize.py +155 -0
  221. snowflake/connector/vendored/urllib3/packages/six.py +1076 -0
  222. snowflake/connector/vendored/urllib3/poolmanager.py +540 -0
  223. snowflake/connector/vendored/urllib3/request.py +191 -0
  224. snowflake/connector/vendored/urllib3/response.py +885 -0
  225. snowflake/connector/vendored/urllib3/util/__init__.py +49 -0
  226. snowflake/connector/vendored/urllib3/util/connection.py +156 -0
  227. snowflake/connector/vendored/urllib3/util/proxy.py +57 -0
  228. snowflake/connector/vendored/urllib3/util/queue.py +22 -0
  229. snowflake/connector/vendored/urllib3/util/request.py +146 -0
  230. snowflake/connector/vendored/urllib3/util/response.py +107 -0
  231. snowflake/connector/vendored/urllib3/util/retry.py +620 -0
  232. snowflake/connector/vendored/urllib3/util/ssl_.py +495 -0
  233. snowflake/connector/vendored/urllib3/util/ssl_match_hostname.py +159 -0
  234. snowflake/connector/vendored/urllib3/util/ssltransport.py +221 -0
  235. snowflake/connector/vendored/urllib3/util/timeout.py +271 -0
  236. snowflake/connector/vendored/urllib3/util/url.py +435 -0
  237. snowflake/connector/vendored/urllib3/util/wait.py +152 -0
  238. snowflake/connector/version.py +3 -0
  239. snowflake/connector/wif_util.py +407 -0
  240. snowflake_connector_python-3.16.0.dist-info/METADATA +1475 -0
  241. snowflake_connector_python-3.16.0.dist-info/RECORD +246 -0
  242. snowflake_connector_python-3.16.0.dist-info/WHEEL +5 -0
  243. snowflake_connector_python-3.16.0.dist-info/entry_points.txt +4 -0
  244. snowflake_connector_python-3.16.0.dist-info/licenses/LICENSE.txt +202 -0
  245. snowflake_connector_python-3.16.0.dist-info/licenses/NOTICE +8 -0
  246. snowflake_connector_python-3.16.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,885 @@
1
+ from __future__ import absolute_import
2
+
3
+ import io
4
+ import logging
5
+ import sys
6
+ import warnings
7
+ import zlib
8
+ from contextlib import contextmanager
9
+ from socket import error as SocketError
10
+ from socket import timeout as SocketTimeout
11
+
12
+ try:
13
+ try:
14
+ import brotlicffi as brotli
15
+ except ImportError:
16
+ import brotli
17
+ except ImportError:
18
+ brotli = None
19
+
20
+ from . import util
21
+ from ._collections import HTTPHeaderDict
22
+ from .connection import BaseSSLError, HTTPException
23
+ from .exceptions import (
24
+ BodyNotHttplibCompatible,
25
+ DecodeError,
26
+ HTTPError,
27
+ IncompleteRead,
28
+ InvalidChunkLength,
29
+ InvalidHeader,
30
+ ProtocolError,
31
+ ReadTimeoutError,
32
+ ResponseNotChunked,
33
+ SSLError,
34
+ )
35
+ from .packages import six
36
+ from .util.response import is_fp_closed, is_response_to_head
37
+
38
+ log = logging.getLogger(__name__)
39
+
40
+
41
+ class DeflateDecoder(object):
42
+ def __init__(self):
43
+ self._first_try = True
44
+ self._data = b""
45
+ self._obj = zlib.decompressobj()
46
+
47
+ def __getattr__(self, name):
48
+ return getattr(self._obj, name)
49
+
50
+ def decompress(self, data):
51
+ if not data:
52
+ return data
53
+
54
+ if not self._first_try:
55
+ return self._obj.decompress(data)
56
+
57
+ self._data += data
58
+ try:
59
+ decompressed = self._obj.decompress(data)
60
+ if decompressed:
61
+ self._first_try = False
62
+ self._data = None
63
+ return decompressed
64
+ except zlib.error:
65
+ self._first_try = False
66
+ self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
67
+ try:
68
+ return self.decompress(self._data)
69
+ finally:
70
+ self._data = None
71
+
72
+
73
+ class GzipDecoderState(object):
74
+
75
+ FIRST_MEMBER = 0
76
+ OTHER_MEMBERS = 1
77
+ SWALLOW_DATA = 2
78
+
79
+
80
+ class GzipDecoder(object):
81
+ def __init__(self):
82
+ self._obj = zlib.decompressobj(16 + zlib.MAX_WBITS)
83
+ self._state = GzipDecoderState.FIRST_MEMBER
84
+
85
+ def __getattr__(self, name):
86
+ return getattr(self._obj, name)
87
+
88
+ def decompress(self, data):
89
+ ret = bytearray()
90
+ if self._state == GzipDecoderState.SWALLOW_DATA or not data:
91
+ return bytes(ret)
92
+ while True:
93
+ try:
94
+ ret += self._obj.decompress(data)
95
+ except zlib.error:
96
+ previous_state = self._state
97
+ # Ignore data after the first error
98
+ self._state = GzipDecoderState.SWALLOW_DATA
99
+ if previous_state == GzipDecoderState.OTHER_MEMBERS:
100
+ # Allow trailing garbage acceptable in other gzip clients
101
+ return bytes(ret)
102
+ raise
103
+ data = self._obj.unused_data
104
+ if not data:
105
+ return bytes(ret)
106
+ self._state = GzipDecoderState.OTHER_MEMBERS
107
+ self._obj = zlib.decompressobj(16 + zlib.MAX_WBITS)
108
+
109
+
110
+ if brotli is not None:
111
+
112
+ class BrotliDecoder(object):
113
+ # Supports both 'brotlipy' and 'Brotli' packages
114
+ # since they share an import name. The top branches
115
+ # are for 'brotlipy' and bottom branches for 'Brotli'
116
+ def __init__(self):
117
+ self._obj = brotli.Decompressor()
118
+ if hasattr(self._obj, "decompress"):
119
+ self.decompress = self._obj.decompress
120
+ else:
121
+ self.decompress = self._obj.process
122
+
123
+ def flush(self):
124
+ if hasattr(self._obj, "flush"):
125
+ return self._obj.flush()
126
+ return b""
127
+
128
+
129
+ class MultiDecoder(object):
130
+ """
131
+ From RFC7231:
132
+ If one or more encodings have been applied to a representation, the
133
+ sender that applied the encodings MUST generate a Content-Encoding
134
+ header field that lists the content codings in the order in which
135
+ they were applied.
136
+ """
137
+
138
+ def __init__(self, modes):
139
+ self._decoders = [_get_decoder(m.strip()) for m in modes.split(",")]
140
+
141
+ def flush(self):
142
+ return self._decoders[0].flush()
143
+
144
+ def decompress(self, data):
145
+ for d in reversed(self._decoders):
146
+ data = d.decompress(data)
147
+ return data
148
+
149
+
150
+ def _get_decoder(mode):
151
+ if "," in mode:
152
+ return MultiDecoder(mode)
153
+
154
+ if mode == "gzip":
155
+ return GzipDecoder()
156
+
157
+ if brotli is not None and mode == "br":
158
+ return BrotliDecoder()
159
+
160
+ return DeflateDecoder()
161
+
162
+
163
+ class HTTPResponse(io.IOBase):
164
+ """
165
+ HTTP Response container.
166
+
167
+ Backwards-compatible with :class:`http.client.HTTPResponse` but the response ``body`` is
168
+ loaded and decoded on-demand when the ``data`` property is accessed. This
169
+ class is also compatible with the Python standard library's :mod:`io`
170
+ module, and can hence be treated as a readable object in the context of that
171
+ framework.
172
+
173
+ Extra parameters for behaviour not present in :class:`http.client.HTTPResponse`:
174
+
175
+ :param preload_content:
176
+ If True, the response's body will be preloaded during construction.
177
+
178
+ :param decode_content:
179
+ If True, will attempt to decode the body based on the
180
+ 'content-encoding' header.
181
+
182
+ :param original_response:
183
+ When this HTTPResponse wrapper is generated from an :class:`http.client.HTTPResponse`
184
+ object, it's convenient to include the original for debug purposes. It's
185
+ otherwise unused.
186
+
187
+ :param retries:
188
+ The retries contains the last :class:`~urllib3.util.retry.Retry` that
189
+ was used during the request.
190
+
191
+ :param enforce_content_length:
192
+ Enforce content length checking. Body returned by server must match
193
+ value of Content-Length header, if present. Otherwise, raise error.
194
+ """
195
+
196
+ CONTENT_DECODERS = ["gzip", "deflate"]
197
+ if brotli is not None:
198
+ CONTENT_DECODERS += ["br"]
199
+ REDIRECT_STATUSES = [301, 302, 303, 307, 308]
200
+
201
+ def __init__(
202
+ self,
203
+ body="",
204
+ headers=None,
205
+ status=0,
206
+ version=0,
207
+ reason=None,
208
+ strict=0,
209
+ preload_content=True,
210
+ decode_content=True,
211
+ original_response=None,
212
+ pool=None,
213
+ connection=None,
214
+ msg=None,
215
+ retries=None,
216
+ enforce_content_length=True,
217
+ request_method=None,
218
+ request_url=None,
219
+ auto_close=True,
220
+ ):
221
+
222
+ if isinstance(headers, HTTPHeaderDict):
223
+ self.headers = headers
224
+ else:
225
+ self.headers = HTTPHeaderDict(headers)
226
+ self.status = status
227
+ self.version = version
228
+ self.reason = reason
229
+ self.strict = strict
230
+ self.decode_content = decode_content
231
+ self.retries = retries
232
+ self.enforce_content_length = enforce_content_length
233
+ self.auto_close = auto_close
234
+
235
+ self._decoder = None
236
+ self._body = None
237
+ self._fp = None
238
+ self._original_response = original_response
239
+ self._fp_bytes_read = 0
240
+ self.msg = msg
241
+ self._request_url = request_url
242
+
243
+ if body and isinstance(body, (six.string_types, bytes)):
244
+ self._body = body
245
+
246
+ self._pool = pool
247
+ self._connection = connection
248
+
249
+ if hasattr(body, "read"):
250
+ self._fp = body
251
+
252
+ # Are we using the chunked-style of transfer encoding?
253
+ self.chunked = False
254
+ self.chunk_left = None
255
+ tr_enc = self.headers.get("transfer-encoding", "").lower()
256
+ # Don't incur the penalty of creating a list and then discarding it
257
+ encodings = (enc.strip() for enc in tr_enc.split(","))
258
+ if "chunked" in encodings:
259
+ self.chunked = True
260
+
261
+ # Determine length of response
262
+ self.length_remaining = self._init_length(request_method)
263
+
264
+ # If requested, preload the body.
265
+ if preload_content and not self._body:
266
+ self._body = self.read(decode_content=decode_content)
267
+
268
+ def get_redirect_location(self):
269
+ """
270
+ Should we redirect and where to?
271
+
272
+ :returns: Truthy redirect location string if we got a redirect status
273
+ code and valid location. ``None`` if redirect status and no
274
+ location. ``False`` if not a redirect status code.
275
+ """
276
+ if self.status in self.REDIRECT_STATUSES:
277
+ return self.headers.get("location")
278
+
279
+ return False
280
+
281
+ def release_conn(self):
282
+ if not self._pool or not self._connection:
283
+ return
284
+
285
+ self._pool._put_conn(self._connection)
286
+ self._connection = None
287
+
288
+ def drain_conn(self):
289
+ """
290
+ Read and discard any remaining HTTP response data in the response connection.
291
+
292
+ Unread data in the HTTPResponse connection blocks the connection from being released back to the pool.
293
+ """
294
+ try:
295
+ self.read()
296
+ except (HTTPError, SocketError, BaseSSLError, HTTPException):
297
+ pass
298
+
299
+ @property
300
+ def data(self):
301
+ # For backwards-compat with earlier urllib3 0.4 and earlier.
302
+ if self._body:
303
+ return self._body
304
+
305
+ if self._fp:
306
+ return self.read(cache_content=True)
307
+
308
+ @property
309
+ def connection(self):
310
+ return self._connection
311
+
312
+ def isclosed(self):
313
+ return is_fp_closed(self._fp)
314
+
315
+ def tell(self):
316
+ """
317
+ Obtain the number of bytes pulled over the wire so far. May differ from
318
+ the amount of content returned by :meth:``urllib3.response.HTTPResponse.read``
319
+ if bytes are encoded on the wire (e.g, compressed).
320
+ """
321
+ return self._fp_bytes_read
322
+
323
+ def _init_length(self, request_method):
324
+ """
325
+ Set initial length value for Response content if available.
326
+ """
327
+ length = self.headers.get("content-length")
328
+
329
+ if length is not None:
330
+ if self.chunked:
331
+ # This Response will fail with an IncompleteRead if it can't be
332
+ # received as chunked. This method falls back to attempt reading
333
+ # the response before raising an exception.
334
+ log.warning(
335
+ "Received response with both Content-Length and "
336
+ "Transfer-Encoding set. This is expressly forbidden "
337
+ "by RFC 7230 sec 3.3.2. Ignoring Content-Length and "
338
+ "attempting to process response as Transfer-Encoding: "
339
+ "chunked."
340
+ )
341
+ return None
342
+
343
+ try:
344
+ # RFC 7230 section 3.3.2 specifies multiple content lengths can
345
+ # be sent in a single Content-Length header
346
+ # (e.g. Content-Length: 42, 42). This line ensures the values
347
+ # are all valid ints and that as long as the `set` length is 1,
348
+ # all values are the same. Otherwise, the header is invalid.
349
+ lengths = set([int(val) for val in length.split(",")])
350
+ if len(lengths) > 1:
351
+ raise InvalidHeader(
352
+ "Content-Length contained multiple "
353
+ "unmatching values (%s)" % length
354
+ )
355
+ length = lengths.pop()
356
+ except ValueError:
357
+ length = None
358
+ else:
359
+ if length < 0:
360
+ length = None
361
+
362
+ # Convert status to int for comparison
363
+ # In some cases, httplib returns a status of "_UNKNOWN"
364
+ try:
365
+ status = int(self.status)
366
+ except ValueError:
367
+ status = 0
368
+
369
+ # Check for responses that shouldn't include a body
370
+ if status in (204, 304) or 100 <= status < 200 or request_method == "HEAD":
371
+ length = 0
372
+
373
+ return length
374
+
375
+ def _init_decoder(self):
376
+ """
377
+ Set-up the _decoder attribute if necessary.
378
+ """
379
+ # Note: content-encoding value should be case-insensitive, per RFC 7230
380
+ # Section 3.2
381
+ content_encoding = self.headers.get("content-encoding", "").lower()
382
+ if self._decoder is None:
383
+ if content_encoding in self.CONTENT_DECODERS:
384
+ self._decoder = _get_decoder(content_encoding)
385
+ elif "," in content_encoding:
386
+ encodings = [
387
+ e.strip()
388
+ for e in content_encoding.split(",")
389
+ if e.strip() in self.CONTENT_DECODERS
390
+ ]
391
+ if len(encodings):
392
+ self._decoder = _get_decoder(content_encoding)
393
+
394
+ DECODER_ERROR_CLASSES = (IOError, zlib.error)
395
+ if brotli is not None:
396
+ DECODER_ERROR_CLASSES += (brotli.error,)
397
+
398
+ def _decode(self, data, decode_content, flush_decoder):
399
+ """
400
+ Decode the data passed in and potentially flush the decoder.
401
+ """
402
+ if not decode_content:
403
+ return data
404
+
405
+ try:
406
+ if self._decoder:
407
+ data = self._decoder.decompress(data)
408
+ except self.DECODER_ERROR_CLASSES as e:
409
+ content_encoding = self.headers.get("content-encoding", "").lower()
410
+ raise DecodeError(
411
+ "Received response with content-encoding: %s, but "
412
+ "failed to decode it." % content_encoding,
413
+ e,
414
+ )
415
+ if flush_decoder:
416
+ data += self._flush_decoder()
417
+
418
+ return data
419
+
420
+ def _flush_decoder(self):
421
+ """
422
+ Flushes the decoder. Should only be called if the decoder is actually
423
+ being used.
424
+ """
425
+ if self._decoder:
426
+ buf = self._decoder.decompress(b"")
427
+ return buf + self._decoder.flush()
428
+
429
+ return b""
430
+
431
+ @contextmanager
432
+ def _error_catcher(self):
433
+ """
434
+ Catch low-level python exceptions, instead re-raising urllib3
435
+ variants, so that low-level exceptions are not leaked in the
436
+ high-level api.
437
+
438
+ On exit, release the connection back to the pool.
439
+ """
440
+ clean_exit = False
441
+
442
+ try:
443
+ try:
444
+ yield
445
+
446
+ except SocketTimeout:
447
+ # FIXME: Ideally we'd like to include the url in the ReadTimeoutError but
448
+ # there is yet no clean way to get at it from this context.
449
+ raise ReadTimeoutError(self._pool, None, "Read timed out.")
450
+
451
+ except BaseSSLError as e:
452
+ # FIXME: Is there a better way to differentiate between SSLErrors?
453
+ if "read operation timed out" not in str(e):
454
+ # SSL errors related to framing/MAC get wrapped and reraised here
455
+ raise SSLError(e)
456
+
457
+ raise ReadTimeoutError(self._pool, None, "Read timed out.")
458
+
459
+ except (HTTPException, SocketError) as e:
460
+ # This includes IncompleteRead.
461
+ raise ProtocolError("Connection broken: %r" % e, e)
462
+
463
+ # If no exception is thrown, we should avoid cleaning up
464
+ # unnecessarily.
465
+ clean_exit = True
466
+ finally:
467
+ # If we didn't terminate cleanly, we need to throw away our
468
+ # connection.
469
+ if not clean_exit:
470
+ # The response may not be closed but we're not going to use it
471
+ # anymore so close it now to ensure that the connection is
472
+ # released back to the pool.
473
+ if self._original_response:
474
+ self._original_response.close()
475
+
476
+ # Closing the response may not actually be sufficient to close
477
+ # everything, so if we have a hold of the connection close that
478
+ # too.
479
+ if self._connection:
480
+ self._connection.close()
481
+
482
+ # If we hold the original response but it's closed now, we should
483
+ # return the connection back to the pool.
484
+ if self._original_response and self._original_response.isclosed():
485
+ self.release_conn()
486
+
487
+ def _fp_read(self, amt):
488
+ """
489
+ Read a response with the thought that reading the number of bytes
490
+ larger than can fit in a 32-bit int at a time via SSL in some
491
+ known cases leads to an overflow error that has to be prevented
492
+ if `amt` or `self.length_remaining` indicate that a problem may
493
+ happen.
494
+
495
+ The known cases:
496
+ * 3.8 <= CPython < 3.9.7 because of a bug
497
+ https://github.com/urllib3/urllib3/issues/2513#issuecomment-1152559900.
498
+ * urllib3 injected with pyOpenSSL-backed SSL-support.
499
+ * CPython < 3.10 only when `amt` does not fit 32-bit int.
500
+ """
501
+ assert self._fp
502
+ c_int_max = 2 ** 31 - 1
503
+ if (
504
+ (
505
+ (amt and amt > c_int_max)
506
+ or (self.length_remaining and self.length_remaining > c_int_max)
507
+ )
508
+ and not util.IS_SECURETRANSPORT
509
+ and (util.IS_PYOPENSSL or sys.version_info < (3, 10))
510
+ ):
511
+ buffer = io.BytesIO()
512
+ # Besides `max_chunk_amt` being a maximum chunk size, it
513
+ # affects memory overhead of reading a response by this
514
+ # method in CPython.
515
+ # `c_int_max` equal to 2 GiB - 1 byte is the actual maximum
516
+ # chunk size that does not lead to an overflow error, but
517
+ # 256 MiB is a compromise.
518
+ max_chunk_amt = 2 ** 28
519
+ while amt is None or amt != 0:
520
+ if amt is not None:
521
+ chunk_amt = min(amt, max_chunk_amt)
522
+ amt -= chunk_amt
523
+ else:
524
+ chunk_amt = max_chunk_amt
525
+ data = self._fp.read(chunk_amt)
526
+ if not data:
527
+ break
528
+ buffer.write(data)
529
+ del data # to reduce peak memory usage by `max_chunk_amt`.
530
+ return buffer.getvalue()
531
+ else:
532
+ # StringIO doesn't like amt=None
533
+ return self._fp.read(amt) if amt is not None else self._fp.read()
534
+
535
+ def read(self, amt=None, decode_content=None, cache_content=False):
536
+ """
537
+ Similar to :meth:`http.client.HTTPResponse.read`, but with two additional
538
+ parameters: ``decode_content`` and ``cache_content``.
539
+
540
+ :param amt:
541
+ How much of the content to read. If specified, caching is skipped
542
+ because it doesn't make sense to cache partial content as the full
543
+ response.
544
+
545
+ :param decode_content:
546
+ If True, will attempt to decode the body based on the
547
+ 'content-encoding' header.
548
+
549
+ :param cache_content:
550
+ If True, will save the returned data such that the same result is
551
+ returned despite of the state of the underlying file object. This
552
+ is useful if you want the ``.data`` property to continue working
553
+ after having ``.read()`` the file object. (Overridden if ``amt`` is
554
+ set.)
555
+ """
556
+ self._init_decoder()
557
+ if decode_content is None:
558
+ decode_content = self.decode_content
559
+
560
+ if self._fp is None:
561
+ return
562
+
563
+ flush_decoder = False
564
+ fp_closed = getattr(self._fp, "closed", False)
565
+
566
+ with self._error_catcher():
567
+ data = self._fp_read(amt) if not fp_closed else b""
568
+ if amt is None:
569
+ flush_decoder = True
570
+ else:
571
+ cache_content = False
572
+ if (
573
+ amt != 0 and not data
574
+ ): # Platform-specific: Buggy versions of Python.
575
+ # Close the connection when no data is returned
576
+ #
577
+ # This is redundant to what httplib/http.client _should_
578
+ # already do. However, versions of python released before
579
+ # December 15, 2012 (http://bugs.python.org/issue16298) do
580
+ # not properly close the connection in all cases. There is
581
+ # no harm in redundantly calling close.
582
+ self._fp.close()
583
+ flush_decoder = True
584
+ if self.enforce_content_length and self.length_remaining not in (
585
+ 0,
586
+ None,
587
+ ):
588
+ # This is an edge case that httplib failed to cover due
589
+ # to concerns of backward compatibility. We're
590
+ # addressing it here to make sure IncompleteRead is
591
+ # raised during streaming, so all calls with incorrect
592
+ # Content-Length are caught.
593
+ raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
594
+
595
+ if data:
596
+ self._fp_bytes_read += len(data)
597
+ if self.length_remaining is not None:
598
+ self.length_remaining -= len(data)
599
+
600
+ data = self._decode(data, decode_content, flush_decoder)
601
+
602
+ if cache_content:
603
+ self._body = data
604
+
605
+ return data
606
+
607
+ def stream(self, amt=2 ** 16, decode_content=None):
608
+ """
609
+ A generator wrapper for the read() method. A call will block until
610
+ ``amt`` bytes have been read from the connection or until the
611
+ connection is closed.
612
+
613
+ :param amt:
614
+ How much of the content to read. The generator will return up to
615
+ much data per iteration, but may return less. This is particularly
616
+ likely when using compressed data. However, the empty string will
617
+ never be returned.
618
+
619
+ :param decode_content:
620
+ If True, will attempt to decode the body based on the
621
+ 'content-encoding' header.
622
+ """
623
+ if self.chunked and self.supports_chunked_reads():
624
+ for line in self.read_chunked(amt, decode_content=decode_content):
625
+ yield line
626
+ else:
627
+ while not is_fp_closed(self._fp):
628
+ data = self.read(amt=amt, decode_content=decode_content)
629
+
630
+ if data:
631
+ yield data
632
+
633
+ @classmethod
634
+ def from_httplib(ResponseCls, r, **response_kw):
635
+ """
636
+ Given an :class:`http.client.HTTPResponse` instance ``r``, return a
637
+ corresponding :class:`urllib3.response.HTTPResponse` object.
638
+
639
+ Remaining parameters are passed to the HTTPResponse constructor, along
640
+ with ``original_response=r``.
641
+ """
642
+ headers = r.msg
643
+
644
+ if not isinstance(headers, HTTPHeaderDict):
645
+ if six.PY2:
646
+ # Python 2.7
647
+ headers = HTTPHeaderDict.from_httplib(headers)
648
+ else:
649
+ headers = HTTPHeaderDict(headers.items())
650
+
651
+ # HTTPResponse objects in Python 3 don't have a .strict attribute
652
+ strict = getattr(r, "strict", 0)
653
+ resp = ResponseCls(
654
+ body=r,
655
+ headers=headers,
656
+ status=r.status,
657
+ version=r.version,
658
+ reason=r.reason,
659
+ strict=strict,
660
+ original_response=r,
661
+ **response_kw
662
+ )
663
+ return resp
664
+
665
+ # Backwards-compatibility methods for http.client.HTTPResponse
666
+ def getheaders(self):
667
+ warnings.warn(
668
+ "HTTPResponse.getheaders() is deprecated and will be removed "
669
+ "in urllib3 v2.1.0. Instead access HTTPResponse.headers directly.",
670
+ category=DeprecationWarning,
671
+ stacklevel=2,
672
+ )
673
+ return self.headers
674
+
675
+ def getheader(self, name, default=None):
676
+ warnings.warn(
677
+ "HTTPResponse.getheader() is deprecated and will be removed "
678
+ "in urllib3 v2.1.0. Instead use HTTPResponse.headers.get(name, default).",
679
+ category=DeprecationWarning,
680
+ stacklevel=2,
681
+ )
682
+ return self.headers.get(name, default)
683
+
684
+ # Backwards compatibility for http.cookiejar
685
+ def info(self):
686
+ return self.headers
687
+
688
+ # Overrides from io.IOBase
689
+ def close(self):
690
+ if not self.closed:
691
+ self._fp.close()
692
+
693
+ if self._connection:
694
+ self._connection.close()
695
+
696
+ if not self.auto_close:
697
+ io.IOBase.close(self)
698
+
699
+ @property
700
+ def closed(self):
701
+ if not self.auto_close:
702
+ return io.IOBase.closed.__get__(self)
703
+ elif self._fp is None:
704
+ return True
705
+ elif hasattr(self._fp, "isclosed"):
706
+ return self._fp.isclosed()
707
+ elif hasattr(self._fp, "closed"):
708
+ return self._fp.closed
709
+ else:
710
+ return True
711
+
712
+ def fileno(self):
713
+ if self._fp is None:
714
+ raise IOError("HTTPResponse has no file to get a fileno from")
715
+ elif hasattr(self._fp, "fileno"):
716
+ return self._fp.fileno()
717
+ else:
718
+ raise IOError(
719
+ "The file-like object this HTTPResponse is wrapped "
720
+ "around has no file descriptor"
721
+ )
722
+
723
+ def flush(self):
724
+ if (
725
+ self._fp is not None
726
+ and hasattr(self._fp, "flush")
727
+ and not getattr(self._fp, "closed", False)
728
+ ):
729
+ return self._fp.flush()
730
+
731
+ def readable(self):
732
+ # This method is required for `io` module compatibility.
733
+ return True
734
+
735
+ def readinto(self, b):
736
+ # This method is required for `io` module compatibility.
737
+ temp = self.read(len(b))
738
+ if len(temp) == 0:
739
+ return 0
740
+ else:
741
+ b[: len(temp)] = temp
742
+ return len(temp)
743
+
744
+ def supports_chunked_reads(self):
745
+ """
746
+ Checks if the underlying file-like object looks like a
747
+ :class:`http.client.HTTPResponse` object. We do this by testing for
748
+ the fp attribute. If it is present we assume it returns raw chunks as
749
+ processed by read_chunked().
750
+ """
751
+ return hasattr(self._fp, "fp")
752
+
753
+ def _update_chunk_length(self):
754
+ # First, we'll figure out length of a chunk and then
755
+ # we'll try to read it from socket.
756
+ if self.chunk_left is not None:
757
+ return
758
+ line = self._fp.fp.readline()
759
+ line = line.split(b";", 1)[0]
760
+ try:
761
+ self.chunk_left = int(line, 16)
762
+ except ValueError:
763
+ # Invalid chunked protocol response, abort.
764
+ self.close()
765
+ raise InvalidChunkLength(self, line)
766
+
767
+ def _handle_chunk(self, amt):
768
+ returned_chunk = None
769
+ if amt is None:
770
+ chunk = self._fp._safe_read(self.chunk_left)
771
+ returned_chunk = chunk
772
+ self._fp._safe_read(2) # Toss the CRLF at the end of the chunk.
773
+ self.chunk_left = None
774
+ elif amt < self.chunk_left:
775
+ value = self._fp._safe_read(amt)
776
+ self.chunk_left = self.chunk_left - amt
777
+ returned_chunk = value
778
+ elif amt == self.chunk_left:
779
+ value = self._fp._safe_read(amt)
780
+ self._fp._safe_read(2) # Toss the CRLF at the end of the chunk.
781
+ self.chunk_left = None
782
+ returned_chunk = value
783
+ else: # amt > self.chunk_left
784
+ returned_chunk = self._fp._safe_read(self.chunk_left)
785
+ self._fp._safe_read(2) # Toss the CRLF at the end of the chunk.
786
+ self.chunk_left = None
787
+ return returned_chunk
788
+
789
+ def read_chunked(self, amt=None, decode_content=None):
790
+ """
791
+ Similar to :meth:`HTTPResponse.read`, but with an additional
792
+ parameter: ``decode_content``.
793
+
794
+ :param amt:
795
+ How much of the content to read. If specified, caching is skipped
796
+ because it doesn't make sense to cache partial content as the full
797
+ response.
798
+
799
+ :param decode_content:
800
+ If True, will attempt to decode the body based on the
801
+ 'content-encoding' header.
802
+ """
803
+ self._init_decoder()
804
+ # FIXME: Rewrite this method and make it a class with a better structured logic.
805
+ if not self.chunked:
806
+ raise ResponseNotChunked(
807
+ "Response is not chunked. "
808
+ "Header 'transfer-encoding: chunked' is missing."
809
+ )
810
+ if not self.supports_chunked_reads():
811
+ raise BodyNotHttplibCompatible(
812
+ "Body should be http.client.HTTPResponse like. "
813
+ "It should have have an fp attribute which returns raw chunks."
814
+ )
815
+
816
+ with self._error_catcher():
817
+ # Don't bother reading the body of a HEAD request.
818
+ if self._original_response and is_response_to_head(self._original_response):
819
+ self._original_response.close()
820
+ return
821
+
822
+ # If a response is already read and closed
823
+ # then return immediately.
824
+ if self._fp.fp is None:
825
+ return
826
+
827
+ while True:
828
+ self._update_chunk_length()
829
+ if self.chunk_left == 0:
830
+ break
831
+ chunk = self._handle_chunk(amt)
832
+ decoded = self._decode(
833
+ chunk, decode_content=decode_content, flush_decoder=False
834
+ )
835
+ if decoded:
836
+ yield decoded
837
+
838
+ if decode_content:
839
+ # On CPython and PyPy, we should never need to flush the
840
+ # decoder. However, on Jython we *might* need to, so
841
+ # lets defensively do it anyway.
842
+ decoded = self._flush_decoder()
843
+ if decoded: # Platform-specific: Jython.
844
+ yield decoded
845
+
846
+ # Chunk content ends with \r\n: discard it.
847
+ while True:
848
+ line = self._fp.fp.readline()
849
+ if not line:
850
+ # Some sites may not end with '\r\n'.
851
+ break
852
+ if line == b"\r\n":
853
+ break
854
+
855
+ # We read everything; close the "file".
856
+ if self._original_response:
857
+ self._original_response.close()
858
+
859
+ def geturl(self):
860
+ """
861
+ Returns the URL that was the source of this response.
862
+ If the request that generated this response redirected, this method
863
+ will return the final redirect location.
864
+ """
865
+ if self.retries is not None and len(self.retries.history):
866
+ return self.retries.history[-1].redirect_location
867
+ else:
868
+ return self._request_url
869
+
870
+ def __iter__(self):
871
+ buffer = []
872
+ for chunk in self.stream(decode_content=True):
873
+ if b"\n" in chunk:
874
+ chunk = chunk.split(b"\n")
875
+ yield b"".join(buffer) + chunk[0] + b"\n"
876
+ for x in chunk[1:-1]:
877
+ yield x + b"\n"
878
+ if chunk[-1]:
879
+ buffer = [chunk[-1]]
880
+ else:
881
+ buffer = []
882
+ else:
883
+ buffer.append(chunk)
884
+ if buffer:
885
+ yield b"".join(buffer)