3
W­E^P  ã               @   sb  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 edddid�Zejdej d�Z!dZ"ee#ƒj$Z%G dd„ dƒZ&dd„ Z'dd„ Z(d+dd„Z)e j*ƒ dd„ ƒZ+dd „ Z,G d!d"„ d"ƒZ-G d#d$„ d$e-ƒZ.G d%d&„ d&ƒZ/d'd(„ Z0d)d*„ Z1dS ),é    N)ÚPath)Úsettings)ÚHttp404ÚHttpResponseÚHttpResponseNotFound)ÚContextÚEngineÚTemplateDoesNotExist)Úpprint)Úresolve)Útimezone)ÚMultiValueDict)Ú	force_str)Úimport_string)Úget_docs_versionTZi18nzdjango.templatetags.i18n)ÚdebugÚ	librariesz#API|TOKEN|KEY|SECRET|PASS|SIGNATURE)Úflagsz********************c               @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú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             C   s
   || _ d S )N)Ú_wrapped)ÚselfZcallable_setting© r   ú4/tmp/pip-build-78s3knkw/Django/django/views/debug.pyÚ__init__)   s    zCallableSettingWrapper.__init__c             C   s
   t | jƒS )N)Úreprr   )r   r   r   r   Ú__repr__,   s    zCallableSettingWrapper.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   "   s   r   c             C   sd   y6t j| ƒrt}n"t|tƒr0dd„ |jƒ D ƒ}n|}W n tk
