o
    [h                     @   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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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)stat)URLHeaders)FileResponsePlainTextResponse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   |/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/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                   @   s  e Zd Zddddddedeje deded	df
d
dZ	d&dedeje 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_dirr'   r(   r)   r*   returnc                C   sZ   || _ || _| ||| _|| _d| _|r'|d ur)tj|s+t	d| dd S d S d S )NFzDirectory 'z' does not exist)
r'   r(   get_directoriesall_directoriesr)   config_checkedospathisdirRuntimeError)r   r'   r(   r)   r*   r   r   r   r   +   s   zStaticFiles.__init__c                 C   s   g }|dur| | |pg D ]A}tj|}|dus#J d|d|jdus0J d|dtjtj|jdd}tj	|sKJ 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Package z could not be found.zDirectory 'statics' in package z..Zstatics)
append	importlibutil	find_specoriginr/   r0   normpathjoinr1   )r   r'   r(   directoriespackagespecZ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)r.   check_configget_pathget_response)r   r=   r>   r?   r0   responser   r   r   __call__V   s   
zStaticFiles.__call__c                 C   s   t jt jj|d d S )z
        Given the ASGI scope, return the `path` string to serve up,
        with OS specific path seperators, and any '..', '.' components removed.
        r0   /)r/   r0   r8   r9   split)r   r=   r   r   r   rC   d   s   zStaticFiles.get_pathr0   c                    s&  |d dvrt dddS | |I dH \}}|r&t|jr&| |||S |rmt|jrm| jrmtj	
|d}| |I dH \}}|durmt|jrm|d d	sft|d
}|j|j	d	 d}t|dS | |||S | jr| dI dH \}}|durt|jr| j|||ddS t dddS )z`
        Returns an HTTP response, given the incoming path, method and request headers.
        method)GETHEADzMethod Not Allowedi  )r   Nz
index.htmlr0   rG   r=   )r0   )urlz404.htmli  z	Not Found)r   lookup_pathr   S_ISREGst_modefile_responseS_ISDIRr)   r/   r0   r9   endswithr   replacer   )r   r0   r=   	full_pathstat_resultZ
index_pathrM   r   r   r   rD   k   s,   

zStaticFiles.get_responsec              	      sx   | j D ]5}tjtj||}tj|}tj||g|kr"qzt|I d H }||fW   S  ty9   Y qw dS )N) N)r-   r/   r0   realpathr9   commonprefixaio_statFileNotFoundError)r   r0   r'   rU   rV   r   r   r   rN      s   
zStaticFiles.lookup_path   rU   rV   r   c                 C   s>   |d }t |d}t||||d}| |j|rt|jS |S )NrI   rL   )r   rV   rI   )r   r   is_not_modifiedr   r   )r   rU   rV   r=   r   rI   request_headersrE   r   r   r   rQ      s   

zStaticFiles.file_responsec                    sx   | j du rdS z
t| j I dH }W n ty"   td| j  dw t|js8t|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'   rZ   r[   r2   r   rR   rP   S_ISLNK)r   rV   r   r   r   rB      s   
zStaticFiles.check_configresponse_headersr^   c                 C   s   z|d }|d }||krW dS W n	 t y   Y nw z$t|d }t|d }|dur7|dur:||kr=W dS W dS W dS W dS  t yI   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   Tzif-modified-sincezlast-modifiedNF)KeyErrorr   )r   r`   r^   Zif_none_matchr   Zif_modified_sinceZlast_modifiedr   r   r   r]      s4   zStaticFiles.is_not_modified)NN)r\   )r+   N)r"   r#   r$   PathLiketypingListstrboolr   r,   r   r
   r   rF   rC   r	   rD   TupleOptionalr/   rV   rN   intrQ   rB   r   r]   r   r   r   r   r&   *   sh    

$


r&   )importlib.utilr4   r/   r   rc   email.utilsr   Zaiofiles.osrZ   Zstarlette.datastructuresr   r   Zstarlette.responsesr   r   r   r	   Zstarlette.typesr
   r   r   Unionre   rb   r   r&   r   r   r   r   <module>   s    