
    F2j`                    H   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZ ddlmZ ddlmZ ddlmZmZmZmZ dd	lmZ dd
lmZmZmZmZmZmZmZm Z  ddl!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z* ej+        rd dl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3  G d de#          Z4dS )    )annotationsN)asynccontextmanager)timeout   )HTTPHeaderDict)
_TYPE_BODY)	C_INT_MAXCHUNK_AMT_MAXDECODE_GROWTH_FACTORDECODE_MIN_RAW_REFERENCE)AsyncLowLevelResponse)BaseSSLError	HTTPErrorIncompleteReadProtocolErrorReadTimeoutErrorResponseNotReadySSLErrorMustRedialError)ContentDecoderHTTPResponse)is_fp_closedBytesQueueBuffer)Retry   )AsyncHTTPConnection)Message)AsyncHTTPConnectionPool)AsyncExtensionFromHTTP)AsyncTrafficPolicec                  (   e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dHdId#ZdJd&ZedKd(            ZdLd)ZedMd+            Z	edNd-            Z
dOd0ZdPd2ZedQd4            ZdRdSd7Z	 dRdSd8Z	 	 dTdUd:Z	 	 	 dVdWd=Z	 	 	 dVd;d>dXd@Z	 dYdZdCZdLdDZd[dFZdLdGZdS )\AsyncHTTPResponse Nr   Tbodyr   headers>typing.Mapping[str, str] | typing.Mapping[bytes, bytes] | Nonestatusintversionreason
str | Nonepreload_contentbooldecode_contentoriginal_responseAsyncLowLevelResponse | NonepoolAsyncHTTPConnectionPool | None
connectionAsyncHTTPConnection | NonemsgMessage | NoneretriesRetry | Noneenforce_content_lengthrequest_methodrequest_url
auto_closepolice_officer.AsyncTrafficPolice[AsyncHTTPConnection] | NonereturnNonec                   t          |t                    r|| _        nt          |          | _        	 t          |          | _        n# t
          $ r
 d| _        Y nw xY w|| _        || _        || _        d| _	        || _
        d | _        d | _        || _        d| _        d| j        v rW| j                            dd                                          }d |                    d          D             }d|v rd| _        d | _        || _        || _        d | _        d| _        d | _        || _        d| _        |t5          j        d	t8          d
           || _        |r#t          |t<          t>          f          r|| _        |	| _         |
| _!        tE          |d          r|| _        d | _#        || _$        | %                    | j$                  | _&        tO                      | _(        d| _)        || _*        || _+        | j*        S| j*        ,                    | | j!                   | j*        j-        )| j*        j-        ,                    | | j                    d S d S d S )Nr   Fztransfer-encodingr#   c              3  >   K   | ]}|                                 V  d S N)strip).0encs     _/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/urllib3/_async/response.py	<genexpr>z-AsyncHTTPResponse.__init__.<locals>.<genexpr>X   s*      BBBBBBBB    ,chunkedTzjPassing msg=.. is deprecated and no-op in urllib3.future and is scheduled to be removed in a future major.r   )
stacklevelread).
isinstancer   r%   r(   r'   
ValueErrorr)   r*   r.   _has_decoded_content_request_url_retries
_extensionr7   rK   getlowersplit_decoderr9   r<   _body_uncached_read_occurred_fp_original_response_fp_bytes_readwarningswarnDeprecationWarningr5   strbytes_pool_connectionhasattr
chunk_left_request_method_init_lengthlength_remainingr   _decoded_buffer_last_raw_read_size_police_officer_preloaded_contentmemorizeparent)selfr$   r%   r'   r)   r*   r,   r.   r/   r1   r3   r5   r7   r9   r:   r;   r<   r=   tr_enc	encodingss                       rG   __init__zAsyncHTTPResponse.__init__,   s   ( g~.. 	3"DLL)'22DL	f++DKK 	 	 	DKKK	,$)!(3&*9=$,..\%%&92>>DDFFFBBS0A0ABBBII%%#/3&<#$
',$IM"3?M|"     	Jtc5\22 	DJ.2
0:4   	DH '+ ,:,0,=,=d>R,S,S  011
 )*   	 )8+ ))$0@AAA#*6$+44T4:FFFFF	 ,+ 76s   A AAb	bytearrayc                   K   |                      t          |                     d {V }t          |          dk    rdS ||d t          |          <   t          |          S )Nr   )rM   len)ro   rs   temps      rG   readintozAsyncHTTPResponse.readinto   sb      YYs1vv&&&&&&&&t99>>1!AkD		kNt99rI   !typing.AsyncGenerator[None, None]c               T  K   d}	 	 dW V  n# t           $ r}t          | j        dd          |d}~wt          $ r=}dt	          |          vrt          |          |t          | j        dd          |d}~wt          t          f$ r}t          d||          |d}~ww xY wd}|sF| j	        r| j	        
                                 | j        r| j        
                                 d{V  | j	        r/| j	                                        r|                                  dS dS dS # |sF| j	        r| j	        
                                 | j        r| j        
                                 d{V  | j	        r.| j	                                        r|                                  w w w xY w)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.
        FNzRead timed out.zread operation timed outzConnection broken: T)SocketTimeoutr   rb   r   r`   r   OSErrorr   r   r[   closerc   isclosedrelease_conn)ro   