rN   |}Y nX t|ƒr`t|ƒ}|S )zš
    Cleanse an individual setting key/value of sensitive content. If the value
    is a dictionary, recursively cleanse the keys in that dictionary.
    c             S   s   i | ]\}}t ||ƒ|“qS r   )Úcleanse_setting)Ú.0ÚkÚvr   r   r   ú
<dictcomp>:   s    z#cleanse_setting.<locals>.<dictcomp>)	ÚHIDDEN_SETTINGSÚsearchÚCLEANSED_SUBSTITUTEÚ
isinstanceÚdictÚitemsÚ	TypeErrorÚcallabler   )ÚkeyÚvalueÚcleansedr   r   r   r    0   s    


r    c              C   s6   i } x,t tƒD ] }|jƒ rt|tt|ƒƒ| |< qW | S )z{
    Return a dictionary of the settings module with values of sensitive
    settings replaced with stars (*********).
    )Údirr   Úisupperr    Úgetattr)Zsettings_dictr"   r   r   r   Úget_safe_settingsH   s
    r3   éô  c             C   sF   t | |||ƒ}| jƒ r,|jƒ }t||dd�S |jƒ }t||dd�S 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/plain; charset=utf-8)ÚstatusÚcontent_typez	text/htmlN)ÚExceptionReporterZis_ajaxÚget_traceback_textr   Úget_traceback_html)ÚrequestÚexc_typeÚ	exc_valueÚtbÚstatus_codeZreporterÚtextÚhtmlr   r   r   Útechnical_500_responseT   s    rA   c               C   s   t tjƒƒ S )N)r   r   Z!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   Ú%get_default_exception_reporter_filterb   s    rB   c             C   s   t ƒ }t| d|ƒS )NZexception_reporter_filter)rB   r2   )r:   Zdefault_filterr   r   r   Úget_exception_reporter_filterh   s    rC   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚExceptionReporterFilterzv
    Base for all exception reporter filter classes. All overridable hooks
    contain lenient default behaviors.
    c             C   s   |d kri S |j S d S )N)ÚPOST)r   r:   r   r   r   Úget_post_parameterss   s    z+ExceptionReporterFilter.get_post_parametersc             C   s   t |jjƒ ƒS )N)ÚlistÚf_localsr*   )r   r:   Útb_framer   r   r   Úget_traceback_frame_variablesy   s    z5ExceptionReporterFilter.get_traceback_frame_variablesN)r   r   r   r   rF   rJ   r   r   r   r   rD   m   s   rD   c               @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚSafeExceptionReporterFilterz‹
    Use annotations made by the sensitive_post_parameters and
    sensitive_variables decorators to filter out sensitive information.
    c             C   s
   t jdk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   r:   r   r   r   Ú	is_activeƒ   s    z%SafeExceptionReporterFilter.is_activec             C   sD   t |dg ƒ}| j|ƒr@|r@|jƒ }x|D ]}||kr(t||< q(W |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)r2   rM   Úcopyr'   )r   r:   ZmultivaluedictrN   Úparamr   r   r   Úget_cleansed_multivaluedictŒ   s    
z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc             C   s€   |dkri S t |dg ƒ}| j|ƒrv|rv|jjƒ }|dkrRx|D ]}t||< q>W |S x|D ]}||krXt||< qXW |S n|jS dS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        NrN   Ú__ALL__)r2   rM   rE   rO   r'   )r   r:   rN   r/   r"   rP   r   r   r   rF   š   s    


z/SafeExceptionReporterFilter.get_post_parametersc             C   sL   yt |tƒ}W n( tk
r6 } zdj||ƒS d }~X nX |rH| j||ƒ}|S )Nz{!r} while evaluating {!r})r(   r   Ú	ExceptionÚformatrQ   )r   r:   r.   Zis_multivalue_dictÚer   r   r   Úcleanse_special_types³   s    z1SafeExceptionReporterFilter.cleanse_special_typesc       	      C   s  |j }d}x@|dk	rJ|jjdkrBd|jkrB|jd }t|ddƒ}P |j }qW i }| j|ƒr¼|r¼|dkr€xR|jD ]}t||< qnW qäxb|jjƒ D ]*\}}||kr¢t}n| j||ƒ}|||< qŒW n(x&|jjƒ D ]\}}| j||ƒ||< qÈW |jjdkoød|jk�rt|d< t|d< |jƒ S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NZsensitive_variables_wrapperÚsensitive_variablesrR   Ú	func_argsZfunc_kwargs)	Úf_backÚf_codeÚco_namerH   r2   rM   r'   r*   rV   )	r   r:   rI   Úcurrent_framerW   Úwrapperr/   Únamer.   r   r   r   rJ   Á   s4    



z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)	r   r   r   r   rM   rQ   rF   rV   rJ   r   r   r   r   rK   }   s   	rK   c               @   sD   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zddd„Zdd„ Z	dS )r7   z0Organize and coordinate reporting on exceptions.Fc             C   sJ   || _ t| j ƒ| _|| _|| _|| _|| _t| jdd ƒ| _d| _	d | _
d S )NZtemplate_debugF)r:   rC   Úfilterr;   r<   r=   Úis_emailr2   Útemplate_infoÚtemplate_does_not_existÚ
postmortem)r   r:   r;   r<   r=   r`   r   r   r   r   ö   s    zExceptionReporter.__init__c             C   sT  | j r*t| j tƒr*d| _| jjp&| jg| _| jƒ }x€t|ƒD ]t\}}d|kr¨g }xN|d D ]B\}}t	|ƒ}t
|ƒdkrŽd|dd… t
|ƒf }|j||fƒ qZW ||d< |||< q<W d}| j oÈt| j tƒ�r4t| jddƒ}t| jd	dƒ}	|dk	oö|	dk	�r4| jjd
 }
