o
    þÞhò  ã                   @   sŒ   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 G d
d„ deƒZG dd„ deƒZdS )é    N)Úurlsplit)Úsettings)ÚPermissionDenied)Úmail_managers)ÚHttpResponsePermanentRedirect)Úis_valid_path)ÚMiddlewareMixin)Úescape_leading_slashesc                   @   s4   e Zd ZdZeZdd„ Zdd„ Zdd„ Zdd	„ Z	d
S )ÚCommonMiddlewarea  
    "Common" middleware for taking care of some basic operations:

        - Forbid access to User-Agents in settings.DISALLOWED_USER_AGENTS

        - URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings,
          append missing slashes and/or prepends missing "www."s.

            - If APPEND_SLASH is set and the initial URL doesn't end with a
              slash, and it is not found in urlpatterns, form a new URL by
              appending a slash at the end. If this new URL is found in
              urlpatterns, return an HTTP redirect to this new URL; otherwise
              process the initial URL as usual.

          This behavior can be customized by subclassing CommonMiddleware and
          overriding the response_redirect_class attribute.
    c                 C   s’   |j  d¡}|durtjD ]}| |¡rtdƒ‚q| ¡ }tjrC|rE| d¡sG|  	|¡r2|  
|¡}n| ¡ }|  |j› d|› |› �¡S dS dS dS )z‚
        Check for denied User-Agents and rewrite the URL based on
        settings.APPEND_SLASH and settings.PREPEND_WWW
        ÚHTTP_USER_AGENTNzForbidden user agentzwww.z://www.)ÚMETAÚgetr   ÚDISALLOWED_USER_AGENTSÚsearchr   Úget_hostÚPREPEND_WWWÚ
startswithÚshould_redirect_with_slashÚget_full_path_with_slashÚget_full_pathÚresponse_redirect_classÚscheme)ÚselfÚrequestÚ
user_agentÚuser_agent_regexÚhostÚpath© r   úX/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/middleware/common.pyÚprocess_request"   s   

ÿ
öz CommonMiddleware.process_requestc                 C   sT   t jr(|j d¡s(t|ddƒ}t|j|ƒs(td|j |ƒ}|r(|j}t|ddƒS dS )z˜
        Return True if settings.APPEND_SLASH is True and appending a slash to
        the request path turns an invalid path into a valid one.
        ú/ÚurlconfNz%s/Úshould_append_slashTF)r   ÚAPPEND_SLASHÚ	path_infoÚendswithÚgetattrr   Úfunc)r   r   r"   ÚmatchÚviewr   r   r   r   >   s   z+CommonMiddleware.should_redirect_with_slashc                 C   sD   |j dd�}t|ƒ}tjr |jdv r td|j| ¡ | dœ ƒ‚|S )zÈ
        Return the full path of the request with a trailing slash appended.

        Raise a RuntimeError if settings.DEBUG is True and request.method is
        DELETE, POST, PUT, or PATCH.
        T)Úforce_append_slash)ÚDELETEÚPOSTÚPUTÚPATCHa"  You called this URL via %(method)s, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining %(method)s data. Change your form to point to %(url)s (note the trailing slash), or set APPEND_SLASH=False in your Django settings.)ÚmethodÚurl)r   r	   r   ÚDEBUGr0   ÚRuntimeErrorr   )r   r   Únew_pathr   r   r   r   L   s   
þûÿz)CommonMiddleware.get_full_path_with_slashc                 C   sL   |j dkr|  |¡r|  |  |¡¡S |js$| d¡s$tt|jƒƒ|j	d< |S )z¥
        When the status code of the response is 404, it may redirect to a path
        with an appended slash if should_redirect_with_slash() returns True.
        é”  zContent-Length)
Ústatus_coder   r   r   Ú	streamingÚ
has_headerÚstrÚlenÚcontentÚheaders)r   r   Úresponser   r   r   Úprocess_responsed   s
   z!CommonMiddleware.process_responseN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r   r   r>   r   r   r   r   r
      s    r
   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚBrokenLinkEmailsMiddlewarec                 C   s�   |j dkrFtjsF| ¡ }| ¡ }|j dd¡}|  ||||¡sF|j dd¡}|j dd¡}td|  	||¡r7dnd|f d	||||f d
d� |S )z=Send broken link emails for relevant 404 NOT FOUND responses.r5   ÚHTTP_REFERERÚ r   z<none>ÚREMOTE_ADDRzBroken %slink on %sz	INTERNAL z=Referrer: %s
Requested URL: %s
User agent: %s
IP address: %s
T)Úfail_silently)
r6   r   r2   r   r   r   r   Úis_ignorable_requestr   Úis_internal_request)r   r   r=   Údomainr   ÚrefererÚuaÚipr   r   r   r>   w   s*   
ÿúÿ	
ÿôz+BrokenLinkEmailsMiddleware.process_responsec                 C   s   t t dt |¡ |¡ƒS )ze
        Return True if the referring URL is the same domain as the current
        request.
        z^https?://%s/)ÚboolÚrer)   Úescape)r   rJ   rK   r   r   r   rI   ‘   s   z.BrokenLinkEmailsMiddleware.is_internal_requestc                    s€   |sdS t jrˆ  d¡r|ˆ dd… krdS |  ||¡s"d|v r"dS t|ƒ}|jd|fv r4|jˆ kr4dS t‡ fdd„t jD ƒƒS )	z·
        Return True if the given request *shouldn't* notify the site managers
        according to project settings or in situations outlined by the inline
        comments.
        Tr!   Néÿÿÿÿú?rE   c                 3   s   � | ]}|  ˆ ¡V  qd S )N)r   )Ú.0Úpattern©Úurir   r   Ú	<genexpr>²   s   € zBBrokenLinkEmailsMiddleware.is_ignorable_request.<locals>.<genexpr>)	r   r$   r&   rI   r   Únetlocr   ÚanyÚIGNORABLE_404_URLS)r   r   rV   rJ   rK   Úparsed_refererr   rU   r   rH   ™   s    z/BrokenLinkEmailsMiddleware.is_ignorable_requestN)r?   r@   rA   r>   rI   rH   r   r   r   r   rC   v   s    rC   )rO   Úurllib.parser   Údjango.confr   Údjango.core.exceptionsr   Údjango.core.mailr   Údjango.httpr   Údjango.urlsr   Údjango.utils.deprecationr   Údjango.utils.httpr	   r
   rC   r   r   r   r   Ú<module>   s    i