
    Hwga                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZmZmZ d dlmZmZ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 d dl m!Z!m"Z"  edddi      Z#d Z$ G d de%      Z& G d d      Z'd"dZ(e jR                  d        Z*d Z+d Z,d Z- G d d      Z. G d d      Z/d  Z0d! Z1y)#    N)Path)settings)Http404HttpResponseHttpResponseNotFound)ContextEngineTemplateDoesNotExist)pprint)resolve)timezone)MultiValueDict)	force_str)import_string)_lazy_re_compile)PY311get_docs_versionTi18nzdjango.templatetags.i18n)debug	librariesc                 @    t        t              j                  dz  | z  S )z
    Return a path to a builtin template.

    Avoid calling this function at the module level or in a class-definition
    because __file__ may not exist, e.g. in frozen environments.
    	templates)r   __file__parent)names    I/var/www/horilla/myenv/lib/python3.12/site-packages/django/views/debug.pybuiltin_template_pathr      s     >  ;.55    c                       e Zd Zy)ExceptionCycleWarningN)__name__
__module____qualname__ r   r   r    r    )   s    r   r    c                       e Zd ZdZd Zd Zy)CallableSettingWrapperz
    Object to wrap callable appearing in settings.
    * Not to call in the debug page (#21345).
    * Not to break the debug page if the callable forbidding to set attributes
      (#23070).
    c                     || _         y N)_wrapped)selfcallable_settings     r   __init__zCallableSettingWrapper.__init__5   s	    (r   c                 ,    t        | j                        S r(   )reprr)   r*   s    r   __repr__zCallableSettingWrapper.__repr__8   s    DMM""r   N)r!   r"   r#   __doc__r,   r0   r$   r   r   r&   r&   -   s    )#r   r&   c                      t        |       | |||      }| j                  d      r|j                         }t        ||      S |j	                         }t        ||d      S )z
    Create a technical server error response. The last three arguments are
    the values returned from sys.exc_info() and friends.
    z	text/html)statusztext/plain; charset=utf-8)r3   content_type)get_exception_reporter_classacceptsget_traceback_htmlr   get_traceback_text)requestexc_type	exc_valuetbstatus_codereporterhtmltexts           r   technical_500_responserA   <   sg    
 5+G4Wh	SUVH{#**,D55**,3N
 	
r   c                  >     t        t        j                               S r(   )r   r   !DEFAULT_EXCEPTION_REPORTER_FILTERr$   r   r   %get_default_exception_reporter_filterrD   L   s     E=CCDFFr   c                 0    t               }t        | d|      S )Nexception_reporter_filter)rD   getattr)r9   default_filters     r   get_exception_reporter_filterrI   R   s    :<N77HHr   c                 N    t        t        j                        }t        | d|      S )Nexception_reporter_class)r   r   DEFAULT_EXCEPTION_REPORTERrG   )r9    default_exception_reporter_classs     r   r5   r5   W   s.    '4++($ +-M r   c                     | j                   }|	 t        | j                        }|dS |j                  S # t        $ r Y w xY w)N )resolver_matchr   pathr   
_func_path)r9   rP   s     r   
get_callerrS   `   sR    ++N	$W\\2N  '2F^-F-FF  		s   5 	A Ac                   r    e Zd ZdZdZ edej                        Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd Zd Zy)SafeExceptionReporterFilterz
    Use annotations made by the sensitive_post_parameters and
    sensitive_variables decorators to filter out sensitive information.
    z********************z/API|TOKEN|KEY|SECRET|PASS|SIGNATURE|HTTP_COOKIE)flagsc           	      V   |t         j                  k(  rd}n	 | j                  j                  |      }|r| j
                  }nt        |t              r4|j                         D ci c]  \  }}|| j                  ||       }}}nlt        |t              r |D cg c]  }| j                  d|       }}n<t        |t              r*t        |D cg c]  }| j                  d|       c}      }n|}t        |      rt        |      }|S # t        $ r d}Y w xY wc c}}w c c}w c c}w )z
        Cleanse an individual setting key/value of sensitive content. If the
        value is a dictionary, recursively cleanse the keys in that dictionary.
        TFrO   )r   SESSION_COOKIE_NAMEhidden_settingssearch	TypeErrorcleansed_substitute
isinstancedictitemscleanse_settinglisttuplecallabler&   )r*   keyvalueis_sensitivecleansedkvs          r   r`   z+SafeExceptionReporterFilter.cleanse_settingu   s   
 (...L%#33::3? //Ht$BG++-P$!Q4//155PHPt$=BC,,R3CHCu%5Iad222q9IJHHH-h7H#  %$% QCIs#   D
 %DD!D&
DDc                     i }t        t              D ]6  }|j                         s| j                  |t	        t        |            ||<   8 |S )z
        Return a dictionary of the settings module with values of sensitive
        settings replaced with stars (*********).
        )dirr   isupperr`   rG   )r*   settings_dictrh   s      r   get_safe_settingsz-SafeExceptionReporterFilter.get_safe_settings   sM    
 X 	QAyy{#'#7#778Q;O#Pa 	Q r   c           	          t        |d      si S |j                  j                         D ci c]  \  }}|| j                  ||       c}}S c c}}w )zU
        Return a dictionary of request.META with sensitive values redacted.
        META)hasattrrp   r_   r`   r*   r9   rh   ri   s       r   get_safe_request_metaz1SafeExceptionReporterFilter.get_safe_request_meta   sJ     w'I:A,,:L:L:NO$!Q4''1--OOO   Ac           	          t        |d      si S |j                  j                         D ci c]  \  }}|| j                  ||       c}}S c c}}w )zX
        Return a dictionary of request.COOKIES with sensitive values redacted.
        COOKIES)rq   rv   r_   r`   rr   s       r   get_safe_cookiesz,SafeExceptionReporterFilter.get_safe_cookies   sJ     w	*I:A//:O:O:QR$!Q4''1--RRRrt   c                 &    t         j                  du S )a  
        This filter is to add safety in production environments (i.e. DEBUG
        is False). If DEBUG is True then your site is not safe anyway.
        This hook is provided as a convenience to easily activate or
        deactivate the filter on a per request basis.
        F)r   DEBUG)r*   r9   s     r   	is_activez%SafeExceptionReporterFilter.is_active   s     ~~&&r   c                     t        |dg       }| j                  |      r-|r+|j                         }|D ]  }||v s| j                  ||<    |S )z
        Replace the keys in a MultiValueDict marked as sensitive with stars.
        This mitigates leaking sensitive POST parameters if something like
        request.POST['nonexistent_key'] throws an exception (#21098).
        sensitive_post_parameters)rG   rz   copyr\   )r*   r9   multivaluedictr|   params        r   get_cleansed_multivaluedictz7SafeExceptionReporterFilter.get_cleansed_multivaluedict   sc     %,G5PRT$U!>>'"'@+002N2 EN*,0,D,DN5)E r   c                 
   |i S t        |dg       }| j                  |      rV|rT|j                  j                         }|dk(  r|D ]  }| j                  ||<    |S |D ]  }||v s| j                  ||<    |S |j                  S )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        r|   __ALL__)rG   rz   POSTr}   r\   )r*   r9   r|   rg   rh   r   s         r   get_post_parametersz/SafeExceptionReporterFilter.get_post_parameters   s    
 ?I(/4b)% ~~g&+D"<<,,.,	9% ?&*&>&>?#O "; G H,.2.F.FHUOG $O||#r   c                     	 t        |t              }|r| j	                  ||      }|S # t        $ r}dj                  ||      cY d }~S d }~ww xY w)Nz{!r} while evaluating {!r})r]   r   	Exceptionformatr   )r*   r9   re   is_multivalue_dictes        r   cleanse_special_typesz1SafeExceptionReporterFilter.cleanse_special_types   sZ    	A ",E>!B 44WeDE  	A/66q%@@	As   ( 	AAAAc                    |j                   }d}|S|j                  j                  dk(  r+d|j                  v r|j                  d   }t	        |dd      }n|j                   }|Si }| j                  |      rs|rq|dk(  r!|j                  D ]  }| j                  ||<    n|j                  j                         D ]-  \  }}||v r| j                  }n| j                  ||      }|||<   / n7|j                  j                         D ]  \  }}| j                  ||      ||<    |j                  j                  dk(  r,d|j                  v r| j                  |d<   | j                  |d<   |j                         S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        Nsensitive_variables_wrappersensitive_variablesr   	func_argsfunc_kwargs)	f_backf_codeco_namef_localsrG   rz   r\   r_   r   )	r*   r9   tb_framecurrent_framer   wrapperrg   r   re   s	            r   get_traceback_frame_variablesz9SafeExceptionReporterFilter.get_traceback_frame_variables   s    !"'$$,,0MM1]5K5KK (001NO&-g7Ld&S#)00M ' >>'"':"i/$-- >D%)%=%=HTN> $,#4#4#:#:#< +KD%22 $ 8 8 $ : :7E J%*HTN+  (00668 Le!%!;!;GU!KL OO##'DD-1B1BB %)$<$<H[!&*&>&>H]#~~r   N)r!   r"   r#   r1   r\   r   reIrY   r`   rn   rs   rw   rz   r   r   r   r   r$   r   r   rU   rU   j   sQ    
 1&9O:	PS'$64 r   rU   c                   r    e Zd ZdZed        Zed        ZddZd Zd Z	d Z
