o
    4˜Žj@  ã                   @   sø  U 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	 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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#m$Z$m%Z%m&Z& ddl'm(Z(m)Z) ddl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2 dZ3er“ddl4m5Z5 eee6ged f  Z7e8ej9 :d¡ƒZ;ee8 e<d< eƒ Z=ee e<d< edd„ dD ƒƒZ>dZ?eddddddœƒZ@G dd„ deƒZAe=jB C¡  e@ D¡ D ]
\ZEZFe= GeEeF¡ qÛeHƒ ZIeHe jJd  e<d < G d!d"„ d"e2ƒZKdS )#é    N)Úsuppress)ÚEnumÚauto)Ú	MimeTypes)ÚS_ISREG)ÚMappingProxyType)ÚIOÚTYPE_CHECKINGÚAnyÚ	AwaitableÚCallableÚFinalÚIteratorÚListÚOptionalÚSetÚTupleÚUnionÚcasté   )Úhdrs)ÚAbstractStreamWriter)ÚDEFAULT_CHUNK_SIZEÚETAG_ANYÚETagÚmust_be_empty_body)ÚLooseHeadersÚPathLike)ÚHTTPForbiddenÚHTTPNotFoundÚHTTPNotModifiedÚHTTPPartialContentÚHTTPPreconditionFailedÚHTTPRequestRangeNotSatisfiable)ÚStreamResponse)ÚFileResponse)ÚBaseRequestÚAIOHTTP_NOSENDFILEÚ
NOSENDFILEÚCONTENT_TYPESc                 C   s   i | ]}|t j| “qS © )r)   Úencodings_map)Ú.0Úextr*   r*   úU/var/www/kodo/Anonymous/send/lib/python3.10/site-packages/aiohttp/web_fileresponse.pyÚ
<dictcomp>8   s    r/   )ú.brú.gzzapplication/octet-streamr1   r0   z.bz2z.Zz.xz)zapplication/gzipzapplication/x-brotlizapplication/x-bzip2zapplication/x-compresszapplication/x-xzc                   @   s(   e Zd ZdZeƒ Zeƒ Zeƒ Zeƒ ZdS )Ú_FileResponseResultz The result of the file response.N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú	SEND_FILEÚNOT_ACCEPTABLEÚPRE_CONDITION_FAILEDÚNOT_MODIFIEDr*   r*   r*   r.   r2   J   s    
r2   Ú_CLOSE_FUTURESc                       s²  e Zd ZdZedddfdededededB dedB d	df‡ fd
d„Z	de
e deded	efdd„Zdede
e deded	ef
dd„Zddde
e deded	ef
‡ fdd„Zededeedf ded	efdd„ƒZdddeded	edB f‡ fdd „Zddd	edB f‡ fd!d"„Zddd#ed	eeejdB ejedB f fd$d%„Zd#ed	eej dB ejedB f fd&d'„Z!ddd	edB f‡ fd(d)„Z"dddejd*ejd+edB d	edB f
‡ fd,d-„Z#‡  Z$S ).r%   z,A response object can be used to send files.éÈ   NÚpathÚ
chunk_sizeÚstatusÚreasonÚheadersÚreturnc                    s(   t ƒ j|||d� t |¡| _|| _d S )N)r?   r@   rA   )ÚsuperÚ__init__ÚpathlibÚPathÚ_pathÚ_chunk_size)Úselfr=   r>   r?   r@   rA   ©Ú	__class__r*   r.   rD   _   s   
zFileResponse.__init__ÚfobjÚoffsetc                 C   s   |  |¡ | |¡S ©N)ÚseekÚread)rI   rL   rM   r>   r*   r*   r.   Ú_seek_and_readl   s   

