a *Na+n@sVddlmZddlZddlZddlZddlmZddlmZ ddlm Z z ddl Z Wne yjdZ Yn0ddlmZddlmZmZdd lmZmZmZmZmZmZmZmZmZmZdd lmZdd l m!Z!m"Z"e#e$Z%Gd d d e&Z'Gddde&Z(Gddde&Z)e dur(Gddde&Z*Gddde&Z+ddZ,Gdddej-Z.dS))absolute_importN)contextmanager)error)timeout)HTTPHeaderDict) BaseSSLError HTTPException) BodyNotHttplibCompatible DecodeError HTTPErrorIncompleteReadInvalidChunkLength InvalidHeader ProtocolErrorReadTimeoutErrorResponseNotChunkedSSLError)six) is_fp_closedis_response_to_headc@s$eZdZddZddZddZdS)DeflateDecodercCsd|_d|_t|_dS)NT) _first_try_datazlib decompressobj_objselfr k/private/var/folders/js/6pj4vh5d4zd0k6bxv74qrbhr0000gr/T/pip-target-22xwyzbs/lib/python/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!r#src@seZdZdZdZdZdS)GzipDecoderStaterrN)r.r/r0 FIRST_MEMBER OTHER_MEMBERS SWALLOW_DATAr r r r!r1Csr1c@s$eZdZddZddZddZdS) GzipDecodercCstdtj|_tj|_dSN)rrr*rr1r3_staterr r r!r"KszGzipDecoder.__init__cCs t|j|Sr#r$r&r r r!r(OszGzipDecoder.__getattr__cCst}|jtjks|st|Sz||j|7}Wn:tjyl|j}tj|_|tj krft|YSYn0|jj }|st|Stj |_t dtj |_qdSr7) bytearrayr9r1r5bytesrr)rrr4 unused_datarr*)rr+retprevious_stater r r!r)Rs   zGzipDecoder.decompressNr-r r r r!r6Jsr6c@seZdZddZddZdS) BrotliDecodercCs0t|_t|jdr"|jj|_n |jj|_dS)Nr))brotliZ Decompressorrhasattrr)processrr r r!r"ns   zBrotliDecoder.__init__cCst|jdr|jSdS)Nflushr)rArrCrr r r!rCus  zBrotliDecoder.flushN)r.r/r0r"rCr r r r!r?jsr?c@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).0mr r r! rz)MultiDecoder.__init__..,)split _decoders)rmodesr r r!r"szMultiDecoder.__init__cCs|jdSNr)rLrCrr r r!rCszMultiDecoder.flushcCst|jD]}||}q |Sr#)reversedrLr))rr+dr r r!r)s zMultiDecoder.decompressN)r.r/r0__doc__r"rCr)r r r r!rD{srDcCs:d|vrt|S|dkrtStdur4|dkr4tStS)NrJgzipbr)rDr6r@r?r)moder r r!rEsrEc@sfeZdZdZddgZedur&edg7ZgdZdJd d Zd dZddZ ddZ e ddZ e ddZ ddZddZddZddZeejfZedureejf7Zdd Zd!d"Zed#d$ZdKd%d&ZdLd(d)Zed*d+Zd,d-ZdMd.d/Zd0d1Z d2d3Z!e d4d5Z"d6d7Z#d8d9Z$d:d;Z%dd?Z'd@dAZ(dBdCZ)dNdDdEZ*dFdGZ+dHdIZ,dS)O HTTPResponsea HTTP Response container. Backwards-compatible with :class:`http.client.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 :class:`http.client.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 :class:`http.client.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. rRdeflateNrS)i-i.i/i3i4rTFcCst|tr||_n t||_||_||_||_||_||_| |_||_ ||_ d|_ d|_ d|_ | |_d|_| |_||_|rt|tjtfr||_ | |_| |_t|dr||_ d|_d|_|jdd}dd|dD}d |vrd |_|||_|r|j s|j|d |_ dS) NrreadFztransfer-encodingrWcss|]}|VqdSr#)rF)rGencr r r! rz(HTTPResponse.__init__..rJchunkedTdecode_content) isinstancerheadersstatusversionreasonstrictr]retriesenforce_content_length auto_close_decoder_body_fp_original_response_fp_bytes_readmsg _request_urlr string_typesr;_pool _connectionrAr[ chunk_leftgetlowerrK _init_lengthlength_remainingrX)rbodyr_r`rarbrcpreload_contentr]original_responsepool connectionrlrdrerequest_method request_urlrftr_enc encodingsr r r!r"sB    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)r`REDIRECT_STATUSESr_rrrr r r!get_redirect_locations  z"HTTPResponse.get_redirect_locationcCs(|jr |jsdS|j|jd|_dSr#)rorp _put_connrr r r! release_conns zHTTPResponse.release_connc Cs,z |Wnttttfy&Yn0dS)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)rXr SocketErrorrr rr r r! drain_conns zHTTPResponse.drain_conncCs"|jr |jS|jr|jddSdS)NT) cache_content)rhrirXrr r r!r+%szHTTPResponse.datacCs|jSr#)rprr r r!rz.szHTTPResponse.connectioncCs t|jSr#)rrirr r r!isclosed2szHTTPResponse.isclosedcCs|jS)z Obtain the number of bytes pulled over the wire so far. May differ from the amount of content returned by :meth:``urllib3.response.HTTPResponse.read`` if bytes are encoded on the wire (e.g, compressed). )rkrr r r!tell5szHTTPResponse.tellcCs|jd}|dur|jr(tddSz.rJrz8Content-Length contained multiple unmatching values (%s)r)i0dHEAD) r_rrr[logwarningsetrKlenrpop ValueErrorrr`)rr{lengthlengthsr`r r r!rt=s6       &zHTTPResponse._init_lengthcshjdd}jdurd|jvr2t|_n2d|vrdfdd|dD}t|rdt|_dS)z= Set-up the _decoder attribute if necessary. content-encodingrWNrJcs"g|]}|jvr|qSr )rFCONTENT_DECODERS)rGerr r!rI|sz.HTTPResponse._init_decoder..)r_rrrsrgrrErKr)rcontent_encodingr~r rr! _init_decoderqs    zHTTPResponse._init_decoderc Csz|s|Sz|jr|j|}WnF|jyd}z,|jdd}td||WYd}~n d}~00|rv||7}|S)zN Decode the data passed in and potentially flush the decoder. rrWzEReceived response with content-encoding: %s, but failed to decode it.N)rgr)DECODER_ERROR_CLASSESr_rrrsr _flush_decoder)rr+r] flush_decoderrrr r r!_decodes  zHTTPResponse._decodecCs$|jr |jd}||jSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r)rgr)rC)rbufr r r!rs zHTTPResponse._flush_decoderc cs:d}zz dVWnty0t|jddYnztyt}z.dt|vrRt|t|jddWYd}~n>d}~0ttfy}ztd||WYd}~n d}~00d}W|s|j r|j |j r|j |j r|j r| nH|s|j r|j |j r|j |j r4|j r4| 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) SocketTimeoutrrorstrrr rrrjcloserprr)r clean_exitrr r r!_error_catchers6    "$     zHTTPResponse._error_catchercCs"||dur|j}|jdur$dSd}t|jdd}||dur`|sV|jnd}d}nPd}|st|j|nd}|dkr|s|jd}|jr|jdvrt |j |jWdn1s0Y|r|j t |7_ |jdur|jt |8_| |||}|r||_ |S)aT Similar to :meth:`http.client.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)rr]rir%rrXrrerur rkrrrh)ramtr]rr fp_closedr+r r r!rXs:    , 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. r\)rr]N)r[supports_chunked_reads read_chunkedrrirX)rrr]liner+r r r!stream+s  zHTTPResponse.streamc Ksb|j}t|ts.tjr"t|}n t|}t|dd}|f|||j|j |j ||d|}|S)a Given an :class:`http.client.HTTPResponse` instance ``r``, return a corresponding :class:`urllib3.response.HTTPResponse` object. Remaining parameters are passed to the HTTPResponse constructor, along with ``original_response=r``. rcr)rvr_r`rarbrcrx) rlr^rrPY2 from_httplibitemsr%r`rarb) ResponseClsr response_kwr_rcrespr r r!rEs$      zHTTPResponse.from_httplibcCs|jSr#r_rr r r! getheadersfszHTTPResponse.getheaderscCs|j||Sr#)r_rr)rr'defaultr r r! getheaderiszHTTPResponse.getheadercCs|jSr#rrr r r!infomszHTTPResponse.infocCs6|js|j|jr |j|js2tj|dSr#)rrirrprfioIOBaserr r r!rqs   zHTTPResponse.closecCsT|jstjj|S|jdur"dSt|jdr8|jSt|jdrL|jjSdSdS)NTrr)rfrrr__get__rirArrr r r!r{s    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)riIOErrorrArrr r r!rs    zHTTPResponse.filenocCs2|jdur.t|jdr.t|jdds.|jSdS)NrCrF)rirAr%rCrr r r!rCs  zHTTPResponse.flushcCsdS)NTr rr r r!readableszHTTPResponse.readablecCs:|t|}t|dkrdS||dt|<t|SdSrN)rXr)rbtempr r r!readintos  zHTTPResponse.readintocCs t|jdS)a Checks if the underlying file-like object looks like a :class:`http.client.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)rArirr r r!rsz#HTTPResponse.supports_chunked_readscCsd|jdurdS|jj}|ddd}zt|d|_Wn$ty^|t||Yn0dS)N;rrr8) rqrirreadlinerKrrrr)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)Nr2)ri _safe_readrq)rrreturned_chunkchunkvaluer r r! _handle_chunks&        zHTTPResponse._handle_chunkccs||jstd|s&td||jrZt|jrZ|jWddS|j j durvWddS| |j dkrq| |}|j||dd}|rv|Vqv|r|}|r|V|j j }|sq|dkrqq|jr|jWdn1s 0YdS)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.zkBody should be http.client.HTTPResponse like. It should have have an fp attribute which returns raw chunks.NrF)r]rs )rr[rrr rrjrrrirrrqrrrr)rrr]rdecodedrr r r!rsF      zHTTPResponse.read_chunkedcCs.|jdur$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)rdrhistoryredirect_locationrmrr r r!geturlszHTTPResponse.geturlccsg}|jddD]n}d|vrt|d}d||ddV|ddD]}|dVqJ|drn|dg}q~g}q||q|rd|VdS)NTr\ rrrr)rrKjoinappend)rbufferrxr r r!__iter__&s    zHTTPResponse.__iter__)rWNrrNrTTNNNNNFNNT)NNF)rN)N)NN)-r.r/r0rQrr@rr"rrrpropertyr+rzrrrtrrrrrrrrrrXr classmethodrrrrrrrrCrrrrrrrrr r r r!rUsz   C    4    7 J         F rU)/ __future__rrloggingr contextlibrsocketrrrrr@ ImportError _collectionsrrzrr exceptionsr r r r rrrrrrpackagesrZ util.responserr getLoggerr.robjectrr1r6r?rDrErrUr r r r!s0        0