3

Z'íc)xã@s–ddlmZddlZddlZddlZddlZddlZddlmZddl	m
Zddl	mZ
y.yddlZWnek
r‚ddlZYnXWnek
rždZYnXddlmZddlmZdd	lmZmZdd
lmZmZmZmZmZmZmZm Z m!Z!m"Z"ddl#m$Z$ddl%m&Z&m'Z'ej(e)ƒZ*Gd
d„de+ƒZ,Gdd„de+ƒZ-Gdd„de+ƒZ.edk	rhGdd„de+ƒZ/Gdd„de+ƒZ0dd„Z1Gdd„dej2ƒZ3dS)é)Úabsolute_importN)Úcontextmanager)Úerror)Útimeouté)Úutil)ÚHTTPHeaderDict)ÚBaseSSLErrorÚ
HTTPException)
ÚBodyNotHttplibCompatibleÚDecodeErrorÚ	HTTPErrorÚIncompleteReadÚInvalidChunkLengthÚ
InvalidHeaderÚ
ProtocolErrorÚReadTimeoutErrorÚResponseNotChunkedÚSSLError)Úsix)Úis_fp_closedÚis_response_to_headc@s$eZdZdd„Zdd„Zdd„ZdS)ÚDeflateDecodercCsd|_d|_tjƒ|_dS)NTó)Ú
_first_tryÚ_dataÚzlibÚ
decompressobjÚ_obj)Úself©r ú3/tmp/pip-build-gk9425m9/urllib3/urllib3/response.pyÚ__init__*szDeflateDecoder.__init__cCst|j|ƒS)N)Úgetattrr)rÚnamer r r!Ú__getattr__/szDeflateDecoder.__getattr__cCs|s|S|js|jj|ƒS|j|7_y |jj|ƒ}|rFd|_d|_|Stjk
rŠd|_tjtjƒ|_z|j|jƒSd|_XYnXdS)NF)rrÚ
decompressrrrrÚ	MAX_WBITS)rÚdataÚdecompressedr r r!r&2s"zDeflateDecoder.decompressN)Ú__name__Ú
__module__Ú__qualname__r"r%r&r r r r!r)src@seZdZdZdZdZdS)ÚGzipDecoderStaterréN)r*r+r,ÚFIRST_MEMBERÚ
OTHER_MEMBERSÚSWALLOW_DATAr r r r!r-Isr-c@s$eZdZdd„Zdd„Zdd„ZdS)ÚGzipDecodercCstjdtjƒ|_tj|_dS)Né)rrr'rr-r/Ú_state)rr r r!r"QszGzipDecoder.__init__cCst|j|ƒS)N)r#r)rr$r r r!r%UszGzipDecoder.__getattr__cCs¦tƒ}|jtjks|r t|ƒSx€y||jj|ƒ7}Wn8tjk
rn|j}tj|_|tj	krht|ƒS‚YnX|jj
}|s„t|ƒStj	|_tjdtjƒ|_q"WdS)Nr3)
Ú	bytearrayr4r-r1Úbytesrr&rrr0Úunused_datarr')rr(ÚretZprevious_stater r r!r&Xs"
zGzipDecoder.decompressN)r*r+r,r"r%r&r r r r!r2Psr2c@seZdZdd„Zdd„ZdS)Ú
BrotliDecodercCs0tjƒ|_t|jdƒr"|jj|_n
|jj|_dS)Nr&)ÚbrotliZDecompressorrÚhasattrr&Úprocess)rr r r!r"ts
zBrotliDecoder.__init__cCst|jdƒr|jjƒSdS)NÚflushr)r;rr=)rr r r!r={s
zBrotliDecoder.flushN)r*r+r,r"r=r r r r!r9psr9c@s(eZdZdZdd„Zdd„Zdd„ZdS)	ÚMultiDecodera
    From RFC7231:
        If one or more encodings have been applied to a representation, the
        sender that applied the encodings MUST generate a Content-Encoding
        header field that lists the content codings in the order in which
        they were applied.
    cCsdd„|jdƒDƒ|_dS)NcSsg|]}t|jƒƒ‘qSr )Ú_get_decoderÚstrip)Ú.0Úmr r r!ú
