o d)x@sddlmZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z zzddlZWn ey@ddlZYnwWn eyMdZYnwddlmZddlmZdd lmZmZdd lmZmZmZmZmZmZmZm Z m!Z!m"Z"dd l#m$Z$dd l%m&Z&m'Z'e(e)Z*Gd dde+Z,Gddde+Z-Gddde+Z.edurGddde+Z/Gddde+Z0ddZ1Gdddej2Z3dS))absolute_importN)contextmanager)error)timeout)util)HTTPHeaderDict) BaseSSLError HTTPException) BodyNotHttplibCompatible DecodeError HTTPErrorIncompleteReadInvalidChunkLength InvalidHeader ProtocolErrorReadTimeoutErrorResponseNotChunkedSSLError)six) is_fp_closedis_response_to_headc@$eZdZddZddZddZdS)DeflateDecodercCsd|_d|_t|_dS)NT) _first_try_datazlib decompressobj_objselfr"k/private/var/folders/v1/_jykv66s6qd26_69j1njbrl80000gr/T/pip-target-p1gutpg6/lib/python/urllib3/response.py__init__*szDeflateDecoder.__init__cC t|j|SNgetattrrr!namer"r"r# __getattr__/ zDeflateDecoder.__getattr__c Cs|s|S|js |j|S|j|7_z|j|}|r#d|_d|_|WStjyId|_ttj |_z ||jWd|_YSd|_ww)NF)rr decompressrrrr MAX_WBITS)r!data decompressedr"r"r#r-2s$   zDeflateDecoder.decompressN__name__ __module__ __qualname__r$r+r-r"r"r"r#r)s rc@seZdZdZdZdZdS)GzipDecoderStaterrN)r2r3r4 FIRST_MEMBER OTHER_MEMBERS SWALLOW_DATAr"r"r"r#r5Isr5c@r) GzipDecodercCstdtj|_tj|_dS)N)rrr.rr5r7_stater r"r"r#r$Qs zGzipDecoder.__init__cCr%r&r'r)r"r"r#r+Ur,zGzipDecoder.__getattr__cCst}|jtjks |st|S z ||j|7}Wntjy5|j}tj|_|tj kr4t|YSw|jj }|s@t|Stj |_t dtj |_q)NTr;) bytearrayr<r5r9bytesrr-rrr8 unused_datarr.)r!r/retprevious_stater"r"r#r-Xs&  zGzipDecoder.decompressNr1r"r"r"r#r:Ps r:c@seZdZddZddZdS) BrotliDecodercCs2t|_t|jdr|jj|_dS|jj|_dS)Nr-)brotliZ Decompressorrhasattrr-processr r"r"r#r$ts  zBrotliDecoder.__init__cCst|jdr |jSdS)Nflushr)rDrrFr r"r"r#rF{s  zBrotliDecoder.flushN)r2r3r4r$rFr"r"r"r#rBps rBc@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# sz)MultiDecoder.__init__..,)split _decoders)r!modesr"r"r#r$szMultiDecoder.__init__cCs|jdSNr)rOrFr r"r"r#rFszMultiDecoder.flushcCst|jD]}||}q|Sr&)reversedrOr-)r!r/dr"r"r#r-s zMultiDecoder.decompressN)r2r3r4__doc__r$rFr-r"r"r"r#rGs  rGcCs:d|vrt|S|dkrtStdur|dkrtStS)NrMgzipbr)rGr:rCrBr)moder"r"r#rHsrHc@seZdZdZddgZeduredg7ZgdZ              dLd d Zd dZddZ ddZ e ddZ e ddZ ddZddZddZddZeejfZedurdeejf7Zdd Zd!d"Zed#d$Zd%d&ZdMd'd(ZdNd*d+Zed,d-Zd.d/ZdOd0d1Z d2d3Z!d4d5Z"e d6d7Z#d8d9Z$d:d;Z%dd?Z'd@dAZ(dBdCZ)dDdEZ*dPdFdGZ+dHdIZ,dJdKZ-dS)Q 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. rUdeflateNrV)i-i.i/i3i4rTFcCst|tr ||_nt||_||_||_||_||_||_| |_||_ ||_ d|_ d|_ d|_ | |_d|_| |_||_|rHt|tjtfrH||_ | |_| |_t|drV||_ d|_d|_|jdd}dd|dD}d |vrvd |_|||_|r|j s|j|d |_ dSdSdS) NrreadFztransfer-encodingrZcss|]}|VqdSr&)rI)rJencr"r"r# sz(HTTPResponse.__init__..rMchunkedTdecode_content) isinstancerheadersstatusversionreasonstrictr`retriesenforce_content_length auto_close_decoder_body_fp_original_response_fp_bytes_readmsg _request_urlr string_typesr>_pool _connectionrDr^ chunk_leftgetlowerrN _init_lengthlength_remainingr[)r!bodyrbrcrdrerfpreload_contentr`original_responsepool connectionrorgrhrequest_method request_urlritr_enc encodingsr"r"r#r$sD     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)rcREDIRECT_STATUSESrbrur r"r"r#get_redirect_location s  z"HTTPResponse.get_redirect_locationcCs(|jr|jsdS|j|jd|_dSr&)rrrs _put_connr r"r"r# release_conns  zHTTPResponse.release_connc Cs,z|WdSttttfyYdSw)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)r[r SocketErrorr r r r"r"r# drain_conn s zHTTPResponse.drain_conncCs"|jr|jS|jr|jddSdS)NT) cache_content)rkrlr[r r"r"r#r/+s  zHTTPResponse.datacC|jSr&)rsr r"r"r#r}4szHTTPResponse.connectioncCs t|jSr&)rrlr r"r"r#isclosed8s zHTTPResponse.isclosedcCr)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). )rnr r"r"r#tell;szHTTPResponse.tellcCs|jd}|durD|jrtddSztdd|dD}t|dkr-td|| }Wn t y=d}Ynw|d krDd}zt |j }Wn t yVd }Ynw|d vsjd |kred ksjn|d krld }|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)rJvalr"r"r#rL]sz-HTTPResponse._init_length..rMrz8Content-Length contained multiple unmatching values (%s)r)i0dHEAD) rbrur^logwarningsetrNlenrpop ValueErrorrrc)r!r~lengthlengthsrcr"r"r#rwCs:     &zHTTPResponse._init_lengthcsvjdd}jdur5|jvrt|_dSd|vr7fdd|dD}t|r9t|_dSdSdSdS)z= Set-up the _decoder attribute if necessary. content-encodingrZNrMcs"g|] }|jvr|qSr")rICONTENT_DECODERS)rJer r"r#rLs  z.HTTPResponse._init_decoder..)rbrurvrjrrHrNr)r!content_encodingrr"r r# _init_decoderws   zHTTPResponse._init_decoderc Csn|s|Sz |jr|j|}Wn|jy,}z|jdd}td||d}~ww|r5||7}|S)zN Decode the data passed in and potentially flush the decoder. rrZzEReceived response with content-encoding: %s, but failed to decode it.N)rjr-DECODER_ERROR_CLASSESrbrurvr _flush_decoder)r!r/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)rjr-rF)r!bufr"r"r#rs zHTTPResponse._flush_decoderc cs d}zkzdVWn>tyt|jddty3}zdt|vr(t|t|jddd}~wttfyG}ztd||d}~wwd}W|s]|j rU|j |j r]|j |j rk|j rm| dSdSdS|s|j ry|j |j r|j |j r|j r| www)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) SocketTimeoutrrrr strrr rrrmclosersrr)r! clean_exitrr"r"r#_error_catchersB          zHTTPResponse._error_catchercCs|jsJd}|r ||ks|jr]|j|kr]tjs]tjs tjdkr]t}d}|dus.|dkrY|dur WddS | |j dkrIn| |}|j||dd}|r[|Vq?|rg|}|rg|V |j j }|sqn|dkrvnqh|jr|jWddSWddS1swYdS) 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.NTrF)r`rs )rr^rrr rrmrrrlrrrtrrrr)r!rr`rdecodedrr"r"r#rsZ         ("zHTTPResponse.read_chunkedcCs*|jdurt|jjr|jjdjS|jS)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)rgrhistoryredirect_locationrpr r"r"r#geturl[szHTTPResponse.geturlccsg}|jddD]7}d|vr;|d}d||ddV|ddD]}|dVq&|dr8|dg}q g}q ||q |rKd|VdSdS)NTr_ rrrr)rrNjoinappend)r!rrxr"r"r#__iter__fs    zHTTPResponse.__iter__)rZNrrNrTTNNNNNFNNT)NNF)rNr&)NN).r2r3r4rTrrCrr$rrrpropertyr/r}rrrwrrrrrrrrrrr[r classmethodrrrrrrrrFrrrrrrrrr"r"r"r#rXs~   C   4   7 0 H         F rX)4 __future__rrloggingrrr contextlibrsocketrrrrZ brotlicffirC ImportErrorrZr _collectionsrr}r r exceptionsr r r rrrrrrrpackagesrZ util.responserr getLoggerr2robjectrr5r:rBrGrHrrXr"r"r"r#sD          0