o
    h2                     @   sF  d dl Z d dlZd dlmZ d dlm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 d dlmZ d d	lmZ d d
lmZmZmZmZmZmZmZ 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m%Z% d dl&m'Z'm(Z( d dl)m*Z* d dl+m,Z,m-Z-m.Z.m/Z/ d dl0m1Z1 zd dl2m3Z3m4Z5 d dl6m7Z7 d dl0m8Z8 d dl9m:Z: W n e;y   edw e:j<Z=e3j>Z?e8j@ZAze3jBZCW n	 eDy   Y nw G dd deZEG dd deZFdd  ZGd!d" ZHd#d$ ZBd%d& ZId5d'd(ZJd)d* ZKd+d, ZLd-d. ZMd/d0 ZNeeEd1d2 ZOd3d4 ZPdS )6    N)isawaitable)urlsplit)continue_trace)OP)_check_minimum_versionIntegrationDidNotEnable)RequestExtractor_filter_headers)ignore_logger)TransactionSource)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEparse_versionreraise)TYPE_CHECKING)	Container)Any)Callable)Optional)Union)Dict)RequestRequestParameters)BaseHTTPResponse)EventEventProcessorExcInfoHint)Route)Sanic__version__)SanicException)Router)ErrorHandlerzSanic not installedc                   @   s<   e Zd ZdZde ZdZedhfddZedd Z	dS )	SanicIntegrationsanicz
auto.http.Ni  c                 C   s   |pt  | _dS )aG  
        The unsampled_statuses parameter can be used to specify for which HTTP statuses the
        transactions should not be sent to Sentry. By default, transactions are sent for all
        HTTP statuses, except 404. Set unsampled_statuses to None to send transactions for all
        HTTP statuses, including 404.
        N)set_unsampled_statuses)selfunsampled_statuses r.   _/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/integrations/sanic.py__init__?   s   zSanicIntegration.__init__c                   C   s`   t tt_tttj tstdt tdrt	d tjd ur+tjdk r+t
  d S t  d S )NzUThe sanic integration for Sentry requires Python 3.7+  or the aiocontextvars package.z0.8.root   	   )r   SANIC_VERSIONr(   versionr   r   r   r   
startswithr   _setup_legacy_sanic_setup_sanicr.   r.   r.   r/   
setup_onceI   s   



zSanicIntegration.setup_once)
__name__
__module____qualname__
identifieroriginr6   	frozensetr0   staticmethodr:   r.   r.   r.   r/   r(   :   s    

r(   c                   @   sL   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d Z
dS )SanicRequestExtractorc                 C   s   | j jd u rdS t| j jS )Nr   )requestbodylenr,   r.   r.   r/   content_lengthk   s   z$SanicRequestExtractor.content_lengthc                 C   s   t | jjS N)dictrC   cookiesrF   r.   r.   r/   rJ   q   s   zSanicRequestExtractor.cookiesc                 C      | j jS rH   )rC   rD   rF   r.   r.   r/   raw_datau      zSanicRequestExtractor.raw_datac                 C   rK   rH   )rC   formrF   r.   r.   r/   rN   y   rM   zSanicRequestExtractor.formc                 C   s   t  rH   )NotImplementedErrorrF   r.   r.   r/   is_json}   s   zSanicRequestExtractor.is_jsonc                 C   rK   rH   )rC   jsonrF   r.   r.   r/   rQ      rM   zSanicRequestExtractor.jsonc                 C   rK   rH   )rC   filesrF   r.   r.   r/   rR      rM   zSanicRequestExtractor.filesc                 C   s   t |jpdS )Nr.   )rE   rD   )r,   filer.   r.   r/   size_of_file   s   z"SanicRequestExtractor.size_of_fileN)r;   r<   r=   rG   rJ   rL   rN   rP   rQ   rR   rT   r.   r.   r.   r/   rB   j   s    rB   c                   C   s   t t_ tt_d S rH   )_startupr#   _sentry_error_handler_lookupr'   lookupr.   r.   r.   r/   r9      s   
r9   c                   C   s   t t_tt_tt_d S rH   )	_legacy_handle_requestr#   handle_request_legacy_router_getr&   getrV   r'   rW   r.   r.   r.   r/   r8      s   
r8   c                    s>   |  dt |  dt |  dt t| I d H  d S )Nzhttp.lifecycle.requestzhttp.lifecycle.responsezhttp.routing.after)signal_context_enter_context_exit_set_transactionold_startuprF   r.   r.   r/   rU      s
   rU   c                    s   t  td u| j_| jjsd S t| }t  | j_	| jj	
 }|  |t| tt| jtj| jtjtjd}t |
 | j_d S )N)opnamesourcer?   )
sentry_sdk
get_clientget_integrationr(   ctx_sentry_do_integrationweakrefrefisolation_scope_sentry_scope	__enter__clear_breadcrumbsadd_event_processor_make_request_processorr   rI   headersr   HTTP_SERVERpathr   URLr?   start_transaction_sentry_transaction)rC   weak_requestscopetransactionr.   r.   r/   r]      s,   

r]   c              	      s   t  c | jjs	 W d    d S t t}|d u rd n|j}t  ) | jj	| | jj j
t|to;||jvM  _
| jjd d d  W d    n1 sQw   Y  | jjd d d  W d    d S 1 sjw   Y  d S rH   )r   rg   rh   rd   re   rf   r(   statusrv   set_http_statussampled
isinstancer+   __exit__rl   )rC   responseintegrationresponse_statusr.   r.   r/   r^      s"   

"r^   c                    sj   | j jr3t ! t }|j| jjdd}|j	|t
jd W d    d S 1 s,w   Y  d S d S )N .rc   )rg   rh   r   rd   get_current_scoperb   replaceappstripset_transaction_namer   	COMPONENT)rC   route_rx   
route_namer.   r.   r/   r_      s   "r_   c                    sR   t | t| |g|R i |  d u rd S t td u r! S  fdd}|S )Nc                    s   zOz# | |}t |r|I d H }|W W tjr$tjdkr%t| I d H  S S S  ty:   t }t| t|  Y nw W tjrMtjdkrOt| I d H  d S d S d S tjratjdkrbt| I d H  w w w )Nr2   )	r   r(   r6   r^   	Exceptionsysexc_info_capture_exceptionr   )rC   	exceptionr   r   old_error_handlerr.   r/   sentry_wrapped_error_handler   s(   

zB_sentry_error_handler_lookup.<locals>.sentry_wrapped_error_handler)r   old_error_handler_lookuprd   re   rf   r(   )r,   r   argskwargsr   r.   r   r/   rV      s   rV   c                    s   t  td u rt| |g|R i |I d H S t|}t  *}|  |	t
| t| |g|R i |}t|rC|I d H }|W  d    S 1 sOw   Y  d S rH   )rd   re   rf   r(   old_handle_requestri   rj   rk   rn   ro   rp   r   )r,   rC   r   r   rw   rx   r   r.   r.   r/   rX     s   


$rX   c                 G   s   t | g|R  }t td urht L t }tjrEtjdkrE| jj	j
}|d j
}|d| r<|t|d d  }|j|tjd n|j|d jtjd W d    |S W d    |S 1 scw   Y  |S )N)r3      r   z%s.   r   )old_router_getrd   re   rf   r(   r   get_isolation_scoper6   rg   r   rb   r7   rE   r   r   r   r;   )r,   r   rvrx   sanic_app_namesanic_router.   r.   r/   rZ     s,   



rZ   c                 C   s   t  : t| t jdddd\}}|r.t|d d dr.|d d jr.	 W d    d S tj||d W d    d S 1 s@w   Y  d S )	Nr)   F)typehandled)client_options	mechanismr   r   quiet)hint)r   r   rd   re   optionshasattrr   capture_event)r   eventr   r.   r.   r/   r   =  s   
$
"r   c                    s    fdd}|S )Nc                    s   z|rt |d d trW d S W n	 ty   Y nw   }|d u r#| S t A t|}||  | d }t|j}d|j|j	|j
f |d< |j|d< |j|d< d|ji|d	< tt|j|d
< W d    | S 1 sjw   Y  | S )Nr   r   rC   z	%s://%s%surlquery_stringmethodREMOTE_ADDRenvrq   )
issubclassr%   KeyErrorr   rB   extract_into_eventr   r   schemenetlocrs   queryr   remote_addrr
   rI   rq   )r   r   rC   	extractorrequest_infourlpartsrw   r.   r/   sanic_processorO  s8   





z0_make_request_processor.<locals>.sanic_processorr.   )rw   r   r.   r   r/   rp   M  s   !rp   rH   )Qr   ri   inspectr   urllib.parser   rd   r   sentry_sdk.constsr   sentry_sdk.integrationsr   r   r   $sentry_sdk.integrations._wsgi_commonr	   r
   sentry_sdk.integrations.loggingr   sentry_sdk.tracingr   sentry_sdk.utilsr   r   r   r   r   r   r   typingr   collections.abcr   r   r   r   r   r   sanic.requestr   r   sanic.responser   sentry_sdk._typesr   r   r    r!   sanic.routerr"   r)   r#   r$   r5   sanic.exceptionsr%   r&   sanic.handlersr'   ImportErrorrW   r   rY   r   r[   r   rU   r`   AttributeErrorr(   rB   r9   r8   r]   r^   r_   rV   rX   rZ   r   rp   r.   r.   r.   r/   <module>   sl    $

0$
	"