<listcomp>‹sz)MultiDecoder.__init__.<locals>.<listcomp>ú,)ÚsplitÚ	_decoders)rÚmodesr r r!r"ŠszMultiDecoder.__init__cCs|jdjƒS)Nr)rFr=)rr r r!r=szMultiDecoder.flushcCs"xt|jƒD]}|j|ƒ}qW|S)N)ÚreversedrFr&)rr(Údr r r!r&szMultiDecoder.decompressN)r*r+r,Ú__doc__r"r=r&r r r r!r>sr>cCs:d|krt|ƒS|dkrtƒStdk	r4|dkr4tƒStƒS)NrDÚgzipÚbr)r>r2r:r9r)Úmoder r r!r?–sr?c@steZdZdZddgZedk	r&edg7Zdddd	d
gZdQdd„Zdd„Zdd„Z	dd„Z
edd„ƒZedd„ƒZ
dd„Zdd„Zdd „Zd!d"„ZeejfZedk	r¬eejf7Zd#d$„Zd%d&„Zed'd(„ƒZd)d*„ZdRd+d,„ZdTd/d0„Zed1d2„ƒZd3d4„ZdUd5d6„Z d7d8„Z!d9d:„Z"ed;d<„ƒZ#d=d>„Z$d?d@„Z%dAdB„Z&dCdD„Z'dEdF„Z(dGdH„Z)dIdJ„Z*dVdKdL„Z+dMdN„Z,dOdP„Z-dS)WÚHTTPResponseaá
    HTTP Response container.

    Backwards-compatible with :class:`http.client.HTTPResponse` but the response ``body`` is
    loaded and decoded on-demand when the ``data`` property is accessed.  This
    class is also compatible with the Python standard library's :mod:`io`
    module, and can hence be treated as a readable object in the context of that
    framework.

    Extra parameters for behaviour not present in :class:`http.client.HTTPResponse`:

    :param preload_content:
        If True, the response's body will be preloaded during construction.

    :param decode_content:
        If True, will attempt to decode the body based on the
        'content-encoding' header.

    :param original_response:
        When this HTTPResponse wrapper is generated from an :class:`http.client.HTTPResponse`
        object, it's convenient to include the original for debug purposes. It's
        otherwise unused.

    :param retries:
        The retries contains the last :class:`~urllib3.util.retry.Retry` that
        was used during the request.

    :param enforce_content_length:
        Enforce content length checking. Body returned by server must match
        value of Content-Length header, if present. Otherwise, raise error.
    rKÚdeflateNrLi-i.i/i3i4ÚrTFcCst|tƒr||_n
t|ƒ|_||_||_||_||_||_|
|_||_	||_
d|_d|_d|_
|	|_d|_||_||_|rt|tjtfƒr||_|
|_||_t|dƒr¬||_
d|_d|_|jjddƒjƒ}dd„|jdƒDƒ}d	|krìd
|_|j|ƒ|_|r|jr|j|d|_dS)NrÚreadFztransfer-encodingrPcss|]}|jƒVqdS)N)r@)rAÚencr r r!ú	<genexpr>sz(HTTPResponse.__init__.<locals>.<genexpr>rDÚchunkedT)Údecode_content) Ú
isinstancerÚheadersÚstatusÚversionÚreasonÚstrictrUÚretriesÚenforce_content_lengthÚ
auto_closeÚ_decoderÚ_bodyÚ_fpÚ_original_responseÚ_fp_bytes_readÚmsgÚ_request_urlrÚstring_typesr6Ú_poolÚ_connectionr;rTÚ
chunk_leftÚgetÚlowerrEÚ_init_lengthÚlength_remainingrQ)rÚbodyrWrXrYrZr[Úpreload_contentrUÚoriginal_responseÚpoolÚ
connectionrdr\r]Úrequest_methodÚrequest_urlr^Útr_encÚ	encodingsr r r!r"ÉsB


zHTTPResponse.__init__cCs|j|jkr|jjdƒSdS)a
        Should we redirect and where to?

        :returns: Truthy redirect location string if we got a redirect status
            code and valid location. ``None`` if redirect status and no
            location. ``False`` if not a redirect status code.
        ÚlocationF)rXÚREDIRECT_STATUSESrWrj)rr r r!Úget_redirect_locationsz"HTTPResponse.get_redirect_locationcCs,|js|jrdS|jj|jƒd|_dS)N)rgrhÚ	_put_conn)rr r r!Úrelease_connszHTTPResponse.release_conncCs.y|jƒWnttttfk
r(YnXdS)zÏ
        Read and discard any remaining HTTP response data in the response connection.

        Unread data in the HTTPResponse connection blocks the connection from being released back to the pool.
        N)rQr
