
    j                         S SK r S SKJr  S SKJr  S SKJr  S SKJrJ	r	J
r
  S SKJr  S SKJr  S SKJr  S S	KJr   " S
 S\5      r " S S\5      rg)    N)urlsplit)settings)PermissionDenied)MailerDoesNotExistmail_managersmailers)HttpResponsePermanentRedirect)is_valid_path)MiddlewareMixin)escape_leading_slashesc                   4    \ rS rSrSr\rS rS rS r	S r
Srg)	CommonMiddleware   a  
"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                    UR                   R                  S5      nUb8  [        R                   H$  nUR	                  U5      (       d  M  [        S5      e   UR                  5       n[        R                  (       ay  U(       aq  UR                  S5      (       dZ  U R                  U5      (       a  U R                  U5      nOUR                  5       nU R                  UR                   SU U 35      $ ggg)zj
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/html/KUNJ/venv/lib/python3.13/site-packages/django/middleware/common.pyprocess_request CommonMiddleware.process_request"   s     \\%%&78
!$,$C$C #**:66*+ABB %D
 !D1H1H
 ..w7744W=,,.//7>>2B'$PTv0VWW 2ID    c                 0   [         R                  (       a  UR                  R                  S5      (       da  [	        USS5      n[        UR                  U5      (       d9  [        SUR                  -  U5      nU(       a  UR                  n[	        USS5      $ g)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   +CommonMiddleware.should_redirect_with_slash>   sz    
   ):):)C)CC)H)Hgy$7G !2!2G<<%eg.?.?&?I ::D"4)>EEr'   c                     UR                  SS9n[        U5      n[        R                  (       a<  UR                  S;   a,  [        SUR                  UR                  5       U-   S.-  5      eU$ )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   DEBUGr:   RuntimeErrorr   )r   r   new_paths      r$   r   )CommonMiddleware.get_full_path_with_slashL   sr     ((D(A)(3>>gnn0RRM &nn"++-8
 
 r'   c                 6   UR                   S:X  a6  U R                  U5      (       a   U R                  U R                  U5      5      nUR                  (       dA  UR                  S5      (       d+  [        [        UR                  5      5      UR                  S'   U$ )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_response!CommonMiddleware.process_responsed   s     3&4+J+J7+S+S33--g6H !!(*=*=>N*O*O14S9I9I5J1KH-.r'    N)__name__
__module____qualname____firstlineno____doc__r	   r   r%   r   r   rJ   __static_attributes__rL   r'   r$   r   r      s$    $ <X80r'   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)BrokenLinkEmailsMiddlewarex   Nc                    UR                   S:X  a  [        R                  (       d  UR                  5       nUR	                  5       nUR
                  R                  SS5      nU R                  XX55      (       d{  UR
                  R                  SS5      nUR
                  R                  SS5      nU R                  SU R                  X55      (       a  SOS< S	U< 3S
U< SU< SU< SU< S3	5        U$ )z=Send broken link emails for relevant 404 NOT FOUND responses.rA   HTTP_REFERER r   z<none>REMOTE_ADDRzBroken z	INTERNAL zlink on z
Referrer: z
Requested URL: z
User agent: z
IP address: 
)
rB   r   r<   r   r   r   r   is_ignorable_request	send_mailis_internal_request)r   r   rI   domainr#   refereruaips           r$   rJ   +BrokenLinkEmailsMiddleware.process_response|   s    3&x~~%%'F((*Dll&&~r:G,,WFLL\\%%&7B\\%%mX>
  $77HH (!#$  +24RA r'   c                     [         R                  (       d  [        X/UQ7SS0UD6  g  [        X/UQ7SU R                  0UD6  g ! [         a     g f = f)Nfail_silentlyTusing)r   _is_configuredr   re   r   )r   subjectmessageargskwargss        r$   r\   $BrokenLinkEmailsMiddleware.send_mail   sW    %%'PTPPP	'NTNNvN! 		s   A 
AAc                 p    [        [        R                  " S[        R                  " U5      -  U5      5      $ )zM
Return True if the referring URL is the same domain as the current
request.
z^https?://%s/)boolrer1   escape)r   r^   r_   s      r$   r]   .BrokenLinkEmailsMiddleware.is_internal_request   s(     BHH_ryy/@@'JKKr'   c                 Z  ^ U(       d  g[         R                  (       a   TR                  S5      (       a
  UTSS :X  a  gU R                  X45      (       d  SU;   a  g[	        U5      nUR
                  SU4;   a  UR                  T:X  a  g[        U4S j[         R                   5       5      $ )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?rX   c              3   D   >#    U  H  oR                  T5      v   M     g 7f)N)r   ).0patternuris     r$   	<genexpr>BBrokenLinkEmailsMiddleware.is_ignorable_request.<locals>.<genexpr>   s     R6Q7>>#&&6Qs    )	r   r,   r.   r]   r   netlocr#   anyIGNORABLE_404_URLS)r   r   rw   r^   r_   parsed_referers     `   r$   r[   /BrokenLinkEmailsMiddleware.is_ignorable_request   s        S\\#%6%67c#2h;N ''88SG^ "'*  RL0^5H5HC5ORh6Q6QRRRr'   rL   )
rM   rN   rO   rP   re   rJ   r\   r]   r[   rR   rL   r'   r$   rT   rT   x   s    E2	LSr'   rT   )rn   urllib.parser   django.confr   django.core.exceptionsr   django.core.mailr   r   r   django.httpr	   django.urlsr
   django.utils.deprecationr   django.utils.httpr   r   rT   rL   r'   r$   <module>r      sB    	 !   3 G G 5 % 4 4h hVIS ISr'   