o
    h%                     @   s  d dl 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mZmZmZ d dlmZ erNd 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ye   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 dZ%e!re  d dl&m'Z% W d   n1 sw   Y  G dd deZ(G dd dZ)dZ*G dd deZ+dd Z,dd Z-dd Z.dd Z/d d! Z0d"d# Z1d$d% Z2dS )&    N)_check_minimum_versionIntegrationDidNotEnable)RequestExtractor)SentryWsgiMiddleware)SOURCE_FOR_STYLE)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionparse_version)TYPE_CHECKING)Any)Dict)Optional)EventEventProcessor)__version__zFalcon not installedTF)_UNSETc                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )FalconRequestExtractorc                 C      | j jS N)requestenvself r   `/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/integrations/falcon.pyr   5      zFalconRequestExtractor.envc                 C   r   r   )r   cookiesr   r   r   r   r   9   r   zFalconRequestExtractor.cookiesc                 C      d S r   r   r   r   r   r   form=      zFalconRequestExtractor.formc                 C   r   r   r   r   r   r   r   filesA   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_dataE   s   zFalconRequestExtractor.raw_datac                 C   sB   d }t   | jj}W d    n1 sw   Y  |tur|S d S r   )r   r   _media_FALCON_UNSET)r   cached_mediar   r   r   jsonR   s   
	zFalconRequestExtractor.jsonN)	__name__
__module____qualname__r   r   r    r"   r$   r(   r   r   r   r   r   4   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  t}|d u rd S t  }d|_|t|| d S )Nfalcon)
sentry_sdk
get_clientget_integrationFalconIntegrationget_isolation_scope_nameadd_event_processor_make_request_event_processor)r   reqrespargskwargsintegrationscoper   r   r   process_requesth   s   z&SentryFalconMiddleware.process_requestN)r)   r*   r+   __doc__r<   r   r   r   r   r,   e   s    r,   uri_templatepathc                   @   s4   e Zd ZdZde ZdZd
ddZedd Zd	S )r1   r-   z
auto.http. 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   rD   r   r   r   __init__|   s   
zFalconIntegration.__init__c                  C   s(   t t} tt|  t  t  t  d S r   )r   FALCON_VERSIONr   r1   _patch_wsgi_app_patch_handle_exception_patch_prepare_middleware)versionr   r   r   
setup_once   s
   

zFalconIntegration.setup_onceN)r?   )	r)   r*   r+   
identifieroriginrD   rE   staticmethodrK   r   r   r   r   r1   v   s    

	r1   c                     s   t j  fdd} | t _d S )Nc                    sD   t  t}|d u r ||S t fddtjd}|||S )Nc                    s    | |S r   r   )envi
start_resp)original_wsgi_appr   r   r   <lambda>   s    zB_patch_wsgi_app.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>)span_origin)r.   r/   r0   r1   r   rM   )r   r   start_responser:   sentry_wrappedrQ   r   r   sentry_patched_wsgi_app   s   
z0_patch_wsgi_app.<locals>.sentry_patched_wsgi_app)falcon_app_class__call__)rW   r   rV   r   rG      s   
rG   c                     s&   t j tt  fdd} | t _d S )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||rVt|t jdddd\}}t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   )rZ   r-   Responser\   r   r   r   r_      s    
r-   F)typehandled)client_options	mechanism)hint)r   next_exception_leads_to_http_5xxr
   r.   r/   optionscapture_event)r   r8   exresponsewas_handledeventre   original_handle_exceptionr   r   sentry_patched_handle_exception   s$   


z@_patch_handle_exception.<locals>.sentry_patched_handle_exception)rX   _handle_exceptionr	   r1   )rp   r   rn   r   rH      s   
rH   c                     s    t j 	d fdd	} | t _d S )NFc                    s@   |r | ||S t  t}|d urt g| pg  }  | |S r   )r.   r/   r0   r1   r,   )
middlewareindependent_middlewareasgir:   original_prepare_middlewarer   r   !sentry_patched_prepare_middleware   s   
zD_patch_prepare_middleware.<locals>.sentry_patched_prepare_middleware)NFF)falcon_helpersprepare_middleware)rw   r   ru   r   rI      s   
rI   c                 C   sH   t | tjo| jp
dd}t | tjtjjf }|s|o#t p#t|S )NrA   5)	rZ   r-   	HTTPErrorstatus
startswithhttp_status
HTTPStatusFALCON3_has_http_5xx_status)rj   rk   is_server_erroris_unhandled_errorr   r   r   rg      s   	rg   c                 C   s   | j dS )Nrz   )r|   r}   )rk   r   r   r   r      s   r   c                 C   s.   |j |jd}|| | d< dt| i| d< d S )Nr>   transactionsourcetransaction_info)r?   r@   r   )rm   rD   r   name_for_styler   r   r    _set_transaction_name_and_source   s
   r   c                    s    fdd}|S )Nc                    sH   t |  j t  t|  W d    | S 1 sw   Y  | S r   )r   rD   r   r   extract_into_event)rm   re   r:   r6   r   r   event_processor  s   
z6_make_request_event_processor.<locals>.event_processorr   )r6   r:   r   r   r   r   r5     s   	r5   )3r.   sentry_sdk.integrationsr   r   r   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk.integrations.wsgir   sentry_sdk.tracingr   sentry_sdk.utilsr   r	   r
   r   typingr   r   r   r   sentry_sdk._typesr   r   r-   r   rF   ImportErrorfalcon.app_helpersapp_helpersrx   ApprX   r   falcon.api_helpersapi_helpersAPIr&   falcon.requestr   r   r,   rB   r1   rG   rH   rI   rg   r   r   r5   r   r   r   r   <module>   sZ    1&