d Zd	 Z	 ddZd Zd Zd Zy
)ExceptionReporterz0Organize and coordinate reporting on exceptions.c                     t        d      S )Nztechnical_500.htmlr   r/   s    r   html_template_pathz$ExceptionReporter.html_template_path'  s    $%9::r   c                     t        d      S )Nztechnical_500.txtr   r/   s    r   text_template_pathz$ExceptionReporter.text_template_path+  s    $%899r   c                     || _         t        | j                         | _        || _        || _        || _        || _        t        | j                  dd       | _        d| _	        d | _
        y )Ntemplate_debugF)r9   rI   filterr:   r;   r<   is_emailrG   template_infotemplate_does_not_exist
postmortem)r*   r9   r:   r;   r<   r   s         r   r,   zExceptionReporter.__init__/  sZ    3DLLA " $T^^5EtL',$r   c                     dj                  | j                  j                  | j                  j                         | j                  j	                               S )z
        Return an absolute URI from variables available in this request. Skip
        allowed hosts protection, so may return insecure URI.
        z{scheme}://{host}{path})schemehostrQ   )r   r9   r   _get_raw_hostget_full_pathr/   s    r   _get_raw_insecure_uriz'ExceptionReporter._get_raw_insecure_uri;  sI    
 )//<<&&++-++- 0 
 	
