
    g                         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e      Z G d de      Zy)    N)urlparse)settings)PermissionDenied)mail_managers)HttpResponsePermanentRedirect)is_valid_path)MiddlewareMixin)escape_leading_slashesc                   ,    e Zd ZdZeZd Zd Zd Zd Z	y)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                    |j                   j                  d      }|1t        j                  D ]  }|j	                  |      st        d       |j                         }t        j                  rj|rg|j                  d      sU| j                  |      r| j                  |      }n|j                         }| j                  |j                   d| |       S yyy)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paths         p/var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/middleware/common.pyprocess_requestz CommonMiddleware.process_request"   s     \\%%&78
!$,$C$C #**:6*+ABB %D
 !D1H
 ..w744W=,,.//7>>2B'$PTv0VWW 2ID    c                    t         j                  rr|j                  j                  d      sWt	        |dd      }t        |j                  |      s4t        d|j                  z  |      }|r|j                  }t	        |dd      S y)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views        r!   r   z+CommonMiddleware.should_redirect_with_slash>   st    
   ):):)C)CC)Hgy$7G !2!2G<%eg.?.?&?I ::D"4)>EEr#   c                     |j                  d      }t        |      }t        j                  r:|j                  dv r,t        d|j                  |j                         |z   dz        |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   DEBUGr5   RuntimeErrorr   )r   r   new_paths      r!   r   z)CommonMiddleware.get_full_path_with_slashL   sr     ((D(A)(3>>gnn0RRM &nn"++-8
 
 r#   c                    |j                   dk(  r1| j                  |      r | j                  | j                  |            S |j                  s<|j                  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   responses      r!   process_responsez!CommonMiddleware.process_responsed   sz     3&4+J+J7+S//0M0Mg0VWW !!(*=*=>N*O14S9I9I5J1KH-.r#   N)
__name__
__module____qualname____doc__r   r   r"   r   r   rD    r#   r!   r   r      s$    $ <X80r#   r   c                       e Zd Zd Zd Zd Zy)BrokenLinkEmailsMiddlewarec                    |j                   dk(  rt        j                  s|j                         }|j	                         }|j
                  j                  dd      }| j                  ||||      sm|j
                  j                  dd      }|j
                  j                  dd      }t        d| j                  ||      rdndd	|d
|d|d|d|d	d       |S )z=Send broken link emails for relevant 404 NOT FOUND responses.r;   HTTP_REFERER r   z<none>REMOTE_ADDRzBroken z	INTERNAL zlink on z
Referrer: z
Requested URL: z
User agent: z
IP address: 
T)fail_silently)
r<   r   r7   r   r   r   r   is_ignorable_requestr   is_internal_request)r   r   rC   domainr    refereruaips           r!   rD   z+BrokenLinkEmailsMiddleware.process_responsew   s    3&x~~%%'F((*Dll&&~r:G,,WdFGL\\%%&7B\\%%mX>
  $77H (!#$  +24RA"& r#   c                 l    t        t        j                  dt        j                  |      z  |            S )ze
        Return True if the referring URL is the same domain as the current
        request.
        z^https?://%s/)boolrer-   escape)r   rT   rU   s      r!   rS   z.BrokenLinkEmailsMiddleware.is_internal_request   s(     BHH_ryy/@@'JKKr#   c                 &   |syt         j                  rj                  d      r	|dd k(  ry| j                  ||      sd|v ryt	        |      }|j
                  d|fv r|j                  k(  ryt        f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?rN   c              3   @   K   | ]  }|j                          y w)N)r   ).0patternuris     r!   	<genexpr>zBBrokenLinkEmailsMiddleware.is_ignorable_request.<locals>.<genexpr>   s     R6Q77>>#&6Qs   )	r   r(   r*   rS   r   netlocr    anyIGNORABLE_404_URLS)r   r   rb   rT   rU   parsed_referers     `   r!   rR   z/BrokenLinkEmailsMiddleware.is_ignorable_request   s        S\\#%67c#2h;N ''8SG^ "'*  RL0^5H5HC5ORh6Q6QRRRr#   N)rE   rF   rG   rD   rS   rR   rI   r#   r!   rK   rK   v   s    4LSr#   rK   )rZ   urllib.parser   django.confr   django.core.exceptionsr   django.core.mailr   django.httpr   django.urlsr   django.utils.deprecationr	   django.utils.httpr
   r   rK   rI   r#   r!   <module>rp      s=    	 !   3 * 5 % 4 4f fR<S <Sr#   