/ lib / urllib3 / response.pyc
response.pyc
  1  o

  2  ��Ycv�@s�ddlmZddlZddlZddlZddlZddlmZddlm	Z
  3  ddlmZzzddl
ZWn
ey<ddlZYnwWneyIdZYnwddlmZddlmZdd	lmZmZdd
  4  lmZmZmZmZmZmZmZmZm Z m!Z!ddl"m#Z#ddl$m%Z%m&Z&e�'e(�Z)Gd
d�de*�Z+Gdd�de*�Z,Gdd�de*�Z-edur�Gdd�de*�Z.Gdd�de*�Z/dd�Z0Gdd�dej1�Z2dS)�)�absolute_importN)�contextmanager)�error)�timeout�)�util)�HTTPHeaderDict)�BaseSSLError�
HTTPException)
  5  �BodyNotHttplibCompatible�DecodeError�	HTTPError�IncompleteRead�InvalidChunkLength�
InvalidHeader�
ProtocolError�ReadTimeoutError�ResponseNotChunked�SSLError)�six)�is_fp_closed�is_response_to_headc@�$eZdZdd�Zdd�Zdd�ZdS)�DeflateDecodercCsd|_d|_t��|_dS)NT�)�
  6  _first_try�_data�zlib�
decompressobj�_obj��self�r"��C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\response.py�__init__)szDeflateDecoder.__init__cC�t|j|�S�N��getattrr�r!�namer"r"r#�__getattr__.�zDeflateDecoder.__getattr__cCs�|s|S|js
|j�|�S|j|7_z|j�|�}|r#d|_d|_|WStjyId|_t�tj�|_z|�|j�Wd|_YSd|_ww)NF)rr�
  7  decompressrrrr�	MAX_WBITS)r!�dataZdecompressedr"r"r#r-1s$�zDeflateDecoder.decompressN��__name__�
  8  __module__�__qualname__r$r+r-r"r"r"r#r(src@seZdZdZdZdZdS)�GzipDecoderStaterr�N)r1r2r3�FIRST_MEMBER�
OTHER_MEMBERS�SWALLOW_DATAr"r"r"r#r4Hsr4c@r)�GzipDecodercCst�dtj�|_tj|_dS)N�)rrr.rr4r6�_stater r"r"r#r$PszGzipDecoder.__init__cCr%r&r'r)r"r"r#r+Tr,zGzipDecoder.__getattr__cCs�t�}|jtjks|st|�S	z
  9  ||j�|�7}Wntjy5|j}tj|_|tj	kr4t|�YS�w|jj
 10  }|s@t|�Stj	|_t�dtj�|_q)NTr:)
�	bytearrayr;r4r8�bytesrr-rrr7�unused_datarr.)r!r/�retZprevious_stater"r"r#r-Ws&
 11  ��zGzipDecoder.decompressNr0r"r"r"r#r9Osr9c@seZdZdd�Zdd�ZdS)�
BrotliDecodercCs2t��|_t|jd�r|jj|_dS|jj|_dS)Nr-)�brotliZDecompressorr�hasattrr-�processr r"r"r#r$ss
 12  zBrotliDecoder.__init__cCst|jd�r|j��SdS)N�flushr)rBrrDr r"r"r#rDzs
 13  zBrotliDecoder.flushN)r1r2r3r$rDr"r"r"r#r@osr@c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�MultiDecodera
 14      From RFC7231:
 15          If one or more encodings have been applied to a representation, the
 16          sender that applied the encodings MUST generate a Content-Encoding
 17          header field that lists the content codings in the order in which
 18          they were applied.
 19      cCsdd�|�d�D�|_dS)NcSsg|]}t|����qSr")�_get_decoder�strip)�.0�mr"r"r#�
 20  <listcomp>�sz)MultiDecoder.__init__.<locals>.<listcomp>�,)�split�	_decoders)r!�modesr"r"r#r$�szMultiDecoder.__init__cCs|jd��S�Nr)rMrDr r"r"r#rD��zMultiDecoder.flushcCst|j�D]}|�|�}q|Sr&)�reversedrMr-)r!r/�dr"r"r#r-�szMultiDecoder.decompressN)r1r2r3�__doc__r$rDr-r"r"r"r#rE�s
 21  rEcCs:d|vrt|�S|dkrt�Stdur|dkrt�St�S)NrK�gzip�br)rEr9rAr@r)�moder"r"r#rF�srFc@s�eZdZdZddgZeduredg7Zgd�Z																
 22  				dLdd�Zd