r   c           
      F   | j                   rKt        | j                   t              r1d| _        | j                  j
                  xs | j                  g| _        | j                         }t        |      D ]a  \  }}d|v rSg }|d   D ]D  \  }}t        |      }t        |      dkD  rd|dd t        |      fz  }|j                  ||f       F ||d<   |||<   c d}| j                   rt        | j                   t              rt        | j                  dd      }t        | j                  d	d      }	|O|	M| j                  j                  d
   }
t        |
t!        |dz
  d      t#        |	dz   t        |
             dd      }ddlm} | j(                  d}n 	 t+        | j(                  j,                        }i d| j0                  d|d|d| j(                  d| j2                  j5                  | j(                        d| j2                  j7                  | j(                        j9                         d|dt;        | j2                  j=                  | j(                        j9                               d| j2                  j?                         dt@        jB                  ddt@        jD                  dd z  dtG        jH                         d |       d t@        jJ                  d!| jL                  d"| j                  d#| j                  }| j(                  y| j(                  jN                  j9                         |d$<   | j(                  jP                  j9                         |d%<   | jS                         |d&<   tU        | j(                        |d'<   | j                   r| j                   jV                  |d(<   | j                  rHt+        | j                        |d)<   t        | j                  d*d      x}rd+d+jY                  |      z   |d,<   |r|d-   |d.<   |S # t.        $ r d}Y lw xY w)/z5Return a dictionary containing traceback information.Tvarsi   u   %s… <trimmed %d bytes string>r   rO   startNend      asciireplace)errors)get_versionz%[unable to retrieve the current user]r   unicode_hintframesr9   request_metarequest_COOKIES_itemsuser_strfiltered_POST_itemsr   sys_executablesys_version_infoz%d.%d.%d   server_timedjango_version_infosys_pathr   r   r   request_GET_itemsrequest_FILES_itemsrequest_insecure_uriraising_view_nameexception_typeexception_value	__notes__
exception_notes	lastframe)-r:   
issubclassr
   r   r;   chainr   get_traceback_frames	enumerater   lenappendUnicodeErrorrG   argsr   maxmindjangor   r9   struserr   r   r   rs   rw   r_   ra   r   rn   sys
executableversion_infor   nowrQ   r   GETFILESr   rS   r!   join)r*   r   iframe
frame_varsrh   ri   r   r   r   unicode_strr   r   c	exc_notess                  r   get_traceback_dataz$ExceptionReporter.get_traceback_dataF  s   ==Z7KL+/D("nn22Ft~~6FDO**,!&) 
	HAu
!&M .DAqq	A1v}=1TCPQF@SS%%q!f-. !+fF1I
	 ==Z|DDNNGT:E$..%6C S_"nn11!4(EAIq 1Ca[AQ4RS$ 
 	'<<HCt||001

L
 f
 t||	

 DKK==dllK
 $T[[%A%A$,,%O%U%U%W
 
 "4//=CCE$
 557
 cnn
 
S-=-=a-B B
 8<<>
 ";=
  !
" T//#
$ &t'C'C%
& $//'
* <<#%)\\%5%5%;%;%=A!"'+||'9'9'?'?'AA#$(,(B(B(DA$%%/%=A!" =="&--"8"8A>>#&t~~#6A #DNNKFFyF'+dii	.B'B#$#BZAkNS  C CCs   P P P c                    | j                   j                  d      5 }t        j                  |j	                               }ddd       t        | j                         d      }j                  |      S # 1 sw Y   5xY w)z1Return HTML version of debug 500 HTTP error page.utf-8encodingNF)use_l10n)r   openDEBUG_ENGINEfrom_stringreadr   r   renderr*   fhtr   s       r   r7   z$ExceptionReporter.get_traceback_html  si    $$))7); 	4r((3A	4D++->xx{	4 	4s   $A55A>c                    | j                   j                  d      5 }t        j                  |j	                               }ddd       t        | j                         dd      }j                  |      S # 1 sw Y   6xY w)z7Return plain text version of debug 500 HTTP error page.r   r   NF)
autoescaper   )r   r   r   r   r   r   r   r   r   s       r   r8   z$ExceptionReporter.get_traceback_text  sk    $$))7); 	4r((3A	4D++-%%Pxx{	4 	4s   $A66A?c                 2   d }t        |d      r$	 |j                  |      }||j                         }|6	 t	        |d      5 }|j                         j                         }d d d        |S |S # t        $ r Y Ww xY w# 1 sw Y   |S xY w# t        $ r Y |S w xY w)N
get_sourcerb)rq   r   ImportError
splitlinesr   r   OSError)r*   filenameloadermodule_namesourcefps         r   _get_sourcezExceptionReporter._get_source  s    6<(**;7 !**,>(D) 4RWWY113F4 v  4   s@   A- B	 A<!B	 -	A98A9<BB	 B	 		BBNc                    | j                  |||      }|dg dg fS t        |d   t              rTd}|dd D ]0  }t        j                  d|      }	|	s|	d   j                  d      } n |D 
cg c]  }
t        |
|d       }}
t        d||z
        }||z   }	 ||| }||   }||dz   | }||||fS c c}
