o
    )f1-                     @   s  U d dl Z d dlZd dlZd dl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mZmZ ddlmZmZ ddlmZmZmZm Z  dd	l!m"Z" d
Z#ergddl$m%Z% ee	e&ged f  Z'e(ej)*dZ+e
e( e,d< G dd de"Z-dS )    N)IOTYPE_CHECKINGAny	AwaitableCallableFinalIteratorListOptionalTupleUnioncast   )hdrs)AbstractStreamWriter)ETAG_ANYETagmust_be_empty_body)LooseHeadersPathLike)HTTPNotModifiedHTTPPartialContentHTTPPreconditionFailedHTTPRequestRangeNotSatisfiable)StreamResponse)FileResponse)BaseRequestZAIOHTTP_NOSENDFILE
NOSENDFILEc                       s,  e Zd ZdZ				d&dedededee d	ee d
df 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fddZdddeded
ee
 f fddZddd
ee
 f fdd Zd!ed
eejejef fd"d#Zddd
ee
 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#   r$   )super__init__pathlibPath_path_chunk_size)selfr    r!   r"   r#   r$   	__class__ 8lib/python3.10/site-packages/aiohttp/web_fileresponse.pyr'   /   s   
zFileResponse.__init__writerfobjoffsetcountc                    s   | j }t }|d |j|I d H  |d |j|I d H }|rA||I d H  || }|dkr1n|d |jt||I d H }|s | I d H  |S Nr   )	r+   asyncioget_event_looprun_in_executorseekreadwriteminZdrain)r,   r1   r2   r3   r4   r!   loopchunkr/   r/   r0   _sendfile_fallback<   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s,J z|||||I d H  W n tyM   | ||||I d H  Y S w t  	 I d H  |S N)
r&   preparer   compressionr?   Z_loop	transportsendfileNotImplementedErrorZ	write_eof)r,   r@   r2   r3   r4   r1   r=   rD   r-   r/   r0   	_sendfileR   s   
zFileResponse._sendfile
etag_valueetags.c                    s4   t |dkr|d jtkrdS t fdd|D S )Nr   r   Tc                 3   s     | ]}|j s|j kV  qd S rA   )Zis_weakvalue).0etagrH   r/   r0   	<genexpr>k   s    z2FileResponse._strong_etag_match.<locals>.<genexpr>)lenrJ   r   any)rH   rI   r/   rM   r0   _strong_etag_matchg   s   zFileResponse._strong_etag_matchlast_modifiedc                    s2   |  tj d| _|| _|| _t |I d H S )NF)
set_statusr   status_codeZ_length_checkrL   rR   r&   rB   )r,   r@   rH   rR   r-   r/   r0   _not_modifiedm   s   zFileResponse._not_modifiedc                    s&   |  tj d| _t |I d H S r5   )rS   r   rT   content_lengthr&   rB   )r,   r@   r-   r/   r0   _precondition_failedx   s   z!FileResponse._precondition_failedcheck_for_gzipped_filec                 C   sL   | j }|r||jd }z|| dfW S  ty   Y nw || dfS )zReturn the file path, stat result, and gzip status.

        This method should be called from a thread executor
        since it calls os.stat which may block.
        z.gzTF)r*   Z	with_namenamestatOSError)r,   rX   filepathZ	gzip_pathr/   r/   r0   _get_file_path_stat_and_gzip   s   z)FileResponse._get_file_path_stat_and_gzipc                    s  t  }d|jtjd v }|d | j|I d H \}}}|j	dd|j
d}|j}|j}	|	d urB| ||	sB| |I d H S |j}
|
d ur\|	d u r\|j|
 kr\| |I d H S |j}|d urs| ||rs| |||I d H S |j}|d ur|d u r|j| kr| |||I d H S tj| jvrtt|\}}|sd}d}n|rdnd }d}| j}|j
}|}d }|j}|d u s|j| krIz|j}|j}|j}W n! ty   d| | jtj< | t j! t" #|I d H  Y S w |d us|d urI|d	k r|d u r||7 }|d	k rd	}|| }nt$|d ur|n||| }||krAd| | jtj< | t j! t" #|I d H S t%j!}| | |rO|| _&|rX|| jtj'< |retj| jtj(< d| _)|| _*|j| _+|| _,d
| jtj-< t.t/|}|t%j!krd0||| d || jtj< |d	kst1|j2| j3rt" #|I d H S |d |j4dI d H }|r|}nd	}z| 5||||I d H W t 6|d |j7I d H  S t 6|d |j7I d H  w )Ngzip x-zapplication/octet-streamTFzbytes */r   byteszbytes {}-{}/{}r   rb)8r6   r7   r$   getr   ZACCEPT_ENCODINGlowerr8   r]   st_mtime_nsst_sizest_mtimeZif_matchrQ   rW   Zif_unmodified_sinceZ	timestampZif_none_matchrU   Zif_modified_sinceZCONTENT_TYPE	mimetypesZ
guess_typestrZ_statusZif_rangeZ
http_rangestartstop
ValueErrorZCONTENT_RANGErS   r   rT   r&   rB   r<   r   Zcontent_typeZCONTENT_ENCODINGZVARY_compressionrL   rR   rV   ZACCEPT_RANGESr   intformatr   methodr"   openrG   Zshieldclose)r,   r@   r=   rX   r\   str^   rH   rR   ZifmatchZ
unmodsinceZifnonematchZmodsinceZctencodingZshould_set_ctr"   	file_sizer4   rk   ZifrangerngendZ
real_startr2   r3   r-   r/   r0   rB      s   








8zFileResponse.prepare)r   r   NN)__name__
__module____qualname____doc__r   ro   r
   rj   r   r'   r   r   r   r?   rG   staticmethodr   r   boolrQ   floatrU   rW   r(   r)   osstat_resultr]   rB   __classcell__r/   r/   r-   r0   r   ,   s~    
 
"r   ).r6   ri   r   r(   typingr   r   r   r   r   r   r   r	   r
   r   r   r   r_   r   abcr   Zhelpersr   r   r   Ztypedefsr   r   Zweb_exceptionsr   r   r   r   Zweb_responser   __all__Zweb_requestr   rb   Z_T_OnChunkSentr~   environrd   r   __annotations__r   r/   r/   r/   r0   <module>   s"   
 8