o
    h!                     @  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 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   i/var/www/html/construction_image-detection-poc/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__W   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   e   s   zStaticFiles.get_pathr7   r
   c              
     sz  |d dvrt ddztj| j|I dH \}}W n$ ty'   t dd ty? } z|jtjkr9t dd|d}~ww |rOt	
|jrO| |||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  i  z
index.htmlr7   rU   rJ   )r7   )urlz404.html)stat_resultr   )r   anyio	to_threadrun_synclookup_pathPermissionErrorOSErrorerrnoENAMETOOLONGstatS_ISREGst_modefile_responseS_ISDIRr*   r6   r7   rD   endswithr   replacer	   r   )r   r7   rJ   	full_pathr]   exc
index_pathr\   r   r   r   rQ   m   s:   





zStaticFiles.get_response!tuple[str, os.stat_result | None]c              
   C  s   | j D ]G}tj||}| jrtj|}tj|}ntj|}tj|}tj||gt|kr5qz
|t	|fW   S  t
tfyJ   Y qw dS )N) N)r4   r6   r7   rD   r,   abspathrealpath
commonpathrT   rf   FileNotFoundErrorNotADirectoryError)r   r7   r(   joined_pathrm   r   r   r   ra      s   
zStaticFiles.lookup_path   rm   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   rm   r]   rJ   r   request_headersrR   r   r   r   ri      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   rf   ru   r9   rj   rh   S_ISLNK)r   r]   r   r   r   rO      s   
zStaticFiles.check_configresponse_headersr   r}   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   r}   if_none_matchr   if_modified_sincelast_modifiedr   r   r   r|      s0   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   rp   )rx   )
rm   ry   r]   rz   rJ   r   r   r{   r0   r
   )r0   r1   )r   r   r}   r   r0   r/   )r#   r$   r%   r   r3   rS   rP   rQ   ra   ri   rO   r|   r   r   r   r   r'   '   s$    



*
r'   ) 
__future__r   rd   importlib.utilr?   r6   rf   typingemail.utilsr   r^   anyio.to_threadstarlette._utilsr   starlette.datastructuresr   r   starlette.exceptionsr   starlette.responsesr   r	   r
   starlette.typesr   r   r   UnionrT   ry   r   r'   r   r   r   r   <module>   s"    