o
    e!                     @  s   d dl m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 d d
lmZ d dlmZ dgZG dd deZG dd deZdS )    )annotationsN)basename)urlparse)settings)finders)staticfiles_storage)FileResponse)get_script_prefix)
WhiteNoise)ensure_leading_trailing_slashWhiteNoiseMiddlewarec                   @  s   e Zd ZdZdd ZdS )WhiteNoiseFileResponsea  
    Wrap Django's FileResponse to prevent setting any default headers. For the
    most part these just duplicate work already done by WhiteNoise but in some
    cases (e.g. the content-disposition header introduced in Django 3.0) they
    are actively harmful.
    c                 O  s   d S N )selfargskwargsr   r   H/var/www/html/venv/lib/python3.10/site-packages/whitenoise/middleware.pyset_headers   s   z"WhiteNoiseFileResponse.set_headersN)__name__
__module____qualname____doc__r   r   r   r   r   r      s    r   c                      sf   e Zd ZdZdef fdd	Zdd Zedd Zd	d
 Z	 fddZ
dd Zdd Zdd Z  ZS )r   zh
    Wrap WhiteNoise to allow it to function as Django middleware, rather
    than WSGI middleware.
    Nc                   sP  || _ z|j}W n ty   |j}Y nw z|j}W n ty+   |jr'd}nd}Y nw z|j}W n ty<   d}Y nw z|j}W n tyM   d}Y nw z|j}W n ty^   d }Y nw z|j}W n tyo   d }Y nw z|j	}	W n ty   d }	Y nw z|j
}
W n ty   d }
Y nw t jd |||||||	|
d	 z|j| _W n ty   |j| _Y nw z|j| _W n* ty   t|jpdj| _t d}|r| j|r| jt|d  | _Y nw t| j| _|j| _| jr| j| j| jd z|j}W n ty   d }Y nw |r| | | jr$| js&|   d S d S d S )	Nr   <   Tzutf-8)	applicationautorefreshmax_ageallow_all_originscharset	mimetypesadd_headers_function
index_fileimmutable_file_test /)prefix)get_responseWHITENOISE_AUTOREFRESHAttributeErrorDEBUGWHITENOISE_MAX_AGEWHITENOISE_ALLOW_ALL_ORIGINSWHITENOISE_CHARSETWHITENOISE_MIMETYPESWHITENOISE_ADD_HEADERS_FUNCTIONWHITENOISE_INDEX_FILEWHITENOISE_IMMUTABLE_FILE_TESTsuper__init__WHITENOISE_USE_FINDERSuse_findersWHITENOISE_STATIC_PREFIXstatic_prefixr   
STATIC_URLpathr	   rstrip
startswithlenr   STATIC_ROOTstatic_root	add_filesWHITENOISE_ROOTr   add_files_from_finders)r   r&   r   r   r   r   r   r   r    r!   r"   script_prefixroot	__class__r   r   r2   %   s   










zWhiteNoiseMiddleware.__init__c                 C  s@   | j r
| |j}n| j|j}|d ur| ||S | |S r   )r   	find_file	path_infofilesgetserver&   )r   requeststatic_filer   r   r   __call__u   s   
zWhiteNoiseMiddleware.__call__c                 C  sN   |  |j|j}t|j}t|jpd|d}|d= |jD ]\}}|||< q|S )Nr   )statuszcontent-type)r&   methodMETAintrM   r   fileheaders)rK   rJ   responserM   http_responsekeyvaluer   r   r   rI   ~   s   

zWhiteNoiseMiddleware.servec                 C  s   i }t  D ]2}|d D ]*\}}t|dd pdd}d| j||r%dnd|ddf}|||	| qqdd |
 D }| D ]\}}| j|||d qFd S )Nr%   r#   r$   \c                 S  s   i | ]}|t |qS r   )osstat).0r8   r   r   r   
<dictcomp>   s    z?WhiteNoiseMiddleware.add_files_from_finders.<locals>.<dictcomp>)
stat_cache)r   get_finderslistgetattrstripjoinr6   replace
setdefaultr8   valuesitemsadd_file_to_dictionary)r   rG   finderr8   storager%   urlr\   r   r   r   r@      s"   

	z+WhiteNoiseMiddleware.add_files_from_findersc                 #  sV    | j r|| jrt|t| jd  }|r|V  t |}|D ]}|V  q#d S r   )r4   r:   r6   r   findr;   r1   candidate_paths_for_url)r   ri   r8   pathsrC   r   r   rk      s   z,WhiteNoiseMiddleware.candidate_paths_for_urlc                 C  s^   | | jsdS |t| jd }| |}||krdS | |}|r-t|t|kr-dS dS )z
        Determine whether given URL represents an immutable file (i.e. a
        file with a hash of its contents as part of its name) which can
        therefore be cached forever
        FNT)r:   r6   r;   get_name_without_hashget_static_urlr   )r   r8   ri   namename_without_hash
static_urlr   r   r   r"      s   

z(WhiteNoiseMiddleware.immutable_file_testc                 C  s(   t j|\}}t j|d }|| S )aW  
        Removes the version hash from a filename e.g, transforms
        'css/application.f3ea4bcc2.css' into 'css/application.css'

        Note: this is specific to the naming scheme used by Django's
        CachedStaticFilesStorage. You may have to override this if
        you are using a different static files versioning system
        r   )rX   r8   splitext)r   filenamename_with_hashextro   r   r   r   rm      s   	z*WhiteNoiseMiddleware.get_name_without_hashc                 C  s"   zt |W S  ty   Y d S w r   )r   ri   
ValueError)r   ro   r   r   r   rn      s
   z#WhiteNoiseMiddleware.get_static_url)r   r   r   r   r   r2   rL   staticmethodrI   r@   rk   r"   rm   rn   __classcell__r   r   rC   r   r      s    P	

	)
__future__r   rX   	posixpathr   urllib.parser   django.confr   django.contrib.staticfilesr   "django.contrib.staticfiles.storager   django.httpr   django.urlsr	   whitenoise.baser
   whitenoise.string_utilsr   __all__r   r   r   r   r   r   <module>   s    