
    *Gj                        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 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 d dlmZmZmZ d dlmZmZ dd	lmZ dd
lm Z m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z= ej>        dk    rd dlm?Z? n edd          Z?ej>        dk    rd dl	m@Z@ neeAeBddf         Z@ ede@          ZCdZDerddlEmFZF  G d deG          ZH G d  d!eG          ZId"eJdz  d#eKeJdz  eLeJeJf         f         fd$ZM	 d5d&eeJeJf         d'eJd#eJdz  fd(ZN G d) d*          ZO G d+ d          ZP e;ePe7jQ        ,           G d- d.e8                      ZR G d/ d0          ZSeKe8eJeJf         ZT G d1 d2e8          ZU G d3 d4          ZVdS )6    N)deque)AsyncIteratorIteratorMappingSequence)TracebackType)TYPE_CHECKINGAnyTypeVarUnioncast)	parse_qslunquote	urlencode)CIMultiDictCIMultiDictProxy   )AbstractStreamWriter)ZLibCompressorZLibDecompressor)CONTENT_DISPOSITIONCONTENT_ENCODINGCONTENT_LENGTHCONTENT_TRANSFER_ENCODINGCONTENT_TYPE)CHARDEFAULT_CHUNK_SIZETOKENparse_mimetypereify)HeadersParser)BadHttpMessage)internal_logger)JsonPayloadLookupErrorOrderPayloadStringPayloadget_payloadpayload_type)StreamReader)      )Selfr.   BodyPartReader)bound)r,      )Bufferzmemoryview[int]zmemoryview[bytes]_Buffer)MultipartReaderMultipartWriterr/   BadContentDispositionHeaderBadContentDispositionParamparse_content_dispositioncontent_disposition_filename)ClientResponsec                       e Zd ZdS )r6   N__name__
__module____qualname__     Y/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/aiohttp/multipart.pyr6   r6   C           DrA   r6   c                       e Zd ZdS )r7   Nr<   r@   rA   rB   r7   r7   G   rC   rA   r7   headerreturnc                    dt           dt          fddt           dt          fd}dt           dt          ffd}dt           dt          fd}dt           dt          fd}d                    t          t          j        t                              d	d
t           dt           dt           fd}| sd i fS |                     d          ^}} |          s%t          j	        t          |                      d i fS i }|r|                    d          }	|	s"t          j	        t          |                      <d|	vr%t          j	        t          |                      d i fS |	                    dd          \  }
}|
                                                                }
|                                }|
|v r%t          j	        t          |                      d i fS  |
          s#t          j	        t          |	                      ||
          rP ||          r ||dd                   }n |          s#t          j	        t          |	                     hn_ ||
          r ||          r|                    dd          \  }}}|pd}n#t          j	        t          |	                     	 t!          ||d          }n# t"          $ r% t          j	        t          |	                     Y w xY wd} ||          r)d} ||dd                             d                    }ne |          rd}nW|rU| d|d          } ||          r=|                    d            ||dd                             d                    }d}|r%t          j	        t          |                      d i fS |||
<   ||                                |fS )NstringrF   c                 P    t          |           ot          t          |           k    S N)boolr   setrH   s    rB   is_tokenz+parse_content_disposition.<locals>.is_tokenN   s    F||4V 44rA   c                 :    | d         | d         cxk    odk    nc S )Nr   "r@   rM   s    rB   	is_quotedz,parse_content_disposition.<locals>.is_quotedQ   s,    ayF2J----#-----rA   c                 L     |           o|                      d          dk    S )N'   )count)rH   rN   s    rB   
is_rfc5987z-parse_content_disposition.<locals>.is_rfc5987T   s)    x:FLL$5$5$::rA   c                 ,    |                      d          S )N*)endswithrM   s    rB   is_extended_paramz4parse_content_disposition.<locals>.is_extended_paramW   s    s###rA   c                     |                      d          dz   }|sdS |                     d          r
| |d         n	| |d          }|                                S )NrY   r   FrP   )findrZ   isdigit)rH   pos	substrings      rB   is_continuous_paramz6parse_content_disposition.<locals>.is_continuous_paramZ   sa    kk#" 	5&,ooc&:&:LF3r6NNstt	  """rA    )charstextrc   c                6    t          j        d| dd|           S )Nz\\([z])z\1)resub)rd   rc   s     rB   unescapez+parse_content_disposition.<locals>.unescapea   s"    v(u(((%666rA   ;r   =r   rP   rT   rU   utf-8strictTFz\/)strrK   joinmaprf   escaper   splitwarningswarnr6   poplowerstriplstripr7   r   UnicodeDecodeError)rE   rR   rW   r[   ra   rh   disptypepartsparamsitemkeyvalueencoding_failed_valuerN   s                   @rB   r8   r8   K   s   5 5 5 5 5 5.# .$ . . . .;3 ;4 ; ; ; ; ; ;$# $$ $ $ $ $#C #D # # # # -/GGC	44H4H,I,I 7 7 7s 7c 7c 7 7 7 7  Rx||C((Hu8H 1&99:::RxF
 @yy|| 	M5f==>>>d??M5f==>>>8OZZQ''
Uiikk!!&==M5f==>>>8Ox}} +	 M4T::;;;  %% '	 y  qt--Xe__ 8>>??? s##  	 z%   %*[[a%8%8"!U#.w8>>???x::%   8>>???
 Fy # qt!3!3E!:!:;;% 	# # "..E!H..9V$$ #IIaLLL$HVAbD\%8%8%?%?@@E"F  9&AABBBRxsA  @D >>V##s   <K +K=<K=filenamer{   namec                 2  
 d|z  
| sd S 
| v r| 
         S || v r| |         S g }t          