d�Zdd�Z	dd�Z
 23  edd��Zedd��Z
dd�Zdd�Zdd�Zdd�ZeejfZedurdeejf7Zdd �Zd!d"�Zed#d$��Zd%d&�ZdMd'd(�ZdNd*d+�Zed,d-��Zd.d/�ZdOd0d1�Z d2d3�Z!d4d5�Z"ed6d7��Z#d8d9�Z$d:d;�Z%d<d=�Z&d>d?�Z'd@dA�Z(dBdC�Z)dDdE�Z*dPdFdG�Z+dHdI�Z,dJdK�Z-dS)Q�HTTPResponsea�
 24      HTTP Response container.
 25  
 26      Backwards-compatible with :class:`http.client.HTTPResponse` but the response ``body`` is
 27      loaded and decoded on-demand when the ``data`` property is accessed.  This
 28      class is also compatible with the Python standard library's :mod:`io`
 29      module, and can hence be treated as a readable object in the context of that
 30      framework.
 31  
 32      Extra parameters for behaviour not present in :class:`http.client.HTTPResponse`:
 33  
 34      :param preload_content:
 35          If True, the response's body will be preloaded during construction.
 36  
 37      :param decode_content:
 38          If True, will attempt to decode the body based on the
 39          'content-encoding' header.
 40  
 41      :param original_response:
 42          When this HTTPResponse wrapper is generated from an :class:`http.client.HTTPResponse`
 43          object, it's convenient to include the original for debug purposes. It's
 44          otherwise unused.
 45  
 46      :param retries:
 47          The retries contains the last :class:`~urllib3.util.retry.Retry` that
 48          was used during the request.
 49  
 50      :param enforce_content_length:
 51          Enforce content length checking. Body returned by server must match
 52          value of Content-Length header, if present. Otherwise, raise error.
 53      rT�deflateNrU)i-i.i/i3i4�rTFcCst|t�r	||_nt|�|_||_||_||_||_||_|
|_||_	||_
 54  d|_d|_d|_
|	|_d|_||_||_|rHt|tjtf�rH||_|
 55  |_||_t|d�rV||_
d|_d|_|j�dd���}dd�|�d�D�}d	|vrvd
 56  |_|�|�|_|r�|js�|j|d�|_dSdSdS)Nr�readFztransfer-encodingrYcss�|]}|��VqdSr&)rG)rH�encr"r"r#�	<genexpr>s�z(HTTPResponse.__init__.<locals>.<genexpr>rK�chunkedT��decode_content) �
 57  isinstancer�headers�status�version�reason�strictr_�retries�enforce_content_length�
 58  auto_close�_decoder�_body�_fp�_original_response�_fp_bytes_read�msg�_request_urlr�string_typesr=�_pool�_connectionrBr]�
 59  chunk_left�get�lowerrL�_init_length�length_remainingrZ)r!�bodyrarbrcrdre�preload_contentr_�original_response�pool�
 60  connectionrnrfrg�request_method�request_urlrh�tr_enc�	encodingsr"r"r#r$�sD
 61  
 62  
 63  
 64  �zHTTPResponse.__init__cCs|j|jvr|j�d�SdS)a
 65          Should we redirect and where to?
 66  
 67          :returns: Truthy redirect location string if we got a redirect status
 68              code and valid location. ``None`` if redirect status and no
 69              location. ``False`` if not a redirect status code.
 70          �locationF)rb�REDIRECT_STATUSESrartr r"r"r#�get_redirect_locationsz"HTTPResponse.get_redirect_locationcCs(|jr|jsdS|j�|j�d|_dSr&)rqrr�	_put_connr r"r"r#�release_conns
 71  zHTTPResponse.release_conncCs,z|��WdSttttfyYdSw)z�
 72          Read and discard any remaining HTTP response data in the response connection.
 73  
 74          Unread data in the HTTPResponse connection blocks the connection from being released back to the pool.
 75          N)rZr
�SocketErrorr	r
 76  r r"r"r#�
 77  drain_conns
 78  �zHTTPResponse.drain_conncCs"|jr|jS|jr|jdd�SdS)NT)�
cache_content)rjrkrZr r"r"r#r/*s
 79  �zHTTPResponse.datacC�|jSr&)rrr r"r"r#r|3szHTTPResponse.connectioncCs
 80  t|j�Sr&)rrkr r"r"r#�isclosed7s
 81  zHTTPResponse.isclosedcCr�)z�
 82          Obtain the number of bytes pulled over the wire so far. May differ from
 83          the amount of content returned by :meth:``urllib3.response.HTTPResponse.read``
 84          if bytes are encoded on the wire (e.g, compressed).
 85          )rmr r"r"r#�tell:szHTTPResponse.tellcCs�|j�d�}|durD|jrt�d�dSztdd�|�d�D��}t|�dkr-td|��|�	�}Wnt
 86  y=d}Ynw|d	krDd}zt|j�}Wnt
 87  yVd	}Ynw|d
 88  vsjd|kredksjn|d
krld	}|S)zM
 89          Set initial length value for Response content if available.
 90          zcontent-lengthNz�Received response with both Content-Length and Transfer-Encoding set. This is expressly forbidden by RFC 7230 sec 3.3.2. Ignoring Content-Length and attempting to process response as Transfer-Encoding: chunked.cSsg|]}t|��qSr")�int)rH�valr"r"r#rJ\sz-HTTPResponse._init_length.<locals>.<listcomp>rKrz8Content-Length contained multiple unmatching values (%s)r)��i0�d���HEAD)
rartr]�log�warning�setrL�lenr�pop�
 91  ValueErrorr�rb)r!r}�length�lengthsrbr"r"r#rvBs:�����&zHTTPResponse._init_lengthcsv�j�dd���}�jdur5|�jvrt|��_dSd|vr7�fdd�|�d�D�}t|�r9t|��_dSdSdSdS)z=
 92          Set-up the _decoder attribute if necessary.
 93          �content-encodingrYNrKcs"g|]
}|���jvr|���qSr")rG�CONTENT_DECODERS)rH�er r"r#rJ�s
 94  �z.HTTPResponse._init_decoder.<locals>.<listcomp>)rartrurir�rFrLr�)r!�content_encodingr�r"r r#�
_init_decodervs
 95  
 96  
 97  ��zHTTPResponse._init_decoderc
 98  Csn|s|Sz|jr|j�|�}Wn|jy,}z|j�dd���}td||��d}~ww|r5||��7}|S)zN
 99          Decode the data passed in and potentially flush the decoder.