ÚSocketErrorr	r
)rr r r!Ú
drain_conn szHTTPResponse.drain_conncCs"|jr|jS|jr|jddSdS)NT)Ú
cache_content)r`rarQ)rr r r!r(+szHTTPResponse.datacCs|jS)N)rh)rr r r!rr4szHTTPResponse.connectioncCs
t|jƒS)N)rra)rr r r!Úisclosed8szHTTPResponse.isclosedcCs|jS)zì
        Obtain the number of bytes pulled over the wire so far. May differ from
        the amount of content returned by :meth:``urllib3.response.HTTPResponse.read``
        if bytes are encoded on the wire (e.g, compressed).
        )rc)rr r r!Útell;szHTTPResponse.tellcCsâ|jjdƒ}|dk	rŠ|jr(tjdƒdSy<tdd„|jdƒDƒƒ}t|ƒdkrZtd|ƒ‚|j	ƒ}Wnt
k
r|d}YnX|d	krŠd}yt|jƒ}Wnt
k
r°d	}YnX|dksÚd|koÌd
knsÚ|dkrÞd	}|S)zM
        Set initial length value for Response content if available.
        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)rAÚvalr r r!rC]sz-HTTPResponse._init_length.<locals>.<listcomp>rDrz8Content-Length contained multiple unmatching values (%s)réÌé0édéÈÚHEAD)rƒr„)
rWrjrTÚlogÚwarningÚsetrEÚlenrÚpopÚ
ValueErrorrrX)rrsÚlengthÚlengthsrXr r r!rlCs0

(zHTTPResponse._init_lengthcshˆjjddƒjƒ}ˆjdkrd|ˆjkr2t|ƒˆ_n2d|krd‡fdd„|jdƒDƒ}t|ƒrdt|ƒˆ_dS)z=
        Set-up the _decoder attribute if necessary.
        zcontent-encodingrPNrDcs"g|]}|jƒˆjkr|jƒ‘qSr )r@ÚCONTENT_DECODERS)rAÚe)rr r!rCƒsz.HTTPResponse._init_decoder.<locals>.<listcomp>)rWrjrkr_rr?rEr‹)rÚcontent_encodingrvr )rr!Ú
_init_decoderws


zHTTPResponse._init_decodercCsx|s|Sy|jr|jj|ƒ}WnD|jk
rb}z&|jjddƒjƒ}td||ƒ‚WYdd}~XnX|rt||jƒ7}|S)zN
        Decode the data passed in and potentially flush the decoder.
        zcontent-encodingrPzEReceived response with content-encoding: %s, but failed to decode it.N)r_r&ÚDECODER_ERROR_CLASSESrWrjrkrÚ_flush_decoder)rr(rUÚ
flush_decoderr‘r’r r r!Ú_decodeŽszHTTPResponse._decodecCs$|jr |jjdƒ}||jjƒSdS)zk
        Flushes the decoder. Should only be called if the decoder is actually
        being used.
        r)r_r&r=)rÚbufr r r!r•¤szHTTPResponse._flush_decoderccsòd}zªy
dVWn–tk
r2t|jddƒ‚Ynvtk
rt}z(dt|ƒkrVt|ƒ‚t|jddƒ‚WYdd}~Xn4ttfk
r¦}ztd||ƒ‚WYdd}~XnXd}Wd|sÔ|j	rÄ|j	j
ƒ|jrÔ|jj
ƒ|j	rì|j	jƒrì|j
ƒXdS)zç
        Catch low-level python exceptions, instead re-raising urllib3
        variants, so that low-level exceptions are not leaked in the
        high-level api.

        On exit, release the connection back to the pool.
        FNzRead timed out.zread operation timed outzConnection broken: %rT)Ú
SocketTimeoutrrgr	Ústrrr
r|rrbÚcloserhrr{)rÚ
clean_exitr‘r r r!Ú_error_catcher¯s(	
 

zHTTPResponse._error_catchercCsÌ|js
t‚d
}|r||ks*|jrª|j|krªtjrªtjsBtjdkrªtj	ƒ}d}xR|dks`|dkr |dk	r|t
||ƒ}||8}n|}|jj|ƒ}|s’P|j|ƒ~qPW|j
ƒS|dk	r¾|jj|ƒS|jjƒSdS)
a_
        Read a response with the thought that reading the number of bytes
        larger than can fit in a 32-bit int at a time via SSL in some
        known cases leads to an overflow error that has to be prevented
        if `amt` or `self.length_remaining` indicate that a problem may
        happen.

        The known cases:
          * 3.8 <= CPython < 3.9.7 because of a bug
            https://github.com/urllib3/urllib3/issues/2513#issuecomment-1152559900.
          * urllib3 injected with pyOpenSSL-backed SSL-support.
          * CPython < 3.10 only when `amt` does not fit 32-bit int.
        r.éréé
