o
    Zh%                     @   s  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
 d dlmZ d dlmZmZmZ d dlmZ erRd d	lmZ d d
lmZ d dlmZ d dlmZmZ zd dlZd dlmZ W n eyi   edw zd dlZejZej Z!dZ"W n ey   d dl#Zej$Zej%Z!dZ"Y nw G dd deZ&G dd de'Z(dZ)G dd deZ*dd Z+dd Z,dd Z-dd  Z.d!d" Z/d#d$ Z0d%d& Z1dS )'    )absolute_import)Hub)IntegrationDidNotEnable)RequestExtractor)SentryWsgiMiddleware)SOURCE_FOR_STYLE)capture_internal_exceptionsevent_from_exceptionparse_version)TYPE_CHECKING)Any)Dict)Optional)EventEventProcessorN)__version__zFalcon not installedTFc                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zer dd Z	dS dd Z	dS )FalconRequestExtractorc                 C      | j jS N)requestenvself r   U/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/falcon.pyr   0      zFalconRequestExtractor.envc                 C   r   r   )r   cookiesr   r   r   r   r   4   r   zFalconRequestExtractor.cookiesc                 C      d S r   r   r   r   r   r   form8      zFalconRequestExtractor.formc                 C   r   r   r   r   r   r   r   files<   r    zFalconRequestExtractor.filesc                 C   s   |   }|dkr
dS d S )Nr   z[REQUEST_CONTAINING_RAW_DATA])content_length)r   r"   r   r   r   raw_data@   s   zFalconRequestExtractor.raw_datac                 C   s$   z| j jW S  tjjy   Y d S w r   )r   mediafalconerrorsHTTPBadRequestr   r   r   r   jsonO   s
   
zFalconRequestExtractor.jsonc                 C   s*   z| j jW S  tjjy   | j j Y S w r   )r   r$   r%   r&   r'   Z_mediar   r   r   r   r(   X   s
   
N)
__name__
__module____qualname__r   r   r   r!   r#   FALCON3r(   r   r   r   r   r   /   s    	r   c                   @   s   e Zd ZdZdd ZdS )SentryFalconMiddlewarez9Captures exceptions in Falcon requests and send to Sentryc                 O   s`   t j}|t}|d u rd S | }d|_|t|| W d    d S 1 s)w   Y  d S )Nr%   )r   currentget_integrationFalconIntegrationZconfigure_scope_nameZadd_event_processor_make_request_event_processor)r   reqrespargskwargshubintegrationscoper   r   r   process_requestg   s   

"z&SentryFalconMiddleware.process_requestN)r)   r*   r+   __doc__r:   r   r   r   r   r-   d   s    r-   uri_templatepathc                   @   s*   e Zd ZdZdZd	ddZedd ZdS )
r0   r%    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)r   rB   r   r   r   __init__{   s   
zFalconIntegration.__init__c                  C   sD   t t} | d u rtdt| dk rtdt  t  t  d S )NzUnparsable Falcon version: {})      zFalcon 1.4 or newer required.)r   FALCON_VERSIONr   format_patch_wsgi_app_patch_handle_exception_patch_prepare_middleware)versionr   r   r   
setup_once   s   
zFalconIntegration.setup_onceN)r=   )r)   r*   r+   
identifierrB   rC   staticmethodrL   r   r   r   r   r0   v   s    
	r0   c                        t j  fdd} | t _d S )Nc                    s@   t j}|t}|d u r ||S t fdd}|||S )Nc                    s    | |S r   r   )ZenviZ
start_resp)original_wsgi_appr   r   r   <lambda>   s    zB_patch_wsgi_app.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>)r   r.   r/   r0   r   )r   r   Zstart_responser7   r8   Zsentry_wrappedrP   r   r   sentry_patched_wsgi_app   s   

z0_patch_wsgi_app.<locals>.sentry_patched_wsgi_app)falcon_app_class__call__)rS   r   rR   r   rH      s   
rH   c                     rO   )Nc           
         s   d  }}t   tdd |D }tdd |D }W d    n1 s$w   Y   | g|R  }|d u s9|d u r;|S tj}|t}|d urct||rc|j}t||j	dddd\}}	|j
||	d |S )	Nc                 s   s    | ]
}t |tr|V  qd S r   )
isinstance	Exception.0argumentr   r   r   	<genexpr>   s    zS_patch_handle_exception.<locals>.sentry_patched_handle_exception.<locals>.<genexpr>c                 s   s     | ]}t |tjr|V  qd S r   )rV   r%   ResponserX   r   r   r   r[      s    
r%   F)typeZhandled)Zclient_optionsZ	mechanism)hint)r	   nextr   r.   r/   r0   _exception_leads_to_http_5xxclientr
   optionsZcapture_event)
r   r5   exresponseZwas_handledr7   r8   ra   eventr^   Zoriginal_handle_exceptionr   r   sentry_patched_handle_exception   s*   


z@_patch_handle_exception.<locals>.sentry_patched_handle_exception)rT   Z_handle_exception)rg   r   rf   r   rI      s   
$rI   c                     s    t j 	d fdd	} | t _d S )NFc                    sB   |r | ||S t j}|t}|d urt g| pg  }  | |S r   )r   r.   r/   r0   r-   )Z
middlewareZindependent_middlewareZasgir7   r8   Zoriginal_prepare_middlewarer   r   !sentry_patched_prepare_middleware   s   

zD_patch_prepare_middleware.<locals>.sentry_patched_prepare_middleware)NFF)falcon_helpersZprepare_middleware)ri   r   rh   r   rJ      s   
rJ   c                 C   sH   t | tjo| jp
dd}t | tjtjjf }|s|o#t p#t|S )Nr?   5)	rV   r%   	HTTPErrorstatus
startswithZhttp_status
HTTPStatusr,   _has_http_5xx_status)rc   rd   Zis_server_errorZis_unhandled_errorr   r   r   r`      s   	r`   c                 C   s   | j dS )Nrk   )rm   rn   )rd   r   r   r   rp      s   rp   c                 C   s.   |j |jd}|| | d< dt| i| d< d S )Nr<   ZtransactionsourceZtransaction_info)r=   r>   r   )re   rB   r   Zname_for_styler   r   r    _set_transaction_name_and_source  s
   rr   c                    s    fdd}|S )Nc                    sH   t |  j t  t|  W d    | S 1 sw   Y  | S r   )rr   rB   r	   r   Zextract_into_event)re   r^   r8   r3   r   r   event_processor  s   
z6_make_request_event_processor.<locals>.event_processorr   )r3   r8   rt   r   rs   r   r2     s   	r2   )2
__future__r   Zsentry_sdk.hubr   Zsentry_sdk.integrationsr   r   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk.integrations.wsgir   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr	   r
   r   Zsentry_sdk._typesr   typingr   r   r   r   r   r%   r   rF   ImportErrorZfalcon.app_helpersZapp_helpersrj   ZApprT   r,   Zfalcon.api_helpersZapi_helpersZAPIr   objectr-   r@   r0   rH   rI   rJ   r`   rp   rr   r2   r   r   r   r   <module>   sR    5+
