U f @sddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddlmZddlmZddlmZe jrddlmZz.z ddlZWnek rddlZYnXWnek rdZYnXz ddlZWn eeefk r d Z Yn2Xe!e"e#e$d ej%&Z'e'd kr8d Z nd Z dd l(m)Z)ddlm*Z*ddl+m,Z,ddl-m.Z.m/Z/m0Z0ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;ddlZ>ddl?m@Z@e jrddlAmBZBeCeDZEGdddZFGdddeFZGGdddZHGdddeFZIedk r4GdddeFZJe rJGdd d eFZKGd!d"d"eFZLd#dd$d%d&ZMGd'd(d(ZNGd)d*d*ejOZPGd+d,d,ePZdS)-) annotationsN)contextmanager) HTTPMessage) HTTPResponse)timeout)BaseHTTPConnectionFz^([0-9]+)\.([0-9]+))rT)util) _TYPE_BODY)HTTPHeaderDict) BaseSSLErrorHTTPConnection HTTPException) BodyNotHttplibCompatible DecodeError HTTPErrorIncompleteReadInvalidChunkLength InvalidHeader ProtocolErrorReadTimeoutErrorResponseNotChunkedSSLError) is_fp_closedis_response_to_head)Retry)HTTPConnectionPoolc@s*eZdZdddddZddddZdS) ContentDecoderbytesdatareturncCs tdSNNotImplementedErrorselfr!r(A/opt/alt/python38/lib/python3.8/site-packages/urllib3/response.py decompressGszContentDecoder.decompressr"cCs tdSr#r$r'r(r(r)flushJszContentDecoder.flushN)__name__ __module__ __qualname__r*r-r(r(r(r)rFsrc@s8eZdZddddZdddddZddd d Zd S) DeflateDecoderNoner+cCsd|_d|_t|_dS)NT) _first_try_datazlib decompressobj_objr,r(r(r)__init__OszDeflateDecoder.__init__rr c Cs|s|S|js|j|S|j|7_z"|j|}|rFd|_d|_|WStjk rd|_ttj |_z||jWYSd|_XYnXdS)NF)r4r8r*r5r6errorr7 MAX_WBITS)r'r! decompressedr(r(r)r*Ts"  zDeflateDecoder.decompresscCs |jSr#r8r-r,r(r(r)r-jszDeflateDecoder.flushNr.r/r0r9r*r-r(r(r(r)r1Nsr1c@seZdZdZdZdZdS)GzipDecoderStaterrN)r.r/r0 FIRST_MEMBER OTHER_MEMBERS SWALLOW_DATAr(r(r(r)r?nsr?c@s8eZdZddddZdddddZddd d Zd S) GzipDecoderr2r+cCstdtj|_tj|_dSN)r6r7r;r8r?rA_stater,r(r(r)r9uszGzipDecoder.__init__rr cCst}|jtjks|st|Sz||j|7}Wn<tjk rn|j}tj|_|tj krht|YSYnX|jj }|st|Stj |_t dtj |_qdSrE) bytearrayrGr?rCrr8r*r6r:rB unused_datar7r;)r'r!retprevious_stater(r(r)r*ys   zGzipDecoder.decompresscCs |jSr#r=r,r(r(r)r-szGzipDecoder.flushNr>r(r(r(r)rDtsrDc@s(eZdZddddZddddZdS) BrotliDecoderr2r+cCs<t|_t|jdr(t|d|jjnt|d|jjdS)Nr*)brotliZ Decompressorr8hasattrsetattrr*processr,r(r(r)r9s  zBrotliDecoder.__init__rcCst|jdr|jSdS)Nr-r3)rNr8r-r,r(r(r)r-s  zBrotliDecoder.flushN)r.r/r0r9r-r(r(r(r)rLsrLc@s8eZdZddddZdddddZddd d Zd S) ZstdDecoderr2r+cCst|_dSr#)zstdZstdDecompressorr7r8r,r(r(r)r9szZstdDecoder.__init__rr cCsZ|sdS|j|g}|jjrP|jjrP|jj}t|_||j|qd|S)Nr3) r8r*eofrIrRrSr7appendjoin)r'r!Z data_partsrIr(r(r)r*szZstdDecoder.decompresscCs|j}|jjstd|S)NzZstandard data is incomplete)r8r-rTr)r'rJr(r(r)r-s zZstdDecoder.flushNr>r(r(r(r)rQs rQc@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. strr2)modesr"cCsdd|dD|_dS)NcSsg|]}t|qSr() _get_decoderstrip).0mr(r(r) sz)MultiDecoder.__init__..,)split _decoders)r'rYr(r(r)r9szMultiDecoder.__init__rr+cCs|jdSNr)rar-r,r(r(r)r-szMultiDecoder.flushr cCst|jD]}||}q |Sr#)reversedrar*)r'r!dr(r(r)r*s zMultiDecoder.decompressN)r.r/r0__doc__r9r-r*r(r(r(r)rWsrWrX)moder"cCsLd|krt|S|dkrtStdk r4|dkr4tStrF|dkrFtStS)Nr_)gzipx-gzipbrrR)rWrDrMrLHAS_ZSTDrQr1)rfr(r(r)rZs rZc@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(). r2r+cCst|_d|_dSrb) collectionsdequebuffer_sizer,r(r(r)r9s zBytesQueueBuffer.__init__intcCs|jSr#)ror,r(r(r)__len__szBytesQueueBuffer.__len__rr cCs"|j||jt|7_dSr#)rnrUrolenr&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)Nrr3zbuffer is emptyzn should be > 0) rn RuntimeError ValueErrorioBytesIOpopleftrrwrite appendleftrogetvalue) r'rtZfetchedrJ remainingchunkZ chunk_lengthZ left_chunkZ right_chunkr(r(r)gets0     zBytesQueueBuffer.getcsl|js|jdkstdStdkr2}n0t}|fddttD| }d|_|S)Nrr3rc3s|]}VqdSr#)ry)r\_rnr(r) &sz+BytesQueueBuffer.get_all..) rnroAssertionErrorrrpoprwrx writelinesranger|)r'resultrJr(rr)get_alls   zBytesQueueBuffer.get_allN) r.r/r0rer9rqrsrrr(r(r(r)rks rkc @s,eZdZUdddgZedk r&edg7Zer4edg7Zddd d d gZeej fZ d e d <edk rhe ej f7Z erxe e j f7Z ddddddddddddd ddZddddZeddddZd dd!d"Zeddd#d$Zejddd%d&d$Zed'dd(d)Zeddd*d+Zejddd,d-d+ZdXd/d0d1d2d3d4ZdYd/d0ddd6d7d8ZdZd/d0dd2d9d:Zd[d/d0d1d2d;d<Zddd=d>Zddd?d@ZdddAdBZdddCdDZdd0dddEdFdGZdddHdIZ dJddKdLdMZ!dNddOdPZ"d\ddddQdRdSZ#dNddTdUZ$dddVdWZ%dS)]BaseHTTPResponsergrhdeflateNrirRi-i.i/i3i4ztuple[type[Exception], ...]DECODER_ERROR_CLASSES)headersretries>typing.Mapping[str, str] | typing.Mapping[bytes, bytes] | NonerprX str | Nonebool Retry | Noner2) rstatusversionversion_stringreasondecode_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)rWsz,BaseHTTPResponse.__init__..r_chunkedT) isinstancer rrrrrr_has_decoded_content _request_urlrrrlowerr`_decoder) r'rrrrrrrrtr_enc encodingsr(r(r)r9;s$  zBaseHTTPResponse.__init__z"str | None | typing.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_locationrcCs tdSr#r$r,r(r(r)r!jszBaseHTTPResponse.dataz typing.AnycCs|jd}t|S)a Deserializes the body of the HTTP response as a Python object. The body of the HTTP response must be encoded using UTF-8, as per `RFC 8529 Section 8.1 `_. To use a custom JSON decoder pass the result of :attr:`HTTPResponse.data` to your custom decoder instead. If the body of the HTTP response is not decodable to UTF-8, a `UnicodeDecodeError` will be raised. If the body of the HTTP response is not a valid JSON document, a `json.JSONDecodeError` will be raised. Read more :ref:`here `. :returns: The body of the HTTP response as a Python object. zutf-8)r!decode_jsonloadsr&r(r(r)jsonns zBaseHTTPResponse.jsoncCs tdSr#r$r,r(r(r)urlszBaseHTTPResponse.urlrr"cCs tdSr#r$r'rr(r(r)rszBaseHTTPConnection | 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..)rrrrrrZr`)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. r3)rr*r-r,r(r(r)rszBaseHTTPResponse._flush_decoderrH)br"cCs:|t|}t|dkrdS||dt|<t|SdSrb)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.r@category stacklevel)warningswarnDeprecationWarningrr,r(r(r) getheaderss zBaseHTTPResponse.getheaders)namedefaultr"cCstjdtdd|j||S)NzHTTPResponse.getheader() is deprecated and will be removed in urllib3 v2.1.0. Instead use HTTPResponse.headers.get(name, default).r@r)rrrrr)r'rrr(r(r) getheader s zBaseHTTPResponse.getheadercCs|jSr#)rr,r(r(r)infoszBaseHTTPResponse.infocCs|jSr#)rr,r(r(r)geturlszBaseHTTPResponse.geturl)rN)NNF)NN)NN)N)&r.r/r0rrMrjrIOErrorr6r:r__annotations__rRZ ZstdErrorr9rpropertyr!rrsetterrrrrrrrrrrrrrrrrrr(r(r(r)r,sh      "#       rcseZdZdZdWddd d d d d d d ddddd d d d ddfdd ZddddZddddZeddddZeddddZ d dd d!Z d dd"d#Z d d$d%d&d'Z e d(dd)d*ZdXd+d,d$d dd-d.d/ZdYd+d,d$d dd-d0d1ZdZd$d2d dd3d4d5Zd[d$d2dd6d7d8Zd\d$d2d:d6d;d<Zd dd=d>Zddd?d@Zed ddAdBZd ddCdDZdddEdFZd ddGdHZdddIdJZd$ddKdLdMZd]d$d2d:d6dNdOZed ddPdQZejd ddRdSdQZdTddUdVZZ 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. rNrHTTP/?Tr rrprXrrz_HttplibHTTPResponse | NonezHTTPConnectionPool | NonezHTTPConnection | Nonez_HttplibHTTPMessage | Nonerr2)bodyrrrrrpreload_contentroriginal_responsepoolrmsgrenforce_content_lengthrequest_methodr auto_closer"c stj|||||||| d||_||_d|_d|_| |_d|_| |_|r^t |t t fr^||_| |_ | |_ t|drz||_d|_|||_t|_|r|js|j|d|_dS)N)rrrrrrrrrrr)superr9rr_body_fp_original_response_fp_bytes_readrrrXr_pool _connectionrN chunk_left _init_lengthlength_remainingrk_decoded_bufferr)r'rrrrrrrrrrrrrrrrr __class__r(r)r9=s8    zHTTPResponse.__init__r+cCs(|jr |jsdS|j|jd|_dSr#)rr _put_connr,r(r(r)r{s 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)rs zHTTPResponse.drain_connrcCs"|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) rrrlogwarningr`rrrrrvrpr)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 rXrrexpectedpartialrrr)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'rrZ c_int_maxrnZ max_chunk_amtZ chunk_amtr!r(r(r)_fp_read!sD      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. NclosedFr r3r) rgetattrrrrrrrrrr)r'rr fp_closedr!r(r(r) _raw_readZs6      zHTTPResponse._raw_readrrcCs"||dkr|j}|r(|dkr(d}n&|dk rNd}t|j|krN|j|S||}|dkpl|dkol| }|st|jdkr|S|dkr||||}|r||_nx|s|jrt d|S||||}|j |t|j|kr|r||}||||}|j |q|j|}|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.) NrFr) rrrrrrrrrrrurs)r'rrrr!r decoded_datar(r(r)rs>      zHTTPResponse.readrcCs|dkr|j}|r|dkrd}|jr\|s0tdt|jdkr\|dkrP|jS|j|S|dkrhdS|j|dd}|r|dkr|S|| }| |||}|j ||s|rq|jddd}q|dkr|jS|j|S)a Similar to ``http.client.HTTPResponse.read1`` and documented in :meth:`io.BufferedReader.read1`, but with an additional parameter: ``decode_content``. :param amt: How much of the content to read. :param decode_content: If True, will attempt to decode the body based on the 'content-encoding' header. NrzaCalling read1(decode_content=False) is not supported after read1(decode_content=True) was called.r3Tr i ) rrrurrrrrrrrrs)r'rrr!rrr(r(r)rs8      zHTTPResponse.read1rz#typing.Generator[bytes, None, None]ccsZ|jr$|r$|j||dEdHn2t|jr  8C;6L7    Mr)Q __future__rrlrwrrloggingrer typingrr6 contextlibr http.clientrZ_HttplibHTTPMessagerZ_HttplibHTTPResponsesocketrr TYPE_CHECKINGZ_base_connectionrZ brotlicffirM ImportErrorZ zstandardrRAttributeErrorrvrjtuplemaprpsearch __version__groupsZ _zstd_versionrr r _collectionsr rr rr exceptionsrrrrrrrrrrZ util.responserrZ util.retryrconnectionpoolr getLoggerr.rrr1r?rDrLrQrWrZrkrrr(r(r(r)sp              0      Iq