fd|                                 D                       }t          |          D ]i\  }\  }}|                    dd          \  }}|                    d          r
|d d         }|t          |          k    r|                    |           i |sd S d                    |          }d|v r/|                    dd          \  }	}}|	pd	}	t          ||	d
          S |S )Nz%s*c              3   P   K   | ] \  }}|                               ||fV  !d S rJ   )
startswith).0r}   r~   name_sufs      rB   	<genexpr>z/content_disposition_filename.<locals>.<genexpr>   sP       
 
'S%S^^H=U=U
%L
 
 
 
 
 
rA   rY   r   rP   rb   rT   rU   rk   rl   )	sorteditems	enumeraterq   rZ   rm   appendrn   r   )r{   r   rz   fnparamsnumr}   r~   r   tailr   r   s             @rB   r9   r9      si    t|H t	V		h	d| 
 
 
 
+1<<>>
 
 
 
 
 "+8!4!4 	 	C#uiiQ''GAt}}S!! !CRCys3xxU#### 	4%<<!&S!!4!4Ha*7H5(H555rA   c                   x    e Zd ZdZ	 	 	 	 	 	 ddZdd	Zded
         fdZdefdZ	ded
         dz  fdZ
ddZdS )MultipartResponseWrapperzzWrapper around the MultipartReader.

    It takes care about
    underlying connection and close it when it needs in.
    respr:   streamr4   rF   Nc                 "    || _         || _        d S rJ   )r   r   )selfr   r   s      rB   __init__z!MultipartResponseWrapper.__init__   s    
 	rA   c                     | S rJ   r@   r   s    rB   	__aiter__z"MultipartResponseWrapper.__aiter__       rA   )r4   r/   c                 P   K   |                                   d {V }|t          |S rJ   nextStopAsyncIterationr   parts     rB   	__anext__z"MultipartResponseWrapper.__anext__   7       YY[[      <$$rA   c                 >    | j         j                                        S )z2Returns True when all response data had been read.)r   contentat_eofr   s    rB   r   zMultipartResponseWrapper.at_eof   s    y '')))rA   c                    K   | j                                          d{V }| j                                         r|                                  d{V  |S )z#Emits next multipart reader object.N)r   r   r   releaser   r|   s     rB   r   zMultipartResponseWrapper.next   sh       [%%''''''''; 	!,,..       rA   c                 H   K   | j                                          d{V  dS )z_Release the connection gracefully.

        All remaining content is read to the void.
        N)r   r   r   s    rB   r   z MultipartResponseWrapper.release   s4      
 i!!!!!!!!!!!rA   )r   r:   r   r4   rF   N)rF   r   rF   N)r=   r>   r?   __doc__r   r   r   r   rK   r   r   r   r@   rA   rB   r   r      s          " 
	      	2	3   * * * * *	2	3d	:   " " " " " "rA   r   c                      e Zd ZdZdZddeej        edde	ddd	e
d
ededz  dededee         ddfdZdedefdZde	fdZde	dz  fdZdddede	fdZefdede	fdZdede	fdZdede	fdZde	fdZd1dZddd edz  defd!Zddd edz  deeef         dz  fd"Zddd edz  dee eef                  fd#Z!defd$Z"d%e#de#e	z  fd&Z$defd'Z%d%e#de#e	z  fd(Z&d%e#de'e#e	z           fd)Z(d%e#de#e	z  fd*Z)d%e#de'e#e	z           fd+Z*d%e#de#e	z  fd,Z+d-edefd.Z,e-dedz  fd/            Z.e-dedz  fd0            Z/dS )2r/   z&Multipart reader for single body part.i    mixedN)subtypedefault_charsetmax_decompress_sizeclient_max_sizemax_size_error_clsboundaryheadersCIMultiDictProxy[str]r   r   r   r   r   r   rF   c                   || _         || _        t          |          dz   | _        || _        || _        d| _        |dk    | _        | j        rd n| j                             t          d           }	|	t          |	          nd | _        d| _        t                      | _        d | _        d| _        i | _        || _        || _        || _        d S )NrU   F	form-datar   )r   	_boundarylen_boundary_len_content_default_charset_at_eof_is_form_datagetr   int_length_read_bytesr   _unread_prev_chunk_content_eof_cache_max_decompress_size_client_max_size_max_size_error_cls)
r   r   r   r   r   r   r   r   r   lengths
             rB   r   zBodyPartReader.__init__  s     ! ]]Q. /$3+W1A1A.RV1W1W&,&8s6{{{d%*WW)-&($7! /#5   rA   r   c                     | S rJ   r@   r   s    rB   r   zBodyPartReader.__aiter__&  r   rA   c                 P   K   |                                   d {V }|t          |S rJ   r   r   s     rB   r   zBodyPartReader.__anext__)  s5      YY[[      <$$rA   c                 F   K   |                                   d {V }|sd S |S rJ   )readr   s     rB   r   zBodyPartReader.next/  s7      YY[[       	4rA   Fdecoder   c                  K   | j         rdS t                      }| j         sl|                    |                     | j                   d{V            t          |          | j        k    r|                     | j                  | j         l|rut                      }|                     |          2 3 d{V }|                    |           t          |          | j        k    r|                     | j                  O6 |S |S )zReads body part data.

        decode: Decodes data following by encoding
                method from Content-Encoding header. If it missed
                data remains untouched
        rA   N)	r   	bytearrayextend
read_chunk
chunk_sizer   r   r   decode_iter)r   r   datadecoded_datads        rB   r   zBodyPartReader.read5  s=      < 	3{{, 	FKKdoodo>>>>>>>>???4yy4000..t/DEEE , 	F  	 $;;L++D11 J J J J J J Ja##A&&&|$$t'<<<2243HIII = 2  s   2Dsizec                 ^  K   | j         rdS | j        r|                     |           d{V }n|                     |           d{V }| j                            t                    }|rT|                                dk    r;d                    |	                                          }t          |          dz  }|dk    r|                                 sd|z
  }d}| j        r0| j        d|         }| j        t          |          d         | _        t          |          |k    r3|| j                            dt          |          z
             d{V z  }|sd| _         |d                    |	                                          z  }||z  }t          |          dz  }|dk    r|                                 | xj        t          |          z  c_        | j        | j        k    rd| _         | j         r2| j                                         d{V dk    rt#          d          |S )	zWReads body part content chunk of the specified size.

        size: chunk size
        rA   Nbase64   r   T   
z3Reader did not read all the data or it is malformed)r   r   _read_chunk_from_length_read_chunk_from_streamr   r   r   ru   rn   rq   r   r   r   r   r   r   readline
ValueError)r   r   chunkr   stripped_chunk	remainderover_chunk_size
over_chunks           rB   r   zBodyPartReader.read_chunkL  sR     
 < 	3< 	=66t<<<<<<<<EE66t<<<<<<<<E <##$=>> 	4((H44 XXekkmm44NN++a/Iq..."#i- 
