o
    Þ­jˆ!  ã                   @  sÈ   d dl m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	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mZmZ d dlmZmZmZ ejed	f ZG d
d„ deƒZG dd„ dƒZdS )é    )ÚannotationsN)Ú	parsedate)Úget_route_path)ÚURLÚHeaders)ÚHTTPException)ÚFileResponseÚRedirectResponseÚResponse)ÚReceiveÚScopeÚSendzos.PathLike[str]c                      s"   e Zd ZdZd‡ fdd„Z‡  ZS )ÚNotModifiedResponse)zcache-controlzcontent-locationÚdateÚetagÚexpiresÚvaryÚheadersr   c                   s&   t ƒ jd‡ fdd„| ¡ D ƒd� d S )Ni0  c                   s    i | ]\}}|ˆ j v r||“qS © )ÚNOT_MODIFIED_HEADERS)Ú.0ÚnameÚvalue©Úselfr   úR/var/www/html/CropPilot/venv/lib/python3.10/site-packages/starlette/staticfiles.pyÚ
<dictcomp>"   s
    
þz0NotModifiedResponse.__init__.<locals>.<dictcomp>)Ústatus_coder   )ÚsuperÚ__init__Úitems)r   r   ©Ú	__class__r   r   r      s   
þ
þzNotModifiedResponse.__init__)r   r   )Ú__name__Ú
__module__Ú__qualname__r   r   Ú__classcell__r   r   r!   r   r      s    	r   c                   @  s~   e Zd Zddddddœd6dd„Z		d7d8dd„Zd9dd„Zd:dd„Zd;d!d"„Zd<d$d%„Z	&d=d>d-d.„Z	d?d/d0„Z
d@d4d5„ZdS )AÚStaticFilesNFT)Ú	directoryÚpackagesÚhtmlÚ	check_dirÚfollow_symlinkr(   úPathLike | Noner)   ú"list[str | tuple[str, str]] | Noner*   Úboolr+   r,   ÚreturnÚNonec                C  s`   || _ || _|  ||¡| _|| _d| _|| _|r*|d ur,tj 	|¡s.t
d|› d�ƒ‚d S d S d S )NFúDirectory 'z' does not exist)r(   r)   Úget_directoriesÚall_directoriesr*   Úconfig_checkedr,   ÚosÚpathÚisdirÚRuntimeError)r   r(   r)   r*   r+   r,   r   r   r   r   +   s   	ÿzStaticFiles.__init__úlist[PathLike]c                 C  sÄ   g }|dur|  |¡ |pg D ]P}t|tƒr|\}}nd}tj |¡}|dus/J d|›d�ƒ‚|jdus<J d|›d�ƒ‚tj 	tj 
|jd|¡¡}tj |¡sZJ d|›d|›d�ƒ‚|  |¡ q|S )z�
        Given `directory` and `packages` arguments, return a list of all the
        directories that should be used for serving static files from.
        NÚstaticszPackage z could not be found.z..r2   z' in package )ÚappendÚ
isinstanceÚtupleÚ	importlibÚutilÚ	find_specÚoriginr6   r7   ÚnormpathÚjoinr8   )r   r(   r)   ÚdirectoriesÚpackageÚstatics_dirÚspecÚpackage_directoryr   r   r   r3   =   s(   	


ÿÿþzStaticFiles.get_directoriesÚscoper   Úreceiver   Úsendr   c                 Ã  s^   �|d dks	J ‚| j s|  ¡ I dH  d| _ |  |¡}|  ||¡I dH }||||ƒI dH  dS )z'
        The ASGI entry point.
        ÚtypeÚhttpNT)r5   Úcheck_configÚget_pathÚget_response)r   rJ   rK   rL   r7   Úresponser   r   r   Ú__call__\   s   €
zStaticFiles.__call__Ústrc                 C  s"   t |ƒ}tj tjj| d¡Ž ¡S )z�
        Given the ASGI scope, return the `path` string to serve up,
        with OS specific path separators, and any '..', '.' components removed.
        ú/)r   r6   r7   rC   rD   Úsplit)r   rJ   Ú
route_pathr   r   r   rP   j   s   zStaticFiles.get_pathr7   r
   c                 Ã  sX  �|d dvrt dd�‚ztj | j|¡I dH \}}W n ty'   t dd�‚ ty.   ‚ w |r>t |j	¡r>|  