clean_exites      rG   _error_catcherz AsyncHTTPResponse._error_catcher   sG      
+	$K  S S S 'tz49JKKQRR S S S-SVV;;"1++1,&tz49JKKQRR_- K K K#$?!$?$?CCJK J  3 * 4+11333
 # 3*00222222222 & $4+B+K+K+M+M $!!#####$ $ $ $  3 * 4+11333
 # 3*00222222222 & $4+B+K+K+M+M $!!####$ $s;    D' 
B .B 8A33B BB  D' 'B F'c                   K   	 |                                   d{V  n# t          t          t          f$ r Y nw xY w| j        rt                      | _        d| _        dS dS )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)	_raw_readr   r|   r   rP   r   ri   rW   ro   s    rG   
drain_connzAsyncHTTPResponse.drain_conn   s      	..""""""""""7L1 	 	 	D	$ 	! $4#5#5D  DMMM		! 	!s    99HTTPHeaderDict | Nonec                Z    | j         dS t          | j         d          r| j         j        S dS )z
        Retrieve post-response (trailing headers) if any.
        This WILL return None if no HTTP Trailer Headers have been received.
        Ntrailers)rZ   rd   r   r   s    rG   r   zAsyncHTTPResponse.trailers   s5     8448Z(( 	%8$$trI   AsyncExtensionFromHTTP | Nonec                    | j         S rC   )rS   r   s    rG   	extensionzAsyncHTTPResponse.extension   s
    rI   itemr   c                   K   | j         t          d          t          | j        d          st	                      |                    |            d {V  || _         d S )Nzextension already plugged in_dsa)rS   r|   rd   rZ   r   start)ro   r   s     rG   start_extensionz!AsyncHTTPResponse.start_extension   sl      ?&8999tx(( 	%"$$$jjrI   
typing.Anyc                D   K   t          j        | j         d{V           S )a  
        Parses the body of the HTTP response as JSON.

        To use a custom JSON decoder pass the result of :attr:`HTTPResponse.data` to the decoder.

        This method can raise either `UnicodeDecodeError` or `json.JSONDecodeError`.

        Read more :ref:`here <json>`.
        N)_jsonloadsdatar   s    rG   jsonzAsyncHTTPResponse.json  s&       {??????+++rI   ra   c                l   K   | j         r| j         S | j        r|                     d           d {V S d S )NT)cache_content)rX   rZ   rM   r   s    rG   r   zAsyncHTTPResponse.data  sO       : 	:8 	7666666666trI   amt
int | Nonec                  K   | j         sJ |r|t          k    s| j        r| j        t          k    rt          j        dk     rt          j                    }t          | j         t                    }||dk    r|t          |t                    }||z  }nt          }	 |r!| j                             |           d{V }n| j                             |          }n# t          $ r Y n%w xY w|sn|                    |           ~||dk    |                                S t          | j         t                    r | j                             |           d{V S || j                             |          n| j                                         S )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.
        )   
   Nr   )rZ   r	   rh   sysversion_infoioBytesIOrN   r   minr
   rM   rO   writegetvalue)ro   r   bufferis_async_ll	chunk_amtr   s         rG   _fp_readzAsyncHTTPResponse._fp_read  s      xx"	N9__% %*.*?)*K*K((Z\\F %TX/DEEK+? #C 7 7I9$CC -I" 8%)X]]9%=%=======#x}}Y77!   E T"""! +" ??$$$ $($9:: 0!X]]3/////////),48==%%%dhmmooMs   =C 
