U
    ·}lj!  ã                   @  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   ú9/tmp/pip-unpacked-wheel-deqjg_vi/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                   sn  || _ z
|j}W n tk
r*   |j}Y nX z
|j}W n$ tk
rZ   |jrRd}nd}Y nX z
|j}W n tk
r~   d}Y nX z
|j}W n tk
r¢   d}Y nX z
|j}W n tk
rÆ   d }Y nX z
|j}W n tk
rê   d }Y nX z
|j	}	W n tk
�r   d }	Y nX z
|j
}
W n tk
�r6   d }
Y nX tƒ jd |||||||	|
d�	 z|j| _W n tk
�r€   |j| _Y nX z|j| _W n^ tk
�rì   t|j�p¬dƒj| _tƒ  d¡}|�rè| j |¡�rè| jt|ƒd … | _Y nX t| jƒ| _|j| _| j�r| j| j| jd� z
|j}W n tk
�r@   d }Y nX |�rR|  |¡ | j�rj| j�sj|  ¡  d S )	Nr   é<   Tzutf-8)	ZapplicationÚautorefreshÚmax_ageÚallow_all_originsÚcharsetÚ	mimetypesÚadd_headers_functionÚ
index_fileÚimmutable_file_testÚ ú/)Úprefix)Úget_responseZWHITENOISE_AUTOREFRESHÚAttributeErrorÚDEBUGZWHITENOISE_MAX_AGEZWHITENOISE_ALLOW_ALL_ORIGINSZWHITENOISE_CHARSETZWHITENOISE_MIMETYPESZWHITENOISE_ADD_HEADERS_FUNCTIONZWHITENOISE_INDEX_FILEZWHITENOISE_IMMUTABLE_FILE_TESTÚsuperÚ__init__ZWHITENOISE_USE_FINDERSÚuse_findersZWHITENOISE_STATIC_PREFIXÚstatic_prefixr   Z
STATIC_URLÚpathr	   ÚrstripÚ
startswithÚlenr   ZSTATIC_ROOTZstatic_rootZ	add_filesZWHITENOISE_ROOTr   Úadd_files_from_finders)r   r%   r   r   r   r   r   r   r   r    r!   Zscript_prefixÚroot©Ú	__class__r   r   r)   %   sŒ    














÷


zWhiteNoiseMiddleware.__init__c                 C  s@   | j r|  |j¡}n| j |j¡}|d k	r6|  ||¡S |  |¡S r   )r   Z	find_fileZ	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 ]\}}|||< q8|S )Nr   )Ústatuszcontent-type)r%   ÚmethodZMETAÚintr:   r   ÚfileÚheaders)r8   r7   Úresponser:   Úhttp_responseÚkeyÚvaluer   r   r   r6   ~   s    

zWhiteNoiseMiddleware.servec                 C  sª   i }t  ¡ D ]d}| d ¡D ]T\}}t|dd ƒp0d d¡}d | j||rJdnd| dd¡f¡}| || 	|¡¡ qqdd„ | 
¡ D ƒ}| ¡ D ]\}}| j|||d� qŒd S )Nr$   r"   r#   ú\c                 S  s   i | ]}|t  |¡“qS r   )ÚosÚstat)Ú.0r,   r   r   r   Ú
<dictcomp>˜   s      z?WhiteNoiseMiddleware.add_files_from_finders.<locals>.<dictcomp>)Ú
stat_cache)r   Zget_findersÚlistÚgetattrÚstripÚjoinr+   ÚreplaceÚ
setdefaultr,   ÚvaluesÚitemsZadd_file_to_dictionary)r   r4   Úfinderr,   Zstorager$   ÚurlrH   r   r   r   r0   ‰   s    

üÿ	z+WhiteNoiseMiddleware.add_files_from_findersc                 #  sT   | j r4| | j¡r4t |t| jƒd … ¡}|r4|V  tƒ  |¡}|D ]
}|V  qDd S r   )r*   r.   r+   r   Úfindr/   r(   Úcandidate_paths_for_url)r   rR   r,   Úpathsr2   r   r   rT   œ   s    z,WhiteNoiseMiddleware.candidate_paths_for_urlc                 C  s^   |  | j¡sdS |t| jƒd… }|  |¡}||kr8dS |  |¡}|rZt|ƒt|ƒkrZd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.   r+   r/   Úget_name_without_hashÚget_static_urlr   )r   r,   rR   ÚnameZname_without_hashZ
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   )rD   r,   Úsplitext)r   ÚfilenameZname_with_hashÚextrX   r   r   r   rV   ¹   s    	z*WhiteNoiseMiddleware.get_name_without_hashc                 C  s(   zt  |¡W S  tk
r"   Y d S X d S r   )r   rR   Ú
ValueError)r   rX   r   r   r   rW   Æ   s    z#WhiteNoiseMiddleware.get_static_url)r   r   r   r   r   r)   r9   Ústaticmethodr6   r0   rT   r!   rV   rW   Ú__classcell__r   r   r2   r   r      s   P	

	)Ú
__future__r   rD   Ú	posixpathr   Úurllib.parser   Zdjango.confr   Zdjango.contrib.staticfilesr   Z"django.contrib.staticfiles.storager   Zdjango.httpr   Zdjango.urlsr	   Zwhitenoise.baser
   Zwhitenoise.string_utilsr   Ú__all__r   r   r   r   r   r   Ú<module>   s   