CYh? PddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddlmZddlmZddlmZ ddlZ ddlZeeeej<dej>jAxZ!Z!e!dkrdZd d l$m%Z%d d l&m'Z'd d l(m)Z)d d l*m+Z+m,Z,m-Z-d dl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8d dl9m:Z:m;Z;d dlr)r;r<errorr= MAX_WBITS)r&r' decompresseds r(r)zDeflateDecoder.decompressRsK99''- - d  "99//5L"'!  zz "#DO**DNN?;DI "tzz2! T  "s%,A//AC$3C C$ C  C$c6|jjSr"r>r.r-s r(r.zDeflateDecoder.flushhyy  r+Nr1Noner/r2r3r4r5r?r)r.r6r+r(r8r8Ls) ",!r+r8ceZdZdZdZdZy)GzipDecoderStaterr N)r3r4r5 FIRST_MEMBER OTHER_MEMBERS SWALLOW_DATAr6r+r(rKrKlsLMLr+rKc$eZdZddZddZddZy) GzipDecoderctjdtjz|_tj |_yN)r<r=rBr>rKrM_stater-s r(r?zGzipDecoder.__init__ss*&&rDNN':; &33 r+c$t}|jtjk(s|s t |S ||j j |z }|j j}|s t |Stj|_tjdtjz|_#tj$rC|j}tj|_|tjk(r t |cYSwxYwrS) bytearrayrUrKrOr0r>r)r<rArN unused_datar=rB)r&r'retprevious_states r(r)zGzipDecoder.decompresswsk ;;*77 7t:  tyy++D1199((DSz!*88DK**2+>?DI:: !%.;; !%5%C%CC :% sB99AD Dc6|jjSr"rEr-s r(r.zGzipDecoder.flushrFr+NrGr/r2rIr6r+r(rQrQrs4@*!r+rQceZdZddZddZy) BrotliDecoderctj|_t|jdr"t |d|jj yt |d|jj y)Nr))brotli Decompressorr>hasattrsetattrr)processr-s r(r?zBrotliDecoder.__init__sL++-DItyy,/lDII,@,@AlDII,=,=>r+cdt|jdr|jjSy)Nr.r+)rar>r.r-s r(r.zBrotliDecoder.flushs$tyy'*yy((r+NrGr2)r3r4r5r?r.r6r+r(r]r]s  ? r+r]c$eZdZddZddZddZy) ZstdDecodercRtjj|_yr")zstdZstdDecompressorr=r>r-s r(r?zZstdDecoder.__init__s--/==?DIr+c|sy|jj|g}|jjr|jjr|jj}t j j |_|j|jj||jjr|jjrdj|S)Nr+) r>r)eofrXrhrir=appendjoin)r&r' data_partsrXs r(r)zZstdDecoder.decompresss))..t45J))--DII$9$9"ii33  113AAC !!$))"6"6{"CD))--DII$9$988J' 'r+c||jj}|jjs td|S)NzZstandard data is incomplete)r>r.rkr)r&rYs r(r.zZstdDecoder.flushs.))//#C99==!"@AAJr+NrGr/r2rIr6r+r(rfrfs @ ( r+rfc(eZdZdZddZddZddZy) 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. c|jdDcgc]}t|jc}|_ycc}w)N,)split _get_decoderstrip _decoders)r&modesms r(r?zMultiDecoder.__init__s+;@;;s;KLa,qwwy1LLs =c<|jdjSNr)rwr.r-s r(r.zMultiDecoder.flushs~~a &&((r+c\t|jD]}|j|}|Sr")reversedrwr))r&r'ds r(r)zMultiDecoder.decompresss-$..) &A<<%D & r+N)rxstrr1rHr2r/)r3r4r5__doc__r?r.r)r6r+r(rqrqsM)r+rqcd|vr t|S|dk(r tSt|dk(r tSt|dk(r t St S)Nrsgzipbrrh)rqrQr_r]rhrfr8)modes r(rurusT d{D!! v~} ddl DFN}  r+c0eZdZdZddZddZd dZd dZy) 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(). cDtj|_d|_yr{) collectionsdequebuffer_sizer-s r(r?zBytesQueueBuffer.__init__s+6+<+<+>  r+c|jSr")rr-s r(__len__zBytesQueueBuffer.__len__s zzr+cv|jj||xjt|z c_yr")rrlrlenr%s r(putzBytesQueueBuffer.puts& 4  c$i r+cd|dk(ry|js td|dkr tdd}tj}||kr||z }|jj }t |}||kr\|d|||d}}|j||jj||xj|zc_ |jS|j||xj|zc_ ||z }|js |jS||kr|jS)Nrr+zbuffer is emptyzn should be > 0) r RuntimeError ValueErrorioBytesIOpopleftrwrite appendleftrgetvalue) r&nfetchedrY remainingchunk chunk_length left_chunk right_chunks r(getzBytesQueueBuffer.gets 601 1 U./ /jjlkG IKK'')Eu:L<'*/ *;U9:=NK  *% &&{3 i' ||~ %  l* | #G;;||~%k$||~r+NrGr1int)r'r0r1rH)rrr1r0)r3r4r5rr?rrrr6r+r(rrs  r+rc4eZdZUddgZeedgz Zeedgz ZgdZeejfZ de d<ee ejfz Z ee ejfz Z ddd  d!d Z d"d Zed#d Zd$d Zed%dZej&d&dZed'dZed(dZej&d)dZ d* d+dZ d, d-dZ d. d+dZd/dZd/dZd/dZd/dZ d0dZd#dZd1dZd2dZ d3d4dZ!d2dZ"d%d Z#y)5BaseHTTPResponserdeflateNrrh)i-i.i/i3i4ztuple[type[Exception], ...]DECODER_ERROR_CLASSES)headersretriesclt|tr||_nt||_||_||_||_||_d|_||_||_ d|_ |jjddj}d|jdD} d| vrd|_ d|_y)NFztransfer-encodingc3<K|]}|jywr")rv).0encs r( z,BaseHTTPResponse.__init__..Cs>SSYY[>srschunkedT) isinstancer rstatusversionreasondecode_content_has_decoded_content _request_urlrrrlowerrt_decoder) r&rrrrr request_urlrtr_enc encodingss r(r?zBaseHTTPResponse.__init__)s g~ ."DL)'2DL   ,$)!(3  !!"5r:@@B>FLL,=>  !DL/3 r+cj|j|jvr|jjdSy)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-s r(get_redirect_locationz&BaseHTTPResponse.get_redirect_locationIs. ;;$00 0<<##J/ /r+ctr"r#r-s r(r'zBaseHTTPResponse.dataU !##r+cb|jjd}tj|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%s r(jsonzBaseHTTPResponse.jsonYs'yy({{4  r+ctr"r#r-s r(urlzBaseHTTPResponse.urlfrr+ctr"r#r&rs r(rzBaseHTTPResponse.urljrr+ctr"r#r-s r( connectionzBaseHTTPResponse.connectionnrr+c|jSr")_retriesr-s r(rzBaseHTTPResponse.retriesrs }}r+cj|*|jr|jdj|_||_y)N)historyredirect_locationrr)r&rs r(rzBaseHTTPResponse.retriesvs-  7??r*<CIIK == 4#8#88 ,-= > ((.33C8wwyD$9$99GGI  $01A$BDM ) !s-3B8cf|s|jr td|S |jr"|jj|}d|_|r||jz }|S#|j$r?}|j j ddj}td|z||d}~wwxYw)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&r'r flush_decoderrrs r(_decodezBaseHTTPResponse._decodes (("<K }}}}//5,0)  D'') )D )) #||//0BBGMMO ')9:   s.A""B01:B++B0c|jr6|jjd|jjzSy)zk Flushes the decoder. Should only be called if the decoder is actually being used. r+)rr)r.r-s r(rzBaseHTTPResponse._flush_decoders5 ====++C04==3F3F3HH Hr+c|jt|}t|dk(ry||dt|t|Sr{)rr)r&btemps r(readintozBaseHTTPResponse.readintos;yyQ  t9>!AkD Nt9 r+cRtjdtd|jS)Nz|HTTPResponse.getheaders() is deprecated and will be removed in urllib3 v2.1.0. Instead access HTTPResponse.headers directly.rLcategory stacklevel)warningswarnDeprecationWarningrr-s r( getheaderszBaseHTTPResponse.getheaderss&  O'  ||r+crtjdtd|jj ||S)NzHTTPResponse.getheader() is deprecated and will be removed in urllib3 v2.1.0. Instead use HTTPResponse.headers.get(name, default).rLr)rrrrr)r&namedefaults r( getheaderzBaseHTTPResponse.getheaders3  V'  ||g..r+c|jSr")rr-s r(infozBaseHTTPResponse.infos ||r+c|jSr")rr-s r(geturlzBaseHTTPResponse.geturls xxr+)r>typing.Mapping[str, str] | typing.Mapping[bytes, bytes] | Nonerrrrr str | Nonerboolrrr Retry | Noner1rH)r1zstr | None | Literal[False]r2)r1z typing.Anyr1r)rrr1rHr1HTTPConnection | None)r1r)rrr1rHiN)r int | Noner bool | Noner1typing.Iterator[bytes]NNFrr rr rrr1r0NNrG)r'r0rr rrr1r0)rrWr1r)r1r r")rrrrr1r)$r3r4r5rr_rhrIOErrorr<rAr__annotations__ ZstdErrorr?rpropertyr'rrsetterrrrrrrrrrrrrrrrrr6r+r(rrsE * TF" VH$1:A4::9N6N &,,0 $..!22 SW !%4P4 4  4  44 44 4@ $$ !$$ ZZ$$$$ ^^  FJ$$7B$ $&*# $ $$$ $  $&*$ $$$ $$$$C&+6GK </r+rceZdZdZ d dfd ZddZddZeddZeddZ d dZ d!dZ d"d Z e d#d Zd$d%d Z d$ d%d Z d& d'd Z d( d)dZd dZddZed dZd!dZddZd dZddZd%dZ d* d)dZed+dZej:d,dZd-dZxZS).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. c t|||||||| | |_||_d|_d|_||_d|_| |_|rt|ttfr||_| |_ | |_ t|dr||_d|_|j!||_t%|_|r%|js|j)||_yyy)N)rrrrrrrrrr)superr?enforce_content_length auto_close_body_fp_original_response_fp_bytes_readmsgrrr0_pool _connectionra chunk_left _init_lengthlength_remainingr_decoded_bufferr)r&bodyrrrrpreload_contentroriginal_responsepoolrr rrrequest_methodrr __class__s r(r?zHTTPResponse.__init__s& )#  '=#$ 04"3 Jtc5\2DJ % 4 DH'+!% 1 1. A 01 4::.ADJ$.?r+c|jr |jsy|jj|jd|_yr")r!r" _put_connr-s r(rzHTTPResponse.release_connUs4zz!1!1 T--.r+cd |jy#ttttf$rYywxYw)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-s r(rzHTTPResponse.drain_conn\s+   IIK7L-@   s //cp|jr |jS|jr|jdSy)NT)r)rrrr-s r(r'zHTTPResponse.datags0 ::::  8899490 0r+c|jSr")r"r-s r(rzHTTPResponse.connectionrsr+c,t|jSr")rrr-s r(isclosedzHTTPResponse.isclosedvsDHH%%r+c|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-s r(tellzHTTPResponse.tellys """r+c|jjd}|~|jrtj dy |j dDchc] }t |}}t|dkDrtd|z|j}|dkrd}nd} t |j}|dvsd |cxkrd ksn|d k(rd}|Scc}w#t$rd}YBwxYw#t$rd}Y=wxYw) 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.rsr z8Content-Length contained multiple unmatching values (%s)r)i0dHEAD) rrrlogwarningrtrrrpoprr)r&r+content_lengthvallengthslengthrs r(r$zHTTPResponse._init_lengths &*\\%5%56F%G  %||  " 0>/C/CC/HI3s8IIwS>STT- U0    3t9 , $$0%%T2% 9 U Us BD  Dc|j| |j}|5d}t|j|k\r|jj |S|j |}|duxs |dk7xr| }|st|jdk(r|S||j |||}|r||_|S|s|jr td|S|j |||}|jj|t|j|kr\|rZ|j |}|j |||}|jj|t|j|kr|rZ|jj |}|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.) NFrr) rrrr&rrWrrrrr)r&rrrr'r decoded_datas r(rzHTTPResponse.readKs4   !!00N ?!M4''(C/++//44~~c"t >q(=X D001Q6K ;<<nmDD! . )",,&@ <<nmLL  $ $\ 2d**+c1d~~c*#||D.-P $$((6 d**+c1d''++C0D r+c#lK|jr,|jr|j||Ed{yt|jrt |j dkDrI|j||}|r|t|js/t |j dkDrHyy7|w)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. rNr)rr)rsupports_chunked_readsrrrrr&r)r&rrr's r(rzHTTPResponse.streams$ <># #7+ !r+c.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)rarr-s r(r[z#HTTPResponse.supports_chunked_readsstxx&&r+c|jy|jjj}|j ddd} t |d|_y#t $r|jt||dwxYw)N;r rrT) r#rrfreadlinertrrrr)r&lines r(_update_chunk_lengthz!HTTPResponse._update_chunk_lengthsu ?? &xx{{##%zz$"1% ;!$mDO ; JJL$T40d : ;s A'Bcnd}|K|jj|j}|}|jjdd|_|S|jB||jkr3|jj|}|j|z |_|}|S||jk(rA|jj|}|jjdd|_|}|S|jj|j}|jjdd|_|S)NrL)r _safe_readr#)r&rreturned_chunkrvalues r( _handle_chunkzHTTPResponse._handle_chunks ;HH''8E"N HH   ""DO__ (S4??-BHH'',E"oo3DO"NDOO #HH'',E HH   ""DO"N "XX00AN HH   ""DOr+c#FK|j|js td|js t d|j 5|j r9t|j r$|j j dddy|jj dddy |j|jdk(rn,|j|}|j||d}|r|L|r|j}|r||j:|jjj!}|sn|dk(rn |j:|j r|j jdddy#1swYyxYww)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 )rrrr[rrFrrrrrfrkr#rprrri)r&rrrdecodedrjs r(rzHTTPResponse.read_chunkeds ||$B **,*P   ") 0&&+>t?V?V+W''--/ ) 0) 0xx{{") 0) 0))+??a'**3/,,.'!M--/!M((&xx{{++-7? ((&&&''--/S) 0) 0) 0s=AF!=F F!F2 F!;B*F&&F F!FF!c|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-s r(rzHTTPResponse.urlPs   r+c||_yr"rtrs r(rzHTTPResponse.urlYs r+c#.Kg}|jdD]d}d|vrM|jd}dj||dzdz|ddD] }|dz |dr|dg}Qg}T|j|f|rdj|yyw)NTr r+rr r)rrtrmrl)r&rrchunksxs r(__iter__zHTTPResponse.__iter__]s [[[5 %E~U+hhv&2U::"$Ae)O$":$Rj\FF e$ % ((6" " sBB)rNrrNTTNNNNNTNNT)"r'r rrrrrrrrr(rrrr)z_HttplibHTTPResponse | Noner*zHTTPConnectionPool | Nonerr r z_HttplibHTTPMessage | Nonerrrrr+rrrrrr1rHrGr2r)r1rr)r+rr1r )r1z"typing.Generator[None, None, None]r")rr r1r0rrr )rr rr r1z#typing.Generator[bytes, None, None]rr)rrr1rH)r1r ) r3r4r5rr?rrrr'rr4r6r$rrFrRrWrrr]rrTrcr.r[rkrprrrrz __classcell__)r,s@r(rrshDRV! $#9=*.,0*. $'+%)"&#:B:BP:B :B  :B  :B:B:B7:B(:B*:B(:B:B!%:B#:B  !:B"#:B$ %:Bx    &#6p5$5$n.Nd( ( (X&*# E E$E E  EPFJ7B ,8"   $' ;.EIF0F06AF0 ,F0P!! ZZ  #r+r)rrr1r )Q __future__rrrrrloggingrerLtypingrr< contextlibr http.clientr_HttplibHTTPMessager_HttplibHTTPResponsesocketrrD brotlicffir_ ImportError zstandardrhtuplemaprsearch __version__groups _zstd_versionAttributeErrorrrr _base_connectionr _collectionsr rr rr exceptionsrrrrrrrrrr util.responserr util.retryr TYPE_CHECKINGtyping_extensionsrconnectionpoolr getLoggerr3r<r r8rKrQr]rfrqrurr_rr6r+r(rs"   %:<+#  %* C143C3CDKKMN%MMw ((CC   = )2g!$$!^!@ !.!< "n*>*  88v[ryy[|t ##t #I  F  Z0 Ds=E9 AF9 FF FF FF F%$F%