o
    Ci"                     @   s   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 )    N)	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ef fddZ  ZS )NotModifiedResponse)zcache-controlzcontent-locationdateetagexpiresvary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   m/var/www/html/fyndo/python/python_agents/rag_suite/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__)__name__
__module____qualname__r   r   r   __classcell__r   r   r    r   r      s    	r   c                   @   sT  e Zd Zdddddddeje dejejejeej	eef f   de
de
d	e
d
dfddZ		d'deje dejejejeej	eef f   d
eje fddZdededed
dfddZded
efddZdeded
efddZded
ej	eejej f fddZ	d(dedejdeded
ef
dd Zd)d!d"Zd#ed$ed
e
fd%d&ZdS )*StaticFilesNFT)	directorypackageshtml	check_dirfollow_symlinkr'   r(   r)   r*   r+   returnc                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__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..r-   z' in package )append
isinstancetuple	importlibutil	find_specoriginr1   r2   normpathjoinr3   )r   r'   r(   directoriespackagestatics_dirspecpackage_directoryr   r   r   r.   =   s(   


zStaticFiles.get_directoriesscopereceivesendc                    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)r0   check_configget_pathget_response)r   rD   rE   rF   r2   responser   r   r   __call__^   s   
zStaticFiles.__call__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   r1   r2   r=   r>   split)r   rD   
route_pathr   r   r   rJ   l   s   zStaticFiles.get_pathr2   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 |rt|j	r| jrtj|d}tj| j|I dH \}}|durt|j	r|d d	st|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)z`
        Returns an HTTP response, given the incoming path, method and request headers.
        method)GETHEADi  )r   Ni  z
index.htmlr2   rN   rD   )r2   )urlz404.htmli  )stat_resultr   )r   anyio	to_threadrun_synclookup_pathPermissionErrorOSErrorstatS_ISREGst_modefile_responseS_ISDIRr)   r1   r2   r>   endswithr   replacer   r   )r   r2   rD   	full_pathrV   
index_pathrU   r   r   r   rK   t   s@   




zStaticFiles.get_responsec              
   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)r/   r1   r2   r>   r+   abspathrealpath
commonpathr]   FileNotFoundErrorNotADirectoryError)r   r2   r'   joined_pathrd   r   r   r   rZ      s   
zStaticFiles.lookup_path   rd   rV   r   c                 C   s4   t |d}t|||d}| |j|rt|jS |S )NrT   )r   rV   )r   r   is_not_modifiedr   r   )r   rd   rV   rD   r   request_headersrL   r   r   r   r`      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'   rW   rX   rY   r1   r]   rj   r4   ra   r_   S_ISLNK)r   rV   r   r   r   rI      s   
zStaticFiles.check_configresponse_headersro   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)rO   KeyErrorr   )r   rq   ro   if_none_matchr   if_modified_sincelast_modifiedr   r   r   rn      s4   zStaticFiles.is_not_modified)NN)rm   )r,   N)r"   r#   r$   typingOptionalPathLikeListUnionstrTupleboolr   r.   r   r
   r   rM   rJ   r	   rK   r1   rV   rZ   intr`   rI   r   rn   r   r   r   r   r&   (   sx    	


!,


r&   )importlib.utilr9   r1   r]   rz   email.utilsr   rW   anyio.to_threadstarlette._utilsr   starlette.datastructuresr   r   starlette.exceptionsr   starlette.responsesr   r   r	   starlette.typesr
   r   r   r~   r   r|   r   r&   r   r   r   r   <module>   s    