w # t        $ r	 dg dg fcY S w xY w)z
        Return context_lines before and after lineno from file.
        Return (pre_context_lineno, pre_context, context_line, post_context).
        Nr   r      s   coding[:=]\s*([-\w.]+)r   r   )	r  r]   bytesr   rZ   decoder   r   
IndexError)r*   r   linenocontext_linesr   r  r  r   linematchslinelower_boundupper_boundpre_contextcontext_linepost_contexts                   r   _get_lines_from_filez&ExceptionReporter._get_lines_from_file  s    !!(FK@>T2%%
 fQi'Hr
  		"<dC$Qxw7H DJJ%c%95JFJ!Vm34},	& V4K!&>L!&1*{;L K|CC K  	&T2%%	&s   -B0B5 5CCc                 d    t        |dd       }t        |dd       }t        |dd       }|xs |rd S |S )N	__cause____suppress_context____context__)rG   )r*   r;   explicitsuppress_contextimplicits        r   _get_explicit_or_implicit_causez1ExceptionReporter._get_explicit_or_implicit_cause  sA    9k48"9.DdK9mT:C$4DC(Cr   c                    g }| j                   }|rG|j                  |       | j                  |      }||v rt        j                  d|z  t
               n|rGg }|s|S |j                         }|s| j                  n|j                  }	 |j                  | j                  ||             	 |j                         }|j                  }?# t        $ r Y |S w xY w)NzHCycle in the exception chain detected: exception '%s' encountered again.)r;   r   r  warningswarnr    popr<   __traceback__extendget_exception_traceback_framesr	  )r*   
exceptionsr;   r   r<   s        r   r   z&ExceptionReporter.get_traceback_frames  s    
NN	i(<<YGIJ&)+45)   M NN$	&TWWI,C,CMM$==iLM&NN,	 ((B   s   +C 	CCc              #     K   | j                  |      }t        |dd      }|	||d dd |.|j                  j                  j	                  d      r|j
                  }5|j                  j                  j                  }|j                  j                  j                  }|j                  dz
  }|j                  j                  j	                  d      }|j                  j                  j	                  d      xs d	}	| j                  ||d
