a
    ¥Q•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)Úurlparse)Ú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r2tjD ]}| |¡rtdƒ‚q| ¡ }tjr†|r†| d¡s†|  	|¡rd|  
|¡}n| ¡ }|  |j› 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ÚrequestZ
user_agentZuser_agent_regexÚhostÚpath© r   úQ/var/www/sistema_ama/venv/lib/python3.9/site-packages/django/middleware/common.pyÚprocess_request"   s    



z CommonMiddleware.process_requestc                 C   sT   t jrP|j d¡sPt|ddƒ}t|j|ƒsPtd|j |ƒ}|rP|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
        POST, PUT, or PATCH.
        T)Úforce_append_slash)Ú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   ÚDEBUGr-   Ú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sH| d¡sHt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rŒtjsŒ| ¡ }| ¡ }|j dd¡}|  ||||¡sŒ|j dd¡}|j dd¡}td|  	||¡rndnd|f d	||||f d
d� |S )z=Send broken link emails for relevant 404 NOT FOUND responses.r2   Ú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)
r3   r   r/   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   rG   rH   r   r   r   rF   ‘   s    z.BrokenLinkEmailsMiddleware.is_internal_requestc                    s€   |sdS t jr,ˆ  d¡r,|ˆ dd… kr,dS |  ||¡sDd|v rDdS t|ƒ}|jd|fv rh|jˆ krhd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éÿÿÿÿú?rB   c                 3   s   | ]}|  ˆ ¡V  qd S )N)r   )Ú.0Úpattern©Úurir   r   Ú	<genexpr>²   ó    zBBrokenLinkEmailsMiddleware.is_ignorable_request.<locals>.<genexpr>)	r   r"   r$   rF   r   Únetlocr   ÚanyÚIGNORABLE_404_URLS)r   r   rS   rG   rH   Zparsed_refererr   rR   r   rE   ™   s     z/BrokenLinkEmailsMiddleware.is_ignorable_requestN)r<   r=   r>   r;   rF   rE   r   r   r   r   r@   v   s   r@   )rL   Úurllib.parser   Údjango.confr   Údjango.core.exceptionsr   Údjango.core.mailr   Údjango.httpr   Údjango.urlsr   Údjango.utils.deprecationr   Údjango.utils.httpr	   r
   r@   r   r   r   r   Ú<module>   s   i