U
    æ=i!  ã                   @  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   úU/var/www/html/mysite/school/venv/lib/python3.8/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                   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)	Ú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   Ú
STATIC_URLÚpathr	   ÚrstripÚ
startswithÚlenr   ÚSTATIC_ROOTZstatic_rootÚ	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   Ú	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 ]\}}|||< q8|S )Nr   )Ústatuszcontent-type)r&   ÚmethodÚMETAÚintr@   r   ÚfileÚheaders)r>   r=   Úresponser@   Úhttp_responseÚkeyÚvaluer   r   r   r<   ~   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   Úget_findersÚlistÚgetattrÚstripÚjoinr,   ÚreplaceÚ
setdefaultr.   ÚvaluesÚitemsÚadd_file_to_dictionary)r   r:   Úfinderr.   Ústorager%   ÚurlrO   r   r   r   r4   ‰   s    

üÿ	z+WhiteNoiseMiddleware.add_files_from_findersc                 #  sT   | j r4| | j¡r4t |t| jƒd … ¡}|r4|V  tƒ  |¡}|D ]
}|V  qDd S r   )r+   r0   r,   r   Úfindr1   r)   Úcandidate_paths_for_url)r   r\   r.   Úpathsr6   r   r   r^   œ   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)r0   r,   r1   Úget_name_without_hashÚget_static_urlr   )r   r.   r\   Ú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   )rK   r.   Úsplitext)r   ÚfilenameZname_with_hashÚextrb   r   r   r   r`   ¹   s    	z*WhiteNoiseMiddleware.get_name_without_hashc                 C  s(   zt  |¡W S  tk
r"   Y d S X d S r   )r   r\   Ú
ValueError)r   rb   r   r   r   ra   Æ   s    z#WhiteNoiseMiddleware.get_static_url)r   r   r   r   r   r*   r?   Ústaticmethodr<   r4   r^   r"   r`   ra   Ú__classcell__r   r   r6   r   r      s   P	

	)Ú
__future__r   rK   Ú	posixpathr   Úurllib.parser   Údjango.confr   Z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   