
    ;3hlt              	       \   U d dl Z d dlZd dl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	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mZmZmZ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#m$Z$ dd
l%m&Z& ddl'm(Z(m)Z) dZ*dZ+ee,   e-d<   dZ.ee,   e-d<    e/       Z0ee jb                  d      e-d<   erd dlm2Z2  G d de3      Z4 G d de5ejl                        Z7dedededdfdZ8e7jr                  dded   ded e7ddfd!Z: G d" d#      Z;ed   Z<ee<ef   Z= G d$ d%      Z> G d& de	      Z? G d' d(e?      Z@ G d) d*e@      ZA G d+ d,eA      ZB G d- d.e?      ZC G d/ d0eC      ZD G d1 d2eC      ZE G d3 d4eC      ZF G d5 d6e@      ZGerd d7lmHZHmIZI eIeJ   ZKeHeJ   ZLnd d7lMmHZHmIZI eIZKeHZL G d8 d9e?      ZN G d: d;eN      ZO e>       ZPePj                  e@eJeReSf       ePj                  eAe5       ePj                  eBej                         ePj                  eDej                         ePj                  eEej                         ePj                  eFej                  ej                  f       ePj                  eCej                         ePj                  eOe&       ePj                  eNeHe7j                         y)<    N)ABCabstractmethod)chain)IOTYPE_CHECKINGAnyDictFinalIterableOptionalSetTextIOTupleTypeUnion)CIMultiDict   )hdrs)AbstractStreamWriter)	_SENTINELcontent_disposition_headerguess_filenameparse_mimetypesentinel)StreamReader)JSONEncoder_CIMultiDict)PAYLOAD_REGISTRYget_payloadpayload_typePayloadBytesPayloadStringPayloadIOBasePayloadBytesIOPayloadBufferedReaderPayloadTextIOPayloadStringIOPayloadJsonPayloadAsyncIterablePayloadi   TOO_LARGE_BYTES_BODYi   	READ_SIZE_CLOSE_FUTURES)Listc                       e Zd Zy)LookupErrorN)__name__
__module____qualname__     O/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/aiohttp/payload.pyr0   r0   A   s    r5   r0   c                       e Zd ZdZdZdZy)Ordernormal	try_firsttry_lastN)r1   r2   r3   r9   r:   r;   r4   r5   r6   r8   r8   E   s    FIHr5   r8   dataargskwargsreturnr!   c                 4    t        j                  | g|i |S N)r   get)r<   r=   r>   s      r6   r   r   K   s    6t6v66r5   orderfactorytyperD   c                4    t         j                  | ||       y NrC   )r   register)rE   rF   rD   s      r6   register_payloadrJ   O   s     gt59r5   c                   V    e Zd Zej                  ddededdfdZded   ded   fd	Zy)
r    rC   rF   rD   r?   Nc                     || _         || _        y rA   )rF   rD   )selfrF   rD   s      r6   __init__zpayload_type.__init__V   s    	
r5   rE   r!   c                 J    t        || j                  | j                         |S rH   )rJ   rF   rD   )rM   rE   s     r6   __call__zpayload_type.__call__Z   s    $))4::>r5   )	r1   r2   r3   r8   r9   r   rN   r   rP   r4   r5   r6   r    r    U   s>    49LL S E T Y DO r5   r    c                   p    e Zd ZdZdZddZeddeded	d
deddf
dZe	j                  ddedede	ddfdZy)PayloadRegistryzZPayload registry.

    note: we need zope.interface for more efficient adapter search
    _first_normal_last_normal_lookupr?   Nc                 <    g | _         g | _        g | _        i | _        y rA   rS   rM   s    r6   rN   zPayloadRegistry.__init__k   s    243513
