a
    ^g                      @  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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d fddZ  ZS )NotModifiedResponse)zcache-controlzcontent-locationdateetagexpiresvaryr   )headersc                   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   c/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/starlette/staticfiles.py
<dictcomp>#       z0NotModifiedResponse.__init__.<locals>.<dictcomp>)status_coder   )super__init__items)r   r   	__class__r   r   r        s    zNotModifiedResponse.__init__)__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ddddddd	d
dZd0ddddddZdddddddZdddddZddddddZdddd d!Zd1d#d$dd%dd&d'd(Z	dd)d*d+Z
d,d,dd-d.d/ZdS )2StaticFilesNFT)	directorypackageshtml	check_dirfollow_symlinkzPathLike | Nonez"list[str | tuple[str, str]] | NoneboolNone)r)   r*   r+   r,   r-   returnc                C  sX   || _ || _| ||| _|| _d| _|| _|rT|d urTtj	|sTt
d| d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__zlist[PathLike])r)   r*   r0   c                 C  s   g }|dur| | |pg D ]}t|tr6|\}}nd}tj|}|dus^J d|d|jdusxJ d|dtj	tj
|jd|}tj|sJ 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.
        NZstaticszPackage z could not be found.z..r1   z' in package )append
isinstancetuple	importlibutil	find_specoriginr5   r6   normpathjoinr7   )r   r)   r*   directoriespackageZstatics_dirspecpackage_directoryr   r   r   r2   :   s$    	


zStaticFiles.get_directoriesr   r   r   )scopereceivesendr0   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)r4   check_configget_pathget_response)r   rF   rG   rH   r6   responser   r   r   __call__W   s    
zStaticFiles.__call__str)rF   r0   c                 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   r5   r6   r@   rA   split)r   rF   Z
route_pathr   r   r   rL   e   s    zStaticFiles.get_pathr
   )r6   rF   r0   c              
     s  |d dvrt ddztj| j|I dH \}}W nX tyP   t ddY n> ty } z&|jtjkrtt dd|W Y d}~n
d}~0 0 |rt	
|jr| |||S |rLt	|jrL| jrLtj|d}tj| j|I dH \}}|durLt	
|jrL|d	 d
s>t|d}|j|jd
 d}t|dS | |||S | jrtj| jdI dH \}}|rt	
|jrt||ddS t dddS )z`
        Returns an HTTP response, given the incoming path, method and request headers.
        method)GETHEADi  )r   Ni  i  z
index.htmlr6   rQ   rF   )r6   )urlz404.html)stat_resultr   )r   anyio	to_threadrun_synclookup_pathPermissionErrorOSErrorerrnoZENAMETOOLONGstatS_ISREGst_modefile_responseS_ISDIRr+   r5   r6   rA   endswithr   replacer	   r   )r   r6   rF   	full_pathrX   excZ
index_pathrW   r   r   r   rM   m   s4    



zStaticFiles.get_responsez!tuple[str, os.stat_result | None])r6   r0   c              
   C  s   | j D ]}tj||}| jr,tj|}ntj|}tj|}tj||gt|kr^qz|t	|fW   S  t
tfy   Y qY q0 qdS )N) N)r3   r5   r6   rA   r-   abspathrealpath
commonpathrP   r`   FileNotFoundErrorNotADirectoryError)r   r6   r)   Zjoined_pathrg   r   r   r   r\      s    
zStaticFiles.lookup_path   PathLikezos.stat_resultint)rg   rX   rF   r   r0   c                 C  s4   t |d}t|||d}| |j|r0t|jS |S )NrV   )r   rX   )r   r   is_not_modifiedr   r   )r   rg   rX   rF   r   request_headersrN   r   r   r   rc      s
    

zStaticFiles.file_response)r0   c                   s~   | j du rdS ztjtj| j I dH }W n$ tyN   td| j  dY n0 t|j	szt
|j	sztd| j  d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)   rY   rZ   r[   r5   r`   rm   r8   rd   rb   S_ISLNK)r   rX   r   r   r   rK      s    
zStaticFiles.check_configr   )response_headersrs   r0   c                 C  s   z2|d }|d }|dd | dD v r0W dS W n tyD   Y n0 z:t|d }t|d }|d	ur~|d	ur~||kr~W dS W n ty   Y n0 d
S )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>   r   z/StaticFiles.is_not_modified.<locals>.<listcomp>,Tzif-modified-sincezlast-modifiedNF)rR   KeyErrorr   )r   ru   rs   Zif_none_matchr   Zif_modified_sinceZlast_modifiedr   r   r   rr      s    

zStaticFiles.is_not_modified)NN)ro   )r$   r%   r&   r    r2   rO   rL   rM   r\   rc   rK   rr   r   r   r   r   r(   '   s"     * r(   ) 
__future__r   r_   importlib.utilr<   r5   r`   typingemail.utilsr   rY   Zanyio.to_threadZstarlette._utilsr   Zstarlette.datastructuresr   r   Zstarlette.exceptionsr   Zstarlette.responsesr   r	   r
   Zstarlette.typesr   r   r   UnionrP   rp   r   r(   r   r   r   r   <module>   s    