VYY@sdddlmZddlmZddlZddlZddlZddlmZ ddlm Z ddl m Z ddlmZmZmZmZmZmZmZdd lmZmZmZdd lmZdd lmZm Z dd l!m"Z"m#Z#ej$e%Z&Gd dde'Z(Gddde'Z)ddZ*Gdddej+Z,dS))absolute_import)contextmanagerN)timeout)error)HTTPHeaderDict)BodyNotHttplibCompatible ProtocolError DecodeErrorReadTimeoutErrorResponseNotChunkedIncompleteRead InvalidHeader) string_types binary_typePY3) http_client) HTTPException BaseSSLError) is_fp_closedis_response_to_headc@s4eZdZddZddZddZdS)DeflateDecodercCs(d|_t|_tj|_dS)NT) _first_tryr_datazlib decompressobj_obj)selfrF/tmp/pip-build-04bmskau/requests/requests/packages/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)rdataZ decompressedrrrr$ s"    zDeflateDecoder.decompressN)__name__ __module__ __qualname__r r#r$rrrrrs   rc@s4eZdZddZddZddZdS) GzipDecodercCstjdtj|_dS)N)rrr%r)rrrrr 9szGzipDecoder.__init__cCst|j|S)N)r!r)rr"rrrr#<szGzipDecoder.__getattr__cCs|s |S|jj|S)N)rr$)rr&rrrr$?szGzipDecoder.decompressN)r'r(r)r r#r$rrrrr*7s   r*cCs|dkrtStS)Ngzip)r*r)moderrr _get_decoderEs r.c@seZdZdZddgZdddddgZd d d d d d d d d d d d d d ddZddZddZe ddZ e ddZ ddZ ddZ ddZddZd d!Zed"d#Zd d d d$d%ZdDd d(d)Zed*d+Zd,d-Zd d.d/Zd0d1Ze d2d3Zd4d5Zd6d7Zd8d9Zd:d;Zd<d=Zd>d?Z d@dAZ!d d dBdCZ"d S)E 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, attempts to decode specific content-encoding's based on headers (like 'gzip' and 'deflate') will be skipped and raw data will be used instead. :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. r,deflatei-i.i/i3i4NrTFcCs}t|tr||_nt||_||_||_||_||_||_| |_| |_ d|_ d|_ d|_ | |_ d|_|rt|ttfr||_ | |_| |_t|dr||_ d|_d|_|jjddj}dd|jdD}d |krBd |_|j||_|ry|j ry|jd ||_ dS) NrreadFztransfer-encodingr1css|]}|jVqdS)N)strip).0encrrr sz(HTTPResponse.__init__..,chunkedTdecode_content) isinstancerheadersstatusversionreasonstrictr9retriesenforce_content_length_decoder_body_fp_original_response_fp_bytes_read basestringr_pool _connectionhasattrr8 chunk_leftgetlowersplit _init_lengthlength_remainingr2)rbodyr;r<r=r>r?preload_contentr9original_responsepool connectionr@rArequest_methodtr_enc encodingsrrrr qs<                     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)r<REDIRECT_STATUSESr;rL)rrrrget_redirect_locationsz"HTTPResponse.get_redirect_locationcCs8|j s|j rdS|jj|jd|_dS)N)rHrI _put_conn)rrrr release_connszHTTPResponse.release_conncCs-|jr|jS|jr)|jddSdS)N cache_contentT)rCrDr2)rrrrr&s  zHTTPResponse.datacCs|jS)N)rI)rrrrrUszHTTPResponse.connectioncCs|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). )rF)rrrrtellszHTTPResponse.tellcCs1|jjd}|dk r8|jr8tjddS|dk ryTtdd|jdD}t|dkrtd||j }Wnt k rd}YnX|d krd}yt |j }Wnt k rd }YnX|dks'd |kod 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)r4valrrr s z-HTTPResponse._init_length..r7rz8Content-Length contained multiple unmatching values (%s)r0dHEAD)rcrd) r;rLr8logwarningsetrNlenrpop ValueErrorr`r<)rrVlengthZlengthsr<rrrrOs,  "      4zHTTPResponse._init_lengthcCsL|jjddj}|jdkrH||jkrHt||_dS)z= Set-up the _decoder attribute if necessary. zcontent-encodingr1N)r;rLrMrBCONTENT_DECODERSr.)rcontent_encodingrrr _init_decoderszHTTPResponse._init_decodercCsy%|r$|jr$|jj|}Wn\ttjfk r}z3|jjddj}td||WYdd}~XnX|r|r||j 7}|S)zN Decode the data passed in and potentially flush the decoder. zcontent-encodingr1zEReceived response with content-encoding: %s, but failed to decode it.N) rBr$IOErrorrrr;rLrMr _flush_decoder)rr&r9 flush_decodererprrr_decodes  zHTTPResponse._decodecCs0|jr,|jjd}||jjSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. )rBr$flush)rbufrrrrss 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 rHrstrr SocketErrorr rEcloserIisclosedr])r clean_exitrurrr_error_catcher!s(   '&     zHTTPResponse._error_catcherc CsH|j|dkr|j}|jdkr2dSd}d}|j|dkro|jj}d}nkd}|jj|}|dkr| r|jjd}|jr|jdkrt|j |jWdQRX|rD|j t |7_ |jdk r |jt |8_|j |||}|rD||_ |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.) NFTr)rN) rqr9rDrr2r}rArPr rFrkrvrC)ramtr9r^rtr&rrrr2Zs4        zHTTPResponse.readr+ccsz|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. r9rN)r8supports_chunked_reads read_chunkedrrDr2)rrr9liner&rrrstreams 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?rrQr;r<r=r>rS) msgr:rritems from_httplibr!r<r=r>) ResponseClsr response_kwr;r?resprrrrs      zHTTPResponse.from_httplibcCs|jS)N)r;)rrrr getheadersszHTTPResponse.getheaderscCs|jj||S)N)r;rL)rr"defaultrrr getheaderszHTTPResponse.getheadercCs0|js|jj|jr,|jjdS)N)closedrDr}rI)rrrrr}s   zHTTPResponse.closecCsV|jdkrdSt|jdr2|jjSt|jdrN|jjSdSdS)NTr~r)rDrJr~r)rrrrrs  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)rDrrrJr)rrrrrs  zHTTPResponse.filenocCs2|jdk r.t|jdr.|jjSdS)Nrx)rDrJrx)rrrrrxs!zHTTPResponse.flushcCsdS)NTr)rrrrreadableszHTTPResponse.readablecCsO|jt|}t|dkr+dS||dt|s"   4!