zFileResponse._seek_and_readÚwriterÚcountc              	   Ã   sŒ   �| j }t ¡ }| d | j||t||ƒ¡I d H }|r=| |¡I d H  |t|ƒ }|dkr-n| d |jt||ƒ¡I d H }|s| 	¡ I d H  |S ©Nr   )
rH   ÚasyncioÚget_event_loopÚrun_in_executorrQ   ÚminÚwriteÚlenrP   Údrain)rI   rR   rL   rM   rS   r>   ÚloopÚchunkr*   r*   r.   Ú_sendfile_fallbackp   s   €
ÿûzFileResponse._sendfile_fallbackÚrequestr&   c              	   ƒ   s´   �t ƒ  |¡I d H }|d usJ ‚ts| jr |  ||||¡I d H S |j}|j}|d u r.tdƒ‚z| ||||¡I d H  W n t	yO   |  ||||¡I d H  Y S w t ƒ  
¡ I d H  |S )NzConnection lost)rC   Úpreparer(   Úcompressionr^   Ú_loopÚ	transportÚConnectionResetErrorÚsendfileÚNotImplementedErrorÚ	write_eof)rI   r_   rL   rM   rS   rR   r\   rc   rJ   r*   r.   Ú	_sendfile…   s    €
ÿzFileResponse._sendfileÚ
etag_valueÚetags.Úweakc                   s6   t |ƒdkr|d jtkrdS t‡ ‡fdd„|D ƒƒS )Nr   r   Tc                 3   s$   � | ]}ˆs	|j s|jˆ kV  qd S rN   )Úis_weakÚvalue)r,   Úetag©ri   rk   r*   r.   Ú	<genexpr>Ÿ   s   € ÿÿ
ÿz+FileResponse._etag_match.<locals>.<genexpr>)rZ   rm   r   Úany)ri   rj   rk   r*   ro   r.   Ú_etag_match›   s
   ÿzFileResponse._etag_matchÚlast_modifiedc                 ƒ   s2   �|   tj¡ d| _|| _|| _tƒ  |¡I d H S )NF)Ú
set_statusr    Ústatus_codeÚ_length_checkrn   rs   rC   r`   )rI   r_   ri   rs   rJ   r*   r.   Ú_not_modified£   s   €zFileResponse._not_modifiedc                 ƒ   s&   �|   tj¡ d| _tƒ  |¡I d H S rT   )rt   r"   ru   Úcontent_lengthrC   r`   )rI   r_   rJ   r*   r.   Ú_precondition_failed®   s   €z!FileResponse._precondition_failedÚaccept_encodingc                 C   sB  |   |¡\}}}|stjd|dfS |jd›d|jd›�}|j }dur2| j||dd�s2tjd||fS |j }durK|du rK|j	| 
¡ krKtjd||fS |j }	dura| j||	dd�ratjd||fS |j }
durz|	du rz|j	|
 
¡ krztjd||fS | d¡}ttƒ� t | ¡ ¡}W d  ƒ n1 s•w   Y  tj|||fS )a  Return the response result, io object, stat result, and encoding.

        If an uncompressed file is returned, the encoding is set to
        :py:data:`None`.

        This method should be called from a thread executor
        since it calls os.stat which may block.
        NÚxú-F)rk   TÚrb)Ú_get_file_path_stat_encodingr2   r8   Úst_mtime_nsÚst_sizeÚif_matchrr   r9   Úif_unmodified_sinceÚst_mtimeÚ	timestampÚif_none_matchr:   Úif_modified_sinceÚopenr   ÚOSErrorÚosÚstatÚfilenor7   )rI   r_   rz   Ú	file_pathÚstÚfile_encodingri   ÚifmatchÚ
unmodsinceÚifnonematchÚmodsincerL   r*   r*   r.   Ú_make_responseµ   s6   
ÿÿÿ

ûzFileResponse._make_responsec              	   C   s®   | j }t ¡ D ]<\}}||vrq| |j| ¡}ttƒ� | ¡ }t|j	ƒr4|||fW  d   ƒ   S W d   ƒ n1 s>w   Y  q| 
¡ }t|j	ƒrR||d fS d |d fS rN   )rG   ÚENCODING_EXTENSIONSÚitemsÚwith_suffixÚsuffixr   rˆ   Úlstatr   Úst_moderŠ   )rI   rz   rŒ   Úfile_extensionrŽ   Úcompressed_pathr�   r*   r*   r.   r~   í   s   

üý€z)FileResponse._get_file_path_stat_encodingc              
   ƒ   sŽ  �t  ¡ }|j tjd¡ ¡ }z| d | j||¡I d H \}}}}W n/ t	y9   |  