68r5   )_CHAINr<   r=   rZ   z!Type[chain[_PayloadRegistryItem]]r>   r!   c                   | j                   r.| j                   D ]  \  }}t        ||      s ||g|i |c S  | j                  j                  t	        |            x}r ||g|i |S t        |t
              r|S  || j                  | j                        D ]  \  }}t        ||      s ||g|i |c S  t               rA   )	rT   
isinstancerW   rB   rF   r!   rU   rV   r0   )rM   r<   rZ   r=   r>   rE   type_lookup_factorys           r6   rB   zPayloadRegistry.getq   s     ;;"&++ :dE*"49$9&99: "0044T$Z@@>@!$8888dG$K$T\\4::> 	6NGU$&t5d5f55	6 mr5   rC   rE   rF   rD   c                   |t         j                  u r| j                  j                  ||f       y |t         j                  u rT| j
                  j                  ||f       t        |t              r|D ]  }|| j                  |<    y || j                  |<   y |t         j                  u r| j                  j                  ||f       y t        d|      )NzUnsupported order )r8   r:   rT   appendr9   rU   r\   r   rW   r;   rV   
ValueError)rM   rE   rF   rD   ts        r6   rI   zPayloadRegistry.register   s     EOO#KK/ell"LL$0$) 5A-4D''*5 -4##D)enn$JJwo.1%;<<r5   )r?   N)r1   r2   r3   __doc__	__slots__rN   r   r   rB   r8   r9   PayloadTyperI   r4   r5   r6   rR   rR   c   s~    
 AI9 7<	  4	
  
0 BG="=*-=9>=	=r5   rR   c                      e Zd ZU dZeed<   dZee   ed<   de	ddfde
deeeeeef   eeeef      f      deedef   dee   d	ee   d
e
ddfdZedee   fd       Zedee   fd       Zedefd       Zedefd       Zedee   fd       Zedefd       Z	 	 ddededede
ddf
dZedd	ededefd       Zededdfd       Zdedee   ddfdZ y) r!   application/octet-stream_default_content_typeN_sizevalueheaderscontent_typefilenameencodingr>   r?   c                 (   || _         || _        t               | _        || _        |t
        ur ||| j                  t        j                  <   n| j                  ut        j                  dk\  rt        j                  }nt        j                  } || j                        d   }|| j                  }|| j                  t        j                  <   n'| j                  | j                  t        j                  <   |r| j                  j                  |       y y )N)      r   )	_encoding	_filenamer   _headers_valuer   r   CONTENT_TYPEsysversion_info	mimetypesguess_file_type