éNrliÿÿÿ)rŸr i)raÚAssertionErrorrmrÚIS_SECURETRANSPORTÚIS_PYOPENSSLÚsysÚversion_infoÚioÚBytesIOÚminrQÚwriteÚgetvalue)rÚamtZ	c_int_maxÚbufferZ
max_chunk_amtZ	chunk_amtr(r r r!Ú_fp_readçs(



zHTTPResponse._fp_readcCsö|jƒ|dkr|j}|jdkr$dSd}t|jddƒ}|jƒd|sN|j|ƒnd}|dkr`d}n>d}|dkrž|rž|jjƒd}|jrž|jdkržt	|j
|jƒ‚WdQRX|rò|j
t|ƒ7_
|jdk	rÚ|jt|ƒ8_|j|||ƒ}|rò||_
|S)aT
        Similar to :meth:`http.client.HTTPResponse.read`, but with two additional
        parameters: ``decode_content`` and ``cache_content``.

        :param amt:
            How much of the content to read. If specified, caching is skipped
            because it doesn't make sense to cache partial content as the full
            response.

        :param decode_content:
            If True, will attempt to decode the body based on the
            'content-encoding' header.

        :param cache_content:
            If True, will save the returned data such that the same result is
            returned despite of the state of the underlying file object. This
            is useful if you want the ``.data`` property to continue working
            after having ``.read()`` the file object. (Overridden if ``amt`` is
            set.)
        NFÚclosedrTr)rN)r“rUrar#rr®r›r]rmrrcr‹r—r`)rr¬rUr~r–Z	fp_closedr(r r r!rQs6

	


zHTTPResponse.readr.r3ccsZ|jr.|jƒr.xF|j||dD]
}|VqWn(x&t|jƒsT|j||d}|r0|Vq0WdS)a_
        A generator wrapper for the read() method. A call will block until
        ``amt`` bytes have been read from the connection or until the
        connection is closed.

        :param amt:
            How much of the content to read. The generator will return up to
            much data per iteration, but may return less. This is particularly
            likely when using compressed data. However, the empty string will
            never be returned.

        :param decode_content:
            If True, will attempt to decode the body based on the
            'content-encoding' header.
        )rU)r¬rUN)rTÚsupports_chunked_readsÚread_chunkedrrarQ)rr¬rUÚliner(r r r!Ústream_szHTTPResponse.streamc
Ksb|j}t|tƒs.tjr"tj|ƒ}nt|jƒƒ}t|ddƒ}|f|||j|j	|j
||dœ|—Ž}|S)a
        Given an :class:`http.client.HTTPResponse` instance ``r``, return a
        corresponding :class:`urllib3.response.HTTPResponse` object.

        Remaining parameters are passed to the HTTPResponse constructor, along
        with ``original_response=r``.
        r[r)rnrWrXrYrZr[rp)rdrVrrÚPY2Úfrom_httplibÚitemsr#rXrYrZ)ÚResponseClsÚrÚresponse_kwrWr[Úrespr r r!rµys 	
zHTTPResponse.from_httplibcCstjdtdd|jS)Nz|HTTPResponse.getheaders() is deprecated and will be removed in urllib3 v2.1.0. Instead access HTTPResponse.headers directly.r.)ÚcategoryÚ
stacklevel)ÚwarningsÚwarnÚDeprecationWarningrW)rr r r!Ú
getheadersšs
zHTTPResponse.getheaderscCstjdtdd|jj||ƒS)Nz‚HTTPResponse.getheader() is deprecated and will be removed in urllib3 v2.1.0. Instead use HTTPResponse.headers.get(name, default).r.)r»r¼)r½r¾r¿rWrj)rr$Údefaultr r r!Ú	getheader£s
zHTTPResponse.getheadercCs|jS)N)rW)rr r r!Úinfo­szHTTPResponse.infocCs6|js|jjƒ|jr |jjƒ|js2tjj|ƒdS)N)r¯rar›rhr^r§ÚIOBase)rr r r!r›±s

zHTTPResponse.closecCsT|jstjjj|ƒS|jdkr"dSt|jdƒr8|jjƒSt|jdƒrL|jjSdSdS)NTrr¯)r^r§rÄr¯Ú__get__rar;r)rr r r!r¯»s