tj¡ tƒ  |¡I d H  Y S  tyP   |  
tj¡ tƒ  |¡I d H  Y S w |tju re|  
tj¡ tƒ  |¡I d H S |tju rr|  |¡I d H S |tju r�|jd›d|jd›�}|j}	|  |||	¡I d H S |d us•J ‚z|  ||||¡I d H W | d |j¡}
t |
¡ |
 tj¡ S | d |j¡}
t |
¡ |
 tj¡ w )NÚ r{   r|   ) rU   Úget_running_looprA   Úgetr   ÚACCEPT_ENCODINGÚlowerrW   r“   ÚPermissionErrorrt   r   ru   rC   r`   rˆ   r   r2   r8   r9   ry   r:   r   r€   rƒ   rw   Ú_prepare_open_fileÚcloser;   ÚaddÚadd_done_callbackÚremove)rI   r_   r\   rz   Úresponse_resultrL   r�   rŽ   ri   rs   Úclose_futurerJ   r*   r.   r`      s@   €
ÿü



ü
zFileResponse.preparer�   rŽ   c                 ƒ   s>  �| j }|j}|j}|}d }	|j }
d u s||
 ¡ kr”z|j}|j}	|j}W n! tyG   d|› �| j	t
j< |  tj¡ tƒ  |¡I d H  Y S w |	d ur”|	dk rc|d u rc|	|7 }	|	dk r^d}	||	 }nt|d urj|n||ƒ|	 }|	|krŒd|› �| j	t
j< |  tj¡ tƒ  |¡I d H S tj}|  |¡ t
j| j	vr°tjdkr£tj}ntj}|| jƒd p®t| _|rÂ|| j	t
j< t
j| j	t
j< d| _|j d›d|jd›�| _!|| _"|| _#d| j	t
j$< |tjkrú|	}|d usçJ ‚d|› d|| d	 › d
|› �| j	t
j< |dk�st%|j&|ƒ�rtƒ  |¡I d H S |	�pd}|  '||||¡I d H S )Nzbytes */r   )é   é   Fr{   r|   Úbyteszbytes r   ú/)(Ú_statusr€   rƒ   Úif_ranger„   Ú
http_rangeÚstartÚstopÚ
ValueErrorÚ_headersr   ÚCONTENT_RANGErt   r#   ru   rC   r`   rX   r!   ÚCONTENT_TYPEÚsysÚversion_infor)   Úguess_file_typeÚ
guess_typerG   ÚFALLBACK_CONTENT_TYPEÚcontent_typeÚCONTENT_ENCODINGrŸ   ÚVARYÚ_compressionr   rn   rs   rx   ÚACCEPT_RANGESr   Úmethodrh   )rI   r_   rL   r�   rŽ   r?   Ú	file_sizeÚ
file_mtimerS   r°   ÚifrangeÚrngÚendÚguesserÚ
real_startrM   rJ   r*   r.   r¢   -  sh   €

ô

ÿ




ÿ
zFileResponse._prepare_open_file)%r3   r4   r5   r6   r   r   ÚintÚstrr   rD   r   r
   r«   rQ   r   r^   rh   ÚstaticmethodÚtupler   Úboolrr   Úfloatrw   ry   r2   ÚioÚBufferedReaderr‰   Ústat_resultr“   rE   rF   r~   r`   r¢   Ú__classcell__r*   r*   rJ   r.   r%   \   s¨    úþýüûúùÿÿÿÿ
þÿÿÿÿþ$ÿÿÿþÿþÿÿÿ
þ8ÿ
þ-þýüûúr%   )LrU   rÎ   r‰   rE   r¶   Ú
contextlibr   Úenumr   r   Ú	mimetypesr   rŠ   r   Útypesr   Útypingr   r	   r
   r   r   r   r   r   r   r   r   r   r   rœ   r   Úabcr   Úhelpersr   r   r   r   Útypedefsr   r   Úweb_exceptionsr   r   r    r!   r"   r#   Úweb_responser$   Ú__all__Úweb_requestr&   r«   Ú_T_OnChunkSentrÌ   Úenvironrž   r(   Ú__annotations__r)   r”   rº   ÚADDITIONAL_CONTENT_TYPESr2   r+   Úclearr•   r»   Ú	extensionÚadd_typeÚsetr;   ÚFuturer%   r*   r*   r*   r.   Ú<module>   sR   
 < ÿûÿ

