a YY@sddlmZddlmZddlZddlZddlZddlmZ ddlm Z ddl m Z ddlmZmZmZmZmZmZmZdd lmZmZmZdd lmZdd lmZm Z dd l!m"Z"m#Z#e$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@s$eZdZddZddZddZdS)DeflateDecodercCsd|_t|_t|_dSNT) _first_tryr_datazlib decompressobj_objselfr I/Users/ymaher/Downloads/lambdas_org/requests/packages/urllib3/response.py__init__szDeflateDecoder.__init__cCs t|j|SNgetattrrrnamer r r! __getattr__szDeflateDecoder.__getattr__c Cs|s|S|js|j|S|j|7_z"|j|}|rFd|_d|_|WStjyd|_ttj |_z||jWd|_YSd|_0Yn0dS)NF)rr decompressrrrr MAX_WBITS)rdata decompressedr r r!r) s&   zDeflateDecoder.decompressN__name__ __module__ __qualname__r"r(r)r r r r!rsrc@s$eZdZddZddZddZdS) GzipDecodercCstdtj|_dS)N)rrr*rrr r r!r"9szGzipDecoder.__init__cCs t|j|Sr#r$r&r r r!r(<szGzipDecoder.__getattr__cCs|s|S|j|Sr#)rr))rr+r r r!r)?szGzipDecoder.decompressNr-r r r r!r17sr1cCs|dkrtStS)Ngzip)r1r)moder r r! _get_decoderEsr5c@seZdZdZddgZgdZd?d d Zd d ZddZe ddZ e ddZ ddZ ddZ ddZddZddZeddZd@d d!ZdAd#d$Zed%d&Zd'd(ZdBd)d*Zd+d,Ze d-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Z d;d<Z!dCd=d>Z"dS)D 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. r3deflate)i-i.i/i3i4NrTFcCst|tr||_n t||_||_||_||_||_||_| |_| |_ d|_ d|_ d|_ | |_ d|_|r|t|ttfr|||_ | |_| |_t|dr||_ d|_d|_|jdd}dd|dD}d |vrd |_|||_|r|j s|j|d |_ dS) NrreadFztransfer-encodingr8css|]}|VqdSr#)strip).0encr r r! z(HTTPResponse.__init__..,chunkedTdecode_content) isinstancerheadersstatusversionreasonstrictrBretriesenforce_content_length_decoder_body_fp_original_response_fp_bytes_read basestringr_pool _connectionhasattrr@ chunk_leftgetlowersplit _init_lengthlength_remainingr9)rbodyrDrErFrGrHpreload_contentrBoriginal_responsepool connectionrIrJrequest_methodtr_enc encodingsr r r!r"qs<     zHTTPResponse.__init__cCs|j|jvr|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)rEREDIRECT_STATUSESrDrUrr r r!get_redirect_locations  z"HTTPResponse.get_redirect_locationcCs(|jr |jsdS|j|jd|_dSr#)rQrR _put_connrr r r! release_conns zHTTPResponse.release_conncCs"|jr |jS|jr|jddSdS)NT) cache_content)rLrMr9rr r r!r+szHTTPResponse.datacCs|jSr#)rRrr r r!r^szHTTPResponse.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). )rOrr r r!tellszHTTPResponse.tellcCs|jd}|dur(|jr(tddS|durzr>z-HTTPResponse._init_length..r?rz8Content-Length contained multiple unmatching values (%s)r)i0dHEAD) rDrUr@logwarningsetrWlenrpop ValueErrorrirE)rr_lengthlengthsrEr r r!rXs.        &zHTTPResponse._init_lengthcCs4|jdd}|jdur0||jvr0t||_dS)z= Set-up the _decoder attribute if necessary. content-encodingr8N)rDrUrVrKCONTENT_DECODERSr5)rcontent_encodingr r r! _init_decoderszHTTPResponse._init_decoderc Cs~z|r|jr|j|}WnJttjfyd}z,|jdd}td||WYd}~n d}~00|rz|rz|| 7}|S)zN Decode the data passed in and potentially flush the decoder. rxr8zEReceived response with content-encoding: %s, but failed to decode it.N) rKr)IOErrorrrrDrUrVr _flush_decoder)rr+rB flush_decodererzr r r!_decodes  zHTTPResponse._decodecCs$|jr |jd}||jSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r>)rKr)flush)rbufr r r!r}s zHTTPResponse._flush_decoderc cs2d}zz dVWnty0t|jddYnttyn}z(dt|vrLt|jddWYd}~n>d}~0ttfy}ztd||WYd}~n d}~00d}W|s|jr|j |j r|j |jr|j r| nF|s|jr|j |j r|j |jr,|j r,| 0dS)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 rQrstrr SocketErrorr rNcloserRisclosedrf)r clean_exitrr r r!_error_catcher!s6    "$     zHTTPResponse._error_catchercCs||dur|j}|jdur$dSd}d}|p|durN|j}d}nHd}|j|}|dkr|s|jd}|jr|jdvrt|j |jWdn1s0Y|r|j t |7_ |jdur|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) r{rBrMrr9rrJrYr rOrsrrL)ramtrBrgr~r+r r r!r9Zs4      , 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. rA)rrBN)r@supports_chunked_reads read_chunkedrrMr9)rrrBliner+r r r!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``. rHr)rZrDrErFrGrHr\) msgrCrritems from_httplibr%rErFrG) ResponseClsr response_kwrDrHrespr r r!rs"    zHTTPResponse.from_httplibcCs|jSr#)rDrr r r! getheadersszHTTPResponse.getheaderscCs|j||Sr#)rDrU)rr'defaultr r r! getheaderszHTTPResponse.getheadercCs$|js|j|jr |jdSr#)closedrMrrRrr r r!rs zHTTPResponse.closecCs@|jdurdSt|jdr$|jSt|jdr8|jjSdSdS)NTrr)rMrSrrrr r r!rs    zHTTPResponse.closedcCs6|jdurtdnt|jdr*|jStddS)Nz-HTTPResponse has no file to get a fileno fromfilenozOThe file-like object this HTTPResponse is wrapped around has no file descriptor)rMr|rSrrr r r!rs     zHTTPResponse.filenocCs$|jdur t|jdr |jSdS)Nr)rMrSrrr r r!rszHTTPResponse.flushcCsdSrr rr r r!readableszHTTPResponse.readablecCs:|t|}t|dkrdS||dt|<t|SdS)Nr)r9rs)rbtempr r r!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)rSrMrr r r!r sz#HTTPResponse.supports_chunked_readscCsd|jdurdS|jj}|ddd}zt|d|_Wn$ty^|t |Yn0dS)N;rrr2) rTrMrreadlinerWrirurhttplibr )rrr r r!_update_chunk_lengths   z!HTTPResponse._update_chunk_lengthcCsd}|dur2|j|j}|}|jdd|_nv||jkrZ|j|}|j||_|}nN||jkr|j|}|jdd|_|}n |j|j}|jdd|_|S)N)rM _safe_readrT)rrreturned_chunkchunkvaluer r r! _handle_chunk!s&        zHTTPResponse._handle_chunkccs||jstd|s&td|jrDt|jrD|jdS|| |j dkrbq| |}|j ||dd}|rN|VqN|r| }|r|V|jj}|sq|dkrqq|jr|jWdn1s0YdS)z Similar to :meth:`HTTPResponse.read`, but with an additional parameter: ``decode_content``. :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)rBr~s )r{r@r rrrNrrrrrTrrr}rMrr)rrrBrdecodedrr r r!r7sB      zHTTPResponse.read_chunked)r8NrrNrTTNNNNFN)NNF)rN)N)NN)#r.r/r0__doc__ryrcr"rdrfpropertyr+r^rhrXr{rr}rrr9r classmethodrrrrrrrrrrrrrr r r r!r6LsL! 0   0   8 E       r6)- __future__r contextlibrriologgingsocketrrrr _collectionsr exceptionsrr r r r r rZ packages.sixrrPrrpackages.six.movesrrr^rr util.responserr getLoggerr.rpobjectrr1r5IOBaser6r r r r!s"     $  !