U eb @sddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddlmZddlmZddlmZe jrddlmZz.z ddlZWnek rddlZYnXWnek rdZYnXz:ddlZeee e!d ej"#Z$Z$e$d krdZWn e%ee&fk r8dZYnXdd l'm(Z(dd lm)Z)dd l*m+Z+ddl,m-Z-m.Z.m/Z/ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:ddl;mm?Z?e jrddl m@Z@ddlAmBZBeCeDZEGdddZFGdddeFZGGdddZHGdddeFZIedk r>GdddeFZJedk rXGdddeFZKGd d!d!eFZLd"dd#d$d%ZMGd&d'd'ZNGd(d)d)ejOZPGd*d+d+ePZdS),) annotationsN)contextmanager) HTTPMessage) HTTPResponse)timeout)BaseHTTPConnectionz^([0-9]+)\.([0-9]+))r)util) _TYPE_BODY)HTTPHeaderDict) BaseSSLErrorHTTPConnection HTTPException) BodyNotHttplibCompatible DecodeError HTTPErrorIncompleteReadInvalidChunkLength InvalidHeader ProtocolErrorReadTimeoutErrorResponseNotChunkedSSLError) is_fp_closedis_response_to_head)Retry)Literal)HTTPConnectionPoolc@s*eZdZdddddZddddZdS) ContentDecoderbytesdatareturncCs tdSNNotImplementedErrorselfr"r)>/opt/hc_python/lib/python3.8/site-packages/urllib3/response.py decompressHszContentDecoder.decompressr#cCs tdSr$r%r(r)r)r*flushKszContentDecoder.flushN)__name__ __module__ __qualname__r+r.r)r)r)r*rGsrc@s8eZdZddddZdddddZddd d Zd S) DeflateDecoderNoner,cCsd|_d|_t|_dS)NT) _first_try_datazlib decompressobj_objr-r)r)r*__init__PszDeflateDecoder.__init__r r!c Cs|s|S|js|j|S|j|7_z"|j|}|rFd|_d|_|WStjk rd|_ttj |_z||jWYSd|_XYnXdS)NF)r5r9r+r6r7errorr8 MAX_WBITS)r(r" decompressedr)r)r*r+Us"  zDeflateDecoder.decompresscCs |jSr$r9r.r-r)r)r*r.kszDeflateDecoder.flushNr/r0r1r:r+r.r)r)r)r*r2Osr2c@seZdZdZdZdZdS)GzipDecoderStaterrN)r/r0r1 FIRST_MEMBER OTHER_MEMBERS SWALLOW_DATAr)r)r)r*r@osr@c@s8eZdZddddZdddddZddd d Zd S) GzipDecoderr3r,cCstdtj|_tj|_dSN)r7r8r<r9r@rB_stater-r)r)r*r:vszGzipDecoder.__init__r r!cCst}|jtjks|st|Sz||j|7}Wn<tjk rn|j}tj|_|tj krht|YSYnX|jj }|st|Stj |_t dtj |_qdSrF) bytearrayrHr@rDr r9r+r7r;rC unused_datar8r<)r(r"retprevious_stater)r)r*r+zs   zGzipDecoder.decompresscCs |jSr$r>r-r)r)r*r.szGzipDecoder.flushNr?r)r)r)r*rEusrEc@s(eZdZddddZddddZdS) BrotliDecoderr3r,cCs<t|_t|jdr(t|d|jjnt|d|jjdS)Nr+)brotliZ Decompressorr9hasattrsetattrr+processr-r)r)r*r:s  zBrotliDecoder.__init__r cCst|jdr|jSdS)Nr.r4)rOr9r.r-r)r)r*r.s  zBrotliDecoder.flushN)r/r0r1r:r.r)r)r)r*rMsrMc@s8eZdZddddZdddddZddd d Zd S) ZstdDecoderr3r,cCst|_dSr$)zstdZstdDecompressorr8r9r-r)r)r*r:szZstdDecoder.__init__r r!cCsZ|sdS|j|g}|jjrP|jjrP|jj}t|_||j|qd|S)Nr4) r9r+eofrJrSrTr8appendjoin)r(r"Z data_partsrJr)r)r*r+szZstdDecoder.decompresscCs|j}|jjstd|S)NzZstandard data is incomplete)r9r.rUr)r(rKr)r)r*r.s zZstdDecoder.flushNr?r)r)r)r*rRs rRc@s>eZdZdZdddddZddd d Zddd d d 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. strr3)modesr#cCsdd|dD|_dS)NcSsg|]}t|qSr)) _get_decoderstrip).0mr)r)r* sz)MultiDecoder.__init__..,)split _decoders)r(rZr)r)r*r:szMultiDecoder.__init__r r,cCs|jdSNr)rbr.r-r)r)r*r.szMultiDecoder.flushr!cCst|jD]}||}q |Sr$)reversedrbr+)r(r"dr)r)r*r+s zMultiDecoder.decompressN)r/r0r1__doc__r:r.r+r)r)r)r*rXsrXrY)moder#cCsPd|krt|S|dkrtStdk r4|dkr4tStdk rJ|dkrJtStS)Nr`)gzipx-gzipbrrS)rXrErNrMrSrRr2)rgr)r)r*r[sr[c@sZeZdZdZddddZddddZd dd d d Zdd d ddZd dddZdS)BytesQueueBuffera Memory-efficient bytes buffer To return decoded data in read() and still follow the BufferedIOBase API, we need a buffer to always return the correct amount of bytes. This buffer should be filled using calls to put() Our maximum memory usage is determined by the sum of the size of: * self.buffer, which contains the full data * the largest chunk that we will copy in get() The worst case scenario is a single chunk, in which case we'll make a full copy of the data inside get(). r3r,cCst|_d|_dSrc) collectionsdequebuffer_sizer-r)r)r*r:s zBytesQueueBuffer.__init__intcCs|jSr$)ror-r)r)r*__len__szBytesQueueBuffer.__len__r r!cCs"|j||jt|7_dSr$)rnrVrolenr'r)r)r*puts zBytesQueueBuffer.put)nr#c Cs|dkr dS|jstdn|dkr,tdd}t}||kr||}|j}t|}||kr|d|||d}}|||j||j |8_ qn|||j |8_ ||7}|js8qq8| S)Nrr4zbuffer is emptyzn should be > 0) rn RuntimeError ValueErrorioBytesIOpopleftrrwrite appendleftrogetvalue) r(rtZfetchedrK remainingchunkZ chunk_lengthZ left_chunkZ right_chunkr)r)r*gets0     zBytesQueueBuffer.getcsl|js|jdkstdStdkr2}n0t}|fddttD| }d|_|S)Nrr4rc3s|]}VqdSr$)ry)r]_rnr)r* 'sz+BytesQueueBuffer.get_all..) rnroAssertionErrorrrpoprwrx writelinesranger|)r(resultrKr)rr*get_alls   zBytesQueueBuffer.get_allN) r/r0r1rfr:rqrsrrr)r)r)r*rks rkc @s2eZdZUdddgZedk r&edg7Zedk r8edg7Zddd d d gZeej fZ d e d <edk rle ej f7Z edk re ej f7Z ddddddddddddddZ ddddZeddddZddd d!Zeddd"d#Zejddd$d%d#Zed&dd'd(Zeddd)d*Zejddd+d,d*ZdXd.d/d0d1d2d3ZdYd.d/ddd5d6d7ZdZd.d/dd1d8d9Zd[d.d/d0d1d:d;Zdddd?Zddd@dAZdddBdCZdd/dddDdEdFZdddGdHZdIddJdKdLZ dMddNdOZ!d\dPdddQdRdSZ"dMddTdUZ#dddVdWZ$dS)]BaseHTTPResponserhrideflateNrjrSi-i.i/i3i4ztuple[type[Exception], ...]DECODER_ERROR_CLASSES)headersretries>typing.Mapping[str, str] | typing.Mapping[bytes, bytes] | Nonerp str | Nonebool Retry | Noner3)rstatusversionreasondecode_content request_urlrr#c Cst|tr||_n t||_||_||_||_||_d|_||_||_ d|_ |j dd }dd| dD} d| krd|_ d|_|dS) NFztransfer-encodingcss|]}|VqdSr$)r\)r]encr)r)r*rVsz,BaseHTTPResponse.__init__..r`chunkedT) isinstancer rrrrr_has_decoded_content _request_urlrrrlowerra_decoder) r(rrrrrrrtr_enc encodingsr)r)r*r:<s"  zBaseHTTPResponse.__init__zstr | None | Literal[False]r,cCs|j|jkr|jdSdS)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)rREDIRECT_STATUSESrrr-r)r)r*get_redirect_location]s  z&BaseHTTPResponse.get_redirect_locationr cCs tdSr$r%r-r)r)r*r"iszBaseHTTPResponse.dataz typing.AnycCs|jd}t|S)a Parses the body of the HTTP response as JSON. To use a custom JSON decoder pass the result of :attr:`HTTPResponse.data` to the decoder. This method can raise either `UnicodeDecodeError` or `json.JSONDecodeError`. Read more :ref:`here `. zutf-8)r"decode_jsonloadsr'r)r)r*jsonms zBaseHTTPResponse.jsoncCs tdSr$r%r-r)r)r*urlzszBaseHTTPResponse.urlrr#cCs tdSr$r%r(rr)r)r*r~szBaseHTTPConnection | NonecCs tdSr$r%r-r)r)r* connectionszBaseHTTPResponse.connectioncCs|jSr$)_retriesr-r)r)r*rszBaseHTTPResponse.retries)rr#cCs&|dk r|jr|jdj|_||_dS)N)historyredirect_locationrr)r(rr)r)r*rs int | None bool | Nonetyping.Iterator[bytes]amtrr#cCs tdSr$r%r(rrr)r)r*streamszBaseHTTPResponse.streamFrr cache_contentr#cCs tdSr$r%)r(rrrr)r)r*readszBaseHTTPResponse.readcCs tdSr$r%rr)r)r*read1szBaseHTTPResponse.read1cCs tdSr$r%rr)r)r* read_chunkedszBaseHTTPResponse.read_chunkedcCs tdSr$r%r-r)r)r* release_connszBaseHTTPResponse.release_conncCs tdSr$r%r-r)r)r* drain_connszBaseHTTPResponse.drain_conncCs tdSr$r%r-r)r)r*closeszBaseHTTPResponse.closecsdjdd}jdkr`|jkr2t|_n.d|kr`fdd|dD}|r`t|_dS)z= Set-up the _decoder attribute if necessary. content-encodingrNr`cs"g|]}|jkr|qSr))r\CONTENT_DECODERS)r]er-r)r*r_sz2BaseHTTPResponse._init_decoder..)rrrrrr[ra)r(content_encodingrr)r-r* _init_decoders    zBaseHTTPResponse._init_decoder)r"r flush_decoderr#c Cs|s|jrtd|Sz|jr0|j|}d|_WnF|jk rx}z&|jdd}td|||W5d}~XYnX|r|| 7}|S)zN Decode the data passed in and potentially flush the decoder. _Calling read(decode_content=False) is not supported after read(decode_content=True) was called.TrrzEReceived response with content-encoding: %s, but failed to decode it.N) rrurr+rrrrr_flush_decoder)r(r"rrrrr)r)r*_decodes.   zBaseHTTPResponse._decodecCs |jr|jd|jSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r4)rr+r.r-r)r)r*rszBaseHTTPResponse._flush_decoderrI)br#cCs:|t|}t|dkrdS||dt|<t|SdSrc)rrr)r(rtempr)r)r*readintos  zBaseHTTPResponse.readintor cCstjdtdd|jS)Nz|HTTPResponse.getheaders() is deprecated and will be removed in urllib3 v2.1.0. Instead access HTTPResponse.headers directly.rAcategory stacklevel)warningswarnDeprecationWarningrr-r)r)r* getheaderss zBaseHTTPResponse.getheadersrY)namedefaultr#cCstjdtdd|j||S)NzHTTPResponse.getheader() is deprecated and will be removed in urllib3 v2.1.0. Instead use HTTPResponse.headers.get(name, default).rAr)rrrrr)r(rrr)r)r* getheaders zBaseHTTPResponse.getheadercCs|jSr$)rr-r)r)r*info szBaseHTTPResponse.infocCs|jSr$)rr-r)r)r*geturlszBaseHTTPResponse.geturl)rN)NNF)NN)NN)N)%r/r0r1rrNrSrIOErrorr7r;r__annotations__Z ZstdErrorr:rpropertyr"rrsetterrrrrrrrrrrrrrrrrrr)r)r)r*r-sh       !        rcseZdZdZdVddddd d d d d d ddd d d d ddfdd ZddddZddddZeddddZed dddZ d dddZ ddd d!Z d d"d#d$d%Z e d&dd'd(ZdWd)d*d"d dd+d,d-ZdXd)d*d"d dd+d.d/ZdYd"d0d dd1d2d3ZdZd"d0dd4d5d6Zd[d"d0d8d4d9d:Zd dd;d<Zddd=d>Zed dd?d@ZdddAdBZdddCdDZd ddEdFZdddGdHZd"ddIdJdKZd\d"d0d8d4dLdMZed ddNdOZejdPddQdRdOZdSddTdUZZ S)]ra 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. rNrTr rrprrz_HttplibHTTPResponse | NonezHTTPConnectionPool | NonezHTTPConnection | Nonez_HttplibHTTPMessage | Nonerr3)bodyrrrrpreload_contentroriginal_responsepoolrmsgrenforce_content_lengthrequest_methodr auto_closer#c stj||||||| d| |_||_d|_d|_||_d|_| |_|r\t |t t fr\||_| |_ | |_ t|drx||_d|_|||_t|_|r|js|j|d|_dS)N)rrrrrrrrrr)superr:rr_body_fp_original_response_fp_bytes_readrrrYr _pool _connectionrO chunk_left _init_lengthlength_remainingrk_decoded_bufferr)r(rrrrrrrrrrrrrrrr __class__r)r*r:4s6    zHTTPResponse.__init__r,cCs(|jr |jsdS|j|jd|_dSr$)rr _put_connr-r)r)r*rps zHTTPResponse.release_connc Cs.z |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)rrOSErrorr rr-r)r)r*rws zHTTPResponse.drain_connr cCs"|jr |jS|jr|jddSdS)NT)r)rrrr-r)r)r*r"s  zHTTPResponse.datacCs|jSr$)rr-r)r)r*rszHTTPResponse.connectioncCs t|jSr$)rrr-r)r)r*isclosedszHTTPResponse.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). )rr-r)r)r*tellszHTTPResponse.tellr)rr#cCs|jd}|dk r|jr(tddSz8dd|dD}t|dkrVtd||}Wnt k rxd}YqX|d krd}nd}zt |j }Wnt k rd }YnX|d ksd |krd ksn|d krd }|S)zM Set initial length value for Response content if available. zcontent-lengthNzReceived 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.cSsh|] }t|qSr))rp)r]valr)r)r* sz,HTTPResponse._init_length..r`rz8Content-Length contained multiple unmatching values (%s)r)i0dHEAD) rrrlogwarningrarrrrrvrpr)r(rcontent_lengthlengthslengthrr)r)r*rs8     &zHTTPResponse._init_lengthz$typing.Generator[(None, None, None)]c csxd}z$z dVWntk rF}zt|jdd|W5d}~XYntk r}z*dt |krlt ||t|jdd|W5d}~XYnt k r}zB|j dk r|j dk r|j |j krd}n d|}t|||W5d}~XYn:ttfk r"}ztd|||W5d}~XYnXd}W5|sV|jrD|j|jrV|j|jrr|jrr|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 outz!Response may not contain content.zConnection broken: T)rrrrr SocketTimeoutrrr rYrrexpectedpartialrrr)r( clean_exitrargr)r)r*_error_catchers<        $  zHTTPResponse._error_catcherFr)rrr#cCs|js td}|r||ks2|dkr|jr|j|krtjsBtjdkr|rR|j|St }d}|dksn|dkr|dk rt ||}||8}n|}|j |}|sq| |~q^| S|r|dk r|j|S|jS|dk r|j |S|j 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. iN) ir)rrrr IS_PYOPENSSLsys version_inforrwrxminrrzr|)r(rr c_int_maxrn max_chunk_amt chunk_amtr"r)r)r*_fp_readsD      zHTTPResponse._fp_readc Cs|jdkrdSt|jdd}||s8|j||dnd}|dk r|dkr|s|j|jr|jdk r|jdkrt|j|jn(|r|dkr|r|jt |kr|jW5QRX|r|jt |7_|jdk r|jt |8_|S)z7 Reads `amt` of bytes from the socket. NclosedFrr4r) rgetattrrrrrrrrrr)r(rr fp_closedr"r)r)r* _raw_readOs6      zHTTPResponse._raw_readrrcCs ||dkr|j}|dk rs  zHTTPResponse.flushcCs t|jdS)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)rOrr-r)r)r*rFsz#HTTPResponse.supports_chunked_readscCsx|jdk rdS|jj}|ddd}zt|d|_Wn8tk rr||rdt||dn t ddYnXdS)N;rrrGzResponse ended prematurely) rrrreadlinerarprvrrr)r(liner)r)r*_update_chunk_lengthOs  z!HTTPResponse._update_chunk_length)rr#cCsd}|dkr2|j|j}|}|jdd|_n|jdk rd||jkrd|j|}|j||_|}nN||jkr|j|}|jdd|_|}n |j|j}|jdd|_|S)NrA)r _safe_readr)r(rreturned_chunkr~valuer)r)r* _handle_chunkas&       zHTTPResponse._handle_chunkc cs||jstd|s&td||jrXt|jrX|jW5QRdS|j j dkrrW5QRdS| |j dkrq| |}|j||dd}|rr|Vqr|r|}|r|V|j dk r|j j }|sq|dkrqq|jr|jW5QRXdS)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)rrs )rrrrrrrrrrrr rr$rrr)r(rrr~decodedrr)r)r*rwsL       zHTTPResponse.read_chunkedcCs|jS)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. rr-r)r)r*rszHTTPResponse.urlrYrcCs ||_dSr$r&rr)r)r*rsrccsg}|jddD]n}d|krt|d}d||ddV|ddD]}|dVqJ|drn|dg}q~g}q||q|rd|VdS)NTr r4rrr)rrarWrV)r(rnr~chunksxr)r)r*__iter__s    zHTTPResponse.__iter__)rNrrNTTNNNNNTNNT)N)N)NNF)NN)rN)NN)!r/r0r1rfr:rrrr"rrrrrrrrrrrrrrrr.rr r$rrrr* __classcell__r)r)rr*rs"4<  8C;6I4    Hr)Q __future__rrlrwrrloggingrer typingrr7 contextlibr http.clientrZ_HttplibHTTPMessagerZ_HttplibHTTPResponsesocketrr TYPE_CHECKINGZ_base_connectionrZ brotlicffirN ImportErrorZ zstandardrStuplemaprpsearch __version__groupsZ _zstd_versionAttributeErrorrvrr r _collectionsr rr rr exceptionsrrrrrrrrrrZ util.responserrZ util.retryrrconnectionpoolr getLoggerr/rrr2r@rErMrRrXr[rkrrr)r)r)r*sp             0        Ig