a
    xdL                     @   s&  d dl m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
 d dl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 erd dlmZmZmZmZm Z m!Z! d dlm"Z" zld dl#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l0m1Z1m2Z2m3Z3m4Z4 W n e5yP   edY n0 zd dl6m7Z7 W n  e5y   d dl8m7Z7 Y n0 zd dl9Z9W n e5y   dZ9Y n0 dZ:dZ;G dd deZ<dd Z=d1ddZ>dd  Z?d!d" Z@d#d$ ZAd%d& ZBd'd( ZCd)d* ZDd+d, ZEG d-d. d.ZFd/d0 ZGdS )2    )absolute_importN)	iteritems)MYPY)Hub_should_send_default_pii)DidNotEnableIntegration)_is_json_content_typerequest_body_within_bounds)SentryAsgiMiddleware)SOURCE_FOR_STYLE)TRANSACTION_SOURCE_ROUTEAnnotatedValuecapture_internal_exceptionsevent_from_exceptiontransaction_from_function)Any	AwaitableCallableDictOptionalUnion)Event)	Starlette)
UploadFile)
Middleware)AuthenticationMiddleware)Request)Match)ASGIAppReceiveScopeSendzStarlette is not installed)ExceptionMiddlewarezgeneric Starlette request)endpointurlc                   @   s*   e Zd ZdZdZd	ddZedd ZdS )
StarletteIntegration	starlette r%   c                 C   s"   |t vrtd|t f || _d S )Nz7Invalid value for transaction_style: %s (must be in %s))TRANSACTION_STYLE_VALUES
ValueErrortransaction_style)selfr+    r-   Y/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/integrations/starlette.py__init__C   s    zStarletteIntegration.__init__c                   C   s   t   t  t  d S N)patch_middlewarespatch_asgi_apppatch_request_responser-   r-   r-   r.   
setup_onceL   s    zStarletteIntegration.setup_onceN)r%   )__name__
__module____qualname__
identifierr+   r/   staticmethodr4   r-   r-   r-   r.   r&   >   s
   
	r&   c                    s*   | j   fdd} jdv}|r&|| _ | S )Nc                     s   t j}|t}|d urt| d jj}|jd|d0}|d|  | i |I d H  W d    q1 sh0    Y  n | i |I d H  d S )Nr   zstarlette.middleware)opdescriptionzstarlette.middleware_name)r   currentget_integrationr&   	__class__r5   Z
start_spanZset_tag)argskwargshubintegrationZmiddleware_nameZmiddleware_spanold_callr-   r.   _create_span_callX   s    
4z6_enable_span_for_middleware.<locals>._create_span_call)rE   %_sentry_authenticationmiddleware_call _sentry_exceptionmiddleware_call)__call__r5   )middleware_classrE   not_yet_patchedr-   rC   r.   _enable_span_for_middlewareT   s    
rK   Fc                 C   sR   t j}|td u rd S t| |jr*|jjnd tj|dd\}}|j||d d S )N)typehandled)Zclient_optionsZ	mechanism)hint)	r   r<   r=   r&   r   clientoptionsr8   Zcapture_event)	exceptionrM   rA   eventrN   r-   r-   r.   _capture_exceptiont   s    

rS   c                    s4   | j fdd}|| _ | j  fdd}|| _dS )zX
    Capture all exceptions in Starlette app and
    also extract user information.
    c                    sJ   | g|R i | | j    fdd}| j  D ]}|| j |< q6d S )Nc                    s   |d }t |do|jdk}|r,t|dd d }t|jD ]}| v r: | } qTq:|d u r`d S t|r|| g|R i |I d H S || g|R i |S d S )Nr   Z
staus_codei  T)rM   )hasattrstatus_coderS   rL   __mro___is_async_callable)r,   r?   r@   expZis_http_server_errorZold_handlerclsZold_handlersr-   r.   !_sentry_patched_exception_handler   s    zfpatch_exception_middleware.<locals>._sentry_middleware_init.<locals>._sentry_patched_exception_handler)Z_exception_handlerscopykeys)r,   r?   r@   r[   keyold_middleware_initrZ   r.   _sentry_middleware_init   s
    
z;patch_exception_middleware.<locals>._sentry_middleware_initc                    s    t |  | |||I d H  d S r0   _add_user_to_sentry_scoper,   scopereceivesendrC   r-   r.   rG      s    zDpatch_exception_middleware.<locals>._sentry_exceptionmiddleware_callN)r/   rH   )rI   ra   rG   r-   )rD   r`   r.   patch_exception_middleware   s    !rh   c                 C   s   d| vrdS t  sdS tj}|tdu r.dS | |}i }| d }t|dd}|rb|d|j t|dd}|r|d|j	 t|dd}|r|d|j
 ||_W d   n1 s0    Y  dS )zZ
    Extracts user information from the ASGI scope and
    adds it to Sentry's scope.
    userNusernameidemail)r   r   r<   r=   r&   configure_scopegetattr