# K!%!12B?2B!CJ'+'7J8I8I'JD$z??o55(:(:1s:;N(O(O"O"O"O"O"O"OOJ! (#'DL#((:+;+;+=+=">">>#//!3	! q...$ 	CJJ&t|++DL< 	T$-"8"8":":::::::gEERSSSrA   c                    K   | j         
J d            t          || j         | j        z
            }| j                            |           d {V }| j                                        rd| _        |S )Nz(Content-Length required for chunked readT)r   minr   r   r   r   r   )r   r   r   r   s       rB   r   z&BodyPartReader._read_chunk_from_lengthx  s       |'')S'''t|d.>>??
m((44444444=!! 	 DLrA   c           
        K   || j         k    s
J d            | j        d u }|r(d| j                            |           d {V z   | _        d}t	          |          | j         k     r|| j                            |           d {V z  }| xj        t          | j                                                  z  c_        | j        dk    rt          d          | j        rnt	          |          | j         k     t	          |          |k    r,| j        	                    ||d                     |d |         }| j        J | j        |z   }d| j
        z   }|r|                    |          }nF|                    |t          dt	          | j                  t	          |          z
                      }|dk    rt          j                    5  t          j        dt                      | j        	                    ||d                     d d d            n# 1 swxY w Y   | j        d |         | _        |t	          | j                  |         }|sd	| _        | j        |rdndd          }|| _        |S )
Nz<Chunk size must be greater or equal than boundary length + 2r   rA   rU   zReading after EOFr   ignore)categoryT)r   r   r   r   r   r   r   r   r   unread_datar   r]   maxrr   catch_warningsfilterwarningsDeprecationWarningr   )r   r   first_chunkr   windowrg   idxresults           rB   r   z&BodyPartReader._read_chunk_from_stream  s      D&&&&I '&&&$. 	H&t}/A/A$/G/G)G)G)G)G)G)GGD %jj4---4=--d333333333ET]%9%9%;%;!<!<< 1$$ !4555   %jj4--- u::M%%eDEEl333%4%LE+++!E)& 	M++c""CC++c3q#d.>*?*?#c((*J#K#KLLC!88(** 8 8';MNNNN))&,7778 8 8 8 8 8 8 8 8 8 8 8 8 8 8  $/5D3t/00367E $#!{"9!!";";< s   	>HHHc                 6  K   | j         rdS | j        r| j                                        }n| j                                         d{V }|                    | j                  rV|                    d          }| j        }| j        dz   }||k    s||k    r#d| _         | j                            |           dS n]| j                                         d{V }|                    | j                  r
|dd         }| j                            |           |S )z Reads body part by line by line.rA   Nr      --T)	r   r   popleftr   r   r   r   rstripr   )r   linesliner   last_boundary	next_lines         rB   r   zBodyPartReader.readline  s8     < 	3< 	2<''))DD//11111111D??4>** 	+ KK((E~H NU2M  E]$:$:###D)))s %;
 #m4466666666I##DN33 !CRCyL	***rA   c                 |   K   | j         rdS | j         s)|                     | j                   d{V  | j         'dS dS )z0Like read(), but reads all the data to the void.N)r   r   r   r   s    rB   r   zBodyPartReader.release  se      < 	F, 	3//$/222222222 , 	3 	3 	3 	3 	3rA   )r   r   c                   K   |                      d           d{V }|p|                     d          }|                    |          S )z;Like read(), but assumes that body part contains text data.Tr   Nrk   default)r   get_charsetr   r   r   r   s      rB   rd   zBodyPartReader.text  sZ      YYdY++++++++ @t///@@{{8$$$rA   c                  K   |                      d           d{V }|sdS |p|                     d          }t          t          t          t
          f         t          j        |                    |                              S )z<Like read(), but assumes that body parts contains JSON data.Tr   Nrk   r  )	r   r  r   dictrm   r
   jsonloadsr   r  s      rB   r	  zBodyPartReader.json  s      YYdY++++++++ 	4@t///@@DcNDJt{{8/D/D$E$EFFFrA   c                2  K   |                      d           d{V }|sg S ||}n|                     d          }	 |                                                    |          }n # t          $ r t          d|z            w xY wt          |d|          S )zFLike read(), but assumes that body parts contain form urlencoded data.Tr   Nrk   r  z'data cannot be decoded with %s encoding)keep_blank_valuesr   )r   r  r   r   rx   r   r   )r   r   r   real_encodingr   s        rB   formzBodyPartReader.form  s      YYdY++++++++ 	I$MM ,,W,==M	X;;==//>>LL! 	X 	X 	XFVWWW	X ""
 
 
 	
s   'A' 'Bc                     | j         S )z<Returns True if the boundary was reached or False otherwise.r   r   s    rB   r   zBodyPartReader.at_eof  
    |rA   r   c                 L    t           | j        v r|                     |          S |S )z>Apply Content-Transfer-Encoding decoding if header is present.)r   r   _decode_content_transfer)r   r   s     rB    _apply_content_transfer_decodingz/BodyPartReader._apply_content_transfer_decoding  s(    $4400666rA   c                 .    | j          ot          | j        v S )z5Check if Content-Encoding decoding should be applied.)r   r   r   r   s    rB   _needs_content_decodingz&BodyPartReader._needs_content_decoding  s     %%J*:dl*JJrA   c                     |                      |          }|                                 r|                     |          S |S )a  Decodes data synchronously.

        Decodes data according the specified Content-Encoding
        or Content-Transfer-Encoding headers value.

        Note: For large payloads, consider using decode_iter() instead
        to avoid blocking the event loop during decompression.
        )r  r  _decode_content)r   r   decodeds      rB   r   zBodyPartReader.decode  sD     77=='')) 	1''000rA   c                   K   |                      |          }|                                 r%|                     |          2 3 d{V }|W V  6 dS |W V  dS )a$  Async generator that yields decoded data chunks.

        Decodes data according the specified Content-Encoding
        or Content-Transfer-Encoding headers value.

        This method offloads decompression to an executor for large payloads
        to avoid blocking the event loop.
        N)r  r  _decode_content_async)r   r   r  r   s       rB   r   zBodyPartReader.decode_iter  s       77=='')) 	55g>>       a ?>> MMMMMMs   Ac                     | j                             t          d                                          }|dk    r|S |dv r+t	          |d                              || j                  S t          d|           )Nrb   identity>   gzipdeflateTr   suppress_deflate_header
max_lengthunknown content encoding: )r   r   r   ru   r   decompress_syncr   RuntimeErrorr   r   r   s      rB   r  zBodyPartReader._decode_content$  s    <##$4b99??AAz!!K***#!(,   odt/HoIIJ
 BBBCCCrA   c                  K   | j                             t          d                                          }|dk    r|W V  d S |dv rmt	          |d          }|                    || j                   d {V W V  |j        r.|                    d| j                   d {V W V  |j        ,d S d S t          d|           )	Nrb   r  >   r  r  Tr   r"  rA   r$  )	r   r   r   ru   r   
decompressr   data_availabler&  )r   r   r   r   s       rB   r  z$BodyPartReader._decode_content_async0  s!      <##$4b99??AAz!!JJJJJJ,,, !(,  A Td6OPPPPPPPPPPPP" TLL9RLSSSSSSSSSSSS " T T T T T FHFFGGGrA   c                     | j                             t          d                                          }|dk    rt	          j        |          S |dk    rt          j        |          S |dv r|S t          d|           )Nrb   r   quoted-printable)binary8bit7bit#unknown content transfer encoding: )	r   r   r   ru   r   	b64decodebinasciia2b_qpr&  r'  s      rB   r  z'BodyPartReader._decode_content_transferA  s    <##$=rBBHHJJx#D)))+++?4(((333KOXOOPPPrA   r  c                     | j                             t          d          }t          |          }|j                            d| j        p|          S )z>Returns charset parameter from Content-Type header or default.rb   charset)r   r   r   r   
parametersr   )r   r  ctypemimetypes       rB   r  zBodyPartReader.get_charsetM  sG      r22!%(("&&y$2G2R7SSSrA   c                     t          | j                            t                              \  }}t	          |d          S )z|Returns name specified in Content-Disposition header.

        If the header is missing or malformed, returns None.
        r   r8   r   r   r   r9   r   r   r{   s      rB   r   zBodyPartReader.nameS  s6     .dl.>.>?R.S.STT	6+FF;;;rA   c                     t          | j                            t                              \  }}t	          |d          S )zReturns filename specified in Content-Disposition header.

        Returns None if the header is missing or malformed.
        r   r:  r;  s      rB   r   zBodyPartReader.filename\  s6     .dl.>.>?R.S.STT	6+FJ???rA   r   )0r=   r>   r?   r   r   r   sysmaxsizer   bytesr+   rm   r   type	Exceptionr   r.   r   r   r   rK   r   r   r   r   r   r   rd   r  r
   r	  listtupler  r   r3   r  r  r   r   r   r  r  r  r  r    r   r   r@   rA   rB   r/   r/     sa       00J &*#5"{.86 6 66 )6 	6 6 t6 !6 6 !O6 
6 6 6 6>         EDL     ,1   D U    . ,6 * *S *% * * * *X# %    +# +% + + + +Z    :3 3 3 3 48 % % %cDj %C % % % % 48 G G GcDj GDcNT<Q G G G G 48 
 
 
cDj 
DsCx<Q 
 
 
 
(    W 5    K K K K K
7 w    g -%2P     
DG 
D% 
D 
D 
D 
DHH	w	'H H H H"
QW 
Q5 
Q 
Q 
Q 
QT3 T3 T T T T <cDj < < < U< @#* @ @ @ U@ @ @rA   )orderc                   x     e Zd ZU eed<   dedededdf fdZdd
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 xZS )BodyPartReaderPayloadr   r~   argskwargsrF   Nc                      t                      j        |g|R i | i }|j        
|j        |d<   |j        
|j        |d<   |r | j        di | d S d S )Nr   r   
attachmentT)rJ  T)superr   r   r   set_content_disposition)r   r~   rG  rH  r{   	__class__s        rB   r   zBodyPartReaderPayload.__init__k  s    0000000!#:!"ZF6N>%!&F: 	G(D(FFvFFFFF	G 	GrA   rk   rl   r   errorsc                      t          d          )NzUnable to decode.	TypeErrorr   r   rN  s      rB   r   zBodyPartReaderPayload.decodew  s    +,,,rA   c                 $   K   t          d          )aR  Raises TypeError as body parts should be consumed via write().

        This is intentional: BodyPartReader payloads are designed for streaming
        large data (potentially gigabytes) and must be consumed only once via
        the write() method to avoid memory exhaustion. They cannot be buffered
        in memory for reuse.
        z:Unable to read body part as bytes. Use write() to consume.rP  rR  s      rB   as_byteszBodyPartReaderPayload.as_bytesz  s       TUUUrA   writerc                   K   | j         }|                    t                     d {V x}r^|                    |          2 3 d {V }|                    |           d {V  #6 |                    t                     d {V x}\d S d S )N)r   )r   r   r   r   write)r   rU  fieldr   r   s        rB   rW  zBodyPartReaderPayload.write  s      "--3E-FFFFFFFFFe 	& ,,U33 & & & & & & &all1oo%%%%%%%% 4 #--3E-FFFFFFFFFe 	& 	& 	& 	& 	&s   A%rk   rl   )r=   r>   r?   r/   __annotations__r
   r   rm   r   r?  rT  r   rW  __classcell__rM  s   @rB   rF  rF  f  s         
Gn 
GS 
GC 
GD 
G 
G 
G 
G 
G 
G- -s -c - - - - -V Vs Vc VQV V V V V&"6 &4 & & & & & & & &rA   rF  c                      e Zd ZU dZeZdZed          dz  ed<   e	Z
ej        ddeddeeef         ded	ed
ededee         ddfdZdedefdZded e	f         dz  fdZedddefd            ZdefdZded e	f         dz  fdZd!dZded e	f         fdZddded e	f         fdZdefdZ de!fdZ"d!dZ#d!dZ$d"dZ%d!d Z&dS )#r4   zMultipart body reader.Nmultipart_reader_clsi     r   max_field_sizemax_headersr   r   r   r   ra  rb  r   rF   c                   t          |t                             | _        | j        j        dk    s
J d            d| j        j        vrt          d|t                   z            || _        d|                                 z                                   | _	        || _
        || _        d | _        d | _        || _        || _        || _        d| _        d| _        g | _        d S )N	multipartz!multipart/* content type expectedr   z$boundary missed for Content-Type: %s--FT)r   r   	_mimetyper@  r6  r   r   _get_boundaryencoder   r   r   r   
_last_part_max_field_size_max_headersr   r   _at_bofr   )r   r   r   r   ra  rb  r   s          rB   r   zMultipartReader.__init__  s     ((=>>~"k1113V111T^66669NN   !3!3!5!55==?? /,0CG-'#5 $&rA   r   c                     | S rJ   r@   r   s    rB   r   zMultipartReader.__aiter__  r   rA   c                 P   K   |                                   d {V }|t          |S rJ   r   r   s     rB   r   zMultipartReader.__anext__  r   rA   responser:   c                 Z    |                      | | |j        |j                            }|S )zConstructs reader instance from HTTP response.

        :param response: :class:`~aiohttp.client.ClientResponse` instance
        )response_wrapper_clsr   r   )clsro  objs      rB   from_responsezMultipartReader.from_response  s7     &&cc(*H,<==
 
 
rA   c                     | j         S )z@Returns True if the final boundary was reached, false otherwise.r  r   s    rB   r   zMultipartReader.at_eof  r  rA   c                    K   | j         rdS |                                  d{V  | j        r"|                                  d{V  d| _        n|                                  d{V  | j         rdS |                                  d{V }| j        | j        j        dk    rt          |t                    rt          |j                            t                              \  }}|                    d          dk    r|                    d           d{V }t!          |          dk    rt#          d          |                                                                | _        |                                  d{V }|| _        | j        S )	z#Emits the next multipart body part.NFr   r   	_charset_       zInvalid default charset)r   _maybe_release_last_partrl  _read_until_first_boundary_read_boundaryfetch_next_partri  rf  r   
isinstancer/   r8   r   r   r   r   r   r&  rv   r   r   )r   r   r   r{   r5  s        rB   r   zMultipartReader.next  s     
 < 	4++---------< 	(11333333333 DLL%%'''''''''< 	4))++++++++ O#&+55400 6 2$,2B2BCV2W2WXXIAvzz&!![00 !% 3 3333333w<<"$$&'@AAA(/(>(>(@(@%!1133333333rA   c                    K   | j         sA|                                  d{V }|dS |                                 d{V  | j         ?dS dS )z=Reads all the body parts to the void till the final boundary.N)r   r   r   r   s     rB   r   zMultipartReader.release  sy      , 	!$$$$$$D|,,..       	 , 	! 	! 	! 	! 	!rA   c                 d   K   |                                   d{V }|                     |          S )z"Returns the next body part reader.N)_read_headers_get_part_reader)r   r   s     rB   r}  zMultipartReader.fetch_next_part  s?       **,,,,,,,,$$W---rA   r   c           	         |                     t          d          }t          |          }|j        dk    rr| j        7 t          |           || j        | j        | j        | j        | j	                  S |                     || j        | j        | j        | j        | j	                  S | 
                    | j        || j        | j        j        | j        | j        | j	                  S )zDispatches the response by the `Content-Type` header.

        Returns a suitable reader instance.

        :param dict headers: Response headers
        rb   rd  Nr`  )r   r   r   r   )r   r   r   r@  r^  r   r   rj  rk  r   part_reader_clsr   rf  r   r   )r   r   r7  r8  s       rB   r  z MultipartReader._get_part_reader  s     L"--!%((=K''(0!tDzzM$($9#'#7 $ 1'+'?    ,, $ 5#3 -#'#; -    ''. $ 5 $ 5#'#; (   rA   c                 t    | j         j        d         }t          |          dk    rt          d|z            |S )Nr   F   z&boundary %r is too long (70 chars max))rf  r6  r   r   )r   r   s     rB   rg  zMultipartReader._get_boundary-  s;    >,Z8x==2EPQQQrA   c                    K   | j         r| j                                         S | j                                         d {V S rJ   )r   rt   r   r   r   s    rB   	_readlinezMultipartReader._readline4  sJ      < 	&<##%%%]++---------rA   c                    K   	 |                                   d {V }|dk    rt          d| j        z            |                                }|| j        k    rd S || j        dz   k    r	d| _        d S p)NTrA   z#Could not find starting boundary %rr   )r  r   r   r   r   r   r   s     rB   r{  z*MultipartReader._read_until_first_boundary9  s      	..********E|| 9T^L   LLNNE&&$.5000#	rA   c                   K   |                                   d {V                                 }|| j        k    rd S || j        dz   k    rd| _        |                                   d {V }|                                   d {V }|d d         dk    r| j                            |           d S | j                            ||g           d S t          d|d| j                  )Nr   TrU   zInvalid boundary z, expected )r  r   r   r   r   r   r   r   )r   r   epiloguer   s       rB   r|  zMultipartReader._read_boundaryG  s     ~~''''''''//11DN""Ddnu,,,DL!^^--------H"nn........I !}%%##I.....
 ##Y$9:::::WWWT^WWXXXrA   c                 `  K   g }	 | j                             | j                   d {V }|                    d          }|                    |           |sn(t          |          | j        k    rt          d          {t          | j                  }|	                    |          \  }}|S )NT)max_line_lengthr   zToo many headers received)ra  )
