3 {^=n @s`ddlmZddlmZddlZddlZddlZddlmZ ddlm Z y ddl Z Wne k rldZ YnXddlmZddlmZmZmZmZmZmZmZmZdd lmZmZdd lmZdd l m!Z!m"Z"dd l#m$Z$m%Z%ej&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|_tj|_dS)NT) _first_try_datazlib decompressobj_obj)selfrf/home/sumanth/workspace/ghrepos/quickstart-citrix-adc-appfw/functions/source/appfw/urllib3/response.py__init__"szDeflateDecoder.__init__cCs t|j|S)N)getattrr)rnamerrr __getattr__'szDeflateDecoder.__getattr__cCs|s|S|js|jj|S|j|7_y |jj|}|rFd|_d|_|Stjk rd|_tjtj |_z |j|jSd|_XYnXdS)NF)rr decompressrrrr MAX_WBITS)rdataZ 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) GzipDecodercCstjdtj|_tj|_dS)N)rrr&rr+r-_state)rrrr r!IszGzipDecoder.__init__cCs t|j|S)N)r"r)rr#rrr r$MszGzipDecoder.__getattr__c Cst}|jtjks| r t|Sxy||jj|7}Wn8tjk rn|j}tj|_|tj krht|SYnX|jj }|st|Stj |_tj dtj |_q"WdS)Nr1) bytearrayr2r+r/bytesrr%rrr.Z unused_datarr&)rr'ZretZprevious_staterrr r%Ps" zGzipDecoder.decompressN)r(r)r*r!r$r%rrrr r0Hsr0c@s$eZdZddZddZddZdS) BrotliDecodercCstj|_dS)N)brotliZ Decompressorr)rrrr r!lszBrotliDecoder.__init__cCs$t|jdr|jj|S|jj|S)Nr%)hasattrrr%process)rr'rrr r%os  zBrotliDecoder.decompresscCst|jdr|jjSdS)Nflushr)r7rr9)rrrr r9ts  zBrotliDecoder.flushN)r(r)r*r!r%r9rrrr r5hsr5c@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|jdD|_dS)NcSsg|]}t|jqSr) _get_decoderstrip).0mrrr sz)MultiDecoder.__init__..,)split _decoders)rZmodesrrr r!szMultiDecoder.__init__cCs|jdjS)Nr)rBr9)rrrr r9szMultiDecoder.flushcCs"xt|jD]}|j|}q W|S)N)reversedrBr%)rr'drrr r%szMultiDecoder.decompressN)r(r)r*__doc__r!r9r%rrrr r:zsr:cCs:d|krt|S|dkrtStdk r4|dkr4tStS)Nr@gzipbr)r:r0r6r5r)moderrr r;sr;c@sleZdZdZddgZedk r&edg7Zdddd d gZdOddZddZddZ ddZ e ddZ e ddZ ddZddZdd Zd!d"ZeejfZedk reejf7Zd#d$Zd%d&Zed'd(ZdPd)d*ZdRd-d.Zed/d0Zd1d2ZdSd3d4Zd5d6Z d7d8Z!e d9d:Z"d;d<Z#d=d>Z$d?d@Z%dAdBZ&dCdDZ'dEdFZ(dGdHZ)dTdIdJZ*dKdLZ+dMdNZ,dS)U 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. rFZdeflateNrGi-i.i/i3i4rTFcCst|tr||_n t||_||_||_||_||_||_| |_||_ ||_ d|_ d|_ d|_ | |_d|_| |_||_|rt|ttfr||_ | |_| |_t|dr||_ d|_d|_|jjddj}dd|jdD}d |krd |_|j||_|r|j r|j|d |_ dS) NrreadFztransfer-encodingrJcss|]}|jVqdS)N)r<)r=encrrr sz(HTTPResponse.__init__..r@chunkedT)decode_content) isinstancerheadersstatusversionreasonstrictrOretriesenforce_content_length auto_close_decoder_body_fp_original_response_fp_bytes_readmsg _request_url basestringr4_pool _connectionr7rN chunk_leftgetlowerrA _init_lengthlength_remainingrK)rbodyrQrRrSrTrUpreload_contentrOoriginal_responsepool connectionr^rVrWrequest_method request_urlrXtr_enc encodingsrrr r!sB    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)rRREDIRECT_STATUSESrQrd)rrrr get_redirect_locations  z"HTTPResponse.get_redirect_locationcCs,|j s|j rdS|jj|jd|_dS)N)rarb _put_conn)rrrr release_connszHTTPResponse.release_conncCs.y |jWnttttfk 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)rKr SocketErrorrr)rrrr drain_conns zHTTPResponse.drain_conncCs"|jr |jS|jr|jddSdS)NT) cache_content)rZr[rK)rrrr r'$szHTTPResponse.datacCs|jS)N)rb)rrrr rl-szHTTPResponse.connectioncCs t|jS)N)rr[)rrrr isclosed1szHTTPResponse.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). )r])rrrr tell4szHTTPResponse.tellcCs|jjd}|dk r|jr(tjddSy.r@rz8Content-Length contained multiple unmatching values (%s)r0dHEAD)r}r~) rQrdrNlogwarningsetrAlenrpop ValueErrorr{rR)rrmlengthZlengthsrRrrr rf<s0     (zHTTPResponse._init_lengthcshjjddj}jdkrd|jkr2t|_n2d|krdfdd|jdD}t|rdt|_dS)z= Set-up the _decoder attribute if necessary. zcontent-encodingrJNr@cs"g|]}|jjkr|jqSr)r<CONTENT_DECODERS)r=e)rrr r?|sz.HTTPResponse._init_decoder..)rQrdrerYrr;rAr)rcontent_encodingrpr)rr _init_decoderps    zHTTPResponse._init_decodercCsx|s|Sy|jr|jj|}WnD|jk rb}z&|jjddj}td||WYdd}~XnX|rt||j7}|S)zN Decode the data passed in and potentially flush the decoder. zcontent-encodingrJzEReceived response with content-encoding: %s, but failed to decode it.N)rYr%DECODER_ERROR_CLASSESrQrdrer _flush_decoder)rr'rO flush_decoderrrrrr _decodes zHTTPResponse._decodecCs$|jr |jjd}||jjSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r)rYr%r9)rZbufrrr rs zHTTPResponse._flush_decoderccsd}zy dVWntk r2t|jddYnptk rn}z"dt|krPt|jddWYdd}~Xn4ttfk 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 rarstrrrvr r\closerbryru)r clean_exitrrrr _error_catchers(      zHTTPResponse._error_catcherc Cs|j|dkr|j}|jdkr$dSd}t|jdd}|jx|dkr`|sV|jjnd}d}nRd}|st|jj|nd}|dkr| r|jjd}|jr|jdkrt |j |jWdQRX|r |j t |7_ |jdk r|jt |8_|j |||}|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)rrOr[r"rrKrrWrgr r]rrrZ)ramtrOrxrZ fp_closedr'rrr rKs8     zHTTPResponse.readr,r1ccsZ|jr.|jr.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. )rO)rrON)rNsupports_chunked_reads read_chunkedrr[rK)rrrOliner'rrr stream+s  zHTTPResponse.streamc Ks`|j}t|ts,tr"t|j}n tj|}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``. rUr)rhrQrRrSrTrUrj) r^rPrritems from_httplibr"rRrSrT) ResponseClsr response_kwrQrUZresprrr rEs    zHTTPResponse.from_httplibcCs|jS)N)rQ)rrrr getheadersfszHTTPResponse.getheaderscCs|jj||S)N)rQrd)rr#defaultrrr getheaderiszHTTPResponse.getheadercCs|jS)N)rQ)rrrr infomszHTTPResponse.infocCs6|js|jj|jr |jj|js2tjj|dS)N)rr[rrbrXioIOBase)rrrr rqs   zHTTPResponse.closecCsT|jstjjj|S|jdkr"dSt|jdr8|jjSt|jdrL|jjSdSdS)NTryr)rXrrr__get__r[r7ry)rrrr r{s    zHTTPResponse.closedcCs6|jdkrtdnt|jdr*|jjStddS)Nz-HTTPResponse has no file to get a fileno fromfilenozOThe file-like object this HTTPResponse is wrapped around has no file descriptor)r[IOErrorr7r)rrrr rs     zHTTPResponse.filenocCs4|jdk r0t|jdr0t|jdd r0|jjSdS)Nr9rF)r[r7r"r9)rrrr r9s  zHTTPResponse.flushcCsdS)NTr)rrrr readableszHTTPResponse.readablecCs:|jt|}t|dkrdS||dt|<t|SdS)Nr)rKr)rbZtemprrr 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)r7r[)rrrr rsz#HTTPResponse.supports_chunked_readsc Csf|jdk rdS|jjj}|jddd}yt|d|_Wn&tk r`|jtj |YnXdS)N;rrr1) rcr[rreadlinerAr{rrhttplibr )rrrrr _update_chunk_lengths  z!HTTPResponse._update_chunk_lengthcCsd}|dkr2|jj|j}|}|jjdd|_nv||jkrZ|jj|}|j||_|}nN||jkr|jj|}|jjdd|_|}n |jj|j}|jjdd|_|S)Nr,)r[ _safe_readrc)rrZreturned_chunkchunkvaluerrr _handle_chunks&        zHTTPResponse._handle_chunkc cs|j|jstd|js&td|j|jrNt|jrN|jjdS|j j dkr^dSx<|j |j dkrtP|j |}|j||dd}|r`|Vq`W|r|j}|r|Vx |j j j}|sP|dkrPqW|jr|jjWdQRXdS)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)rOrs )rrNr rrrr\rrr[rrrcrrrr)rrrOrdecodedrrrr 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. Nr)rVrhistoryredirect_locationr_)rrrr geturlszHTTPResponse.geturlccsg}x|jddD]r}d|krz|jd}dj||ddVx|ddD]}|dVqNW|drt|d g}qg}q|j|qW|rdj|VdS) NT)rO rrrrrr)rrAjoinappend)rbufferrxrrr __iter__&s  zHTTPResponse.__iter__)rJNrrNrTTNNNNNFNNT)NNF)rN)N)NN)-r(r)r*rErr6rrr!rsrurwpropertyr'rlryrzrfrrrrrrrrrrKr classmethodrrrrrrrr9rrrrrrrrrrrr rIsn  1  4   9 J  !      F rI)2 __future__r contextlibrrrloggingsocketrrrrvr6 ImportError _collectionsr exceptionsrr r r r r rrZ packages.sixrr`rpackages.six.movesrrrlrr util.responserr getLoggerr(robjectrr+r0r5r:r;rrIrrrr s2       (