setdefaultrj   rk   rl   ri   )re   rA   sentry_scopeZ	user_infoZstarlette_userrj   Zuser_idrl   r-   r-   r.   rc      s(    
rc   c                    s   | j   fdd}|| _ dS )z/
    Add user information to Sentry scope.
    c                    s     | |||I d H  t | d S r0   rb   rd   rC   r-   r.   rF      s    zNpatch_authentication_middleware.<locals>._sentry_authenticationmiddleware_callN)rH   )rI   rF   r-   rC   r.   patch_authentication_middleware   s    rq   c                     s,   t j dt v} | r( fdd}|t _dS )za
    Patches Starlettes `Middleware` class to record
    spans for every middleware invoked.
    ra   c                    s>   t |} | |fi | |tkr*t| |tkr:t| d S r0   )rK   r   rq   r#   rh   )r,   rY   rP   Zspan_enabled_clsr_   r-   r.   ra     s    z2patch_middlewares.<locals>._sentry_middleware_initN)r   r/   str)rJ   ra   r-   r_   r.   r1      s
    r1   c                     s   t j  fdd} | t _dS )zG
    Instrument Starlette ASGI app using the SentryAsgiMiddleware.
    c                    sV   t jtd u r$ |||I d H S t fddtjd}|j|_||||I d H S )Nc                     s    g| R i |S r0   r-   )akw)old_appr,   r-   r.   <lambda>      zBpatch_asgi_app.<locals>._sentry_patched_asgi_app.<locals>.<lambda>)Zmechanism_type)r   r<   r=   r&   r   r8   Z
_run_asgi3rH   )r,   re   rf   rg   Z
middlewareru   r,   r.   _sentry_patched_asgi_app  s    z0patch_asgi_app.<locals>._sentry_patched_asgi_appN)r   rH   )rz   r-   rx   r.   r2     s    r2   c                 C   s2   t | tjr| j} q t| p0t| o0t| jS r0   )
isinstance	functoolspartialfuncasyncioiscoroutinefunctioncallablerH   )objr-   r-   r.   rW   '  s
    
rW   c                     s    t jj  fdd} | t j_d S )Nc                    s:   |  t  }|r" fdd}|} n fdd}|} | S )Nc                     s   t j}|t}|d u r,| i |I d H S | :}| d }t|}| I d H   fdd}W d    n1 st0    Y  tj|_|	||| | i |I d H S )Nr   c                    s    fdd}|S )Nc                    sX   |  di } r>d v r*t r* d |d< d v r> d |d< || d< t| j | S )Nrequestcookiesdata)getr    _set_transaction_name_and_sourcer+   rR   rN   request_info)inforB   reqr-   r.   event_processorJ  s    zpatch_request_response.<locals>._sentry_request_response.<locals>._sentry_async_func.<locals>._make_request_event_processor.<locals>.event_processorr-   r   rB   r   r   rB   r   r.   _make_request_event_processorH  s    z{patch_request_response.<locals>._sentry_request_response.<locals>._sentry_async_func.<locals>._make_request_event_processor)
r   r<   r=   r&   rm   StarletteRequestExtractorextract_request_infor8   _nameadd_event_processorr?   r@   rA   rB   rp   r   Z	extractorr   Zold_funcr   r.   _sentry_async_func<  s    

*zTpatch_request_response.<locals>._sentry_request_response.<locals>._sentry_async_funcc                     s   t j}|t}|d u r&| i |S | 4}| d }t|}|   fdd}W d    n1 sh0    Y  tj|_|	||| | i |S )Nr   c                    s    fdd}|S )Nc                    s2   |  di } r |d< || d< t| j | S )Nr   r   )r   r   r+   r   )r   rB   r   r-   r.   r   v  s    zpatch_request_response.<locals>._sentry_request_response.<locals>._sentry_sync_func.<locals>._make_request_event_processor.<locals>.event_processorr-   r   r   r   r.   r   t  s    zzpatch_request_response.<locals>._sentry_request_response.<locals>._sentry_sync_func.<locals>._make_request_event_processor)
r   r<   r=   r&   rm   r   extract_cookies_from_requestr8   r   r   r   r   r   r.   _sentry_sync_funch  s    

