U
    µ©ÞaÂ  ã                   @   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k	r2tjD ]}| |¡rtdƒ‚q| ¡ }tjoN|oN| d¡ }|rbd|j	|f nd}|  
|¡r||  |¡}n| ¡ }|s”|| ¡ kr¦||7 }|  |¡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%s://www.%sÚ )ÚMETAÚgetr   ZDISALLOWED_USER_AGENTSÚsearchr   Úget_hostZPREPEND_WWWÚ
startswithÚschemeÚshould_redirect_with_slashÚget_full_path_with_slashÚget_full_pathÚresponse_redirect_class)ÚselfÚrequestÚ
user_agentZuser_agent_regexÚhostZmust_prependZredirect_urlÚpath© r   ú‡/home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/django/middleware/common.pyÚprocess_request"   s    



z CommonMiddleware.process_requestc                 C   s@   t jr<|j d¡s<t|ddƒ}t|j|ƒ o:td|j |ƒ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/F)r   ÚAPPEND_SLASHZ	path_infoÚendswithÚgetattrr   )r   r   r    r   r   r   r   ?   s    þz+CommonMiddleware.should_redirect_with_slashc                 C   sD   |j dd}t|ƒ}tjr@|jdk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)Z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   sJ   |j dkr$|  |¡r$|  |  |¡¡S |jsF| d¡sFtt|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   Z	streamingÚ
has_headerÚstrÚlenÚcontent)r   r   Úresponser   r   r   Úprocess_responsec   s    

z!CommonMiddleware.process_responseN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r3   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.r,   ZHTTP_REFERERr   r   z<none>ZREMOTE_ADDRzBroken %slink on %sz	INTERNAL z=Referrer: %s
Requested URL: %s
User agent: %s
IP address: %s
T)Zfail_silently)
r-   r   r)   r   r   r   r   Úis_ignorable_requestr   Úis_internal_request)r   r   r2   Údomainr   ÚrefererZuaÚipr   r   r   r3   x   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ÚreÚmatchÚescape)r   r;   r<   r   r   r   r:      s    z.BrokenLinkEmailsMiddleware.is_internal_requestc                    s€   |sdS t jr,ˆ  d¡r,|ˆ dd… kr,dS |  ||¡sDd|krDdS t|ƒ}|jd|fk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éÿÿÿÿú?r   c                 3   s   | ]}|  ˆ ¡V  qd S )N)r   )Ú.0Úpattern©Úurir   r   Ú	<genexpr>®   s     zBBrokenLinkEmailsMiddleware.is_ignorable_request.<locals>.<genexpr>)	r   r!   r"   r:   r   Únetlocr   ÚanyZIGNORABLE_404_URLS)r   r   rG   r;   r<   Zparsed_refererr   rF   r   r9   •   s     z/BrokenLinkEmailsMiddleware.is_ignorable_requestN)r4   r5   r6   r3   r:   r9   r   r   r   r   r8   v   s   r8   )r?   Úurllib.parser   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.mailr   Zdjango.httpr   Zdjango.urlsr   Zdjango.utils.deprecationr   Zdjango.utils.httpr	   r
   r8   r   r   r   r   Ú<module>   s   i