C%$C%c                2  K   | j         dS t          | j         dd          }|                                 4 d{V  |s|                     |           d{V nd}|du ot          | j         dd           }||dk    r|r|r|r6t	          |          }| xj        |z  c_        | j        | xj        |z  c_        | j                                          | j        r,| j        %| j        dk    rt          | j        | j                  	 ddd          d{V  n# 1 d{V swxY w Y   |r8|s6t	          |          }| xj        |z  c_        | j        | xj        |z  c_        t          |t          t          f          r|rt	          |          | _        |S )z7
        Reads `amt` of bytes from the socket.
        NclosedFrI   r   )rZ   getattrr   r   rv   r\   rh   r}   r9   r   rN   ra   rt   rj   )ro   r   	fp_closedr   is_foreign_fp_uncloseddata_lens         rG   r   zAsyncHTTPResponse._raw_readL  s      84DHh66	&&(( $	U $	U $	U $	U $	U $	U $	U $	U5>G$--,,,,,,,,,CD
 &)D[ &(E: : 6" C1HHTH>TH) :"4yyH''83'',8--9--    /
U-9-22 ))<d>STTTTI$	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	U $	UL  	2. 	24yyH8+$0%%1%%dUI.// 	1D 	1'*4yyD$s   CD
D$'D$bool | Nonec                   K   |                      |pd|           d{V }d| _        |Gt          |          |k    r4| j                            |           | j                            |          S |S )a  
        Similar to ``http.client.HTTPResponse.read1`` and documented
        in :meth:`io.BufferedReader.read1`, but with an additional parameter:
        ``decode_content``.

        :param amt:
            How much of the content to read.

        :param decode_content:
            If True, will attempt to decode the body based on the
            'content-encoding' header.
        )r   r.   NT)rM   rY   rv   ri   putrT   ro   r   r.   r   s       rG   read1zAsyncHTTPResponse.read1  s      $ YY	r)  
 
 
 
 
 
 
 
 (,$?s4yy3 $$T***'++C000rI   Fr   c                B   K   |                      |||           d {V S )N)r   r.   r   )_read)ro   r   r.   r   s       rG   rM   zAsyncHTTPResponse.read  sJ       ZZ)'   
 
 
 
 
 
 
 
 	
rI   )partialr   c          	       K   	 |                                   || j        }|Ud}|dk     r%| j        rc| j        j        rWt	          | j        t                    t          z  }|                     d|d|          }|r| j	        
                    |           t          | j	                  r| j	                            t          | j	                            | j        rqt          | j        dd          r\| j        Vt!          | j        d          r| j        j        | j        j        j        r%| j                            |            d | _        S S S S S |dk    r | j        rl| j        j        r`t          | j	                  |k     rH|                     d|d|t          | j	                  z
            }| j	        
                    |           |t          | j	                  k    r| j	                            |          | j        rqt          | j        dd          r\| j        Vt!          | j        d          r| j        j        | j        j        j        r%| j                            |            d | _        S S S S S | j        `| j                            |           4 d {V  |                     |           d {V }d d d           d {V  n# 1 d {V swxY w Y   n|                     |           d {V }|sd| _        |r|dk     rt          |          }d}|d}n
|dk    r|sd}|st          | j	                  dk    r| j        r| j        j        s}|| j        rqt          | j        dd          r\| j        Vt!          | j        d          r| j        j        | j        j        j        r%| j                            |            d | _        S S S S S |)|                     |||          }|r| j        s|| _        n|s| j        rt3          d          || j        rqt          | j        dd          r\| j        Vt!          | j        d          r| j        j        | j        j        j        r%| j                            |            d | _        S S S S S |                     ||||t          | j	                  z
  	          }| j	        
                    |           |ot!          | j        d          }	t          | j	                  |k     r|r|	s| j        `| j                            |           4 d {V  |                     |           d {V }d d d           d {V  n# 1 d {V swxY w Y   n|                     |           d {V }|                     ||||t          | j	                  z
  	          }| j	        
                    |           t          | j	                  |k     r|r|	| j	                            |          }|| j        rqt          | j        dd          r\| j        Vt!          | j        d          r| j        j        | j        j        j        r%| j                            |            d | _        S S S S S # | j        rpt          | j        dd          rZ| j        St!          | j        d          r| j        j        | j        j        j        r!| j                            |            d | _        w xY w)
NFr   rI   )flush_decoder
max_length_eotr   Tz_Calling read(decode_content=False) is not supported after read(decode_content=True) was called.)r   )_init_decoderr.   rW   has_unconsumed_tailmaxrj   r   r   _decoderi   r   rv   rT   rZ   r   rk   rd   r   r   forgetborrowr   rY   rX   rP   RuntimeError)
ro   r   r.   r   r   capdecoded_datar   r   surface_per_frames
             rG   r   zAsyncHTTPResponse._read  sZ	     Q	0   %!%!4 %77 } C)J C $ 8 8  3	3  (,||**/'*	 (4 ( ( ( C 044\BBB4/00 S#377D<P8Q8QRR@ 0DHfe440 (4  &110x},x}+ - (//555+/D((0 0 5
 -K 77 ? M=?   455;;'+||**/'*S1E-F-F'F	 (4 ( ( ,00>>>c$"67777#377<<T 0DHfe440 (4  &110x},x}+ - (//555+/D((0 0 5
 -_ #//66t<< 5 5 5 5 5 5 5 5!%!4!4444444D5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 "^^C00000000  4/3,  sQww$ii!M{ $$ $ ,--22 3+/=+L 3 b 0DHfe440 (4  &110x},x}+ - (//555+/D((0 0 5
 -m {||D.-HH  &)E &!%DJ &  0 *D    F 0DHfe440 (4  &110x},x}+ - (//555+/D((0 0 5
 -Q  $||"!"S)=%>%>>	  ,     $((666$+$I&0I0I!,--333EV3
 +7#'#7#>#>t#D#D = = = = = = = =)-)<)<#<#<#<#<#<#<D= = = = = = = = = = = = = = = = = = = = = = = = = = = &*^^C%8%8888888#'<<&%#&T-A)B)B#B	 $0 $ $L (,,\:::# ,--333EV3$ +//44 0DHfe440 (4  &110x},x}+ - (//555+/D((0 0 5
 - 0DHfe440 (4  &110x},x}+ - (//555+/D(////sr   CZ B+Z 9'Z  K<Z 
KZ KA>Z AZ B"Z 8U&Z &
U00Z 3U04BZ A9\   "typing.AsyncGenerator[bytes, None]c               :  K   |dk    rd S | j         d S t          | j                   r| j        s| j        ra| j        j        rW|                     ||d           d {V }|r|W V  t          | j                   9| j        @| j        r| j        j        Sd S d S d S d S )Nr   T)r   r.   r   )rZ   r   ri   rW   r   r   r   s       rG   streamzAsyncHTTPResponse.streamO  s      !88F8FTX&&
	#
	 
	 #'-"C
	
  $        D  



 TX&&
	#
	 
	 #'-"C
	 
	 
	 
	 
	 
	 
	 
	 
	rI   c                X  K   | j         +| j         j        s| j                                          d {V  | j        s | j        r| j                                         | j        r| j                                         d {V  | j        s!t          j                            |            d S d S rC   )r   r   r}   rZ   rc   r<   r   IOBaser   s    rG   r}   zAsyncHTTPResponse.closeb  s      >%dn.C%.&&((((((((({ 	tx 	HNN 	+"((********* 	"IOOD!!!!!	" 	"rI   typing.AsyncIterator[bytes]c               ~  K   g }|                      dd          2 3 d {V }d|v rc|                    d          }d                    g ||d         d          W V  |dd         D ]
}|dz   W V  |d         r
|d         g}kg }n|                    |           6 |rd                    |          W V  d S d S )Nr   T)r.      
rI   r   r   )r   rV   joinappend)ro   r   chunkchunksxs        rG   	__aiter__zAsyncHTTPResponse.__aiter__o  s$      ;;r$;?? 	% 	% 	% 	% 	% 	% 	%%~~U++hh:::E:;;;;;;" $ $Ae)OOOOO":  $Rj\FFFFe$$$$ @  	#((6""""""""	# 	#s   B c                    | j         sH| j        r| j                                         | j        s#t          j                            |            d S d S d S rC   )r   rZ   r}   r<   r   r   r   s    rG   __del__zAsyncHTTPResponse.__del__  s`    { 	&x !   ? &	%%%%%	& 	&& &rI   )r#   Nr   r   NTTNNNNNTNNTN)$r$   r   r%   r&   r'   r(   r)   r(   r*   r+   r,   r-   r.   r-   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r-   r:   r+   r;   r+   r<   r-   r=   r>   r?   r@   )rs   rt   r?   r(   )r?   ry   )r?   r@   )r?   r   )r?   r   )r   r   r?   r@   )r?   r   )r?   ra   rC   )r   r   r?   ra   )NN)r   r   r.   r   r?   ra   )NNF)r   r   r.   r   r   r-   r?   ra   )
r   r   r.   r   r   r-   r   r-   r?   ra   )r   N)r   r   r.   r   r?   r   )r?   r   )__name__
__module____qualname__rr   rx   r   r   r   propertyr   r   r   r   r   r   r   r   rM   r   r   r}   r   r    rI   rG   r"   r"   +   sZ        RV! $#:>/315" $'+%)"&IM%gG gG gG gG gGR    5$ 5$ 5$ 5$n! ! ! !     X    X	 	 	 	
, 
, 
, 
,    X1N 1N 1N 1N 1Nj ; ; ; ; ;~ &*    @ &*#	

 

 

 

 

 &*#	Z0 Z0 Z0 Z0 Z0 Z0 Z0z FJ    &" " " "# # # #"& & & & & &rI   r"   )5
__future__r   r   r   r   r   typingr]   
contextlibr   socketr   r{   _collectionsr   _typingr   	_constantr	   r
   r   r   backend._asyncr   
exceptionsr   r   r   r   r   r   r   r   responser   r   util.responser   r   
util.retryr   r3   r   TYPE_CHECKINGemail.messager   _async.connectionpoolr   contrib.webextensions._asyncr   util._async.traffic_policer    r"   r   rI   rG   <module>r      s+   " " " " " " 				     



   * * * * * * + + + + + + ) ) ) ) ) )                        3 2 2 2 2 2	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 4 3 3 3 3 3 3 3 : : : : : : : :       + + + + + +	 @%%%%%%??????EEEEEE??????[	& [	& [	& [	& [	& [	& [	& [	& [	& [	&rI   