B 0`8n@s`ddlmZddlmZddlZddlZddlZddlmZ ddlm Z y ddl Z Wne k rldZ YnXddlmZddlmZmZmZmZmZmZmZmZdd lmZmZdd lmZdd l m!Z!m"Z"dd l#m$Z$m%Z%e&e'Z(Gd dde)Z*Gddde)Z+Gddde)Z,e dk r2Gddde)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@s$eZdZddZddZddZdS)DeflateDecodercCsd|_d|_t|_dS)NT) _first_try_datazlib decompressobj_obj)selfrk/private/var/folders/sf/wxz_36012wxg_prv29r6623x5vlqx8/T/pip-target-tft40_m7/lib/python/urllib3/response.py__init__"szDeflateDecoder.__init__cCs t|j|S)N)getattrr)rnamerrr __getattr__'szDeflateDecoder.__getattr__c Cs|s|S|js|j|S|j|7_y |j|}|rFd|_d|_|Stjk rd|_ttj |_z ||jSd|_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 r,Asr,c@s$eZdZddZddZddZdS) GzipDecodercCstdtj|_tj|_dS)N)rrr&rr,r._state)rrrr r!IszGzipDecoder.__init__cCs t|j|S)N)r"r)rr#rrr r$MszGzipDecoder.__getattr__cCst}|jtjks|st|Sxy||j|7}Wn8tjk rl|j}tj|_|tj krft|SYnX|jj }|st|Stj |_t dtj |_q WdS)Nr2) bytearrayr3r,r0bytesrr%rrr/ unused_datarr&)rr'retprevious_staterrr r%Ps" zGzipDecoder.decompressN)r)r*r+r!r$r%rrrr r1Hsr1c@seZdZddZddZdS) BrotliDecodercCs0t|_t|jdr"|jj|_n |jj|_dS)Nr%)brotli Decompressorrhasattrr%process)rrrr r!ls   zBrotliDecoder.__init__cCst|jdr|jSdS)Nflushr)r<rr>)rrrr r>ss  zBrotliDecoder.flushN)r)r*r+r!r>rrrr r9hsr9c@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|jdS)Nr)rGr>)rrrr r>szMultiDecoder.flushcCs"xt|jD]}||}q W|S)N)reversedrGr%)rr'drrr r%szMultiDecoder.decompressN)r)r*r+__doc__r!r>r%rrrr r?ysr?cCs:d|krt|S|dkrtStdk r4|dkr4tStS)NrEgzipbr)r?r1r:r9r)moderrr r@sr@c@sleZdZdZddgZedk r&edg7Zdddd d gZdNddZddZddZ ddZ e ddZ e ddZ ddZddZdd Zd!d"ZeejfZedk reejf7Zd#d$Zd%d&Zed'd(ZdOd)d*ZdPd,d-Zed.d/Zd0d1ZdQd2d3Zd4d5Z d6d7Z!e d8d9Z"d:d;Z#dd?Z%d@dAZ&dBdCZ'dDdEZ(dFdGZ)dRdHdIZ*dJdKZ+dLdMZ,dS)S 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. rLdeflateNrMi-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-encodingrQcss|]}|VqdS)N)rA)rBencrrr sz(HTTPResponse.__init__..rEchunkedT)decode_content) isinstancerheadersstatusversionreasonstrictrVretriesenforce_content_length auto_close_decoder_body_fp_original_response_fp_bytes_readmsg _request_url basestringr5_pool _connectionr<rU chunk_leftgetlowerrF _init_lengthlength_remainingrR)rbodyrXrYrZr[r\preload_contentrVoriginal_responsepool connectionrer]r^request_method request_urlr_tr_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)rYREDIRECT_STATUSESrXrk)rrrr get_redirect_locations  z"HTTPResponse.get_redirect_locationcCs(|jr |jsdS|j|jd|_dS)N)rhri _put_conn)rrrr release_conns zHTTPResponse.release_connc Cs.y |Wnttttfk 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 SocketErrorrr)rrrr drain_conns zHTTPResponse.drain_conncCs"|jr |jS|jr|jddSdS)NT) cache_content)rarbrR)rrrr r'#szHTTPResponse.datacCs|jS)N)ri)rrrr rs,szHTTPResponse.connectioncCs t|jS)N)rrb)rrrr isclosed0szHTTPResponse.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). )rd)rrrr tell3szHTTPResponse.tellcCs|jd}|dk r|jr(tddSy.rErz8Content-Length contained multiple unmatching values (%s)r)i0dHEAD) rXrkrUlogwarningsetrFlenrpop ValueErrorrrY)rrtlengthlengthsrYrrr rm;s0     &zHTTPResponse._init_lengthcshjdd}jdkrd|jkr2t|_n2d|krdfdd|dD}t|rdt|_dS)z= Set-up the _decoder attribute if necessary. zcontent-encodingrQNrEcs"g|]}|jkr|qSr)rACONTENT_DECODERS)rBe)rrr rD{sz.HTTPResponse._init_decoder..)rXrkrlr`rr@rFr)rcontent_encodingrwr)rr _init_decoderos    zHTTPResponse._init_decoderc Csx|s|Sy|jr|j|}WnD|jk rb}z$|jdd}td||Wdd}~XYnX|rt||7}|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_decoderrrrrr _decodes zHTTPResponse._decodecCs$|jr |jd}||jSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r)r`r%r>)rbufrrr rs zHTTPResponse._flush_decoderc csd}zy dVWntk r2t|jddYnptk rn}z dt|krPt|jddWdd}~XYn4ttfk r}ztd||Wdd}~XYnXd}Wd|s|jr|j |j r|j |jr|j r| 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 rhrstrrr}r rccloserirr|)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 |jWdQRX|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)rrVrbr"rrRrr^rnr rdrrra)ramtrVrr fp_closedr'rrr rRs6     zHTTPResponse.readccsZ|jr.|r.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'rrr stream*s  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``. r\r)rorXrYrZr[r\rq) rerWrritems from_httplibr"rYrZr[) ResponseClsr response_kwrXr\resprrr rDs    zHTTPResponse.from_httplibcCs|jS)N)rX)rrrr getheaderseszHTTPResponse.getheaderscCs|j||S)N)rXrk)rr#defaultrrr getheaderhszHTTPResponse.getheadercCs|jS)N)rX)rrrr infolszHTTPResponse.infocCs6|js|j|jr |j|js2tj|dS)N)rrbrrir_ioIOBase)rrrr rps   zHTTPResponse.closecCsT|jstjj|S|jdkr"dSt|jdr8|jSt|jdrL|jjSdSdS)NTrr)r_rrr__get__rbr<r)rrrr rzs    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)rbIOErrorr<r)rrrr rs     zHTTPResponse.filenocCs2|jdk r.t|jdr.t|jdds.|jSdS)Nr>rF)rbr<r"r>)rrrr r>s  zHTTPResponse.flushcCsdS)NTr)rrrr readableszHTTPResponse.readablecCs:|t|}t|dkrdS||dt|<t|SdS)Nr)rRr)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<rb)rrrr rsz#HTTPResponse.supports_chunked_readscCsf|jdk rdS|jj}|ddd}yt|d|_Wn&tk r`|t |YnXdS)N;rrr2) rjrbrreadlinerFrrrhttplibr )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)Nr-)rb _safe_readrj)rrreturned_chunkchunkvaluerrr _handle_chunks&        zHTTPResponse._handle_chunkc cs||jstd|s&td||jrNt|jrN|jdS|j j dkr^dSx<| |j dkrtP| |}|j||dd}|r`|Vq`W|r|}|r|Vx |j j }|sP|dkrPqW|jr|jWdQRXdS)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)rVrs )rrUr rrrrcrrrbrrrjrrrr)rrrVrdecodedrrrr 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. N)r]rhistoryredirect_locationrf)rrrr geturlszHTTPResponse.geturlccsg}x|jddD]r}d|krz|d}d||ddVx|ddD]}|dVqNW|drt|dg}qg}q||qW|rd|VdS)NT)rV rrrr)rrFjoinappend)rbufferrxrrr __iter__%s  zHTTPResponse.__iter__)rQNrrNrTTNNNNNFNNT)NNF)rN)N)NN)-r)r*r+rKrr:ryr!rzr|r~propertyr'rsrrrmrrrrrrrrrrRr classmethodrrrrrrrr>rrrrrrrrrrrr rOsn  1  4   9 J  !      F rO)2 __future__r contextlibrrrloggingsocketrrrr}r: ImportError _collectionsr exceptionsrr r r r r rrZ packages.sixrrgrZpackages.six.movesrrrsrrZ util.responserr getLoggerr)robjectrr,r1r9r?r@rrOrrrr s2       (