U o al@s\ddlmZddlmZddlZddlZddlZddlmZ ddlm Z z ddl Z Wne k rldZ YnXddlmZddlmZmZmZmZmZmZmZdd lmZmZdd lmZdd lm Z m!Z!dd l"m#Z#m$Z$e%e&Z'Gd dde(Z)Gddde(Z*Gddde(Z+e dk r.Gddde(Z,Gddde(Z-ddZ.Gdddej/Z0dS))absolute_import)contextmanagerN)timeout)error)HTTPHeaderDict)BodyNotHttplibCompatible ProtocolError DecodeErrorReadTimeoutErrorResponseNotChunkedIncompleteRead InvalidHeader) string_typesPY3) http_client) HTTPException BaseSSLError) is_fp_closedis_response_to_headc@s$eZdZddZddZddZdS)DeflateDecodercCsd|_d|_t|_dS)NT) _first_try_datazlib decompressobj_objselfr]C:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-6mt8ur68\pip\_vendor\urllib3\response.py__init__!szDeflateDecoder.__init__cCs t|j|SNgetattrrrnamerrr __getattr__&szDeflateDecoder.__getattr__c Cs|s|S|js|j|S|j|7_z"|j|}|rFd|_d|_|WStjk rd|_ttj |_z||jWYSd|_XYnXdS)NF)rr decompressrrrr MAX_WBITS)rdata decompressedrrr r()s"  zDeflateDecoder.decompressN__name__ __module__ __qualname__r!r'r(rrrr r src@seZdZdZdZdZdS)GzipDecoderStaterrN)r-r.r/ FIRST_MEMBER OTHER_MEMBERS SWALLOW_DATArrrr r0@sr0c@s$eZdZddZddZddZdS) GzipDecodercCstdtj|_tj|_dSN)rrr)rr0r2_staterrrr r!HszGzipDecoder.__init__cCs t|j|Sr"r#r%rrr r'LszGzipDecoder.__getattr__cCst}|jtjks|st|Sz||j|7}Wn<tjk rn|j}tj|_|tj krht|YSYnX|jj }|st|Stj |_t dtj |_qdSr6) bytearrayr8r0r4bytesrr(rrr3 unused_datarr))rr*retprevious_staterrr r(Os   zGzipDecoder.decompressNr,rrrr r5Gsr5c@s$eZdZddZddZddZdS) BrotliDecodercCst|_dSr")brotliZ Decompressorrrrrr r!kszBrotliDecoder.__init__cCs$t|jdr|j|S|j|S)Nr()hasattrrr(process)rr*rrr r(ns  zBrotliDecoder.decompresscCst|jdr|jSdS)Nflushr)r@rrBrrrr rBss  zBrotliDecoder.flushN)r-r.r/r!r(rBrrrr r>gsr>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|dD|_dS)NcSsg|]}t|qSr) _get_decoderstrip).0mrrr sz)MultiDecoder.__init__..,)split _decoders)rmodesrrr r!szMultiDecoder.__init__cCs|jdSNr)rKrBrrrr rBszMultiDecoder.flushcCst|jD]}||}q |Sr")reversedrKr()rr*drrr r(s zMultiDecoder.decompressN)r-r.r/__doc__r!rBr(rrrr rCysrCcCs:d|krt|S|dkrtStdk r4|dkr4tStS)NrIgzipbr)rCr5r?r>r)moderrr rDsrDc@sdeZdZdZddgZedk r&edg7Zdddd d gZdLddZddZddZ e ddZ e ddZ ddZ ddZddZdd ZeejfZedk reejf7Zd!d"Zd#d$Zed%d&ZdMd'd(ZdNd*d+Zed,d-Zd.d/ZdOd0d1Zd2d3Zd4d5Z e d6d7Z!d8d9Z"d:d;Z#dd?Z%d@dAZ&dBdCZ'dDdEZ(dPdFdGZ)dHdIZ*dJdKZ+dS)Q 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. rQdeflateNrRi-i.i/i3i4rTFcCst|tr||_n t||_||_||_||_||_||_| |_||_ ||_ d|_ d|_ d|_ | |_d|_| |_||_|rt|ttfr||_ | |_| |_t|dr||_ d|_d|_|jdd}dd|dD}d |krd |_|||_|r|j s|j|d |_ dS) NrreadFztransfer-encodingrVcss|]}|VqdSr")rE)rFencrrr sz(HTTPResponse.__init__..rIchunkedTdecode_content) isinstancerheadersstatusversionreasonstrictr\retriesenforce_content_length auto_close_decoder_body_fp_original_response_fp_bytes_readmsg _request_url basestringr:_pool _connectionr@rZ chunk_leftgetlowerrJ _init_lengthlength_remainingrW)rbodyr^r_r`rarbpreload_contentr\original_responsepool connectionrkrcrdrequest_method request_urlretr_enc encodingsrrr r!sB    zHTTPResponse.__init__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)r_REDIRECT_STATUSESr^rqrrrr get_redirect_locations  z"HTTPResponse.get_redirect_locationcCs(|jr |jsdS|j|jd|_dSr")rnro _put_connrrrr release_conns zHTTPResponse.release_conncCs"|jr |jS|jr|jddSdS)NT) cache_content)rgrhrWrrrr r*szHTTPResponse.datacCs|jSr")rorrrr ry!szHTTPResponse.connectioncCs t|jSr")rrhrrrr isclosed%szHTTPResponse.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). )rjrrrr tell(szHTTPResponse.tellcCs|jd}|dk r|jr(tddSz.rIrz8Content-Length contained multiple unmatching values (%s)r)i0dHEAD) r^rqrZlogwarningsetrJlenrpop ValueErrorrr_)rrzlengthlengthsr_rrr rs0s6     &zHTTPResponse._init_lengthcshjdd}jdkrd|jkr2t|_n2d|krdfdd|dD}t|rdt|_dS)z= Set-up the _decoder attribute if necessary. content-encodingrVNrIcs"g|]}|jkr|qSr)rECONTENT_DECODERS)rFerrr rHosz.HTTPResponse._init_decoder..)r^rqrrrfrrDrJr)rcontent_encodingr}rrr _init_decoderds    zHTTPResponse._init_decoderc Csx|s|Sz|jr|j|}WnD|jk rb}z$|jdd}td||W5d}~XYnX|rt||7}|S)zN Decode the data passed in and potentially flush the decoder. rrVzEReceived response with content-encoding: %s, but failed to decode it.N)rfr(DECODER_ERROR_CLASSESr^rqrrr _flush_decoder)rr*r\ flush_decoderrrrrr _decode{s  zHTTPResponse._decodecCs$|jr |jd}||jSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r)rfr(rB)rbufrrr rs zHTTPResponse._flush_decoderc csd}zz dVWntk r2t|jddYnptk rn}z dt |krPt|jddW5d}~XYn4t t fk r}zt d||W5d}~XYnXd}W5|s|jr|j|jr|j|jr|jr|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) ricloserorr SocketTimeoutr rnrstrr SocketErrorr )r clean_exitrrrr _error_catchers(      zHTTPResponse._error_catcherc Cs ||dkr|j}|jdkr$dSd}t|jdd}|v|dkr`|sV|jnd}d}nPd}|st|j|nd}|dkr|s|jd}|jr|jdkrt |j |jW5QRX|r|j t |7_ |jdk r|jt |8_| |||}|r||_ |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)rr\rhr$rrWrrdrtr rjrrrg)ramtr\rr fp_closedr*rrr rWs:     zHTTPResponse.readccsR|jr*|r*|j||dD] }|Vqn$t|jsN|j||d}|r*|Vq*dS)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[)rr\N)rZsupports_chunked_reads read_chunkedrrhrW)rrr\liner*rrr streams  zHTTPResponse.streamc Ks`|j}t|ts,tr"t|}n t|}t|dd}|f|||j|j|j ||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``. rbr)rur^r_r`rarbrw) rkr]rritems from_httplibr$r_r`ra) ResponseClsr response_kwr^rbresprrr r9s$     zHTTPResponse.from_httplibcCs|jSr"r^rrrr getheadersZszHTTPResponse.getheaderscCs|j||Sr")r^rq)rr&defaultrrr getheader]szHTTPResponse.getheadercCs|jSr"rrrrr infoaszHTTPResponse.infocCs6|js|j|jr |j|js2tj|dSr")rrhrroreioIOBaserrrr res   zHTTPResponse.closecCsT|jstjj|S|jdkr"dSt|jdr8|jSt|jdrL|jjSdSdS)NTrr)rerrr__get__rhr@rrrrr ros    zHTTPResponse.closedcCs6|jdkrtdnt|jdr*|jStddS)Nz-HTTPResponse has no file to get a fileno fromfilenozOThe file-like object this HTTPResponse is wrapped around has no file descriptor)rhIOErrorr@rrrrr r|s    zHTTPResponse.filenocCs2|jdk r.t|jdr.t|jdds.|jSdS)NrBrF)rhr@r$rBrrrr rBs  zHTTPResponse.flushcCsdS)NTrrrrr readableszHTTPResponse.readablecCs:|t|}t|dkrdS||dt|<t|SdSrM)rWr)rbtemprrr readintos  zHTTPResponse.readintocCs t|jdS)z Checks if the underlying file-like object looks like a httplib.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@rhrrrr rsz#HTTPResponse.supports_chunked_readscCsf|jdk rdS|jj}|ddd}zt|d|_Wn&tk r`|t |YnXdS)N;rrr7) rprhrreadlinerJrrrhttplibr )rrrrr _update_chunk_lengths  z!HTTPResponse._update_chunk_lengthcCsd}|dkr2|j|j}|}|jdd|_nv||jkrZ|j|}|j||_|}nN||jkr|j|}|jdd|_|}n |j|j}|jdd|_|S)Nr1)rh _safe_readrp)rrreturned_chunkchunkvaluerrr _handle_chunks&        zHTTPResponse._handle_chunkc cs||jstd|s&td||jrXt|jrX|jW5QRdS|j j dkrrW5QRdS| |j dkrq| |}|j||dd}|rr|Vqr|r|}|r|V|j j }|sq|dkrqq|jr|jW5QRXdS)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.zgBody should be httplib.HTTPResponse like. It should have have an fp attribute which returns raw chunks.NrF)r\rs )rrZr rrrrirrrhrrrprrrr)rrr\rdecodedrrrr rsJ      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. N)rcrhistoryredirect_locationrlrrrr geturlszHTTPResponse.geturlccsg}|jddD]n}d|krt|d}d||ddV|ddD]}|dVqJ|drn|dg}q~g}q||q|rd|VdS)NTr[ rrrr)rrJjoinappend)rbufferrxrrr __iter__s    zHTTPResponse.__iter__)rVNrrNrTTNNNNNFNNT)NNF)rN)N)NN),r-r.r/rPrr?rr!rrpropertyr*ryrrrsrrrrrrrrrrWr classmethodrrrrrrrrBrrrrrrrrrrrr rTsx   C   4    8 J         F rT)1 __future__r contextlibrrrloggingsocketrrrrr? ImportError _collectionsr exceptionsrr r r r r rZ packages.sixrrmrZpackages.six.movesrrryrrZ util.responserr getLoggerr-robjectrr0r5r>rCrDrrTrrrr s2       $