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mZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ G d
d dZee
jdgddZdZG dd deZG dd dZG dd dZG dd deZG dd deZG dd deZG dd dZdS )    )annotationsN)
formatdate)	parsedate)
HTTPStatus)BufferedIOBase)mktime)quote)Headersc                   @  s   e Zd ZdZdd ZdS )Responsestatusheadersfilec                 C  s   || _ || _|| _d S Nr   )selfr   r   r    r   R/var/www/Befach/backend/venv/lib/python3.10/site-packages/whitenoise/responders.py__init__   s   
zResponse.__init__N)__name__
__module____qualname__	__slots__r   r   r   r   r   r
      s    r
   )Allowz	GET, HEADr   )zCache-ControlzContent-LocationDateETagExpiresVaryc                      s2   e Zd ZdZdd Zd	ddZ fddZ  ZS )

SlicedFilez
    A file like wrapper to handle seeking to the start byte of a range request
    and to return no further output once the end byte of a range request has
    been reached.
    c                 C  s"   | | || _|| d | _d S N   )seekfileobj	remaining)r   r!   startendr   r   r   r   2   s   
zSlicedFile.__init__c                 C  sL   | j dkrdS |dk r| j }nt|| j }| j|}|  j t|8  _ |S )Nr       )r"   minr!   readlen)r   sizedatar   r   r   r(   7   s   
zSlicedFile.readc                   s   t    | j  d S r   )supercloser!   )r   	__class__r   r   r-   B   s   
zSlicedFile.close)r%   )r   r   r   __doc__r   r(   r-   __classcell__r   r   r.   r   r   +   s
    
r   c                   @  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)r   pathr   	encodings
stat_cachefilesr   r   r   r   H   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HEADrA   rb
HTTP_RANGE)NOT_ALLOWED_RESPONSEis_not_modifiedr9   get_path_and_headersopengetget_range_response
ValueErrorr
   r   OK)r   methodrequest_headersr<   r   file_handlerange_headerr   r   r   get_responseP   s    

zStaticFile.get_responsec           	   	   C  s   g }|D ]}|d dkrt |d }q|| q| ||\}}||kr+| ||S |d ur5t|||}|dd| d| d| f |dt|| d f ttj||S )Nr   Content-Lengthr   Content-Rangezbytes -/)	intappendget_byte_range"get_range_not_satisfiable_responser   strr
   r   PARTIAL_CONTENT)	r   rO   base_headersrN   r   itemr*   r#   r$   r   r   r   rI   e   s    zStaticFile.get_range_responsec                 C  sR   |  |\}}|dk rt|| d}|d u r|d }||fS t||d }||fS )Nr   r   )parse_byte_rangemaxr'   )r   rO   r*   r#   r$   r   r   r   rW   u   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=bytesrS   )strip	partitionrJ   rU   )	rO   units_
range_spec	start_strsepend_strr#   r$   r   r   r   r]      s   
zStaticFile.parse_byte_rangec                 C  s*   | d ur|    ttjdd| fgd S )NrR   zbytes */)r-   r
   r   REQUESTED_RANGE_NOT_SATISFIABLE)rN   r*   r   r   r   rX      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 r   )	FileEntryitemsMissingFileError)r<   r=   r>   r?   encodingalt_pathr   r   r   r4      s   zStaticFile.get_file_statsc                 C  s   t |}|d  }t|dkrd|d< d|vr#|j}|r#t|dd|d< d|vrCt|d }|rCtt|}d|d	d
|jd	d|d< |S )Nr   zAccept-Encodingr   r3   T)usegmtr   "xrS   )r	   r)   mtimer   r   rU   r   r*   )r   headers_listr?   r   	main_filerr   r6   	timestampr   r   r   r5      s   zStaticFile.get_headersc                 C  s8   g }t D ]}|| v r||| | f qttj|d dS )Nr   )NOT_MODIFIED_HEADERSrV   r
   r   NOT_MODIFIED)r   not_modified_headerskeyr   r   r   r8      s   z$StaticFile.get_not_modified_responsec                 C  s   g }t | dd d}|D ]/\}}t|  }t|j|d< |r-||d< td| }ntd}|||j| f q|S )Nc                 S  s
   | d j S r   )r*   )ir   r   r   <lambda>   s   
 z-StaticFile.get_alternatives.<locals>.<lambda>)ry   rQ   zContent-Encodingz\b%s\b )	sortedrk   r	   rY   r*   recompilerV   r<   )r[   r?   r;   files_by_sizerm   
file_entryr   encoding_rer   r   r   r:      s   
zStaticFile.get_alternativesc                 C  sj   | 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r3|| jkS dS )NHTTP_IF_NONE_MATCHFHTTP_IF_MODIFIED_SINCE)rH   r7   r6   KeyErrorr   )r   rM   previous_etaglast_requestedlast_requested_tsr   r   r   rE      s   



zStaticFile.is_not_modifiedc                 C  sD   | dd}|dkrd}| jD ]\}}}||r||f  S qd S )NHTTP_ACCEPT_ENCODINGr|   *)rH   r;   search)r   rM   accept_encodingr   r<   r   r   r   r   rF      s   
zStaticFile.get_path_and_headers)NN)r   r   r   r   rP   rI   rW   staticmethodr]   rX   r4   r5   r8   r:   rE   rF   r   r   r   r   r2   G   s$    



	

	
r2   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)	listrk   rV   r   encoder
   r   FOUNDresponse)r   locationr   r   r   r   r      s   zRedirect.__init__c                 C  s   | j S r   )r   )r   rL   rM   r   r   r   rP      s   zRedirect.get_responser   )r   r   r   r   rP   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   )rl   Nr   r   r   r   r   rl      r   rl   c                   @  r   )IsDirectoryErrorNr   r   r   r   r   r      r   r   c                   @  s&   e Zd ZdZdddZedd ZdS )rj   )r<   r*   rr   Nc                 C  s:   || _ |d u r
tjn|j}| ||}|j| _|j| _d S r   )	r<   osstat__getitem__stat_regular_filest_sizer*   st_mtimerr   )r   r<   r>   stat_functionr   r   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: zNot a regular file: )r   rl   OSErrorerrnoENOENTENAMETOOLONGr   S_ISREGst_modeS_ISDIRr   r   )r<   r   stat_resulter   r   r   r     s   zFileEntry.stat_regular_filer   )r   r   r   r   r   r   r   r   r   r   r   rj     s
    
rj   )
__future__r   r   r   r~   r   email.utilsr   r   httpr   ior   timer   urllib.parser   wsgiref.headersr	   r
   METHOD_NOT_ALLOWEDrD   rv   r   r2   r   	Exceptionr   rl   r   rj   r   r   r   r   <module>   s6    	
 &
