o
    uvXj©  ã                   @   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¦   d|j v rtjD ]}| |j d ¡rtdƒ‚q| ¡ }tjo%|o%| d¡ }|r/d|j|f nd}|  	|¡r<|  
|¡}n| ¡ }|sH|| ¡ krQ||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_AGENTzForbidden user agentzwww.z%s://www.%sÚ N)ÚMETAr   ÚDISALLOWED_USER_AGENTSÚsearchr   Úget_hostÚPREPEND_WWWÚ
startswithÚschemeÚshould_redirect_with_slashÚget_full_path_with_slashÚget_full_pathÚresponse_redirect_class)ÚselfÚrequestÚuser_agent_regexÚhostÚmust_prependÚredirect_urlÚpath© r   úU/var/www/html/myproject/venv/lib/python3.10/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_SLASHÚ	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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   K   s   
þüÿ
z)CommonMiddleware.get_full_path_with_slashc                 C   sJ   |j dkr|  |¡r|  |  |¡¡S |js#| d¡s#t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   Ú	streamingÚ
has_headerÚstrÚlenÚcontent)r   r   Úresponser   r   r    Úprocess_responseb   s   

z!CommonMiddleware.process_responseN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r!   r   r   r9   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.r1   ÚHTTP_REFERERr   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)
r2   r   r.   r   r   r   ÚgetÚis_ignorable_requestr   Úis_internal_request)r   r   r8   Údomainr   ÚrefererÚuaÚipr   r   r    r9   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ÚreÚmatchÚescape)r   rE   rF   r   r   r    rD   Œ   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éÿÿÿÿú?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&   rD   r   Únetlocr   ÚanyÚIGNORABLE_404_URLS)r   r   rR   rE   rF   Úparsed_refererr   rQ   r    rC   ”   s    z/BrokenLinkEmailsMiddleware.is_ignorable_requestN)r:   r;   r<   r9   rD   rC   r   r   r   r    r>   u   s    r>   )rJ   Ú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    h