a oe)x@sddlmZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z z,z ddlZWneyddlZYn0WneydZYn0ddlmZddlmZdd 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.edurdGddde+Z/Gddde+Z0ddZ1Gdddej2Z3dS))absolute_importN)contextmanager)error)timeout)util)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!1C:\Program Files\Certbot\pkgs\urllib3\response.py__init__*szDeflateDecoder.__init__cCs t|j|SNgetattrrr namer!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)r dataZ decompressedr!r!r"r*2s&   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"r1Isr1c@s$eZdZddZddZddZdS) GzipDecodercCstdtj|_tj|_dSN)rrr+rr1r3_staterr!r!r"r#QszGzipDecoder.__init__cCs t|j|Sr$r%r'r!r!r"r)UszGzipDecoder.__getattr__cCst}|jtjks|st|Sz||j|7}Wn:tjyl|j}tj|_|tj krft|YSYn0|jj }|st|Stj |_t dtj |_qdSr7) bytearrayr9r1r5bytesrr*rrr4 unused_datarr+)r r,retZprevious_stater!r!r"r*Xs   zGzipDecoder.decompressNr-r!r!r!r"r6Psr6c@seZdZddZddZdS) BrotliDecodercCs0t|_t|jdr"|jj|_n |jj|_dS)Nr*)brotliZ Decompressorrhasattrr*Zprocessrr!r!r"r#ts   zBrotliDecoder.__init__cCst|jdr|jSdS)Nflushr)r@rrArr!r!r"rA{s  zBrotliDecoder.flushN)r.r/r0r#rAr!r!r!r"r>psr>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)r Zmodesr!r!r"r#szMultiDecoder.__init__cCs|jdSNr)rJrArr!r!r"rAszMultiDecoder.flushcCst|jD]}||}q |Sr$)reversedrJr*)r r,dr!r!r"r*s zMultiDecoder.decompressN)r.r/r0__doc__r#rAr*r!r!r!r"rBsrBcCs:d|vrt|S|dkrtStdur4|dkr4tStS)NrHgzipbr)rBr6r?r>r)moder!r!r"rCsrCc@sneZdZdZddgZedur&edg7ZgdZdLd d Zd dZddZ ddZ e ddZ e ddZ ddZddZddZddZeejfZedureejf7Zdd 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. rOZdeflateNrP)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-encodingrScss|]}|VqdSr$)rD)rEencr!r!r" rz(HTTPResponse.__init__..rHchunkedTdecode_content) isinstancerheadersstatusversionreasonstrictrYretriesenforce_content_length auto_close_decoder_body_fp_original_response_fp_bytes_readmsg _request_urlrZ string_typesr;_pool _connectionr@rW chunk_leftgetlowerrI _init_lengthlength_remainingrT)r bodyr[r\r]r^r_Zpreload_contentrYoriginal_responseZpool connectionrhr`rarequest_methodZ request_urlrbZtr_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[rmrr!r!r"get_redirect_location s  z"HTTPResponse.get_redirect_locationcCs(|jr |jsdS|j|jd|_dSr$)rjrkZ _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)rTr SocketErrorr r rr!r!r" drain_conn s zHTTPResponse.drain_conncCs"|jr |jS|jr|jddSdS)NT) cache_content)rdrerTrr!r!r"r,+szHTTPResponse.datacCs|jSr$)rkrr!r!r"rs4szHTTPResponse.connectioncCs t|jSr$)rrerr!r!r"isclosed8szHTTPResponse.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). )rgrr!r!r"tell;szHTTPResponse.tellcCs|jd}|dur|jr(tddSz.rHrz8Content-Length contained multiple unmatching values (%s)r)i0dZHEAD) r[rmrWlogZwarningsetrIlenrpop ValueErrorrr\)r rtlengthZlengthsr\r!r!r"roCs6       &zHTTPResponse._init_lengthcshjdd}jdurd|jvr2t|_n2d|vrdfdd|dD}t|rdt|_dS)z= Set-up the _decoder attribute if necessary. content-encodingrSNrHcs"g|]}|jvr|qSr!)rDCONTENT_DECODERS)rEerr!r"rGsz.HTTPResponse._init_decoder..)r[rmrnrcrrCrIr)r content_encodingrur!rr" _init_decoderws    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. rrSzEReceived response with content-encoding: %s, but failed to decode it.N)rcr*DECODER_ERROR_CLASSESr[rmrnr _flush_decoder)r r,rY 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)rcr*rA)r bufr!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) SocketTimeoutrrjr strrr rzrrfcloserkr}ry)r Z clean_exitrr!r!r"_error_catchers6    "$     zHTTPResponse._error_catchercCs|js Jd}|r||ks*|jr|j|krtjstjs@tjdkrt}d}|dus\|dkr|durxt ||}||8}n|}|j |}|sq| |~qL| S|dur|j |S|j SdS)a_ Read a response with the thought that reading the number of bytes larger than can fit in a 32-bit int at a time via SSL in some known cases leads to an overflow error that has to be prevented if `amt` or `self.length_remaining` indicate that a problem may happen. The known cases: * 3.8 <= CPython < 3.9.7 because of a bug https://github.com/urllib3/urllib3/issues/2513#issuecomment-1152559900. * urllib3 injected with pyOpenSSL-backed SSL-support. * CPython < 3.10 only when `amt` does not fit 32-bit int. i) iNr) rerprZIS_SECURETRANSPORTZ IS_PYOPENSSLsys version_infoioBytesIOminrTwritegetvalue)r amtZ c_int_maxbufferZ max_chunk_amtZ chunk_amtr,r!r!r"_fp_reads<     zHTTPResponse._fp_readcCs ||dur|j}|jdur$dSd}t|jdd}|l|sN||nd}|dur`d}nrBrCrrRr!r!r!r"s<          0