3 $c)x@sddlmZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z y.y ddlZWnek rddlZYnXWnek rdZYnXddlmZddlmZdd lmZmZdd lmZmZmZmZmZmZmZm Z m!Z!m"Z"dd l#m$Z$dd l%m&Z&m'Z'ej(e)Z*Gd dde+Z,Gddde+Z-Gddde+Z.edk rhGddde+Z/Gddde+Z0ddZ1Gdddej2Z3dS))absolute_importN)contextmanager)error)timeout)util)HTTPHeaderDict) BaseSSLError HTTPException) BodyNotHttplibCompatible DecodeError HTTPErrorIncompleteReadInvalidChunkLength InvalidHeader ProtocolErrorReadTimeoutErrorResponseNotChunkedSSLError)six) is_fp_closedis_response_to_headc@s$eZdZddZddZddZdS)DeflateDecodercCsd|_d|_tj|_dS)NT) _first_try_datazlib decompressobj_obj)selfr 7/tmp/pip-target-ezim9f7s/lib/python/urllib3/response.py__init__*szDeflateDecoder.__init__cCs t|j|S)N)getattrr)rnamer r r! __getattr__/szDeflateDecoder.__getattr__cCs|s|S|js|jj|S|j|7_y |jj|}|rFd|_d|_|Stjk rd|_tjtj |_z |j|jSd|_XYnXdS)NF)rr decompressrrrr MAX_WBITS)rdata decompressedr r r!r&2s"   zDeflateDecoder.decompressN)__name__ __module__ __qualname__r"r%r&r r r r!r)src@seZdZdZdZdZdS)GzipDecoderStaterrN)r*r+r, FIRST_MEMBER OTHER_MEMBERS SWALLOW_DATAr r r r!r-Isr-c@s$eZdZddZddZddZdS) 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__c Cst}|jtjks| r t|Sxy||jj|7}Wn8tjk rn|j}tj|_|tj krht|SYnX|jj }|st|Stj |_tj dtj |_q"WdS)Nr3) bytearrayr4r-r1bytesrr&rrr0 unused_datarr')rr(retprevious_stater r r!r&Xs" zGzipDecoder.decompressN)r*r+r,r"r%r&r r r r!r2Psr2c@seZdZddZddZdS) BrotliDecodercCs0tj|_t|jdr"|jj|_n |jj|_dS)Nr&)brotliZ Decompressorrhasattrr&process)rr r r!r"ts   zBrotliDecoder.__init__cCst|jdr|jjSdS)Nflushr)r<rr>)rr r r!r>{s  zBrotliDecoder.flushN)r*r+r,r"r>r r r r!r:psr:c@s(eZdZdZddZddZddZdS) 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|jdD|_dS)NcSsg|]}t|jqSr ) _get_decoderstrip).0mr r r! sz)MultiDecoder.__init__..,)split _decoders)rmodesr r r!r"szMultiDecoder.__init__cCs|jdjS)Nr)rGr>)rr r r!r>szMultiDecoder.flushcCs"xt|jD]}|j|}q W|S)N)reversedrGr&)rr(dr r r!r&szMultiDecoder.decompressN)r*r+r,__doc__r"r>r&r r r r!r?sr?cCs:d|krt|S|dkrtStdk r4|dkr4tStS)NrEgzipbr)r?r2r;r:r)moder r r!r@sr@c@steZdZdZddgZedk r&edg7Zdddd d gZdQddZddZddZ ddZ e ddZ e ddZ ddZddZdd Zd!d"ZeejfZedk reejf7Zd#d$Zd%d&Zed'd(Zd)d*ZdRd+d,ZdTd/d0Zed1d2Zd3d4ZdUd5d6Z d7d8Z!d9d:Z"e d;d<Z#d=d>Z$d?d@Z%dAdBZ&dCdDZ'dEdFZ(dGdHZ)dIdJZ*dVdKdLZ+dMdNZ,dOdPZ-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. rLdeflateNrMi-i.i/i3i4rTFcCst|tr||_n t||_||_||_||_||_||_| |_||_ ||_ d|_ d|_ d|_ | |_d|_| |_||_|rt|tjtfr||_ | |_| |_t|dr||_ d|_d|_|jjddj}dd|jdD}d |krd |_|j||_|r|j r|j|d |_ dS) NrreadFztransfer-encodingrQcss|]}|jVqdS)N)rA)rBencr r r! sz(HTTPResponse.__init__..rEchunkedT)decode_content) isinstancerheadersstatusversionreasonstrictrVretriesenforce_content_length auto_close_decoder_body_fp_original_response_fp_bytes_readmsg _request_urlr string_typesr6_pool _connectionr<rU chunk_leftgetlowerrF _init_lengthlength_remainingrR)rbodyrXrYrZr[r\preload_contentrVoriginal_responsepool connectionrer]r^request_method request_urlr_tr_enc encodingsr r r!r"sB    zHTTPResponse.__init__cCs|j|jkr|jjdSdS)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)rYREDIRECT_STATUSESrXrk)rr r r!get_redirect_location s  z"HTTPResponse.get_redirect_locationcCs,|j s|j rdS|jj|jd|_dS)N)rhri _put_conn)rr r r! release_connszHTTPResponse.release_conncCs.y |jWnttttfk 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)rRr SocketErrorr r )rr r r! drain_conn s zHTTPResponse.drain_conncCs"|jr |jS|jr|jddSdS)NT) cache_content)rarbrR)rr r r!r(+szHTTPResponse.datacCs|jS)N)ri)rr r r!rs4szHTTPResponse.connectioncCs t|jS)N)rrb)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). )rd)rr r r!tell;szHTTPResponse.tellcCs|jjd}|dk r|jr(tjddSy.rErz8Content-Length contained multiple unmatching values (%s)r0dHEAD)rr) rXrkrUlogwarningsetrFlenrpop ValueErrorrrY)rrtlengthlengthsrYr r r!rmCs0     (zHTTPResponse._init_lengthcshjjddj}jdkrd|jkr2t|_n2d|krdfdd|jdD}t|rdt|_dS)z= Set-up the _decoder attribute if necessary. zcontent-encodingrQNrEcs"g|]}|jjkr|jqSr )rACONTENT_DECODERS)rBe)rr r!rDsz.HTTPResponse._init_decoder..)rXrkrlr`rr@rFr)rcontent_encodingrwr )rr! _init_decoderws    zHTTPResponse._init_decodercCsx|s|Sy|jr|jj|}WnD|jk rb}z&|jjddj}td||WYdd}~XnX|rt||j7}|S)zN Decode the data passed in and potentially flush the decoder. zcontent-encodingrQzEReceived response with content-encoding: %s, but failed to decode it.N)r`r&DECODER_ERROR_CLASSESrXrkrlr _flush_decoder)rr(rV flush_decoderrrr r r!_decodes zHTTPResponse._decodecCs$|jr |jjd}||jjSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r)r`r&r>)rbufr r r!rs zHTTPResponse._flush_decoderccsd}zy dVWntk r2t|jddYnvtk rt}z(dt|krVt|t|jddWYdd}~Xn4ttfk r}ztd||WYdd}~XnXd}Wd|s|j r|j j |j r|j j |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) SocketTimeoutrrhr strrr r}rrccloserirr|)r clean_exitrr r r!_error_catchers(      zHTTPResponse._error_catchercCs|js td }|r||ks*|jr|j|krtj rtjsBtjd krtj }d }xR|dks`|dkr|dk r|t ||}||8}n|}|jj |}|sP|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)rri)rbAssertionErrorrnrIS_SECURETRANSPORT IS_PYOPENSSLsys version_infoioBytesIOminrRwritegetvalue)ramtZ c_int_maxbufferZ max_chunk_amtZ chunk_amtr(r r r!_fp_reads(      zHTTPResponse._fp_readc Cs|j|dkr|j}|jdkr$dSd}t|jdd}|jd|sN|j|nd}|dkr`d}n>d}|dkr| r|jjd}|jr|jdkrt |j |jWdQRX|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.) NFclosedrTr)rN)rrVrbr#rrrr^rnrrdrrra)rrrVrr fp_closedr(r r r!rRs6     zHTTPResponse.readr.r3ccsZ|jr.|jr.xF|j||dD] }|VqWn(x&t|jsT|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. )rV)rrVN)rUsupports_chunked_reads read_chunkedrrbrR)rrrVliner(r r r!stream_s  zHTTPResponse.streamc Ksb|j}t|ts.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)rorXrYrZr[r\rq) rerWrrPY2 from_httplibitemsr#rYrZr[) ResponseClsr response_kwrXr\respr r r!rys     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)warningswarnDeprecationWarningrX)rr r r! getheaderss zHTTPResponse.getheaderscCstjdtdd|jj||S)NzHTTPResponse.getheader() is deprecated and will be removed in urllib3 v2.1.0. Instead use HTTPResponse.headers.get(name, default).r.)rr)rrrrXrk)rr$defaultr r r! getheaders zHTTPResponse.getheadercCs|jS)N)rX)rr r r!infoszHTTPResponse.infocCs6|js|jj|jr |jj|js2tjj|dS)N)rrbrrir_rIOBase)rr r r!rs   zHTTPResponse.closecCsT|jstjjj|S|jdkr"dSt|jdr8|jjSt|jdrL|jjSdSdS)NTrr)r_rrr__get__rbr<r)rr r r!rs    zHTTPResponse.closedcCs6|jdkrtdnt|jdr*|jjStddS)Nz-HTTPResponse has no file to get a fileno fromfilenozOThe file-like object this HTTPResponse is wrapped around has no file descriptor)rbIOErrorr<r)rr r r!rs     zHTTPResponse.filenocCs4|jdk r0t|jdr0t|jdd r0|jjSdS)Nr>rF)rbr<r#r>)rr r r!r>s  zHTTPResponse.flushcCsdS)NTr )rr r r!readableszHTTPResponse.readablecCs:|jt|}t|dkrdS||dt|<t|SdS)Nr)rRr)rbtempr r r!readintos  zHTTPResponse.readintocCs 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)r<rb)rr r r!rsz#HTTPResponse.supports_chunked_readsc Csf|jdk rdS|jjj}|jddd}yt|d|_Wn&tk r`|jt||YnXdS)N;rrr3) rjrbrreadlinerFrrrr)rrr r r!_update_chunk_lengths  z!HTTPResponse._update_chunk_lengthcCsd}|dkr2|jj|j}|}|jjdd|_nv||jkrZ|jj|}|j||_|}nN||jkr|jj|}|jjdd|_|}n |jj|j}|jjdd|_|S)Nr.)rb _safe_readrj)rrreturned_chunkchunkvaluer r r! _handle_chunks&        zHTTPResponse._handle_chunkc cs|j|jstd|js&td|j|jrNt|jrN|jjdS|j j dkr^dSx<|j |j dkrtP|j |}|j||dd}|r`|Vq`W|r|j}|r|Vx |j j j}|sP|dkrPqW|jr|jjWdQRXdS)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)rVrs )rrUrrr rrcrrrbrrrjrrrr)rrrVrdecodedrr r r!rsD        zHTTPResponse.read_chunkedcCs.|jdk r$t|jjr$|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]rhistoryredirect_locationrf)rr r r!geturl[szHTTPResponse.geturlccsg}x|jddD]r}d|krz|jd}dj||ddVx|ddD]}|dVqNW|drt|d g}qg}q|j|qW|rdj|VdS) NT)rV rrrrrr)rrFjoinappend)rrrxr r r!__iter__fs  zHTTPResponse.__iter__)rQNrrNrTTNNNNNFNNT)NNF)rN)N)NN).r*r+r,rKrr;ryr"rzr|r~propertyr(rsrrrmrrrrrrrrrrrRr classmethodrrrrrrrr>rrrrrrrrr r r r!rOsp  1  4   80 H  !      F rO)4 __future__rrloggingrrr contextlibrsocketrr}rrZ brotlicffir; ImportErrorrQr _collectionsrrsr r exceptionsr r r rrrrrrrpackagesrZ util.responserr getLoggerr*robjectrr-r2r:r?r@rrOr r r r!s<        0