o
    \"<f"                     @   s   d dl mZ d dlmZmZ d dlZd dlm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 edd	Zeejd
gdd	ZdZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    )
namedtuple)
formatdate	parsedateN)
HTTPStatus)mktime)quote)HeadersResponsestatusheadersfile)Allowz	GET, HEAD)zCache-ControlzContent-LocationDateETagExpiresVaryc                   @   s   e Zd ZdddZdd Zdd Zdd	 Zed
d Zedd Z	edd Z
dd Zedd Zedd Zdd Zdd ZdS )
StaticFileNc                 C   sP   |  |||}| ||}t|d | _|d | _| || _| ||| _d S )NLast-Modifiedr   )	get_file_statsget_headersr   last_modifiedetagget_not_modified_responsenot_modified_responseget_alternativesalternatives)selfpathr   	encodings
stat_cachefiles r"   L/var/www/html/kck/venv/lib/python3.10/site-packages/whitenoise/responders.py__init__"   s   
zStaticFile.__init__c                 C   s   |dvrt S | |r| jS | |\}}|dkrt|d}nd }|d}|r:z| |||W S  ty9   Y nw tt	j
||S )N)GETHEADr&   rb
HTTP_RANGE)NOT_ALLOWED_RESPONSEis_not_modifiedr   get_path_and_headersopengetget_range_response
ValueErrorr	   r   OK)r   methodrequest_headersr   r   file_handlerange_headerr"   r"   r#   get_response*   s    

zStaticFile.get_responsec           	      C   s   g }|D ]}|d dkrt |d }q|| q| ||\}}||kr+| ||S |d ur8|dkr8|| |dd|||f |dt|| d f ttj	||S )Nr   Content-Length   Content-Rangezbytes {}-{}/{})
intappendget_byte_range"get_range_not_satisfiable_responseseekformatstrr	   r   PARTIAL_CONTENT)	r   r4   base_headersr3   r   itemsizestartendr"   r"   r#   r.   ?   s   
zStaticFile.get_range_responsec                 C   sR   |  |\}}|dk rt|| d}|d u r|d }||fS t||d }||fS )Nr   r7   )parse_byte_rangemaxmin)r   r4   rC   rD   rE   r"   r"   r#   r;   O   s   zStaticFile.get_byte_rangec           	      C   sz   |   d\}}}|dkrt |  d\}}}|s t |s-t| }d }||fS t|}|r7t|nd }||fS )N=bytes-)strip	partitionr/   r9   )	r4   units_
range_spec	start_strsepend_strrD   rE   r"   r"   r#   rF   Y   s   
zStaticFile.parse_byte_rangec                 C   s*   | d ur|    ttjdd|fgd S )Nr8   z
bytes */{})closer	   r   REQUESTED_RANGE_NOT_SATISFIABLEr>   )r3   rC   r"   r"   r#   r<   k   s   z-StaticFile.get_range_not_satisfiable_responsec              	   C   sL   d t | |i}|r$| D ]\}}z	t ||||< W q ty#   Y qw |S N)	FileEntryitemsMissingFileError)r   r   r    r!   encodingalt_pathr"   r"   r#   r   u   s   zStaticFile.get_file_statsc                 C   s   t |}|d  }t|dkrd|d< d|vr$|jj}|r$t|dd|d< d|vr@t|d }|r@tt|}d||jj	|d< |S )	Nr7   zAccept-Encodingr   r   T)usegmtr   z"{:x}-{:x}")
r   lenstatst_mtimer   r   r9   r   r>   st_size)r   headers_listr!   r   	main_filemtimer   	timestampr"   r"   r#   r      s    zStaticFile.get_headersc                 C   s8   g }t D ]}|| v r||| | f qttj|d dS )Nr
   )NOT_MODIFIED_HEADERSr:   r	   r   NOT_MODIFIED)r   not_modified_headerskeyr"   r"   r#   r      s   z$StaticFile.get_not_modified_responsec                 C   s   g }t | dd d}|D ]0\}}t|  }t|jj|d< |r.||d< td| }ntd}|||j	| f q|S )Nc                 S   s   | d j jS )Nr7   )r^   r`   )ir"   r"   r#   <lambda>   s    z-StaticFile.get_alternatives.<locals>.<lambda>)rh   r6   zContent-Encodingz\b%s\b )
sortedrX   r   r?   r^   r`   recompiler:   r   )rA   r!   r   files_by_sizerZ   
file_entryr   encoding_rer"   r"   r#   r      s   
zStaticFile.get_alternativesc                 C   sn   | d}|d ur|| jkS | jd u rdS z|d }W n
 ty%   Y dS w t|}|d ur5t|| jkS dS )NHTTP_IF_NONE_MATCHFHTTP_IF_MODIFIED_SINCE)r-   r   r   KeyErrorr   )r   r2   previous_etaglast_requestedlast_requested_tsr"   r"   r#   r*      s   


zStaticFile.is_not_modifiedc                 C   s8   | dd}| jD ]\}}}||r||f  S q	d S )NHTTP_ACCEPT_ENCODINGrk   )r-   r   search)r   r2   accept_encodingrq   r   r   r"   r"   r#   r+      s   
zStaticFile.get_path_and_headers)NN)__name__
__module____qualname__r$   r5   r.   r;   staticmethodrF   r<   r   r   r   r   r*   r+   r"   r"   r"   r#   r   !   s$    



	

	
r   c                   @   s   e Zd ZdddZdd ZdS )RedirectNc                 C   s@   |rt | ng }|dt|df ttj|d | _d S )NLocationutf8)	listrX   r:   r   encoder	   r   FOUNDresponse)r   locationr   r"   r"   r#   r$      s   zRedirect.__init__c                 C   s   | j S rV   )r   )r   r1   r2   r"   r"   r#   r5      s   zRedirect.get_responserV   )r{   r|   r}   r$   r5   r"   r"   r"   r#   r      s    
r   c                   @      e Zd ZdS )NotARegularFileErrorNr{   r|   r}   r"   r"   r"   r#   r          r   c                   @   r   )rY   Nr   r"   r"   r"   r#   rY      r   rY   c                   @   r   )IsDirectoryErrorNr   r"   r"   r"   r#   r      r   r   c                   @   s"   e Zd ZdddZedd ZdS )rW   Nc                 C   s,   |d u rt jn|j}| ||| _|| _d S rV   )osr^   __getitem__stat_regular_filer   )r   r   r    stat_functionr"   r"   r#   r$      s   
zFileEntry.__init__c              
   C   s   z|| }W n$ t y   t|  ty* } z|jtjtjfv r%t|  d}~ww t|jsEt	|jr>t
d| td| |S )zj
        Wrap `stat_function` to raise appropriate errors if `path` is not a
        regular file
        NzPath is a directory: {0}zNot a regular file: {0})rt   rY   OSErrorerrnoENOENTENAMETOOLONGr^   S_ISREGst_modeS_ISDIRr   r>   r   )r   r   stat_resulter"   r"   r#   r      s   zFileEntry.stat_regular_filerV   )r{   r|   r}   r$   r~   r   r"   r"   r"   r#   rW      s    
rW   )collectionsr   email.utilsr   r   r   httpr   r   rm   r^   timer   urllib.parser   wsgiref.headersr   r	   METHOD_NOT_ALLOWEDr)   re   objectr   r   	Exceptionr   rY   r   rW   r"   r"   r"   r#   <module>   s,    


 &
