o ?cv@sddlmZddlZddlZddlZddlZddlmZddlm Z ddlm Z zzddl ZWn ey<ddlZYnwWn eyIdZYnwddlmZddlmZdd lmZmZdd lmZmZmZmZmZmZmZmZm 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*Z/ddZ0Gdddej1Z2dS))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/cw/wlscbxl13mj6wd668h7l9g9sllkg5j/T/pip-target-b31awkwq/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-1s$   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#r5Hsr5c@r) GzipDecodercCstdtj|_tj|_dS)N)rrr.rr5r7_stater r"r"r#r$Ps zGzipDecoder.__init__cCr%r&r'r)r"r"r#r+Tr,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-Ws&  zGzipDecoder.decompressNr1r"r"r"r#r:Os r:c@seZdZddZddZdS) BrotliDecodercCs2t|_t|jdr|jj|_dS|jj|_dS)Nr-)brotliZ Decompressorrhasattrr-processr r"r"r#r$ss  zBrotliDecoder.__init__cCst|jdr |jSdS)Nflushr)rDrrFr r"r"r#rFzs  zBrotliDecoder.flushN)r2r3r4r$rFr"r"r"r#rBos 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#rFzMultiDecoder.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. rVdeflateNrW)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-encodingr[css|]}|VqdSr&)rI)rJencr"r"r# sz(HTTPResponse.__init__..rMchunkedTdecode_content) isinstancerheadersstatusversionreasonstrictraretriesenforce_content_length auto_close_decoder_body_fp_original_response_fp_bytes_readmsg _request_urlr string_typesr>_pool _connectionrDr_ chunk_leftgetlowerrN _init_lengthlength_remainingr\)r!bodyrcrdrerfrgpreload_contentraoriginal_responsepool connectionrprhrirequest_method request_urlrjtr_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)rdREDIRECT_STATUSESrcrvr r"r"r#get_redirect_location s  z"HTTPResponse.get_redirect_locationcCs(|jr|jsdS|j|jd|_dSr&)rsrt _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_conns zHTTPResponse.drain_conncCs"|jr|jS|jr|jddSdS)NT) cache_content)rlrmr\r r"r"r#r/*s  zHTTPResponse.datacC|jSr&)rtr r"r"r#r~3szHTTPResponse.connectioncCs t|jSr&)rrmr r"r"r#isclosed7s 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). )ror 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) rcrvr_logwarningsetrNlenrpop ValueErrorrrd)r!rlengthlengthsrdr"r"r#rxBs:     &zHTTPResponse._init_lengthcsvjdd}jdur5|jvrt|_dSd|vr7fdd|dD}t|r9t|_dSdSdSdS)z= Set-up the _decoder attribute if necessary. content-encodingr[NrMcs"g|] }|jvr|qSr")rICONTENT_DECODERS)rJer r"r#rLs  z.HTTPResponse._init_decoder..)rcrvrwrkrrHrNr)r!content_encodingrr"r r# _init_decodervs   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. rr[zEReceived response with content-encoding: %s, but failed to decode it.N)rkr-DECODER_ERROR_CLASSESrcrvrwr _flush_decoder)r!r/ra 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)rkr-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) SocketTimeoutrrsr strrr rrrnclosertrr)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)rars )rr_rrr rrnrrrmrrrurrrr)r!rrardecodedrr"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)rhrhistoryredirect_locationrqr r"r"r#geturlNszHTTPResponse.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__Ys    zHTTPResponse.__iter__)r[NrrNrTTNNNNNFNNT)NNF)rNr&)NN).r2r3r4rUrrCrr$rrrpropertyr/r~rrrxrrrrrrrrrrr\r classmethodrrrrrrrrFrrrrrrrrr"r"r"r#rYs~   C   4   7 0 H        F rY)3 __future__rrloggingrr contextlibrsocketrrrrZ brotlicffirC ImportErrorr[r _collectionsrr~r r exceptionsr r r rrrrrrrpackagesrZ util.responserr getLoggerr2robjectrr5r:rBrGrHrrYr"r"r"r#sB          0