100          r�rYzEReceived response with content-encoding: %s, but failed to decode it.N)rir-�DECODER_ERROR_CLASSESrartrur�_flush_decoder)r!r/r_�
flush_decoderr�r�r"r"r#�_decode�s&�����zHTTPResponse._decodecCs$|jr|j�d�}||j��SdS)zk
101          Flushes the decoder. Should only be called if the decoder is actually
102          being used.
103          r)rir-rD)r!�bufr"r"r#r��szHTTPResponse._flush_decoderc
104  cs �d}zkzdVWn>tyt|jdd��ty3}zdt|�vr(t|��t|jdd��d}~wttfyG}ztd||��d}~wwd}W|s]|j	rU|j	�
105  �|jr]|j�
106  �|j	rk|j	��rm|�
�dSdSdS|s�|j	ry|j	�
107  �|jr�|j�
108  �|j	r�|j	��r�|�
�www)z�
109          Catch low-level python exceptions, instead re-raising urllib3
110          variants, so that low-level exceptions are not leaked in the
111          high-level api.
112  
113          On exit, release the connection back to the pool.
114          FNzRead timed out.zread operation timed outzConnection broken: %rT)�
SocketTimeoutrrqr	�strrr
115  r�rrl�closerrr�r�)r!�
116  clean_exitr�r"r"r#�_error_catcher�sB�	
117  ���
118  
119  ��
120  
121  
122  �zHTTPResponse._error_catchercCs�|jsJ�d}|r
||ks|jr]|j|kr]tjs]tjs tjdkr]t��}d}|dus.|dkrY|dur<t	||�}||8}n|}|j�
123  |�}|sK	|��S|�|�~|dus.|dks.|��S|durg|j�
124  |�S|j�
125  �S)a_
126          Read a response with the thought that reading the number of bytes
127          larger than can fit in a 32-bit int at a time via SSL in some
128          known cases leads to an overflow error that has to be prevented
129          if `amt` or `self.length_remaining` indicate that a problem may
130          happen.
131  
132          The known cases:
133            * 3.8 <= CPython < 3.9.7 because of a bug
134              https://github.com/urllib3/urllib3/issues/2513#issuecomment-1152559900.
135            * urllib3 injected with pyOpenSSL-backed SSL-support.
136            * CPython < 3.10 only when `amt` does not fit 32-bit int.
137          i���)��
138  iNr)
rkrwr�IS_SECURETRANSPORT�IS_PYOPENSSL�sys�version_info�io�BytesIO�minrZ�write�getvalue)r!�amtZ	c_int_max�bufferZ
max_chunk_amtZ	chunk_amtr/r"r"r#�_fp_read�s:
139  ��
140  ��
141  
142  
143  
144  ��zHTTPResponse._fp_readcCs|��|dur|j}|jdurdSd}t|jdd�}|���6|s'|�|�nd}|dur0d}nd}|dkrN|sN|j��d}|jrN|jdvrNt	|j
145  |j��Wd�n1sXwY|r�|j
146  t|�7_
147  |jdurv|jt|�8_|�|||�}|r�||_
|S)aT
148          Similar to :meth:`http.client.HTTPResponse.read`, but with two additional
149          parameters: ``decode_content`` and ``cache_content``.
150  
151          :param amt:
152              How much of the content to read. If specified, caching is skipped
153              because it doesn't make sense to cache partial content as the full
154              response.
155  
156          :param decode_content:
157              If True, will attempt to decode the body based on the
158              'content-encoding' header.
159  
160          :param cache_content:
161              If True, will save the returned data such that the same result is
162              returned despite of the state of the underlying file object. This
163              is useful if you want the ``.data`` property to continue working
164              after having ``.read()`` the file object. (Overridden if ``amt`` is
165              set.)
166          NF�closedrTr)rN)r�r_rkr(r�r�r�rgrwrrmr�r�rj)r!r�r_r�r�Z	fp_closedr/r"r"r#rZs6
167  
168  
169  		��
170  zHTTPResponse.read�ccsb�|jr|��r|j||d�D]}|VqdSt|j�s/|j||d�}|r(|Vt|j�rdSdS)a_
171          A generator wrapper for the read() method. A call will block until
172          ``amt`` bytes have been read from the connection or until the
173          connection is closed.
174  
175          :param amt:
176              How much of the content to read. The generator will return up to
177              much data per iteration, but may return less. This is particularly
178              likely when using compressed data. However, the empty string will
179              never be returned.
180  
181          :param decode_content:
182              If True, will attempt to decode the body based on the
183              'content-encoding' header.
184          r^)r�r_N)r]�supports_chunked_reads�read_chunkedrrkrZ)r!r�r_�liner/r"r"r#�stream^s��
185  �zHTTPResponse.streamc
186  Ksb|j}t|t�stjrt�|�}nt|���}t|dd�}|d|||j|j	|j
187  ||d�|��}|S)a
188          Given an :class:`http.client.HTTPResponse` instance ``r``, return a
189          corresponding :class:`urllib3.response.HTTPResponse` object.
190  
191          Remaining parameters are passed to the HTTPResponse constructor, along
192          with ``original_response=r``.
193          rer)rxrarbrcrdrerzNr")rnr`rr�PY2�from_httplib�itemsr(rbrcrd)�ResponseCls�r�response_kwrare�respr"r"r#r�xs$	
194  ��
195  zHTTPResponse.from_httplibcCr�r&�rar r"r"r#�
196  getheaders��zHTTPResponse.getheaderscCs|j�||�Sr&)rart)r!r*�defaultr"r"r#�	getheader�rPzHTTPResponse.getheadercCr�r&r�r r"r"r#�info�r�zHTTPResponse.infocCs:|js|j��|jr|j��|jstj�|�dSdSr&)r�rkr�rrrhr��IOBaser r"r"r#r��s
197  
198  �zHTTPResponse.closecCsP|js
199  tjj�|�S|jdurdSt|jd�r|j��St|jd�r&|jjSdS)NTr�r�)rhr�r�r��__get__rkrBr�r r"r"r#r��s
200  
201  zHTTPResponse.closedcCs0|jdur	td��t|jd�r|j��Std��)Nz-HTTPResponse has no file to get a fileno from�filenozOThe file-like object this HTTPResponse is wrapped around has no file descriptor)rk�IOErrorrBr�r r"r"r#r��s
202  
203  �zHTTPResponse.filenocCs:|jdurt|jd�rt|jdd�s|j��SdSdSdS)NrDr�F)rkrBr(rDr r"r"r#rD�s
204  
205  ��
206  �zHTTPResponse.flushcCsdS)NTr"r r"r"r#�readable�szHTTPResponse.readablecCs6|�t|��}t|�dkrdS||dt|��<t|�SrO)rZr�)r!�b�tempr"r"r#�readinto�s
207  zHTTPResponse.readintocCst|jd�S)a
208          Checks if the underlying file-like object looks like a
209          :class:`http.client.HTTPResponse` object. We do this by testing for
210          the fp attribute. If it is present we assume it returns raw chunks as
211          processed by read_chunked().
212          �fp)rBrkr r"r"r#r��sz#HTTPResponse.supports_chunked_readscCs^|jdurdS|jj��}|�dd�d}z	t|d�|_WdSty.|��t||��w)N�;rrr:)	rsrkr��readlinerLr�r�r�r)r!r�r"r"r#�_update_chunk_length�s
213  
214  �z!HTTPResponse._update_chunk_lengthcCs�d}|dur|j�|j�}|}|j�d�d|_|S||jkr/|j�|�}|j||_|}|S||jkrG|j�|�}|j�d�d|_|}|S|j�|j�}|j�d�d|_|S)Nr5)rk�
215  _safe_readrs)r!r�Zreturned_chunk�chunk�valuer"r"r#�
_handle_chunk�s,
216  �
217  
218  ��zHTTPResponse._handle_chunkccs2�|��|jstd��|��std��|���w|jr/t|j�r/|j��	Wd�dS|j	j
219  dur>	Wd�dS	|��|jdkrIn|�
|�}|j||dd�}|r[|Vq?|rg|��}|rg|V	|j	j
220  ��}|sqn|dkrvnqh|jr�|j��Wd�dSWd�dS1s�wYdS)	a�
221          Similar to :meth:`HTTPResponse.read`, but with an additional
222          parameter: ``decode_content``.
223  
224          :param amt:
225              How much of the content to read. If specified, caching is skipped
226              because it doesn't make sense to cache partial content as the full
227              response.
228  
229          :param decode_content:
230              If True, will attempt to decode the body based on the
231              'content-encoding' header.
232          zHResponse is not chunked. Header 'transfer-encoding: chunked' is missing.zkBody should be http.client.HTTPResponse like. It should have have an fp attribute which returns raw chunks.NTrF)r_r�s
233  )r�r]rr�rr�rlrr�rkr�r�rsr�r�r�r�)r!r�r_r��decodedr�r"r"r#r�sZ���
234  
235  ��
236  
237  ���	�("�zHTTPResponse.read_chunkedcCs*|jdurt|jj�r|jjdjS|jS)z�
238          Returns the URL that was the source of this response.
239          If the request that generated this response redirected, this method
240          will return the final redirect location.
241          N�����)rfr��history�redirect_locationror r"r"r#�geturlNszHTTPResponse.geturlccs��g}|jdd�D]7}d|vr;|�d�}d�|�|ddV|dd�D]}|dVq&|dr8|dg}q	g}q	|�|�q	|rKd�|�VdSdS)NTr^�
242  rrrr�)r�rL�join�append)r!r�r��xr"r"r#�__iter__Ys�
243  �zHTTPResponse.__iter__)rYNrrNrTTNNNNNFNNT)NNF)r�Nr&)NN).r1r2r3rSr�rAr�r$r�r�r��propertyr/r|r�r�rvr�r�rrr�r�r�rr�r�rZr��classmethodr�r�r�r�r�r�r�rDr�r�r�r�r�r�r�r�r"r"r"r#rW�s~ 
244  
245  �C

246  
247  4
248  
249  7
250  0
251  H
252   
253  
254  
255  		
256  FrW)3�
257  __future__rr��loggingr�r�
258  contextlibr�socketrr�rr��
259  brotlicffirA�ImportErrorrYr�_collectionsrr|r	r
260261  exceptionsrrr
rrrrrrr�packagesr�
util.responserr�	getLoggerr1r��objectrr4r9r@rErFr�rWr"r"r"r#�<module>sB���0
262