B k]#j@s\ddlmZddlmZddlZddlZddlZddlmZ ddlm Z y 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_obj)selfri/private/var/folders/_z/lgy7p8v17sqbdp14fgmxdlrsbydzry/T/pip-install-3xgtll61/urllib3/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 decompressedrrrr$%s"   zDeflateDecoder.decompressN)__name__ __module__ __qualname__r r#r$rrrrrsrc@seZdZdZdZdZdS)GzipDecoderStaterrN)r(r)r* FIRST_MEMBER OTHER_MEMBERS SWALLOW_DATArrrrr+<sr+c@s$eZdZddZddZddZdS) GzipDecodercCstdtj|_tj|_dS)N)rrr%rr+r-_state)rrrrr EszGzipDecoder.__init__cCs t|j|S)N)r!r)rr"rrrr#IszGzipDecoder.__getattr__cCst}|jtjks|st|Sxy||j|7}Wn8tjk rl|j}tj|_|tj krft|SYnX|jj }|st|Stj |_t dtj |_q WdS)Nr1) bytearrayr2r+r/bytesrr$rrr. unused_datarr%)rr&retprevious_staterrrr$Ls" zGzipDecoder.decompressN)r(r)r*r r#r$rrrrr0Csr0c@s$eZdZddZddZddZdS) BrotliDecodercCst|_dS)N)brotliZ Decompressorr)rrrrr gszBrotliDecoder.__init__cCs$t|jdr|j|S|j|S)Nr$)hasattrrr$process)rr&rrrr$js  zBrotliDecoder.decompresscCst|jdr|jSdS)Nflushr)r:rr<)rrrrr<os  zBrotliDecoder.flushN)r(r)r*r r$r<rrrrr8csr8c@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)rmodesrrrr ~szMultiDecoder.__init__cCs|jdS)Nr)rEr<)rrrrr<szMultiDecoder.flushcCs"xt|jD]}||}q W|S)N)reversedrEr$)rr&drrrr$szMultiDecoder.decompressN)r(r)r*__doc__r r<r$rrrrr=usr=cCs:d|krt|S|dkrtStdk r4|dkr4tStS)NrCgzipbr)r=r0r9r8r)moderrrr>sr>c@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. rJdeflateNrKi-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-encodingrOcss|]}|VqdS)N)r?)r@encrrr sz(HTTPResponse.__init__..rCchunkedT)decode_content) isinstancerheadersstatusversionreasonstrictrTretriesenforce_content_length_decoder_body_fp_original_response_fp_bytes_readmsg _request_url basestringr4_pool _connectionr:rS chunk_leftgetlowerrD _init_lengthlength_remainingrP)rbodyrVrWrXrYrZpreload_contentrToriginal_responsepool connectionrbr[r\request_method request_urltr_enc encodingsrrrr s@    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)rWREDIRECT_STATUSESrVrh)rrrrget_redirect_locations  z"HTTPResponse.get_redirect_locationcCs(|jr |jsdS|j|jd|_dS)N)rerf _put_conn)rrrr release_conns zHTTPResponse.release_conncCs"|jr |jS|jr|jddSdS)NT) cache_content)r^r_rP)rrrrr&szHTTPResponse.datacCs|jS)N)rf)rrrrrp szHTTPResponse.connectioncCs t|jS)N)rr_)rrrrisclosedszHTTPResponse.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). )ra)rrrrtellszHTTPResponse.tellcCs|jd}|dk r|jr(tddSy.rCrz8Content-Length contained multiple unmatching values (%s)r)i0dHEAD) rVrhrSlogwarningsetrDlenrpop ValueErrorr}rW)rrqlengthlengthsrWrrrrjs,      &zHTTPResponse._init_lengthcshjdd}jdkrd|jkr2t|_n2d|krdfdd|dD}t|rdt|_dS)z= Set-up the _decoder attribute if necessary. zcontent-encodingrONrCcs"g|]}|jkr|qSr)r?CONTENT_DECODERS)r@e)rrrrBXsz.HTTPResponse._init_decoder..)rVrhrir]rr>rDr)rcontent_encodingrtr)rr _init_decoderLs   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-encodingrOzEReceived response with content-encoding: %s, but failed to decode it.N)r]r$DECODER_ERROR_CLASSESrVrhrir _flush_decoder)rr&rT flush_decoderrrrrr_decodeas zHTTPResponse._decodecCs$|jr |jd}||jSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r)r]r$r<)rbufrrrrus 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 rerstrr SocketErrorr r`closerfr{ry)r clean_exitrrrr_error_catchers(      zHTTPResponse._error_catcherc Cs||dkr|j}|jdkr$dSd}d}|f|dkrN|j}d}nHd}|j|}|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.) NFTr)rN) rrTr_rrPrr\rkr rarrr^)ramtrTrzrr&rrrrPs4       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. )rT)rrTN)rSsupports_chunked_reads read_chunkedrr_rP)rrrTliner&rrrstreams  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``. rZr)rlrVrWrXrYrZrn) rbrUrritems from_httplibr!rWrXrY) ResponseClsr response_kwrVrZresprrrrs    zHTTPResponse.from_httplibcCs|jS)N)rV)rrrr getheaders7szHTTPResponse.getheaderscCs|j||S)N)rVrh)rr"defaultrrr getheader:szHTTPResponse.getheadercCs|jS)N)rV)rrrrinfo>szHTTPResponse.infocCs$|js|j|jr |jdS)N)closedr_rrf)rrrrrBs zHTTPResponse.closecCs@|jdkrdSt|jdr$|jSt|jdr8|jjSdSdS)NTr{r)r_r:r{r)rrrrrIs    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)r_IOErrorr:r)rrrrrTs     zHTTPResponse.filenocCs$|jdk r t|jdr |jSdS)Nr<)r_r:r<)rrrrr<]szHTTPResponse.flushcCsdS)NTr)rrrrreadableaszHTTPResponse.readablecCs:|t|}t|dkrdS||dt|<t|SdS)Nr)rPr)rbtemprrrreadintoes  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:r_)rrrrrnsz#HTTPResponse.supports_chunked_readscCsf|jdk rdS|jj}|ddd}yt|d|_Wn&tk r`|t |YnXdS)N;rrr1) rgr_rreadlinerDr}rrhttplibr )rrrrr_update_chunk_lengthws  z!HTTPResponse._update_chunk_lengthcCsd}|dkr2|j|j}|}|jdd|_nv||jkrZ|j|}|j||_|}nN||jkr|j|}|jdd|_|}n |j|j}|jdd|_|S)Nr,)r_ _safe_readrg)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)rTrs )rrSr rrrr`rrr_rrrgrrrr)rrrTrdecodedrrrrrsD       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_locationrc)rrrrgeturlszHTTPResponse.geturlccsdg}x|jddD]r}d|kr||d}d||ddVx|ddD]}|dVqPW|drv|dg}qg}q||qW|rd|VdS)NrT)rT rrr)rrDjoinappend)rbufferrxrrr__iter__s  zHTTPResponse.__iter__)rONrrNrTTNNNNNFNN)NNF)rN)N)NN),r(r)r*rIrr9rvr rwrypropertyr&rpr{r|rjrrrrrrrrrrPr classmethodrrrrrrrr<rrrrrrrrrrrrrMsT  /  0   9 E        C rM)1 __future__r contextlibrriologgingsocketrrrrr9 ImportError _collectionsr exceptionsrr r r r r rZ packages.sixrrdrZpackages.six.movesrrrprrZ util.responserr getLoggerr(robjectrr+r0r8r=r>IOBaserMrrrrs2       $  !