r   r   rj  r   r   r   rk  r"   r!   parse_headers)r   linesr   parserr   raw_headerss         rB   r  zMultipartReader._read_headers^  s      	B-00AU0VVVVVVVVELL))ELL 5zzD---$%@AAA	B d.BCCC%33E::rA   c                    K   | j         e| j                                         s| j                                          d{V  | j                            | j         j                   d| _         dS dS )z8Ensures that the last read body part is read completely.N)ri  r   r   r   r   r   s    rB   rz  z(MultipartReader._maybe_release_last_partl  sx      ?&?))++ 0o--/////////L 7888"DOOO	 '&rA   r   )rF   r   )'r=   r>   r?   r   r   rq  r^  r@  rZ  r/   r  r=  r>  r   r   rm   r+   r   rA  r   r.   r   r   r   classmethodrt  rK   r   r   r   r}  r  rg  r?  r  r{  r|  r  rz  r@   rA   rB   r4   r4     s           4 <@$01D8???$O  #{".8' ' 'c"' '
 ' ' ' !O' 
' ' ' '<     	 .0	1D	8    " 
"   [    !	 .0	1D	8! ! ! !F! ! ! !.	 .0	1. . . .((( 
 .0	1( ( ( (Ts    . . . . .
   Y Y Y Y.   # # # # # #rA   r4   c                   f    e Zd ZU dZded<   dZd(dededz  ddf fd	Zd)d
Zde	e
         dz  de
dz  dedz  ddfdZdee         fdZdefdZdefdZ ej        d          Z ej        d          Zedefd            Zedefd            Zd*dedeeef         dz  defdZdedefdZ	 d*dedeeef         dz  defdZ 	 d*de!e"eef                  eeef         z  deeef         dz  defdZ#ededz  fd            Z$d+d ed!edefd"Z%d+d ed!ede&fd#Z'	 d,d$e(d%eddfd&Z)d-d'Z* xZ+S ).r5   zMultipart body writer.Nr   Tr   r   r   rF   c                 8   ||nt          j                    j        }	 |                    d          | _        n# t
          $ r t          d          d w xY wd| d| j         }t                      	                    d |           g | _
        |dk    | _        d S )Nasciiz(boundary should contain ASCII only charsz
multipart/z; boundary=)content_typer   )uuiduuid4hexrh  r   UnicodeEncodeErrorr   _boundary_valuerK  r   _partsr   )r   r   r   r7  rM  s       rB   r   zMultipartWriter.__init__  s    '3889I	S%__W55DNN! 	S 	S 	SGHHdR	SGWGG1EGGE222#%$3s	   : Ac                     | S rJ   r@   r   s    rB   	__enter__zMultipartWriter.__enter__  r   rA   exc_typeexc_valexc_tbc                     d S rJ   r@   )r   r  r  r  s       rB   __exit__zMultipartWriter.__exit__  s	     	rA   c                 *    t          | j                  S rJ   )iterr  r   s    rB   __iter__zMultipartWriter.__iter__  s    DK   rA   c                 *    t          | j                  S rJ   )r   r  r   s    rB   __len__zMultipartWriter.__len__  s    4;rA   c                     dS )NTr@   r   s    rB   __bool__zMultipartWriter.__bool__  s    trA   s   \A[!#$%&'*+\-.^_`|~\w]+\Zs   [\x00-\x08\x0A-\x1F\x7F]c                 N   | j         }t          j        | j        |          r|                    d          S t          j        | j        |          rt          d          |                    dd          }|                    dd          }d|                    d          z   dz   S )zzWrap boundary parameter value in quotes, if necessary.

        Reads self.boundary and returns a unicode string.
        r  z*boundary value contains invalid characters   \s   \\   "s   \"rQ   )	r   rf   match_valid_tchar_regexr   search_invalid_qdtext_char_regexr   replace)r   r~   quoted_value_contents      rB   r  zMultipartWriter._boundary_value  s    & 8D+U33 	)<<(((9T4e<< 	KIJJJ  %}}UG<<3;;D&II)00999C??rA   c                 6    | j                             d          S )Nr  )r   r   r   s    rB   r   zMultipartWriter.boundary  s    ~$$W---rA   rs  r   c                 8   |t                      }t          |t                    r/|j                            |           |                     |          S 	 t          ||          }|                     |          S # t          $ r t          d|z            w xY w)Nr   zCannot create payload from %r)	r   r~  r'   r   updateappend_payloadr)   r%   rQ  )r   rs  r   payloads       rB   r   zMultipartWriter.append  s    ?!mmGc7## 		4Kw'''&&s+++4%c7;;; **7333  G G G ?# EFFFGs   A< <Br  c                    d}d}| j         rmt          t          t          h|j                                        z  rJ t          |j        vr.dt          | j                   }|	                    d|           n|j        
                    t          d                                          }|r|dvrt          d|           |dk    rd}|j        
                    t          d                                          }|d	vrt          d
|           |dk    rd}|j        }| |s|st          |          |j        t          <   | j                            |||f           |S )z)Adds a new body part to multipart writer.Nzsection-r   )r   rb   )r  r  r  r$  r  )rb   r   r,  r-  r0  r-  )r   r   r   r   r   keysr   r   r  rL  r   ru   r&  r   rm   r   )r   r  r   te_encodingr   r   s         rB   r  zMultipartWriter.append_payload  s   #"& 	< &~7PQ/&&(() ) #'/994#dk"2"244//$/GGG **+;R@@FFHHH LH,KKK"#J#J#JKKK:%% "/--.GLLRRTTK"NNN"#V#V#VWWWh&&" <D[25d))/GX{;<<<rA   c                 j    |t                      }|                     t          ||                    S )zHelper to append JSON part.Nr  )r   r  r$   )r   rs  r   s      rB   append_jsonzMultipartWriter.append_json  s3     ?!mmG"";sG#D#D#DEEErA   c                 6   t          |t          t          f          sJ |t                      }t          |t                    r!t	          |                                          }t          |d          }|                     t          ||d                    S )z&Helper to append form urlencoded part.NT)doseqz!application/x-www-form-urlencoded)r   r  )	r~  r   r   r   rB  r   r   r  r(   )r   rs  r   r   s       rB   append_formzMultipartWriter.append_form
  s     #'233333?!mmGc7## 	$syy{{##CD)))""g4W  
 
 	
rA   c                 
   d}| j         D ][\  }}}|j        }|s|s| dS |t          dt          | j                  z   dz   |z   t          |j                  z   dz             z  }\|dt          | j                  z   dz   z  }|S )zSize of the payload.r   NrU   r   )r  r   r   r   r   _binary_headers)r   totalr   r   r  	part_sizes         rB   r   zMultipartWriter.size  s     +/; 	 	'D(K	I ; )*;ttSdn%%&  d*++	,
   EE 	S(((1,,rA   rk   rl   r   rN  c                 X     d                      fd j        D                       S )zReturn string representation of the multipart data.

        WARNING: This method may do blocking I/O if parts contain file payloads.
        It should not be called in the event loop. Use as_bytes().decode() instead.
        rb   c              3      K   | ]F\  }}}d j         z   dz   |j                                      z   |                                z   V  GdS )re  z
N)r   r  r   )r   r   _e_ter   rN  r   s       rB   r   z)MultipartWriter.decode.<locals>.<genexpr>:  s       
 
 b# m "))(F;;< kkmm	
 
 
 
 
 
rA   )rn   r  rR  s   ```rB   r   zMultipartWriter.decode4  sQ     ww 
 
 
 
 
 
 "&
 
 
 
 
 	
rA   c                   K   g }| j         D ]\  }}}|                    d| j        z   dz              |                    |j                   |                    ||           d{V }|                    |           |                    d           |                    d| j        z   dz              d                    |          S )zReturn bytes representation of the multipart data.

        This method is async-safe and calls as_bytes on underlying payloads.
        r   r   N   --