zHTTPResponse.closedcCs6|jdkrtdƒ‚nt|jdƒr*|jjƒStdƒ‚dS)Nz-HTTPResponse has no file to get a fileno fromÚfilenozOThe file-like object this HTTPResponse is wrapped around has no file descriptor)raÚIOErrorr;rÆ)rr r r!rÆÈs


zHTTPResponse.filenocCs4|jdk	r0t|jdƒr0t|jddƒr0|jjƒSdS)Nr=r¯F)rar;r#r=)rr r r!r=Ós
zHTTPResponse.flushcCsdS)NTr )rr r r!ÚreadableÛszHTTPResponse.readablecCs:|jt|ƒƒ}t|ƒdkrdS||dt|ƒ…<t|ƒSdS)Nr)rQr‹)rÚbÚtempr r r!Úreadintoßs
zHTTPResponse.readintocCst|jdƒS)a
        Checks if the underlying file-like object looks like a
        :class:`http.client.HTTPResponse` object. We do this by testing for
        the fp attribute. If it is present we assume it returns raw chunks as
        processed by read_chunked().
        Úfp)r;ra)rr r r!r°èsz#HTTPResponse.supports_chunked_readscCsf|jdk	rdS|jjjƒ}|jddƒd}yt|dƒ|_Wn&tk
r`|jƒt||ƒ‚YnXdS)Nó;rrr3)	rirarÌÚreadlinerErrr›r)rr²r r r!Ú_update_chunk_lengthñs
z!HTTPResponse._update_chunk_lengthcCs¬d}|dkr2|jj|jƒ}|}|jjdƒd|_nv||jkrZ|jj|ƒ}|j||_|}nN||jkrˆ|jj|ƒ}|jjdƒd|_|}n |jj|jƒ}|jjdƒd|_|S)Nr.)raÚ
_safe_readri)rr¬Úreturned_chunkÚchunkÚvaluer r r!Ú
_handle_chunkÿs&

zHTTPResponse._handle_chunkccsò|jƒ|jstdƒ‚|jƒs&tdƒ‚|jƒº|jrNt|jƒrN|jjƒdS|j	j
dkr^dSx<|jƒ|jdkrtP|j
|ƒ}|j||dd}|r`|Vq`W|r²|jƒ}|r²|Vx |j	j
jƒ}|sÆP|dkr´Pq´W|jrä|jjƒWdQRXdS)aÆ
        Similar to :meth:`HTTPResponse.read`, but with an additional
        parameter: ``decode_content``.

        :param amt:
            How much of the content to read. If specified, caching is skipped
            because it doesn't make sense to cache partial content as the full
            response.

        :param decode_content:
            If True, will attempt to decode the body based on the
            'content-encoding' header.
        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.NrF)rUr–s
)r“rTrr°rrrbrr›rarÌrÏrirÔr—r•rÎ)rr¬rUrÒÚdecodedr²r r r!r±sD




zHTTPResponse.read_chunkedcCs.|jdk	r$t|jjƒr$|jjdjS|jSdS)zÄ
        Returns the URL that was the source of this response.
        If the request that generated this response redirected, this method
        will return the final redirect location.
        Nréÿÿÿÿ)r\r‹ÚhistoryÚredirect_locationre)rr r r!Úgeturl[szHTTPResponse.geturlccsœg}x‚|jddD]r}d|krz|jdƒ}dj|ƒ|ddVx|dd…D]}|dVqNW|drt|d	g}q„g}q|j|ƒqW|r˜dj|ƒVdS)
NT)rUó
rrrrÖrÖrÖ)r³rEÚjoinÚappend)rr­rÒÚxr r r!Ú__iter__fs
zHTTPResponse.__iter__)rPNrrNrTTNNNNNFNNT)NNFé)rßN)N)NN).r*r+r,rJrr:rxr"ryr{r}Úpropertyr(rrrr€rlr“rÇrrr”r—r•rrr®rQr³ÚclassmethodrµrÀrÂrÃr›r¯rÆr=rÈrËr°rÏrÔr±rÙrÞr r r r!rN£sp

1
	4
80
H
!	



		
FrN)4Ú
__future__rr§Úloggingr¥r½rÚ
contextlibrÚsocketrr|rr™Z
brotlicffir:ÚImportErrorrPrÚ_collectionsrrrr	r
Ú
exceptionsrrr
rrrrrrrÚpackagesrZ
util.responserrÚ	getLoggerr*rˆÚobjectrr-r2r9r>r?rÄrNr r r r!Ú<module>s<
0