*zSpatch_request_response.<locals>._sentry_request_response.<locals>._sentry_sync_func)rW   )r~   Zis_coroutiner   r   Zold_request_responser   r.   _sentry_request_response5  s    )'z8patch_request_response.<locals>._sentry_request_response)r'   ZroutingZrequest_response)r   r-   r   r.   r3   1  s    ^r3   c                   @   sd   e Zd Zd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d Zdd ZdS )r   z
    Extracts useful information from the Starlette request
    (like form data or cookies) and adds it to the Sentry event.
    Nc                 C   s
   || _ d S r0   )r   )r,   r   r-   r-   r.   r/     s    z"StarletteRequestExtractor.__init__c                 C   s*   t jj}|d u rd S d }t r&|  }|S r0   )r   r<   rO   r   r   )r,   rO   r   r-   r-   r.   r     s    z6StarletteRequestExtractor.extract_cookies_from_requestc                    s   t jj}|d u rd S d }|  I d H }i }t  t rD|  |d< t||sjtdddd|gg|d}nJ| 	 I d H }|d ur|}n.| 
 I d H rtdddd|gg|d}nd }|d ur||d< W d    n1 s0    Y  |S )	Nr   r(   z!configxr   )remlen!rawr   )r   r<   rO   content_lengthr   r   r   r
   r   parsed_bodyraw_data)r,   rO   r   r   r   r   r-   r-   r.   r     s<    
&z.StarletteRequestExtractor.extract_request_infoc                    s"   |   I d H }|d u rdS t|S )Nr   )r   r   )r,   r   r-   r-   r.   r     s    z(StarletteRequestExtractor.content_lengthc                 C   s   | j jS r0   )r   r   ry   r-   r-   r.   r     s    z!StarletteRequestExtractor.cookiesc                    s   | j  I d H S r0   )r   bodyry   r-   r-   r.   r     s    z"StarletteRequestExtractor.raw_datac                    s   t du rdS | j I dH S )a  
        curl -X POST http://localhost:8000/upload/somethign -H "Content-Type: application/x-www-form-urlencoded" -d "username=kevin&password=welcome123"
        curl -X POST http://localhost:8000/upload/somethign  -F username=Julian -F password=hello123
        N)	multipartr   formry   r-   r-   r.   r     s    zStarletteRequestExtractor.formc                 C   s   t | jjdS )Nzcontent-type)r	   r   headersr   ry   r-   r-   r.   is_json  s    z!StarletteRequestExtractor.is_jsonc                    s   |   sdS | j I dH S )z
        curl -X POST localhost:8000/upload/something -H 'Content-Type: application/json' -d '{"login":"my_login","password":"my_password"}'
        N)r   r   jsonry   r-   r-   r.   r     s    zStarletteRequestExtractor.jsonc                    s   |   I dH }|rpi }t|D ]L\}}t|trbt| I dH }td|ddd|ggd||< q|||< q|S |  I dH }|S )z
        curl -X POST http://localhost:8000/upload/somethign  -F username=Julian -F password=hello123 -F photo=@photo.jpg
        Nr(   r   r   r   )r   r   )r   r   r{   r   r   readr   r   )r,   r   r   r^   valsizeZ	json_datar-   r-   r.   r     s    


z%StarletteRequestExtractor.parsed_body)r5   r6   r7   __doc__r   r/   r   r   r   r   r   r   r   r   r   r-   r-   r-   r.   r     s   +
r   c                 C   s   d}|dkr*|j d}|rt|p&d}nl|dkr|j d }|jD ]R}||j }|d tjkrB|dkrt|d d pzd} qqB|dkrB|j} qqB|st| d< dt	i| d	< d S || d< dt
| i| d	< d S )
Nr(   r$   r%   routerr      ZtransactionsourceZtransaction_info)re   r   r   Zroutesmatchesr   ZFULLpath_DEFAULT_TRANSACTION_NAMEr   r   )rR   r+   r   namer$   r   Zroutematchr-   r-   r.   r     s,    

r   )F)H
__future__r   r   r|   Zsentry_sdk._compatr   Zsentry_sdk._typesr   Zsentry_sdk.hubr   r   Zsentry_sdk.integrationsr   r   Z$sentry_sdk.integrations._wsgi_commonr	   r
   Zsentry_sdk.integrations.asgir   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr   r   r   r   r   typingr   r   r   r   r   r   r   r'   Zstarlette.applicationsr   Zstarlette.datastructuresr   Zstarlette.middlewarer   Z#starlette.middleware.authenticationr   Zstarlette.requestsr   Zstarlette.routingr   Zstarlette.typesr   r    r!   r"   ImportErrorZstarlette.middleware.exceptionsr#   Zstarlette.exceptionsr   r   r)   r&   rK   rS   rh   rc   rq   r1   r2   rW   r3   r   r   r-   r-   r-   r.   <module>   s^    
 
@#
e 