rA   )r  r   r   r  rT  rn   )r   r   rN  rz   r   r  r  
part_bytess           rB   rT  zMultipartWriter.as_bytesC  s      
   "[ 	" 	"MD"cLL/'9::: LL-...  $}}Xv>>>>>>>>JLL$$$ LL!!!! 	UT^+i7888xxrA   rU  close_boundaryc                   K   | j         D ]'\  }}}| j        r&t          |j        v sJ d|j        t                   v sJ |                    d| j        z   dz              d{V  |                    |j                   d{V  |s|rst          |          }|r|                    |           |r|	                    |           |                    |           d{V  |
                                 d{V  n|                    |           d{V  |                    d           d{V  )|r(|                    d| j        z   dz              d{V  dS dS )zWrite body.zname=r   r   Nr  )r  r   r   r   rW  r   r  MultipartPayloadWriterenable_compressionenable_encoding	write_eof)r   rU  r  r   r   r  ws          rB   rW  zMultipartWriter.write^  s      ,0; 	( 	('D(K! D*dl::::$,/B"CCCCC,,ut~5?@@@@@@@@@,,t3444444444 	); 	)*622 3((222 3%%k222jjmm#######kkmm########jj(((((((((,,w'''''''''' 	C,,ut~5	ABBBBBBBBBBB	C 	CrA   c                   K   | j         rdS d| _         t          | j                  D ]b\  }\  }}}|j        sR|j        sK	 |                                 d{V  3# t          $ r"}t          j        d||d           Y d}~Zd}~ww xY wcdS )a  
        Close all part payloads that need explicit closing.

        IMPORTANT: This method must not await anything that might not finish
        immediately, as it may be called during cleanup/cancellation. Schedule
        any long-running operations without awaiting them.
        NTz%Failed to close multipart part %d: %s)exc_info)		_consumedr   r  	autocloseconsumedcloserA  r#   error)r   r   r   r   excs        rB   r  zMultipartWriter.close{  s       > 	F "+4;!7!7 	 	C$1> $- **,,&&&&&&&&    #)?cTX        		 	s   A
B$BB)r   N)rF   r5   rJ   rY  )Tr   ),r=   r>   r?   r   rZ  
_autocloserm   r   r  r@  BaseExceptionr   r  r   _Partr  r   r  rK   r  rf   compiler  r  propertyr  r   r
   r   r'   r   r  r  r   rC  r  r   r   r?  rT  r   rW  r  r[  r\  s   @rB   r5   r5   x  s          LLLJ4 4 4t 4t 4 4 4 4 4 4$   }%, % $	
 
   !(5/ ! ! ! !         $     $$ABB!+,H!I!I@ @ @ @ X@> .# . . . X.4 4# 4S(9D(@ 4G 4 4 4 4$g $' $ $ $ $N =AF FF!(c!2T!9F	F F F F -1
 
eCHo&c)::
 c"T)
 
	
 
 
 
* cDj    X(
 
s 
c 
 
 
 
 
 s c QV    8 DHC C*C<@C	C C C C:       rA   r5   c                   d    e Zd ZdeddfdZdeddfdZ	 ddededz  ddfd	Zdd
Z	de
ddfdZdS )r  rU  rF   Nc                 >    || _         d | _        d | _        d | _        d S rJ   )_writer	_encoding	_compress_encoding_buffer)r   rU  s     rB   r   zMultipartPayloadWriter.__init__  s%    %)0426rA   r   c                 h    |dk    r|| _         t                      | _        d S |dk    r	d| _         d S d S )Nr   r,  )r  r   r  )r   r   s     rB   r  z&MultipartPayloadWriter.enable_encoding  sE    x%DN$-KKD!!!+++/DNNN ,+rA   r  strategyc                 4    t          |d|          | _        d S )NT)r   r!  r  )r   r  )r   r   r  s      rB   r  z)MultipartPayloadWriter.enable_compression  s'     ($(
 
 
rA   c                 ,  K   | j         =| j                                         }|r"d | _         |                     |           d {V  | j        dk    r@| j        r;| j                            t          j        | j                             d {V  d S d S d S )Nr   )r  flushrW  r  r  r  r   	b64encoder  s     rB   r  z MultipartPayloadWriter.write_eof  s      >%N((**E (!%jj'''''''''>X%%$ Rl(()9$:O)P)PQQQQQQQQQQQ &%R RrA   r   c                 t  K   | j         &|r$| j                             |           d {V }|sd S | j        dk    r| j        }|J |                    |           |rxt          t          |          d          \  }}|d |dz           ||dz  d          c}| _        |r8t          j        |          }| j	        
                    |           d {V  d S d S d S | j        dk    r4| j	        
                    t          j        |                     d {V  d S | j	        
                    |           d {V  d S )Nr   r,   r,  )r  compressr  r  r   divmodr   r   r  r  rW  r2  b2a_qp)r   r   bufdivmod	enc_chunkb64chunks          rB   rW  zMultipartPayloadWriter.write  s     >% "n55e<<<<<<<< F>X%%'C???JJu 7!#c((A..S47	#'	NCa		N0	40 7%/	::H,,,X666666666667 77 7 ^111,$$X_U%;%;<<<<<<<<<<<,$$U+++++++++++rA   )r  Nr   )r=   r>   r?   r   r   rm   r  r   r  r  r?  rW  r@   rA   rB   r  r    s        73 7 7 7 7 70 0 0 0 0 0 AE
 