||	      \  }
}}}|
|}
g }d}g }d	x}}t        rt        t        j                  |j                  j                  j!                         |j"                  dz  d             \  }}}}|rf|rdd||z
  z  }d|t%        t'        |dz               z   dz   z  }d| | }dd|z   t%        |      t%        |j)                               z
  z
  z  }d| | }||||	j+                  d      rdnd|||dz   | j,                  j/                  | j0                  |j                        t3        |      ||||
dz   ||d |j
                  }|-y y w)Nr  Tr   )	exc_causeexc_cause_explicitr<   type__traceback_hide__r   
__loader__r!   rO      z<source code not available>r  ^ r      zdjango.r   )r&  r'  r<   r(  r   functionr
  r   idr  r  r  pre_context_linenocolnotb_area_colno)r  rG   r   r   gettb_nextr   co_filenamer   	tb_lineno	f_globalsr  r   next	itertoolsisliceco_positionstb_lastir   r   lstrip
startswithr   r   r9   r0  )r*   r;   r<   r&  r'  r   r/  r
  r   r  r1  r  r  r  r2  r3  _start_column
end_column	underlinespacestb_area_spacess                         r   r#  z0ExceptionReporter.get_exception_traceback_frames   s    88C	$YTB:&&8	  n {{##''(<=ZZ{{))55H{{))11H\\A%F[[**..|<F++//33J?E2K ))" ")%+" <!$&&EM15$$**7792;;!;KT2.1lJ
  J #zL'@ AI L3s6A:3G$G!$KLF 4E%(&'|,s<3F3F3H/IIK&N
 ')(8$DM&&8$/$:$:9$E6$$ 1*AALL"++ f* , ,&81&<!.# & BC ns   III)F)NN)r!   r"   r#   r1   propertyr   r   r,   r   r   r7   r8   r  r  r  r   r#  r$   r   r   r   r   $  sk    :; ; : :
	
N`$ IM"DHDBKr   r   c                    	 |j                   d   d   }	 |j                   d   d   }d}|rh| j
                  dk(  rdt        |      dk(  rVt        |d         dk(  rEt        |d   d   dd	      t        |d   d   d
d	      cxk(  rdk(  rt        |       S  nt        |       S t        | dt        j                        }t        |t        j                        r|j                   }t#        d      j%                  d      5 }t&        j)                  |j+                               }ddd       t-               }t/        |t        j                  |||t1        |      | |j3                         t5        |       d	      }	t7        j9                  |	            S # t        t        t        f$ r | j                  dd }Y w xY w# t        t        t        f$ r* d}| j                  r| j                  j                  nd}Y Vw xY w# 1 sw Y   xY w)zBCreate a technical 404 error response. `exception` is the Http404.r   rQ   r   NtriedF/app_namerO   	namespaceadminTurlconfztechnical_404.htmlr   r   )	rM  root_urlconfrequest_pathurlpatternsresolvedreasonr9   r   r   )r   r	  r[   KeyError	path_inforQ   r   rG   default_urlconfrP   rH  r   ROOT_URLCONFr]   types
ModuleTyper!   r   r   r   r   r   rD   r   r   rn   rS   r   r   )
r9   	exception	error_urlrH  rQ  rM  r   r   reporter_filterr   s
             r   technical_404_responser\  N  s   *NN1%f-	,q!'*
 LLCE
aE!H"aZ4uQx{K4 #7++	 #7++gy(*?*?@G'5++,""	3	4	9	97	9	K 0r$$RWWY/0;=O$11%  )n'99;!+G!4
	
	A  ,,Q 	8, *%%ab)	*
 	8, Q070F0F&&,,DQ&0 0s.   F F7 :$G8#F43F47:G54G58Hc                    t        d      j                  d      5 }t        j                  |j	                               }ddd       t        dt               i      }t        j                  |            S # 1 sw Y   8xY w)z+Create an empty URLconf 404 error response.zdefault_urlconf.htmlr   r   Nversion)	r   r   r   r   r   r   r   r   r   )r9   r   r   r   s       r   rU  rU  }  st    	5	6	;	;W	;	M 0QS$$RWWY/0')	
	A $$0 0s   $A77B )i  )2	functoolsr:  r   r   rW  r  pathlibr   django.confr   django.httpr   r   r   django.templater   r	   r
   django.template.defaultfiltersr   django.urlsr   django.utilsr   django.utils.datastructuresr   django.utils.encodingr   django.utils.module_loadingr   django.utils.regex_helperr   django.utils.versionr   r   r   r   UserWarningr    r&   rA   	lru_cacherD   rI   r5   rS   rU   r   r\  rU  r$   r   r   <module>rn     s      	 
      C C A A 1  ! 6 + 5 6 8 
126	K 	# #
  G G
I
Gw  w tg gT	,-^
%r   