Re=n @sddlmZddlmZddlZddlZddlZddlmZ ddlm Z yddl Z Wne k rdZ YnXddlmZddlmZmZmZmZmZmZmZmZdd lmZmZdd lmZdd l m!Z!m"Z"dd l#m$Z$m%Z%ej&e'Z(Gd dde)Z*Gddde)Z+Gddde)Z,e dk rGddde)Z-Gddde)Z.ddZ/Gdddej0Z1dS))absolute_import)contextmanagerN)timeout)error)HTTPHeaderDict)BodyNotHttplibCompatible ProtocolError DecodeErrorReadTimeoutErrorResponseNotChunkedIncompleteRead InvalidHeader HTTPError) string_typesPY3) http_client) HTTPException BaseSSLError) is_fp_closedis_response_to_headc@s4eZdZddZddZddZdS)DeflateDecodercCs%d|_d|_tj|_dS)NT) _first_try_datazlib decompressobj_obj)selfr/builddir/build/BUILDROOT/alt-python35-pip-20.2.4-5.el8.x86_64/opt/alt/python35/lib/python3.5/site-packages/pip/_vendor/urllib3/response.py__init__"s  zDeflateDecoder.__init__cCst|j|S)N)getattrr)rnamerrr __getattr__'szDeflateDecoder.__getattr__cCs|s |S|js#|jj|S|j|7_y2|jj|}|r_d|_d|_|SWnUtjk rd|_tjtj |_z|j|jSWdd|_XYnXdS)NF)rr decompressrrrr MAX_WBITS)rdata decompressedrrr r%*s"    zDeflateDecoder.decompressN)__name__ __module__ __qualname__r!r$r%rrrr r!s   rc@s"eZdZdZdZdZdS)GzipDecoderStaterrN)r)r*r+ FIRST_MEMBER OTHER_MEMBERS SWALLOW_DATArrrr r,As r,c@s4eZdZddZddZddZdS) GzipDecodercCs)tjdtj|_tj|_dS)N)rrr&rr,r._state)rrrr r!IszGzipDecoder.__init__cCst|j|S)N)r"r)rr#rrr r$MszGzipDecoder.__getattr__c Cst}|jtjks"| r,t|Sxy||jj|7}WnFtjk r|j}tj|_|tj krt|SYnX|jj }|st|Stj |_tj dtj |_q/WdS)Nr2) bytearrayr3r,r0bytesrr%rrr/ unused_datarr&)rr'retprevious_staterrr r%Ps"        zGzipDecoder.decompressN)r)r*r+r!r$r%rrrr r1Hs   r1c@s4eZdZddZddZddZdS) BrotliDecodercCstj|_dS)N)brotliZ Decompressorr)rrrr r!lszBrotliDecoder.__init__cCs2t|jdr"|jj|S|jj|S)Nr%)hasattrrr%process)rr'rrr r%oszBrotliDecoder.decompresscCs#t|jdr|jjSdS)Nflushr)r;rr=)rrrr r=ts zBrotliDecoder.flushN)r)r*r+r!r%r=rrrr r9hs   r9c@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. cCs#dd|jdD|_dS)NcSs"g|]}t|jqSr) _get_decoderstrip).0mrrr s z)MultiDecoder.__init__..,)split _decoders)rmodesrrr r!szMultiDecoder.__init__cCs|jdjS)Nr)rFr=)rrrr r=szMultiDecoder.flushcCs-x&t|jD]}|j|}qW|S)N)reversedrFr%)rr'drrr r%szMultiDecoder.decompressN)r)r*r+__doc__r!r=r%rrrr r>zs   r>cCsOd|krt|S|dkr)tStdk rH|dkrHtStS)NrDgzipbr)r>r1r:r9r)moderrr r?s   r?c@sXeZdZdZddgZedk r7edg7Zdddd d gZd dd d dd d d ddddddddd ddZddZddZ ddZ e ddZ e ddZ ddZddZdd Zd!d"ZeejfZedk r.eejf7Zd#d$Zd%d&Zed'd(Zdddd)d*ZdOdd-d.Zed/d0Zd1d2Zdd3d4Zd5d6Z d7d8Z!e d9d:Z"d;d<Z#d=d>Z$d?d@Z%dAdBZ&dCdDZ'dEdFZ(dGdHZ)dddIdJZ*dKdLZ+dMdNZ,dS)P HTTPResponsea HTTP Response container. Backwards-compatible to httplib's 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 httplib.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 httplib.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. rKdeflateNrLi-i.i/i3i4rTFcCst|tr||_nt||_||_||_||_||_||_| |_||_ ||_ d|_ d|_ d|_ | |_d|_| |_||_|rt|ttfr||_ | |_| |_t|dr||_ d|_d|_|jjddj}dd|jdD}d |kr]d |_|j||_|r|j r|jd ||_ dS) NrreadFztransfer-encodingrPcss|]}|jVqdS)N)r@)rAencrrr sz(HTTPResponse.__init__..rDchunkedTdecode_content) isinstancerheadersstatusversionreasonstrictrUretriesenforce_content_length auto_close_decoder_body_fp_original_response_fp_bytes_readmsg _request_url basestringr5_pool _connectionr;rT chunk_leftgetlowerrE _init_lengthlength_remainingrQ)rbodyrWrXrYrZr[preload_contentrUoriginal_responsepool connectionrdr\r]request_method request_urlr^tr_enc encodingsrrr 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)rXREDIRECT_STATUSESrWrj)rrrr get_redirect_locationsz"HTTPResponse.get_redirect_locationcCs8|j s|j rdS|jj|jd|_dS)N)rgrh _put_conn)rrrr release_connszHTTPResponse.release_conncCs3y|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)rQr SocketErrorrr)rrrr drain_connszHTTPResponse.drain_conncCs-|jr|jS|jr)|jddSdS)N cache_contentT)r`rarQ)rrrr r'$s  zHTTPResponse.datacCs|jS)N)rh)rrrr rr-szHTTPResponse.connectioncCs t|jS)N)rra)rrrr isclosed1szHTTPResponse.isclosedcCs|jS)z Obtain the number of bytes pulled over the wire so far. May differ from the amount of content returned by :meth:``HTTPResponse.read`` if bytes are encoded on the wire (e.g, compressed). )rc)rrrr tell4szHTTPResponse.tellcCs%|jjd}|dk r|jr8tjddSyTtdd|jdD}t|dkrtd||j }Wnt k rd}YnX|d krd}yt |j }Wnt k rd }YnX|dksd |ko d kns|dkr!d }|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.cSsg|]}t|qSr)int)rAvalrrr rCVs z-HTTPResponse._init_length..rDrz8Content-Length contained multiple unmatching values (%s)r0dHEAD)rr) rWrjrTlogwarningsetrElenrpop ValueErrorrrX)rrslengthlengthsrXrrr rl<s0  "      4zHTTPResponse._init_lengthcsjjddj}jdkr|jkrKt|_nId|krfdd|jdD}t|rt|_dS)z= Set-up the _decoder attribute if necessary. zcontent-encodingrPNrDcs1g|]'}|jjkr|jqSr)r@CONTENT_DECODERS)rAe)rrr rC|s z.HTTPResponse._init_decoder..)rWrjrkr_rr?rEr)rcontent_encodingrvr)rr _init_decoderps  zHTTPResponse._init_decodercCs|s |Sy|jr(|jj|}WnV|jk r}z3|jjddj}td||WYdd}~XnX|r||j7}|S)zN Decode the data passed in and potentially flush the decoder. zcontent-encodingrPzEReceived response with content-encoding: %s, but failed to decode it.N)r_r%DECODER_ERROR_CLASSESrWrjrkr _flush_decoder)rr'rU flush_decoderrrrrr _decodes zHTTPResponse._decodecCs0|jr,|jjd}||jjSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r)r_r%r=)rbufrrr rs zHTTPResponse._flush_decoderccs*d}zy dVWntk r;t|jddYntk r}z/dt|krbt|jddWYdd}~Xn>ttfk r}ztd||WYdd}~XnXd}Wd|s|jr|jj |j r|j j |jr%|jj 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) SocketTimeoutr rgrstrrr|r rbcloserhrr{)r clean_exitrrrr _error_catchers(   '&     zHTTPResponse._error_catcherc Cso|j|dkr|j}|jdkr2dSd}t|jdd}|j|dkr|s{|jjnd}d}nwd}|s|jj|nd}|dkr| r|jjd}|jr|jdkrt |j |jWdQRX|rk|j t |7_ |jdk rG|jt |8_|j |||}|rk||_ |S)aP Similar to :meth:`httplib.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)rrUrar"rrQrr]rmr rcrrr`)ramtrUr~r fp_closedr'rrr rQs8         zHTTPResponse.readr-r2ccsz|jr=|jr=x^|j|d|D] }|Vq+Wn9x6t|jsu|jd|d|}|r@|Vq@WdS)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. rUrN)rTsupports_chunked_reads read_chunkedrrarQ)rrrUliner'rrr stream+s zHTTPResponse.streamcKs|j}t|tsBtr3t|j}ntj|}t|dd}|d|d|d|jd|jd|j d|d||}|S) a  Given an :class:`httplib.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[rrnrWrXrYrZrp) rdrVrritems from_httplibr"rXrYrZ) ResponseClsr response_kwrWr[resprrr rEs     zHTTPResponse.from_httplibcCs|jS)N)rW)rrrr getheadersfszHTTPResponse.getheaderscCs|jj||S)N)rWrj)rr#defaultrrr getheaderiszHTTPResponse.getheadercCs|jS)N)rW)rrrr infomszHTTPResponse.infocCsI|js|jj|jr,|jj|jsEtjj|dS)N)rrarrhr^ioIOBase)rrrr rqs      zHTTPResponse.closecCsr|jstjjj|S|jdkr/dSt|jdrN|jjSt|jdrj|jjSdSdS)NTrr)r^rrr__get__rar;r)rrrr r{s   zHTTPResponse.closedcCsM|jdkrtdn+t|jdr=|jjStddS)Nz-HTTPResponse has no file to get a fileno fromfilenozOThe file-like object this HTTPResponse is wrapped around has no file descriptor)raIOErrorr;r)rrrr rs  zHTTPResponse.filenocCsH|jdk rDt|jdrDt|jdd rD|jjSdS)Nr=rF)rar;r"r=)rrrr r=szHTTPResponse.flushcCsdS)NTr)rrrr readableszHTTPResponse.readablecCsO|jt|}t|dkr+dS||dt|r?rrNrrrr s2     :