36:
	
 
 
 
	R 	R 	R 	R, ,4 , , , , , ,rA   r  )r   )Wr   r2  r	  rf   r=  r  rr   collectionsr   collections.abcr   r   r   r   typesr   typingr	   r
   r   r   r   urllib.parser   r   r   	multidictr   r   abcr   compression_utilsr   r   hdrsr   r   r   r   r   helpersr   r   r   r   r    httpr!   http_exceptionsr"   logr#   r  r$   r%   r&   r'   r(   r)   r*   streamsr+   version_infor.   r2   r?  r   r3   __all__client_reqrepr:   RuntimeWarningr6   r7   rm   rC  r  r8   r9   r   r/   	try_firstrF  r4   r  r5   r  r@   rA   rB   <module>r     s      				 



         F F F F F F F F F F F F       ; ; ; ; ; ; ; ; ; ; ; ; ; ; 6 6 6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 3 % % % % % % ? ? ? ? ? ? ? ?              L K K K K K K K K K K K K K       + + + + + +                              " ! ! ! ! !w76!1222Dw&&&&&&&5)%68KKLF
')6
*
*
*  .------	 	 	 	 	. 	 	 		 	 	 	 	 	 	 	d$$Jd$
3:tCH~%&d$ d$ d$ d$P ,6 CH%(4Z   B," ," ," ," ," ," ," ,"^a@ a@ a@ a@ a@ a@ a@ a@H nEO444!& !& !& !& !&G !& !& 54!&Hg# g# g# g# g# g# g# g#T 	gsC Z Z Z Z Zg Z Z Zz7, 7, 7, 7, 7, 7, 7, 7, 7, 7,rA   