o
    þÞhË  ã                   @   s²   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mZ d d
lmZ G dd„ dƒZG dd„ deeƒZG dd„ deeƒZdS )é    )Úurlparse)Úurl2pathname)Úsync_to_async)Úsettings)Úutils)Úserve)ÚASGIHandler)Úresponse_for_exception)ÚWSGIHandlerÚget_path_info)ÚHttp404c                   @   sL   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚStaticFilesHandlerMixinz8
    Common methods used by WSGI and ASGI handlers.
    Tc                 C   s   d S ©N© ©Úselfr   r   úc/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/contrib/staticfiles/handlers.pyÚload_middleware   s   z'StaticFilesHandlerMixin.load_middlewarec                 C   s   t  ¡  tjS r   )r   Úcheck_settingsr   Ú
STATIC_URLr   r   r   r   Úget_base_url   s   z$StaticFilesHandlerMixin.get_base_urlc                 C   s   |  | jj¡o| jj S )zÄ
        Check if the path should be handled. Ignore the path if:
        * the host is provided as part of the base_url
        * the request's path isn't under the media path (or equal)
        )Ú
startswithÚbase_urlÚpathÚnetloc)r   r   r   r   r   Ú_should_handle!   s   z&StaticFilesHandlerMixin._should_handlec                 C   s   |  | jj¡}t|ƒS )zW
        Return the relative path to the media file on disk for the given URL.
        )Úremoveprefixr   r   r   )r   ÚurlÚrelative_urlr   r   r   Ú	file_path)   s   z!StaticFilesHandlerMixin.file_pathc                 C   s   t ||  |j¡dd�S )zServe the request path.T)Úinsecure)r   r   r   )r   Úrequestr   r   r   r   0   s   zStaticFilesHandlerMixin.servec              
   C   s<   z|   |¡W S  ty } zt||ƒW  Y d }~S d }~ww r   )r   r   r	   ©r   r!   Úer   r   r   Úget_response4   s   €ÿz$StaticFilesHandlerMixin.get_responsec              
   Ã   sZ   �zt | jdd�|ƒI d H W S  ty, } zt tdd�||ƒI d H W  Y d }~S d }~ww )NF)Úthread_sensitive)r   r   r   r	   r"   r   r   r   Úget_response_async:   s   €
ÿ€ÿz*StaticFilesHandlerMixin.get_response_asyncN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úhandles_filesr   r   r   r   r   r$   r&   r   r   r   r   r      s    r   c                       s,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚStaticFilesHandlerz�
    WSGI middleware that intercepts calls to the static files directory, as
    defined by the STATIC_URL setting, and serves those files.
    c                    s"   || _ t|  ¡ ƒ| _tƒ  ¡  d S r   )Úapplicationr   r   r   ÚsuperÚ__init__©r   r-   ©Ú	__class__r   r   r/   I   s   zStaticFilesHandler.__init__c                    s(   |   t|ƒ¡s|  ||¡S tƒ  ||¡S r   )r   r   r-   r.   Ú__call__)r   ÚenvironÚstart_responser1   r   r   r3   N   s   zStaticFilesHandler.__call__)r'   r(   r)   r*   r/   r3   Ú__classcell__r   r   r1   r   r,   C   s    r,   c                       s4   e Zd ZdZdd„ Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚASGIStaticFilesHandlerzŽ
    ASGI application which wraps another and intercepts requests for static
    files, passing them off to Django's static file serving.
    c                 C   s   || _ t|  ¡ ƒ| _d S r   )r-   r   r   r   r0   r   r   r   r/   Z   s   zASGIStaticFilesHandler.__init__c                 ƒ   sF   �|d dkr|   |d ¡rtƒ  |||¡I d H S |  |||¡I d H S )NÚtypeÚhttpr   )r   r.   r3   r-   )r   ÚscopeÚreceiveÚsendr1   r   r   r3   ^   s   €zASGIStaticFilesHandler.__call__c                 ƒ   sL   �t ƒ  |¡I d H }|j |j¡ |jr$|js$|j‰ ‡ fdd„}|ƒ |_|S )Nc                    s$   �t tƒˆ ƒI d H D ]} | V  q
d S r   )r   Úlist)Úpart©Ú	_iteratorr   r   Úawrappern   s   €ÿz;ASGIStaticFilesHandler.get_response_async.<locals>.awrapper)r.   r&   Ú_resource_closersÚappendÚcloseÚ	streamingÚis_asyncÚstreaming_content)r   r!   ÚresponserA   r1   r?   r   r&   g   s   €z)ASGIStaticFilesHandler.get_response_async)r'   r(   r)   r*   r/   r3   r&   r6   r   r   r1   r   r7   T   s
    	r7   N)Úurllib.parser   Úurllib.requestr   Úasgiref.syncr   Údjango.confr   Údjango.contrib.staticfilesr   Ú django.contrib.staticfiles.viewsr   Údjango.core.handlers.asgir   Údjango.core.handlers.exceptionr	   Údjango.core.handlers.wsgir
   r   Údjango.httpr   r   r,   r7   r   r   r   r   Ú<module>   s    4