o
    Zh                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZmZ d dlmZmZmZmZmZ d dlmZmZ d d	lmZmZmZ d d
lmZ d dlmZ zd dlm Z! d dl"m#Z#m$Z$ d dl%m&Z& W n e'yw   edw d dl(m)Z) e)rd 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(m0Z0m1Z1 G dd deZ2ej3dd Z4dd Z5dd Z6G dd  d eZ7dS )!    N)iscoroutinefunction)continue_trace)OP)Hub_should_send_default_pii)TRANSACTION_SOURCE_COMPONENTTRANSACTION_SOURCE_ROUTE)HAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEevent_from_exceptioncapture_internal_exceptionstransaction_from_function)IntegrationDidNotEnable)RequestExtractor_filter_headers_is_json_content_type)ignore_logger)	iteritems)version_info)RequestHandler	HTTPError)	coroutinezTornado not installed)TYPE_CHECKING)Any)Optional)Dict)Callable)	Generator)EventEventProcessorc                   @   s   e Zd ZdZedd ZdS )TornadoIntegrationtornadoc                     sz   t dk rtdtstdt td tj t } | r$ fdd}nt fdd}|t_tj	fdd	}|t_	d S )
N)   r   zTornado 5+ requiredzUThe tornado integration for Sentry requires Python 3.7+ or the aiocontextvars packageztornado.accessc                    sJ   t |   | g|R i |I d H W  d    S 1 sw   Y  d S N_handle_request_impl)selfargskwargsold_execute V/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/tornado.pysentry_execute_request_handlerI   s   
$zETornadoIntegration.setup_once.<locals>.sentry_execute_request_handlerc                 ?   sN    t |   | g|R i |E d H }|W  d    S 1 s w   Y  d S r$   r%   )r'   r(   r)   resultr*   r,   r-   r.   P   s
   
$c                    s(   t |||  | |||g|R i |S r$   )_capture_exception)r'   tyvaluetbr(   r)   )old_log_exceptionr,   r-   sentry_log_exception[   s   z;TornadoIntegration.setup_once.<locals>.sentry_log_exception)
TORNADO_VERSIONr   r	   r
   r   r   Z_executer   r   Zlog_exception)Z	awaitabler.   r5   r,   )r+   r4   r-   
setup_once2   s&   
zTornadoIntegration.setup_onceN)__name__
__module____qualname__
identifierstaticmethodr7   r,   r,   r,   r-   r!   /   s    r!   c              	   c   s    t j}|t}|d u rd V  t| }t |\}| jj}| }|	  t
|}|| W d    n1 s:w   Y  t|tjdtd}|j|d| jid d V  W d    n1 s`w   Y  W d    d S W d    d S 1 sxw   Y  d S )Nzgeneric Tornado request)opnamesourceZtornado_request)Zcustom_sampling_context)r   currentget_integrationr!   weakrefrefrequestheadersZconfigure_scopeZclear_breadcrumbs_make_event_processorZadd_event_processorr   r   ZHTTP_SERVERr   Zstart_transaction)r'   hubZintegrationweak_handlerrE   scope	processortransactionr,   r,   r-   r&   c   s6   




"r&   c                 C   s^   t j}|td u rd S t|trd S |j}t| ||f|jdddd\}}|j	||d d S )Nr"   F)typeZhandled)Zclient_optionsZ	mechanism)hint)
r   r@   rA   r!   
isinstancer   clientr   optionsZcapture_event)r1   r2   r3   rG   rO   eventrM   r,   r,   r-   r0      s   

r0   c                    s    fdd}|S )Nc                    sd    }|d u r	| S |j }t  t||j j }t|pd| d< dti| d< W d    n1 s1w   Y  t ; t|}||  | d }d|j	|j
|jf |d< |j|d< |j|d	< d
|ji|d< tt|j|d< W d    n1 sww   Y  t ) |jrt r| di dd W d    | S W d    | S W d    | S 1 sw   Y  | S )N rK   r?   Ztransaction_inforD   z	%s://%s%surlZquery_stringmethodZREMOTE_ADDRenvrE   useris_authenticatedT)rD   r   getattrrT   lowerr   r   TornadoRequestExtractorZextract_into_eventprotocolhostpathqueryZ	remote_ipr   dictrE   current_userr   
setdefault)rQ   rM   handlerrD   rT   Z	extractorZrequest_inforH   r,   r-   tornado_processor   sH   






z0_make_event_processor.<locals>.tornado_processorr,   )rH   rd   r,   rc   r-   rF      s   $rF   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )rZ   c                 C   s   | j jd u rdS t| j jS )Nr   )rD   bodylenr'   r,   r,   r-   content_length   s   z&TornadoRequestExtractor.content_lengthc                 C      dd t | jjD S )Nc                 S   s   i | ]\}}||j qS r,   )r2   .0kvr,   r,   r-   
<dictcomp>       z3TornadoRequestExtractor.cookies.<locals>.<dictcomp>)r   rD   cookiesrg   r,   r,   r-   rp         zTornadoRequestExtractor.cookiesc                 C   s   | j jS r$   )rD   re   rg   r,   r,   r-   raw_data   s   z TornadoRequestExtractor.raw_datac                 C   ri   )Nc                 S   s    i | ]\}}|d d |D qS )c                 S   s   g | ]}| d dqS )latin1replace)decode)rk   rm   r,   r,   r-   
<listcomp>   ro   z;TornadoRequestExtractor.form.<locals>.<dictcomp>.<listcomp>r,   )rk   rl   vsr,   r,   r-   rn      s    z0TornadoRequestExtractor.form.<locals>.<dictcomp>)r   rD   Zbody_argumentsrg   r,   r,   r-   form   s   
zTornadoRequestExtractor.formc                 C   s   t | jjdS )Nzcontent-type)r   rD   rE   getrg   r,   r,   r-   is_json   s   zTornadoRequestExtractor.is_jsonc                 C   ri   )Nc                 S   s   i | ]\}}|r||d  qS )r   r,   rj   r,   r,   r-   rn      s    z1TornadoRequestExtractor.files.<locals>.<dictcomp>)r   rD   filesrg   r,   r,   r-   r{      rq   zTornadoRequestExtractor.filesc                 C   s   t |jpdS )Nr,   )rf   re   )r'   filer,   r,   r-   size_of_file   s   z$TornadoRequestExtractor.size_of_fileN)
r8   r9   r:   rh   rp   rr   rx   rz   r{   r}   r,   r,   r,   r-   rZ      s    rZ   )8rB   
contextlibinspectr   Zsentry_sdk.apir   Zsentry_sdk.constsr   Zsentry_sdk.hubr   r   Zsentry_sdk.tracingr   r   Zsentry_sdk.utilsr	   r
   r   r   r   Zsentry_sdk.integrationsr   r   Z$sentry_sdk.integrations._wsgi_commonr   r   r   Zsentry_sdk.integrations.loggingr   Zsentry_sdk._compatr   r"   r   r6   Ztornado.webr   r   Ztornado.genr   ImportErrorZsentry_sdk._typesr   typingr   r   r   r   r   r   r    r!   contextmanagerr&   r0   rF   rZ   r,   r,   r,   r-   <module>   sB    4
#)