
    P}bg!                         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        o|o|                    d           }|r|j	        d|nd}| 
                    |          r|                     |          }n|                                }|s||                                k    r||z  }|                     |          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schemeshould_redirect_with_slashget_full_path_with_slashget_full_pathresponse_redirect_class)selfrequest
user_agentuser_agent_regexhostmust_prependredirect_urlpaths           k/home/e360mart.nyusoft.in/public_html/e360mart_env/lib/python3.11/site-packages/django/middleware/common.pyprocess_requestz CommonMiddleware.process_request"   s6    \%%&788
!$,$C C C #**:66 C*+ABBBC !!+TTT__V=T=T9TCOW>>UW **733 	+0099DD((**D  	>47#8#8#:#:::D L//=== ;:    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
        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   DEBUGr7   RuntimeErrorr   )r   r   new_paths      r$   r   z)CommonMiddleware.get_full_path_with_slashM   s     ((D(AA)(33> 
	gn0HHHM
 &n"++--8P P		 	 	 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   rF    r&   r$   r   r      sb         $ <> > >:    .    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_REFERERr   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   r9   r   r   r   r   is_ignorable_requestr   is_internal_request)r   r   rE   domainr#   refereruaips           r$   rF   z+BrokenLinkEmailsMiddleware.process_responsex   s   3&&x~&%%''F((**Dl&&~r::G,,WdFGLL \%%&7BB\%%mX>>(,(@(@(Q(QYWYYY 
 +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rer0   escape)r   rU   rV   s      r$   rT   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?r   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-   rT   r   netlocr#   anyIGNORABLE_404_URLS)r   r   rc   rU   rV   parsed_referers     `   r$   rS   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)rG   rH   rI   rF   rT   rS   rK   r&   r$   rM   rM   v   sI          *L L LS S S S Sr&   rM   )r[   urllib.parser   django.confr   django.core.exceptionsr   django.core.mailr   django.httpr   django.urlsr   django.utils.deprecationr	   django.utils.httpr
   r   rM   rK   r&   r$   <module>rq      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8S 8S 8S 8S 8S 8S 8S 8S 8S 8Sr&   