t|
t|d dƒt|	d t
|
ƒƒ… ddd�}ddlm} | jdk�rRd}n,yt| jjƒ}W n tk
�r|   d}Y nX | j||| j|t| jj| jƒjƒ ƒtƒ tjdtj dd…  t!j"ƒ |ƒ tj#| j$| j| jdœ}| jdk	�r| jj%jƒ |d< | jj&jƒ |d< | jj'jƒ |d< | j �r(| j j(|d< | j�r>t| jƒ|d< |�rP|d |d< |S )z5Return a dictionary containing traceback information.TÚvarsi   u   %sâ€¦ <trimmed %d bytes string>r   Ú ÚstartNÚendé   é   ÚasciiÚreplace)Úerrors)Úget_versionz%[unable to retrieve the current user]z%d.%d.%dé   )r`   Úunicode_hintÚframesr:   Úuser_strZfiltered_POST_itemsr   Zsys_executableZsys_version_infoZserver_timeZdjango_version_infoÚsys_pathra   rb   rc   Zrequest_GET_itemsZrequest_FILES_itemsZrequest_COOKIES_itemsZexception_typeZexception_valueZ	lastframeéÿÿÿÿ))r;   Ú
issubclassr	   rb   r<   Úchainrc   Úget_traceback_framesÚ	enumerater
   ÚlenÚappendÚUnicodeErrorr2   Úargsr   ÚmaxÚminÚdjangorm   r:   ÚstrÚuserrS   r`   rG   r_   rF   r*   r3   ÚsysÚ
executableÚversion_infor   ÚnowÚpathra   ÚGETZFILESZCOOKIESr   )r   rp   ÚiÚframeZ
frame_varsr"   r#   ro   rf   rg   Zunicode_strrm   rq   Úcr   r   r   Úget_traceback_data  sp    "


z$ExceptionReporter.get_traceback_datac             C   sH   t tddƒjdd��}tj|jƒ ƒ}W dQ R X t| jƒ dd�}|j|ƒS )z1Return HTML version of debug 500 HTTP error page.Ú	templatesztechnical_500.htmlzutf-8)ÚencodingNF)Úuse_l10n)	r   ÚCURRENT_DIRÚopenÚDEBUG_ENGINEÚfrom_stringÚreadr   rŠ   Úrender)r   ÚfhÚtr‰   r   r   r   r9   I  s    z$ExceptionReporter.get_traceback_htmlc             C   sJ   t tddƒjdd��}tj|jƒ ƒ}W dQ R X t| jƒ ddd�}|j|ƒS )z7Return plain text version of debug 500 HTTP error page.r‹   ztechnical_500.txtzutf-8)rŒ   NF)Z
autoescaper�   )	r   rŽ   r�   r�   r‘   r’   r   rŠ   r“   )r   r”   r•   r‰   r   r   r   r8   P  s    z$ExceptionReporter.get_traceback_textNc                s<  d}t |dƒrBy|j|ƒ}W n tk
r0   Y nX |dk	rB|jƒ }|dkr†y&t|dƒ�}|jƒ jƒ }W dQ R X W n tk
r„   Y nX |dkršdg dg fS t|d tƒröd‰ x6|dd… D ]&}t	j
d|ƒ}	|	rº|	jdƒjdƒ‰ P qºW ‡ fd	d
„|D ƒ}td|| ƒ}
|| }||
|… }|| }||d |… }|
|||fS )z—
        Return context_lines before and after lineno from file.
        Return (pre_context_lineno, pre_context, context_line, post_context).
        NÚ
get_sourceÚrbr   rj   é   s   coding[:=]\s*([-\w.]+)rh   c                s   g | ]}t |ˆ d ƒ‘qS )rk   )r   )r!   Úsline)rŒ   r   r   ú
<listcomp>y  s    z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)Úhasattrr–   ÚImportErrorÚ
splitlinesr�   r’   ÚOSErrorr(   ÚbytesÚrer&   ÚgroupÚdecoder|   )r   ÚfilenameÚlinenoZcontext_linesÚloaderÚmodule_nameÚsourceÚfpÚlineÚmatchZlower_boundZupper_boundÚpre_contextÚcontext_lineÚpost_contextr   )rŒ   r   Ú_get_lines_from_fileW  s<    
z&ExceptionReporter._get_lines_from_filec             C   sz  dd„ }g }| j }x$|r6|j|ƒ ||ƒ}||krP qW g }|sD|S |jƒ }|sV| jn|j}�x|d k	�rt|jjjdƒr€|j}q`|jj	j
}|jj	j}|jd }|jjjdƒ}	|jjjdƒp¼d}
| j||d|	|
ƒ\}}}}|d krð|}g }d	}g }|j||ƒt|d
dƒ||
jdƒ�rdnd|||d | jj| j|jƒt|ƒ||||d dœƒ |j �rl|�rl|jƒ }|j}q`|j}q`W |S )Nc             S   s    t | dd ƒ}t | dd ƒ}|p|S )NÚ	__cause__Ú__context__)r2   )r<   ÚexplicitZimplicitr   r   r   Úexplicit_or_implicit_cause…  s    zJExceptionReporter.get_traceback_frames.<locals>.explicit_or_implicit_causeZ__traceback_hide__rh   Ú
__loader__r   re   é   z<source code not available>r¯   Tzdjango.r~   r€   )Z	exc_causeZexc_cause_explicitr=   Útyper£   Úfunctionr¤   rd   Úidr«   r¬   r­   Úpre_context_lineno)r<   ry   Úpopr=   Ú__traceback__rI   rH   ÚgetÚtb_nextrZ   Úco_filenamer[   Ú	tb_linenoÚ	f_globalsr®   r2   Ú
startswithr_   rJ   r:   r·   )r   r²   Ú
exceptionsr<   rp   r=   r£   r¶   r¤   r¥   r¦   r¸   r«   r¬   r­   r   r   r   rv   „  s`    





z&ExceptionReporter.get_traceback_frames)F)NN)
r   r   r   r   r   rŠ   r9   r8   r®   rv   r   r   r   r   r7   ô   s   
G
-r7   c          )   C   sà  y|j d d }W n( tttfk
r:   | jdd… }Y nX y|j d d }W n tttfk
rl   g }Y nnX | sÒ| jdkrÚt|ƒdkrÚt|d ƒdkrÚt|d d ddƒt|d d d	dƒ  koÌd
kn  rÚt| ƒS t| dt	j
ƒ}t|tjƒrú|j}d}yt| jƒ}W n tk
�r"   Y n\X |j}t|dƒ�r>|j}n"t|dƒ�r`t|jdƒ�r`|jj}t|dƒ�r~|j}d||f }ttddƒjdd��}	tj|	jƒ ƒ}
W dQ R X t|t	j
||t|ƒ| tƒ |dœƒ}t|
j|ƒdd�S )zBCreate a technical 404 error response. `exception` is the Http404.r   r…   rh   NÚtriedú/Zapp_namere   Ú	namespaceZadminÚurlconfr   Ú	__class__r   z%s.%sr‹   ztechnical_404.htmlzutf-8)rŒ   )rÅ   Zroot_urlconfÚrequest_pathZurlpatternsÚreasonr:   r   Zraising_view_namez	text/html)r6   ) r{   Ú
IndexErrorr+   ÚKeyErrorZ	path_infor…   rx   r2   Údefault_urlconfr   ZROOT_URLCONFr(   ÚtypesÚ
ModuleTyper   r   r   Úfuncr›   rÆ   r   r   rŽ   r�   r�   r‘   r’   r   r   r3   r   r“   )r:   Ú	exceptionZ	error_urlrÂ   rÅ   ZcallerZresolver_matchÚobjÚmoduler”   r•   r‰   r   r   r   Útechnical_404_responseË  sT    

8
rÒ   c             C   sN   t tddƒjdd��}tj|jƒ ƒ}W dQ R X tdtƒ iƒ}t|j	|ƒdd�S )	z+Create an empty URLconf 404 error response.r‹   zdefault_urlconf.htmlzutf-8)rŒ   NÚversionz	text/html)r6   )
r   rŽ   r�   r�   r‘   r’   r   r   r   r“   )r:   r”   r•   r‰   r   r   r   rË     s
    rË   )r4   )2Ú	functoolsr    r�   rÌ   Zpathlibr   Zdjango.confr   Zdjango.httpr   r   r   Zdjango.templater   r   r	   Zdjango.template.defaultfiltersr
   Zdjango.urlsr   Zdjango.utilsr   Zdjango.utils.datastructuresr   Zdjango.utils.encodingr   Zdjango.utils.module_loadingr   Zdjango.utils.versionr   r�   ÚcompileÚ
IGNORECASEr%   r'   Ú__file__ÚparentrŽ   r   r    r3   rA   Ú	lru_cacherB   rC   rD   rK   r7   rÒ   rË   r   r   r   r   Ú<module>   s@   

w X8