guess_typerh   update)rM   rj   rk   rl   rm   rn   r>   guessers           r6   rN   zPayload.__init__   s     "!&1mx'L,D/;DMM$++,^^'7*#33#.."4>>215L##99/;DMM$++,/3/I/IDMM$++,MM  ) r5   c                     | j                   S )zSize of the payload.)ri   rY   s    r6   sizezPayload.size   s     zzr5   c                     | j                   S )zFilename of the payload.)rs   rY   s    r6   rm   zPayload.filename        ~~r5   c                     | j                   S )zCustom item headers)rt   rY   s    r6   rk   zPayload.headers   s     }}r5   c                     dj                  | j                  j                         D cg c]  \  }}|dz   |z   dz    c}}      j                  d      dz   S c c}}w )N z: z
utf-8s   
)joinrk   itemsencode)rM   kvs      r6   _binary_headerszPayload._binary_headers   sY     GGt||7I7I7KLtq!QX\F*LMTT 	
Ls   A
c                     | j                   S )zPayload encoding)rr   rY   s    r6   rn   zPayload.encoding   r   r5   c                 <    | j                   t        j                     S )zContent type)rt   r   rv   rY   s    r6   rl   zPayload.content_type   s     }}T..//r5   disptypequote_fields_charsetparamsc                 X    t        |f||d|| j                  t        j                  <   y)z$Sets ``Content-Disposition`` header.)r   r   N)r   rt   r   CONTENT_DISPOSITION)rM   r   r   r   r   s        r6   set_content_dispositionzPayload.set_content_disposition   s3     3M3
#/(3
FL3
d../r5   errorsc                      y)z~Return string representation of the value.

        This is named decode() to allow compatibility with bytes objects.
        Nr4   rM   rn   r   s      r6   decodezPayload.decode   s    r5   writerc                    K   yw)a  Write payload to the writer stream.

        Args:
            writer: An AbstractStreamWriter instance that handles the actual writing

        This is a legacy method that writes the entire payload without length constraints.

        Important:
            For new implementations, use write_with_length() instead of this method.
            This method is maintained for backwards compatibility and will eventually
            delegate to write_with_length(writer, None) in all implementations.

        All payload subclasses must override this method for backwards compatibility,
        but new code should use write_with_length for more flexibility and control.
        Nr4   rM   r   s     r6   writezPayload.write   s     s   content_lengthc                 B   K   | j                  |       d{    y7 w)a  
        Write payload with a specific content length constraint.

        Args:
            writer: An AbstractStreamWriter instance that handles the actual writing
            content_length: Maximum number of bytes to write (None for unlimited)

        This method allows writing payload content with a specific length constraint,
        which is particularly useful for HTTP responses with Content-Length header.

        Note:
            This is the base implementation that provides backwards compatibility
            for subclasses that don't override this method. Specific payload types
            should override this method to implement proper length-constrained writing.

        N)r   rM   r   r   s      r6   write_with_lengthzPayload.write_with_length  s     * jj   s   )Tr   r   strict)!r1   r2   r3   rh   str__annotations__ri   r   intr   r   r   r   r	   r   r   r   rN   propertyr   rm   rk   bytesr   rn   rl   boolr   r   r   r   r   r   r4   r5   r6   r!   r!      s   !;3;E8C= 4<"&"&** ,S#XsCx0IIJ
* Cy01* 3-* 3-* * 
*> hsm   (3-      
 
 
 (3-   0c 0 0 "	



 

 	


 

 


 s c    "6 4  &!*!<DSM!	!r5   c                        e Zd ZU eed<   deeeef   dededdf fdZ	dde
d	e
de
fd
ZdeddfdZdedee   ddfdZ xZS )r"   ru   rj   r=   r>   r?   Nc                 d   d|vrd|d<   t        |   |g|i | t        |t              r|j                  | _        n>t        |t        t        f      rt        |      | _        nt        dt        |            | j
                  t        kD  r d| i}t        j                  dt        fi | y y )Nrl   rg   z%value argument must be byte-ish, not sourcez}Sending a large body directly with raw bytes might lock the event loop. You should probably pass an io.BytesIO object instead)superrN   r\   