|||¡S |rˆt |j	¡rˆ| jrˆtj |d¡}tj | j|¡I dH \}}|durˆt |j	¡rˆ|d  d	¡s�t|d
�}|j|jd	 d�}t|d�S |  
|||¡S | jr§tj | jd¡I dH \}}|r§t |j	¡r§t||dd�S t dd�‚)z`
        Returns an HTTP response, given the incoming path, method and request headers.
        Úmethod)ÚGETÚHEADi•  )r   Ni‘  z
index.htmlr7   rU   ©rJ   )r7   )Úurlz404.htmli”  )Ústat_resultr   )r   ÚanyioÚ	to_threadÚrun_syncÚlookup_pathÚPermissionErrorÚOSErrorÚstatÚS_ISREGÚst_modeÚfile_responseÚS_ISDIRr*   r6   r7   rD   Úendswithr   Úreplacer	   r   )r   r7   rJ   Ú	full_pathr]   Ú
index_pathr\   r   r   r   rQ   r   s@   €
ÿ
ÿÿ

ÿ
zStaticFiles.get_responseú!tuple[str, os.stat_result | None]c              
   C  sŠ   | j D ]?}tj ||¡}| jrtj |¡}ntj |¡}tj |¡}tj ||g¡|kr-qz
|t |¡fW   S  t	t
fyB   Y qw dS )N)Ú N)r4   r6   r7   rD   r,   ÚabspathÚrealpathÚ
commonpathrd   ÚFileNotFoundErrorÚNotADirectoryError)r   r7   r(   Újoined_pathrk   r   r   r   ra   ž   s   
ÿzStaticFiles.lookup_pathéÈ   rk   ÚPathLiker]   úos.stat_resultr   Úintc                 C  s4   t |d�}t|||d�}|  |j|¡rt|jƒS |S )Nr[   )r   r]   )r   r   Úis_not_modifiedr   r   )r   rk   r]   rJ   r   Úrequest_headersrR   r   r   r   rg   °   s   
ÿ
zStaticFiles.file_responsec                 Ã  s€   �| j du rdS ztj tj| j ¡I dH }W n ty&   td| j › d�ƒ‚w t |j	¡s<t 
|j	¡s>td| j › d�ƒ‚dS dS )zÇ
        Perform a one-off configuration check that StaticFiles is actually
        pointed at a directory, so that we can raise loud errors rather than
        just returning 404 responses.
        NzStaticFiles directory 'z' does not exist.zStaticFiles path 'z' is not a directory.)r(   r^   r_   r`   r6   rd   rr   r9   rh   rf   ÚS_ISLNK)r   r]   r   r   r   rO   À   s   €
ÿÿÿÿzStaticFiles.check_configÚresponse_headersr   rz   c                 C  s¤   z|d }|d }|dd„ |  d¡D ƒv rW dS W n	 ty"   Y nw z$t|d ƒ}t|d ƒ}|d	ur?|d	urB||krEW dS W d
S W d
S W d
S  tyQ   Y d
S w )z�
        Given the request and response headers, return `True` if an HTTP
        "Not Modified" response could be returned instead.
        zif-none-matchr   c                 S  s   g | ]}|  d ¡‘qS )z W/)Ústrip)r   Útagr   r   r   Ú
<listcomp>Þ   s    z/StaticFiles.is_not_modified.<locals>.<listcomp>ú,Tzif-modified-sincezlast-modifiedNF)rV   ÚKeyErrorr   )r   r|   rz   Úif_none_matchr   Úif_modified_sinceÚlast_modifiedr   r   r   ry   Ô   s4   ÿÿüùúýýzStaticFiles.is_not_modified)r(   r-   r)   r.   r*   r/   r+   r/   r,   r/   r0   r1   )NN)r(   r-   r)   r.   r0   r:   )rJ   r   rK   r   rL   r   r0   r1   )rJ   r   r0   rT   )r7   rT   rJ   r   r0   r
   )r7   rT   r0   rm   )ru   )
rk   rv   r]   rw   rJ   r   r   rx   r0   r
   )r0   r1   )r|   r   rz   r   r0   r/   )r#   r$   r%   r   r3   rS   rP   rQ   ra   rg   rO   ry   r   r   r   r   r'   *   s$    ùý



,û
r'   )Ú
__future__r   Úimportlib.utilr?   r6   rd   ÚtypingÚemail.utilsr   r^   Úanyio.to_threadÚstarlette._utilsr   Ústarlette.datastructuresr   r   Ústarlette.exceptionsr   Ústarlette.responsesr   r	   r
   Ústarlette.typesr   r   r   ÚUnionrT   rv   r   r'   r   r   r   r   Ú<module>   s     