o |nh @sddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddlmZddlmZddlmZe jrOddlmZzzddlZWn eycddlZYnwWn eypdZYnwdd lmZdd lmZdd lm Z dd l!m"Z"m#Z#m$Z$dd l%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ddl0m1Z1m2Z2ddl3m4Z4e jrddl5m6Z6e7e8Z9GdddZ:Gddde:Z;GdddZzddl?m@Z@dZAGddde:ZBWnAey>zddlCZ@eDeEeFeGde@jHIZJeJd kr!ed!WneKeeLfy1d"ZAYn wdZAGd#dde:ZBYnwGd$d%d%e:ZMd1d)d*ZNGd+d,d,ZOGd-d.d.ejPZQGd/d0d0eQZdS)2) annotationsN)contextmanager) HTTPMessage) HTTPResponse)timeout)BaseHTTPConnection)util) _TYPE_BODY)HTTPHeaderDict) BaseSSLErrorHTTPConnection HTTPException) BodyNotHttplibCompatible DecodeError HTTPErrorIncompleteReadInvalidChunkLength InvalidHeader ProtocolErrorReadTimeoutErrorResponseNotChunkedSSLError) is_fp_closedis_response_to_head)Retry)HTTPConnectionPoolc@s eZdZd ddZd ddZdS) ContentDecoderdatabytesreturncCtNNotImplementedErrorselfrr'H/usr/local/bin/dhwp/env/lib/python3.10/site-packages/urllib3/response.py decompress7zContentDecoder.decompresscCr!r"r#r&r'r'r(flush:r*zContentDecoder.flushNrrr rr r)__name__ __module__ __qualname__r)r,r'r'r'r(r6s rc@*eZdZd ddZd ddZdd d Zd S)DeflateDecoderr NonecCsd|_d|_t|_dS)NT) _first_try_datazlib decompressobj_objr+r'r'r(__init__?szDeflateDecoder.__init__rrc Cs|s|S|js |j|S|j|7_z|j|}|r#d|_d|_|WStjyId|_ttj |_z ||jWd|_YSd|_ww)NF)r6r:r)r7r8errorr9 MAX_WBITS)r&r decompressedr'r'r(r)Ds$   zDeflateDecoder.decompresscC |jSr"r:r,r+r'r'r(r,Z zDeflateDecoder.flushNr r4r-r.r/r0r1r;r)r,r'r'r'r(r3>s  r3c@seZdZdZdZdZdS)GzipDecoderStaterrN)r/r0r1 FIRST_MEMBER OTHER_MEMBERS SWALLOW_DATAr'r'r'r(rD^srDc@r2) GzipDecoderr r4cCstdtj|_tj|_dS)N)r8r9r=r:rDrF_stater+r'r'r(r;es zGzipDecoder.__init__rrcCst}|jtjks |st|S z ||j|7}Wntjy5|j}tj|_|tj kr4t|YSw|jj }|s@t|Stj |_t dtj |_q)NTrJ) bytearrayrKrDrHrr:r)r8r<rG unused_datar9r=)r&rretprevious_stater'r'r(r)is&  zGzipDecoder.decompresscCr?r"r@r+r'r'r(r,~rAzGzipDecoder.flushNrBr-r.rCr'r'r'r(rIds  rIc@s eZdZd ddZd ddZdS) BrotliDecoderr r4cCs>t|_t|jdrt|d|jjdSt|d|jjdS)Nr))brotli Decompressorr:hasattrsetattrr)processr+r'r'r(r;s  zBrotliDecoder.__init__rcCst|jdr |jSdS)Nr,r5)rSr:r,r+r'r'r(r,s  zBrotliDecoder.flushNrBr.)r/r0r1r;r,r'r'r'r(rPs rP)zstdTc@r2) ZstdDecoderr r4cCst|_dSr")rVZstdDecompressorr:r+r'r'r(r;ZstdDecoder.__init__rrcCsd|sdS|j|g}|jjr-|jjr-|jj}t|_||j||jjr-|jjsd|SNr5)r:r)eofrMrVrXappendjoinr&r data_partsrMr'r'r(r)s  ZstdDecoder.decompresscCs|jjstddS)NZstandard data is incompleter5)r:r\rr+r'r'r(r,sZstdDecoder.flushNrBr-r.rCr'r'r'r(rW   rWz^([0-9]+)\.([0-9]+))rz!zstandard module doesn't have eofFc@r2)rWr r4cCst|_dSr")rVrXr9r:r+r'r'r(r;srZrrcCsh|sdS|j|g}|jjr/|jjr/|jj}t|_||j||jjr/|jjsd|Sr[) r:r)r\rMrVrXr9r]r^r_r'r'r(r)s racCs|j}|jjs td|S)Nrb)r:r,r\r)r&rNr'r'r(r,s rcNrBr-r.rCr'r'r'r(rWrdc@s.eZdZdZdddZdd d Zdd 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. modesstrr r4cCsdd|dD|_dS)NcSsg|]}t|qSr') _get_decoderstrip).0mr'r'r( sz)MultiDecoder.__init__..,)split _decoders)r&rgr'r'r(r;szMultiDecoder.__init__rcCs|jdSNr)rpr,r+r'r'r(r,rYzMultiDecoder.flushrcCst|jD]}||}q|Sr")reversedrpr))r&rdr'r'r(r)s zMultiDecoder.decompressN)rgrhr r4r.r-)r/r0r1__doc__r;r,r)r'r'r'r(rfs   rfmoderhr cCsLd|vrt|S|dvrtStdur|dkrtStr#|dkr#tStS)Nrn)gzipx-gzipbrrV)rfrIrQrPHAS_ZSTDrWr3)rur'r'r(ris ric@sBeZdZdZdddZdddZdd d ZdddZdddZdS)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(). r r4cCst|_d|_dSrq) collectionsdequebuffer_sizer+r'r'r(r;s  zBytesQueueBuffer.__init__intcC|jSr")r~r+r'r'r(__len__r*zBytesQueueBuffer.__len__rrcCs"|j||jt|7_dSr")r}r]r~lenr%r'r'r(puts zBytesQueueBuffer.putnc Cs|dkrdS|js td|dkrtdd}t}||krp||}|j}t|}||krT|d|||d}}|||j||j |8_ | S|||j |8_ ||7}|jsl | S||ks| S)Nrr5zbuffer is emptyzn should be > 0) r} RuntimeError ValueErrorioBytesIOpopleftrwrite appendleftr~getvalue) r&rfetchedrN remainingchunk chunk_length left_chunk right_chunkr'r'r(gets6    zBytesQueueBuffer.getcsl|js|jdks JdStdkr}nt}|fddttD|}d|_|S)Nrr5rc3s|]}VqdSr")r)rk_r}r'r( @z+BytesQueueBuffer.get_all..) r}r~rpoprr writelinesranger)r&resultrNr'rr(get_all7s   zBytesQueueBuffer.get_allNrBr r)rrr r4)rrr rr.) r/r0r1rtr;rrrrr'r'r'r(rzs    rzc@seZdZUgdZeduredg7Zeredg7ZgdZeej fZ de d<edur0e ej f7Z er8e e j f7Z dddd\ddZd]ddZed^dd Zd_d"d#Zed`d$d%Zejdad'd%Zedbd)d*Zedcd+d,Zejddd-d,Z dedfd3d4Z   5dgdhd7d8Z  didjd9d:Z  didfd;d<Zdkd=d>Zdkd?d@ZdkdAdBZdkdCdDZdkdEdFZdldIdJZ d^dKdLZ!dmdOdPZ"dndRdSZ#dodpdVdWZ$dndXdYZ%d`dZd[Z&dS)qBaseHTTPResponse)rvrwdeflateNrxrV)i-i.i/i3i4ztuple[type[Exception], ...]DECODER_ERROR_CLASSES)headersretriesr>typing.Mapping[str, str] | typing.Mapping[bytes, bytes] | Nonestatusrversionversion_stringrhreason str | Nonedecode_contentbool request_urlr Retry | Noner r4c Cst|tr ||_nt||_||_||_||_||_||_d|_||_ ||_ d|_ |j dd } dd| dD} d| vrCd|_ d|_|dS) NFztransfer-encodingcss|]}|VqdSr")rj)rkencr'r'r(rqrz,BaseHTTPResponse.__init__..rnchunkedT) isinstancer rrrrrr_has_decoded_content _request_urlrrrlowerro_decoder) r&rrrrrrrrtr_enc encodingsr'r'r(r;Us$  zBaseHTTPResponse.__init__"str | None | typing.Literal[False]cCs|j|jvr |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_locationxs  z&BaseHTTPResponse.get_redirect_locationrcCr!r"r#r+r'r'r(rzBaseHTTPResponse.data 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)rdecode_jsonloadsr%r'r'r(jsons  zBaseHTTPResponse.jsoncCr!r"r#r+r'r'r(urlrzBaseHTTPResponse.urlrcCr!r"r#r&rr'r'r(rrBaseHTTPConnection | NonecCr!r"r#r+r'r'r( connectionrzBaseHTTPResponse.connectioncCrr")_retriesr+r'r'r(rrzBaseHTTPResponse.retriescCs&|dur|jr|jdj|_||_dS)N)historyredirect_locationrr)r&rr'r'r(rs amt int | None bool | Nonetyping.Iterator[bytes]cCr!r"r#r&rrr'r'r(streamszBaseHTTPResponse.streamF cache_contentcCr!r"r#)r&rrrr'r'r(readzBaseHTTPResponse.readcCr!r"r#rr'r'r(read1zBaseHTTPResponse.read1cCr!r"r#rr'r'r( read_chunkedrzBaseHTTPResponse.read_chunkedcCr!r"r#r+r'r'r( release_connr*zBaseHTTPResponse.release_conncCr!r"r#r+r'r'r( drain_connr*zBaseHTTPResponse.drain_conncCr!r"r#r+r'r'r(shutdownr*zBaseHTTPResponse.shutdowncCr!r"r#r+r'r'r(closer*zBaseHTTPResponse.closecsrjdd}jdur3|jvrt|_dSd|vr5fdd|dD}|r7t|_dSdSdSdS)z= Set-up the _decoder attribute if necessary. content-encodingrNrncs"g|] }|jvr|qSr')rjCONTENT_DECODERS)rker+r'r(rms z2BaseHTTPResponse._init_decoder..)rrrrrriro)r&content_encodingrr'r+r( _init_decoders   zBaseHTTPResponse._init_decoderr flush_decoderc Cs|s |jr td|Sz|jr|j|}d|_Wn|jy7}z|jdd}td|||d}~ww|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) rrrr)rrrrr_flush_decoder)r&rrrrrr'r'r(_decodes4  zBaseHTTPResponse._decodecCs |jr|jd|jSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r5)rr)r,r+r'r'r(r szBaseHTTPResponse._flush_decoderbrLcCs6|t|}t|dkrdS||dt|<t|Srq)rr)r&rtempr'r'r(readintos  zBaseHTTPResponse.readintor cCstjdtdd|jS)Nz|HTTPResponse.getheaders() is deprecated and will be removed in urllib3 v2.6.0. Instead access HTTPResponse.headers directly.rEcategory stacklevel)warningswarnDeprecationWarningrr+r'r'r( getheaderss zBaseHTTPResponse.getheadersnamedefaultcCstjdtdd|j||S)NzHTTPResponse.getheader() is deprecated and will be removed in urllib3 v2.6.0. Instead use HTTPResponse.headers.get(name, default).rEr)rrrrr)r&rrr'r'r( getheader(s zBaseHTTPResponse.getheadercCrr")rr+r'r'r(info2r*zBaseHTTPResponse.infocCrr")rr+r'r'r(geturl5r*zBaseHTTPResponse.geturl)rrrrrrrrhrrrrrrrrr r4)r rr.)r rr r)rrr r4)r r)r r)rrr r4rN)rrrrr rNNFrrrrrrr rNNrrrrr rrB)rrrrrrr r)rrLr r)r r r")rrhrrr r)'r/r0r1rrQryrIOErrorr8r<r__annotations__rV ZstdErrorr;rpropertyrrrsetterrrrrrrrrrrrrrrrrrrr'r'r'r(rFsj      #                    rcseZdZdZ                  didjfd'd( Zdkd)d*Zdkd+d,Zedld.d/Zedmd0d1Z dnd2d3Z dod4d5Z dpd7d8Z e dqd:d;Z drdsz,HTTPResponse._init_length..rnrz8Content-Length contained multiple unmatching values (%s)r)i0dHEAD) rrrlogwarningrorrrrrr)r&rcontent_lengthlengthslengthrr'r'r(rs>     &zHTTPResponse._init_lengthtyping.Generator[None]c csd}zzdVWnrty}zt|jdd|d}~wty;}zdt|vr/t||t|jdd|d}~wtye}z|jdurV|jdurV|j|j krVd}nd|}t |||d}~wt t fy{}z t d|||d}~wwd}W|s|j r|j |jr|j |j r|j r|dSdSdS|s|j r|j |jr|j |j r|j r|www)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) SocketTimeoutrrr rhrrexpectedpartialrrrrrrrr)r& clean_exitrargr'r'r(_error_catchersT              zHTTPResponse._error_catcherFrrrcCs |jsJd}|r ||ks|durf|jrf|j|krftjs!tjdkrf|r)|j|St}d}|dus7|dkrb|durEt ||}||8}n|}|j |}|sT | S| |~|dus7|dks7| S|rw|durr|j|S|jS|dur|j |S|j S)aX 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: * 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) rrr IS_PYOPENSSLsys version_inforrrminrrr)r&rr c_int_maxr} max_chunk_amt chunk_amtrr'r'r(_fp_read@s@         zHTTPResponse._fp_readcCs|jdurdSt|jdd}|K|s|j||dnd}|durB|dkrB|sB|j|jrA|jdurA|jdkrAt|j|jn|rV|dkrJ|rQ|jt |krV|jWdn1s`wY|r~|jt |7_|jdur~|jt |8_|S)z7 Reads `amt` of bytes from the socket. NclosedFr1r5r) rgetattrr0r;rrrrrr)r&rr fp_closedrr'r'r( _raw_readys.        zHTTPResponse._raw_readrrcCs0||dur |j}|r|dkrd}n|dur'd}t|j|kr'|j|S||}|dup6|dko6| }|sBt|jdkrB|S|durT||||}|rR||_|S|s_|jr]t d|S||||}|j |t|j|kr|r||}||||}|j |t|j|kr|su|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) rrrrrr?rrrrr)r&rrrrr decoded_datar'r'r(rsB       zHTTPResponse.readcCs|dur|j}|r|dkrd}|jr.|stdt|jdkr.|dur(|jS|j|S|dkr4dS|j|dd}|rA|durC|S| | }| |||}|j ||s\|r]n|jddd}qH|durn|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.r5Tr1i ) rrrrrrrr?rrr)r&rrrrr@r'r'r(rs<       zHTTPResponse.read1rtyping.Generator[bytes]ccsx|jr|r|j||dEdHdSt|jr t|jdkr:|j||d}|r,|Vt|jr t|jdks dSdS)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. r Nr)rr)rsupports_chunked_readsrrrrrr)r&rrrr'r'r(r-s zHTTPResponse.streamcCsdS)NTr'r+r'r'r(readableIszHTTPResponse.readablecCs0|jstd|jdurtd|tjdS)Nz8Cannot shutdown socket as self._sock_shutdown is not setzCCannot shutdown as connection has already been released to the pool)rrrrsocketSHUT_RDr+r'r'r(rLs zHTTPResponse.shutdowncCsFd|_|js|jr|j|jr|j|js!tj|dSdSr")rr<rrrr rIOBaser+r'r'r(rUs   zHTTPResponse.closecCsP|js tjj|S|jdurdSt|jdr|jSt|jdr&|jjSdS)NTrr<)r rrFr<__get__rrSrr+r'r'r(r<as    zHTTPResponse.closedcCs0|jdur tdt|jdr|jStd)Nz-HTTPResponse has no file to get a fileno fromfilenozOThe file-like object this HTTPResponse is wrapped around has no file descriptor)rrrSrHr+r'r'r(rHns   zHTTPResponse.filenocCs:|jdurt|jdrt|jdds|jSdSdSdS)Nr,r<F)rrSr=r,r+r'r'r(r,ys     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)rSrr+r'r'r(rBs z#HTTPResponse.supports_chunked_readscCsn|jdurdS|jj}|ddd}z t|d|_WdSty6||r1t||dt ddw)N;rrrJzResponse ended prematurely) rrrIreadlinerorrrrr)r&liner'r'r(_update_chunk_lengths     z!HTTPResponse._update_chunk_lengthcCsd}|dur|j|j}|}|jdd|_|S|jdur4||jkr4|j|}|j||_|}|S||jkrL|j|}|jdd|_|}|S|j|j}|jdd|_|S)NrE)r _safe_readr)r&rreturned_chunkrvaluer'r'r( _handle_chunks,      zHTTPResponse._handle_chunkccsR||js td|std||jr/t|jr/|j WddS|j j dur> WddS|rF|dkrFd} | |j dkrQn| |}|j||dd}|rc|VqG|ro|}|ro|V|j dur|j j }|s}n |dkrn|j dust|jr|jWddSWddS1swYdS) 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.NrTF)rrs )rrrrBrr0rrrrrIrMrrQrrrK)r&rrrdecodedrLr'r'r(rs^           -"zHTTPResponse.read_chunkedcCr)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.urlrcCs ||_dSr"rSrr'r'r(rs rccsg}|jddD]7}d|vr;|d}d||ddV|ddD]}|dVq&|dr8|dg}q g}q ||q |rKd|VdSdS)NTr  r5rrr)rror^r])r&r}rchunksxr'r'r(__iter__ s    zHTTPResponse.__iter__)rNrrrNTTNNNNNTNNTN)&rr rrrrrrrrhrrrrrrrrrrrrrrrrrrrrrrr rr r r r4rBr.)r r)r rr)rrr r)r r*r")rrrrr rrrrrr)rrrrr rA)rrr rr)rrhr r4)r r)"r/r0r1rtr;rrrrrrrrrr0r;r?rrrrCrrr<rHr,rBrMrQrrrrW __classcell__r'r'rr(r9s" @      8 C;6 L 7       M  r)rurhr r)R __future__rr{rrrloggingrerDr5typingrr8 contextlibr http.clientr_HttplibHTTPMessager_HttplibHTTPResponserr+ TYPE_CHECKING_base_connectionr brotlicffirQ ImportErrorrr r _collectionsr rr r r exceptionsrrrrrrrrrr util.responserr util.retryrconnectionpoolr getLoggerr/r%rr3rDrIrP compressionrVryrW zstandardtuplemaprsearch __version__groups _zstd_versionAttributeErrorrrfrirzrFrr'r'r'r(s             0      ( It