o
    Zh,                     @   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__ O/var/www/html/lang_env/lib/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min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@   _loop	transportsendfileNotImplementedError	write_eof)r,   rA   r2   r3   r4   r1   r>   rF   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 rB   )Zis_weakvalue).0etagrK   r/   r0   	<genexpr>k   s    z2FileResponse._strong_etag_match.<locals>.<genexpr>)lenrM   r   any)rK   rL   r/   rP   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_checkrO   rU   r&   rC   )r,   rA   rK   rU   r-   r/   r0   _not_modifiedm   s   zFileResponse._not_modifiedc                    s&   |  tj d| _t |I d H S r5   )rV   r   rW   content_lengthr&   rC   )r,   rA   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*   	with_namenamestatOSError)r,   r[   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r@| ||	s@| |I d H S |j}
|
d urZ|	d u rZ|j
|
 krZ| |I d H S |j}|d urq| ||rq| |||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rGz|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rG|d	k r|d u r||7 }|d	k rd	}|| }nt#|d ur|n||| }||kr?d| | jtj< | tj  t! "|I d H S t$j }| | |rM|| _%|rV|| jtj&< |rctj| jtj'< d| _(|| _)|j
| _*|| _+d
| jtj,< t-t.|}|t$j krd/||| d || jtj< |d	kst0|j1| j2rt! "|I d H S |d |j3dI d H }|r|}nd	}z| 4||||I d H W t 5|d |j6I d H  S t 5|d |j6I d H  w )Ngzip x-zapplication/octet-streamTFzbytes */r   byteszbytes {}-{}/{}r   rb)7r6   r7   r$   getr   ACCEPT_ENCODINGr8   ra   st_mtime_nsst_sizest_mtimeZif_matchrT   rZ   Zif_unmodified_since	timestampZif_none_matchrX   Zif_modified_sinceZCONTENT_TYPE	mimetypes
guess_typestrZ_statusZif_rangeZ
http_rangestartstop
ValueErrorZCONTENT_RANGErV   r   rW   r&   rC   r<   r   content_typeZCONTENT_ENCODINGZVARY_compressionrO   rU   rY   ZACCEPT_RANGESr   intformatr   methodr"   openrJ   shieldclose)r,   rA   r>   r[   r`   strb   rK   rU   ZifmatchZ
unmodsinceZifnonematchZmodsincectencodingZshould_set_ctr"   	file_sizer4   rq   ZifrangerngendZ
real_startr2   r3   r-   r/   r0   rC      s   








8zFileResponse.prepare)r   r   NN)__name__
__module____qualname____doc__r   rv   r
   rp   r   r'   r   r   r   r@   rJ   staticmethodr   r   boolrT   floatrX   rZ   r(   r)   osstat_resultra   rC   __classcell__r/   r/   r-   r0   r   ,   s~    
 
"r   ).r6   rn   r   r(   typingr   r   r   r   r   r   r   r	   r
   r   r   r   rc   r   abcr   helpersr   r   r   Ztypedefsr   r   Zweb_exceptionsr   r   r   r   Zweb_responser   __all__Zweb_requestr   rf   Z_T_OnChunkSentr   environrh   r   __annotations__r   r/   r/   r/   r0   <module>   s"   
 8