
    Evg                         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dS )    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	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                    |j                             d          }|3t          j        D ]&}|                    |          rt          d          '|                                }t          j        rw|rw|                    d          sd| 	                    |          r| 
                    |          }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paths         U/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/middleware/common.pyprocess_requestz CommonMiddleware.process_request"   s(    \%%&788
!$,$C C C #**:66 C*+ABBBC !! 
	XD 
	X1H1H 
	X
 ..w77 /44W==,,..//7>0V0V$0VPT0V0VWWW
	X 
	X 
	X 
	X 
	X 
	X    c                    t           j        rr|j                            d          sXt	          |dd          }t          |j        |          s2t          d|j        z  |          }|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views        r!   r   z+CommonMiddleware.should_redirect_with_slash>   s    
   	F):)C)CC)H)H 	Fgy$77G !2G<< F%eg.?&?II F :D"4)>EEEur#   c                     |                     d          }t          |          }t          j        r8|j        dv r/t          d|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   s     ((D(AA)(33> 	gn0RRRM &n"++--8 
 
 
 r#   c                     |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   responses      r!   process_responsez!CommonMiddleware.process_responsed   s     3&&4+J+J7+S+S&//0M0Mg0V0VWWW ! 	L(*=*=>N*O*O 	L14S9I5J5J1K1KH-.r#   N)
__name__
__module____qualname____doc__r   r   r"   r   r   rD    r#   r!   r   r      se         $ <X X X8    0    r#   r   c                        e Zd Zd Zd Zd ZdS )BrokenLinkEmailsMiddlewarec                    |j         dk    rt          j        s|                                }|                                }|j                            dd          }|                     ||||          ss|j                            dd          }|j                            dd          }t          d| 	                    ||          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l&&~r::G,,WdFGLL \%%&7BB\%%mX>>
  $77HH$KK!#$ $   +2''444RRRA"&    r#   c                 p    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-     BH_ry/@/@@'JJKKKr#   c                 >   |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 r|j        k    rdS 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   B   K   | ]}|                               V  d S )N)r   ).0patternuris     r!   	<genexpr>zBBrokenLinkEmailsMiddleware.is_ignorable_request.<locals>.<genexpr>   s/      RR77>>#&&RRRRRRr#   )	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      	4   	S\\#%6%6 	7c#2#h;N;N4 ''88 	SG^^4 "'** RL00^5HC5O5O4RRRRh6QRRRRRRr#   N)rE   rF   rG   rD   rS   rR   rI   r#   r!   rK   rK   v   sI          4L L LS S S S 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 3 3 3 3 3 * * * * * * 5 5 5 5 5 5 % % % % % % 4 4 4 4 4 4 4 4 4 4 4 4f f f f f f f fR<S <S <S <S <S <S <S <S <S <Sr#   