memoryviewnbytesri   r   	bytearraylen	TypeErrorrF   r+   warningswarnResourceWarningrM   rj   r=   r>   	__class__s       r6   rN   zBytesPayload.__init__"  s     '%?F>"000eZ(DJy12UDJCDK?STT::,,%FMM,  	
  -r5   rn   r   c                 :    | j                   j                  ||      S rA   )ru   r   r   s      r6   r   zBytesPayload.decode;  s    {{!!(F33r5   r   c                 V   K   |j                  | j                         d{    y7 w)a  Write the entire bytes payload to the writer stream.

        Args:
            writer: An AbstractStreamWriter instance that handles the actual writing

        This method writes the entire bytes content without any length constraint.

        Note:
            For new implementations that need length control, use write_with_length().
            This method is maintained for backwards compatibility and is equivalent
            to write_with_length(writer, None).
        Nr   ru   r   s     r6   r   zBytesPayload.write>  s      ll4;;'''s   )')r   c                    K   |'|j                  | j                  d|        d{    y|j                  | j                         d{    y7 )7 w)a  
        Write bytes payload with a specific content length constraint.

        Args:
            writer: An AbstractStreamWriter instance that handles the actual writing
            content_length: Maximum number of bytes to write (None for unlimited)

        This method writes either the entire byte sequence or a slice of it
        up to the specified content_length. For BytesPayload, this operation
        is performed efficiently using array slicing.

        Nr   r   s      r6   r   zBytesPayload.write_with_lengthM  sJ      %,,t{{?N;<<<,,t{{+++ =+s!   $AA#A
AAAr   )r1   r2   r3   r   r   r   r   r   r   rN   r   r   r   r   r   r   r   __classcell__r   s   @r6   r"   r"     s    M5)Z78ADPS	24s 4c 4 4("6 (4 (,*,<DSM,	,r5   r"   c                   L     e Zd Zddddededee   dee   deddf fd	Z xZS )
r#   Nrn   rl   rj   r=   rn   rl   r>   r?   c                    |/|d}d}n1t        |      }|j                  j                  dd      }n	|d|z  }|}t        |   |j                  |      g|||d| y )Nr   text/plain; charset=utf-8charsettext/plain; charset=%sr   )r   
parametersrB   r   rN   r   )	rM   rj   rn   rl   r=   r>   real_encodingmimetyper   s	           r6   rN   zStringPayload.__init__c  s     # ':),7 ( 3 3 7 7	7 K#7(B$MLL'	
 		
"%	

 	
r5   )r1   r2   r3   r   r   r   rN   r   r   s   @r6   r#   r#   b  sW    
 #'&*

 
 3-	

 sm
 
 

 
r5   r#   c                   6     e Zd Zdee   dededdf fdZ xZS )r(   rj   r=   r>   r?   Nc                 H    t        |   |j                         g|i | y rA   )r   rN   readr   s       r6   rN   zStringIOPayload.__init__  s     777r5   )r1   r2   r3   r   r   r   rN   r   r   s   @r6   r(   r(     s-    8bg 8c 8S 8T 8 8r5   r(   c                   >    e Zd ZU ej                  ed<   	 ddee   dedededdf
 fdZ	d	e
e   dee
e   ef   fd
Zd	e
e   defdZede
e   fd       ZdeddfdZdede
e   ddfdZde
e   ded	e
e   defdZdej0                  ddfdZddededefdZ xZS )r$   ru   rj   dispositionr=   r>   r?   Nc                     d|vrt        |      |d<   t        |   |g|i | | j                  >|;t        j
                  | j                  vr| j                  || j                         y y y y )Nrm   )rm   )r   r   rN   rs   r   r   rk   r   )rM   rj   r   r=   r>   r   s        r6   rN   zIOBasePayload.__init__  sy     V#!/!6F:000>>%+*A''t||;,,[4>>,R < +B%r5   remaining_content_lenc                     | j                   }|| j                  j                  t        |xs t        |xs t                    fS )a  
        Read the file-like object and return both its total size and the first chunk.

        Args:
            remaining_content_len: Optional limit on how many bytes to read in this operation.
                If None, READ_SIZE will be used as the default chunk size.

        Returns:
            A tuple containing:
            - The total size of the remaining unread content (None if size cannot be determined)
            - The first chunk of bytes read from the file object

        This method is optimized to perform both size calculation and initial read
        in a single operation, which is executed in a single executor job to minimize
        context switches and file operations when streaming content.

        )r   ru   r   minr,   )rM   r   r   s      r6   _read_and_available_lenz%IOBasePayload._read_and_available_len  s@    ( yyT[[%%!	#8#EIF
 
 	
r5   c                 H    | j                   j                  |xs t              S )a  
        Read a chunk of data from the file-like object.

        Args:
            remaining_content_len: Optional maximum number of bytes to read.
                If None, READ_SIZE will be used as the default chunk size.

        Returns:
            A chunk of bytes read from the file object, respecting the
            remaining_content_len limit if specified.

        This method is used for subsequent reads during streaming after
        the initial _read_and_available_len call has been made.

        )ru   r   r,   )rM   r   s     r6   _readzIOBasePayload._read  s      {{ 5 BCCr5   c                     	 t        j                  | j                  j                               j                  | j                  j                         z
  S # t        t        f$ r Y y w xY wrA   )osfstatru   filenost_sizetellAttributeErrorOSErrorrY   s    r6   r   zIOBasePayload.size  sR    	88DKK..0199DKK<L<L<NNN( 		s   AA A&%A&r   c                 D   K   | j                  |d       d{    y7 w)a0  
        Write the entire file-like payload to the writer stream.

        Args:
            writer: An AbstractStreamWriter instance that handles the actual writing

        This method writes the entire file content without any length constraint.
        It delegates to write_with_length() with no length limit for implementation
        consistency.

        Note:
            For new implementations that need length control, use write_with_length() directly.
            This method is maintained for backwards compatibility with existing code.

        Nr   r   s     r6   r   zIOBasePayload.write  s       $$VT222     r   c                 &  K   t        j                         }d}|}	 |j                  d| j                  |       d{   \  }}|rt	        |      }||j                  |       d{    n!|j                  |d|        d{    ||z  }||z  }| j                  |||      r	 | j                  |       y|j                  d| j                  |       d{   }|r| j                  |       y7 7 7 r7 # | j                  |       w xY ww)a  
        Write file-like payload with a specific content length constraint.

        Args:
            writer: An AbstractStreamWriter instance that handles the actual writing
            content_length: Maximum number of bytes to write (None for unlimited)

        This method implements optimized streaming of file content with length constraints:

        1. File reading is performed in a thread pool to avoid blocking the event loop
        2. Content is read and written in chunks to maintain memory efficiency
        3. Writing stops when either:
           - All available file content has been written (when size is known)
           - The specified content_length has been reached
        4. File resources are properly closed even if the operation is cancelled

        The implementation carefully handles both known-size and unknown-size payloads,
        as well as constrained and unconstrained content lengths.

        r   N)	asyncioget_running_looprun_in_executorr   r   r   _should_stop_writing_schedule_file_closer   )	rM   r   r   looptotal_written_lenr   available_lenchunk	chunk_lens	            r6   r   zIOBasePayload.write_with_length  s4    . '') .	,)-)=)=d224I* $ M5 J	 )0 ,,u--- ,,u-C.C'DEEE)Y6)!Y.! ,,!#46K  %%d+ #22$**&; ' 2 %%d+;$ .E %%d+sn   D C; C3*C; 'C5(C; C7!C; 'D9 C; C9C; !D3C; 5C; 7C; 9C; ;DDr   r   c                 2    |duxr ||k\  xs |duxr |dk  S )a3  
        Determine if we should stop writing data.

        Args:
            available_len: Known size of the payload if available (None if unknown)
            total_written_len: Number of bytes already written
            remaining_content_len: Remaining bytes to be written for content-length limited responses

        Returns:
            True if we should stop writing data, based on either:
            - Having written all available data (when size is known)
            - Having written all requested content (when content-length is specified)

        Nr   r4   )rM   r   r   r   s       r6   r   z"IOBasePayload._should_stop_writing  s4    ( T)P.?=.P 
!-L2G12L	
r5   r   c                     |j                  d| j                  j                        }t        j	                  |       |j                  t        j                         y)zFSchedule file closing without awaiting to prevent cancellation issues.N)r   ru   closer-   addadd_done_callbackremove)rM   r   close_futures      r6   r   z"IOBasePayload._schedule_file_close-  sB    ++D$++2C2CD 	<(&&~'<'<=r5   rn   r   c                 l    dj                  fd| j                  j                         D              S )Nr   c              3   B   K   | ]  }|j                          y wrA   )r   ).0rrn   r   s     r6   	<genexpr>z'IOBasePayload.decode.<locals>.<genexpr>6  s     Saqxx&1Ss   )r   ru   	readlinesr   s    ``r6   r   zIOBasePayload.decode5  s%    wwS4;;;P;P;RSSSr5   )
attachmentr   )r1   r2   r3   ioIOBaser   r   r   r   rN   r   r   r   r   r   r   r   r   r   r   r   r   r   r   AbstractEventLoopr   r   r   r   s   @r6   r$   r$     sI   II 2>
SW
S+.
SFI
SUX
S	
S
%-c]
	x}e#	$
2D8C= DU D$ hsm  3"6 34 3$:,*:,<DSM:,	:,x
}
 
  (}	

 

0>)B)B >t >Ts Tc T Tr5   r$   c                        e Zd ZU ej                  ed<   ddddededee	   dee	   ded	df fd
Z
dee   d	eee   ef   fdZdee   d	efdZdde	de	d	e	fdZ xZS )r'   ru   Nr   rj   r=   rn   rl   r>   r?   c                    |/|d}d}n/t        |      }|j                  j                  dd      }n|d|z  }t        |   |g|||d| y )Nr   r   r   r   rl   rn   )r   r   rB   r   rN   )rM   rj   rn   rl   r=   r>   r   r   s          r6   rN   zTextIOPayload.__init__<  s|     #":),7#..229gF#7(B	
 		
%	

 	
r5   r   c                     | j                   }| j                  j                  t        |xs t        |xs t                    }|| j
                  r|j                  | j
                        fS |j                         fS )a  
        Read the text file-like object and return both its total size and the first chunk.

        Args:
            remaining_content_len: Optional limit on how many bytes to read in this operation.
                If None, READ_SIZE will be used as the default chunk size.

        Returns:
            A tuple containing:
            - The total size of the remaining unread content (None if size cannot be determined)
            - The first chunk of bytes read from the file object, encoded using the payload's encoding

        This method is optimized to perform both size calculation and initial read
        in a single operation, which is executed in a single executor job to minimize
        context switches and file operations when streaming content.

        Note:
            TextIOPayload handles encoding of the text content before writing it
            to the stream. If no encoding is specified, UTF-8 is used as the default.

        )r   ru   r   r   r,   rr   r   )rM   r   r   r   s       r6   r   z%TextIOPayload._read_and_available_lenX  sd    0 yy  !	#8#EIF
 T^^U\\$..1WWWWr5   c                     | j                   j                  |xs t              }| j                  r|j	                  | j                        S |j	                         S )a  
        Read a chunk of data from the text file-like object.

        Args:
            remaining_content_len: Optional maximum number of bytes to read.
                If None, READ_SIZE will be used as the default chunk size.

        Returns:
            A chunk of bytes read from the file object and encoded using the payload's
            encoding. The data is automatically converted from text to bytes.

        This method is used for subsequent reads during streaming after
        the initial _read_and_available_len call has been made. It properly
        handles text encoding, converting the text content to bytes using
        the specified encoding (or UTF-8 if none was provided).

        )ru   r   r,   rr   r   )rM   r   r   s      r6   r   zTextIOPayload._readv  sB    $   !6!C)D/3~~u||DNN+Q5<<>Qr5   r   c                 6    | j                   j                         S rA   )ru   r   r   s      r6   r   zTextIOPayload.decode  s    {{!!r5   r   )r1   r2   r3   r   
TextIOBaser   r   r   r   r   rN   r   r   r   r   r   r   r   r   s   @r6   r'   r'   9  s    MM #'&*

 
 3-	

 sm
 
 

8X%-c]X	x}e#	$X<R8C= RU R*"s "c " "r5   r'   c                       e Zd ZU ej                  ed<   edefd       Zdde	de	de	fdZ
deddfd	Zded
ee   ddfdZy)r%   ru   r?   c                     | j                   j                         }| j                   j                  dt        j                        }| j                   j                  |       ||z
  S )Nr   )ru   r   seekr   SEEK_END)rM   positionends      r6   r   zBytesIOPayload.size  sJ    ;;##%kkq"++."X~r5   rn   r   c                 V    | j                   j                         j                  ||      S rA   ru   r   r   r   s      r6   r   zBytesIOPayload.decode  "    {{!((6::r5   r   Nc                 B   K   | j                  |d        d {   S 7 wrA   r   r   s     r6   r   zBytesIOPayload.write  s     ++FD9999s   r   c                 6  K   d}|}	 | j                   j                  t              x}r|dkD  rt        j                  d       d{    ||j                  |       d{    nK|j                  |d|        d{    |t        |      z  }|dk  r	 | j                   j                          y|dz  }| j                   j                  t              x}r| j                   j                          y7 7 7 y# | j                   j                          w xY ww)a:  
        Write BytesIO payload with a specific content length constraint.

        Args:
            writer: An AbstractStreamWriter instance that handles the actual writing
            content_length: Maximum number of bytes to write (None for unlimited)

        This implementation is specifically optimized for BytesIO objects:

        1. Reads content in chunks to maintain memory efficiency
        2. Yields control back to the event loop periodically to prevent blocking
           when dealing with large BytesIO objects
        3. Respects content_length constraints when specified
        4. Properly cleans up by closing the BytesIO object when done or on error

        The periodic yielding to the event loop is important for maintaining
        responsiveness when processing large in-memory buffers.

        r   Nr   )ru   r   r,   r   sleepr   r   r   )rM   r   r   
loop_countremaining_bytesr   s         r6   r   z BytesIOPayload.write_with_length  s    , 
(	 ;;++I66%6>
 "--***"* ,,u--- ,,u-=o'>???#s5z1O&!+ KK a
  ;;++I66%6  KK +-? KKs^   D>C: C4C: !C6"C: >C8?C: D2&C: D4C: 6C: 8C: :DDr   )r1   r2   r3   r   BytesIOr   r   r   r   r   r   r   r   r   r   r4   r5   r6   r%   r%     st    JJc  ;s ;c ; ;:"6 :4 :) *) <DSM) 	) r5   r%   c                   B    e Zd ZU ej                  ed<   ddededefdZy)r&   ru   rn   r   r?   c                 V    | j                   j                         j                  ||      S rA   r  r   s      r6   r   zBufferedReaderPayload.decode  r  r5   Nr   )r1   r2   r3   r   BufferedIOBaser   r   r   r4   r5   r6   r&   r&     s(    ;s ;c ; ;r5   r&   c                   X     e Zd Zddej                  fdedededededed	d
f fdZ xZ	S )r)   r   zapplication/jsonrj   rn   rl   dumpsr=   r>   r?   Nc                 \    t        |    ||      j                  |      g|||d| y )Nr   )r   rN   r   )rM   rj   rn   rl   r  r=   r>   r   s          r6   rN   zJsonPayload.__init__  sC     	%L)	
 		
%	

 	
r5   )
r1   r2   r3   jsonr  r   r   r   rN   r   r   s   @r6   r)   r)     s`      .!ZZ

 
 	

 
 
 
 

 
r5   r)   )AsyncIterableAsyncIteratorc                        e Zd ZU dZee   ed<   eed<   dedededdf fdZ	d	e
ddfd
Zd	e
dee   ddfdZddededefdZ xZS )r*   N_iterru   rj   r=   r>   r?   c                     t        |t              s#t        dj                  t	        |                  d|vrd|d<   t        |   |g|i | |j                         | _        y )NzMvalue argument must support collections.abc.AsyncIterable interface, got {!r}rl   rg   )	r\   r  r   formatrF   r   rN   	__aiter__r  r   s       r6   rN   zAsyncIterablePayload.__init__  sf    %/!6$u+.  '%?F>"000__&
r5   r   c                 D   K   | j                  |d       d{    y7 w)a  
        Write the entire async iterable payload to the writer stream.

        Args:
            writer: An AbstractStreamWriter instance that handles the actual writing

        This method iterates through the async iterable and writes each chunk
        to the writer without any length constraint.

        Note:
            For new implementations that need length control, use write_with_length() directly.
            This method is maintained for backwards compatibility with existing code.

        Nr   r   s     r6   r   zAsyncIterablePayload.write  s      $$VT222r   r   c                   K   | j                   y|}	 	 t        j                  dk\  rt        | j                          d{   }n"| j                   j	                          d{   }||j                  |       d{    n/|dkD  r*|j                  |d|        d{    |t        |      z  }7 s7 R7 97 # t        $ r
 d| _         Y yw xY ww)a  
        Write async iterable payload with a specific content length constraint.

        Args:
            writer: An AbstractStreamWriter instance that handles the actual writing
            content_length: Maximum number of bytes to write (None for unlimited)

        This implementation handles streaming of async iterable content with length constraints:

        1. Iterates through the async iterable one chunk at a time
        2. Respects content_length constraints when specified
        3. Handles the case when the iterable might be used twice

        Since async iterables are consumed as they're iterated, there is no way to
        restart the iteration if it's already in progress or completed.

        N)rp   
   r   )r  rw   rx   anext	__anext__r   r   StopAsyncIteration)rM   r   r   r  r   s        r6   r   z&AsyncIterablePayload.write_with_length  s     ( ::(	##w."'

"33E"&**"6"6"88E"* ,,u---$q( ,,u-=o'>???#s5z1O 38- @ " 	DJ	sc   C,B; B3 "B; "B5#B; =B7>!B; B9 B; 5B; 7B; 9B; ;CCCCrn   r   c                     t        d      )NzUnable to decode.)r   r   s      r6   r   zAsyncIterablePayload.decodeD  s    +,,r5   r   )r1   r2   r3   r  r   _AsyncIteratorr   _AsyncIterabler   rN   r   r   r   r   r   r   r   r   s   @r6   r*   r*     s    &*E8N#*'n 'S 'C 'D '3"6 34 3"+*+<DSM+	+Z-s -c - -r5   r*   c                   0     e Zd Zdedededdf fdZ xZS )StreamReaderPayloadrj   r=   r>   r?   Nc                 H    t        |   |j                         g|i | y rA   )r   rN   iter_anyr   s       r6   rN   zStreamReaderPayload.__init__I  s!    );D;F;r5   )r1   r2   r3   r   r   rN   r   r   s   @r6   r'  r'  H  s)    <l <3 <# <$ < <r5   r'  )[r   enumr   r  ry   r   rw   r   abcr   r   	itertoolsr   typingr   r   r   r	   r
   r   r   r   r   r   r   r   	multidictr   r   r   r   helpersr   r   r   r   r   streamsr   typedefsr   r   __all__r+   r   r   r,   setr-   Futurer.   	Exceptionr0   r   Enumr8   r   r9   rJ   r    re   _PayloadRegistryItemrR   r!   r"   r#   r(   r$   r'   r%   r&   r)   r  r  r   r$  r%  collections.abcr*   r'  r   rI   r   r   StringIOr   r  BufferedReaderBufferedRandomr   r;   r4   r5   r6   <module>r<     s     	   	 
  #     "  %  " /  $) eCj (	5: ,/EGNN4() 1 	) 	C 7c 7# 7 7 7
 <A<<:)_:$':38:	:  9o[#-. 4= 4=nB!c B!J@,7 @,F
L 
>8m 8
pTG pTfS"M S"l9 ] 9 x;M ;
, 
( 3"5)N"5)N<"N"NS-7 S-l<. <
 #$    ,	:(F G   - -   /2;; 7   - 7   ."** 5   /"2C2CREVEV1W X   - 3   -| <   .U^^  Tr5   