
    \j=$                     l   U 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r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Zd dlmZ 	 d dlZej:                  Zej>                  Z dZ!dZ%de&d<   e!r e       5  d dl'm(Z% ddd        G d de      Z) G d d      Z*dZ+ G d de      Z,d*dZ-d*dZ.d*dZ/de0dd de1fd!Z2dd de1fd"Z3d#d$d%e4d&d'ddfd(Z5	 	 	 	 	 	 d+d)Z6y# e$ r	  ed      w xY w# e$ r! d dl"ZejF                  ZejH                  Z dZ!Y w xY w# 1 sw Y   xY w),    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 installedTFzOptional[object]_FALCON_UNSET)_UNSETc                   <    e Zd ZddZddZd	dZd	dZd
dZddZy)FalconRequestExtractorc                 .    | j                   j                  S N)requestenvselfs    H/root/env/lib/python3.12/site-packages/sentry_sdk/integrations/falcon.pyr   zFalconRequestExtractor.env5   s    ||    c                 .    | j                   j                  S r   )r   cookiesr   s    r   r!   zFalconRequestExtractor.cookies8   s    ||###r   Nc                      y r    r   s    r   formzFalconRequestExtractor.form;       r   c                      y r   r#   r   s    r   fileszFalconRequestExtractor.files>   r%   r   c                 0    | j                         }|dkD  ryy )Nr   z[REQUEST_CONTAINING_RAW_DATA])content_length)r   r)   s     r   raw_datazFalconRequestExtractor.raw_dataA   s     
 ,,.A2r   c                     d }t               5  | j                  j                  }d d d        |t        ur|S y # 1 sw Y   xY wr   )r	   r   _mediar   )r   cached_medias     r   jsonzFalconRequestExtractor.jsonL   sF    (* 	/  <<..L	/ },	/ 	/s	   7A )returnzDict[str, Any]r/   N)r/   zOptional[str])r/   zOptional[Dict[str, Any]])	__name__
__module____qualname__r   r!   r$   r'   r*   r.   r#   r   r   r   r   4   s      $	r   r   c                   ,    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddZy)SentryFalconMiddlewarez9Captures exceptions in Falcon requests and send to SentryNc                     t        j                         j                  t              }|y t        j                         }d|_        |j                  t        ||             y )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scopes          r   process_requestz&SentryFalconMiddleware.process_requesta   sR     !++-==>OP..0!!"?["QRr   )
r@   r   rA   r   rB   r   rC   r   r/   N)r1   r2   r3   __doc__rF   r#   r   r   r5   r5   ^   s8    C	S	S %	S.3	S?D	S		Sr   r5   uri_templatepathc                   B    e Zd ZdZde ZdZd	deddfdZed
d       Z	y)r;   r7   z
auto.http. transaction_styler/   Nc                 N    |t         vrt        d|dt         d      || _        y )Nz%Invalid value for transaction_style: z (must be in ))TRANSACTION_STYLE_VALUES
ValueErrorrM   )r   rM   s     r   __init__zFalconIntegration.__init__v   s.    $<<$&>@  "3r   c                  ~    t        t              } t        t        |        t	                t                t                y r   )r   FALCON_VERSIONr   r;   _patch_wsgi_app_patch_handle_exception_patch_prepare_middleware)versions    r   
setup_oncezFalconIntegration.setup_once~   s)    /0':!!#r   )rI   r0   )
r1   r2   r3   
identifieroriginrM   strrR   staticmethodrY   r#   r   r   r;   r;   p   s@    J*&F3# 34 3 $ $r   r;   r/   c                  X    t         j                  	 	 	 	 	 	 	 	 dfd} | t         _        y )Nc                      t        j                         j                  t              }|
  ||      S t	         fdt        j
                        } |||      S )Nc                      | |      S r   r#   )envi
start_resporiginal_wsgi_appr   s     r   <lambda>zB_patch_wsgi_app.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>   s    %6tT:%N r   )span_origin)r8   r9   r:   r;   r   r[   )r   r   start_responserD   sentry_wrappedrc   s   `    r   sentry_patched_wsgi_appz0_patch_wsgi_app.<locals>.sentry_patched_wsgi_app   sY     !++-==>OP$T3??-N)00

 c>22r   )r   
falcon.APIr   r   rf   r   r/   r   )falcon_app_class__call__)rh   rc   s    @r   rU   rU      s?    (1133!&38=3	3 !8r   c                  n    t         j                  t        t              dfd       } | t         _        y )Nc                 Z   d x}}t               5  t        d |D              }t        d |D              }d d d         | g| }|||S t        ||      rGt        |t	        j
                         j                  ddd      \  }}t	        j                  ||       |S # 1 sw Y   mxY w)Nc              3   B   K   | ]  }t        |t              s|  y wr   )
isinstance	Exception.0arguments     r   	<genexpr>zS_patch_handle_exception.<locals>.sentry_patched_handle_exception.<locals>.<genexpr>   s     W8z(I7VhWs   c              3   V   K   | ]!  }t        |t        j                        s| # y wr   )ro   r7   Responserq   s     r   rt   zS_patch_handle_exception.<locals>.sentry_patched_handle_exception.<locals>.<genexpr>   s"      %Hfoo1Vs   ))r7   F)typehandled)client_options	mechanism)hint)r	   next_exception_leads_to_http_5xxr   r8   r9   optionscapture_event)r   rB   exresponsewas_handledeventr{   original_handle_exceptions          r   sentry_patched_handle_exceptionz@_patch_handle_exception.<locals>.sentry_patched_handle_exception   s    
 X(* 	WtWWB )- H	 0<t<:) 'H5.)446>>#+>KE4
 $$U6-	 	s   %B!!B*)r   ri   rB   r   r/   r   )rj   _handle_exceptionr
   r;   )r   r   s    @r   rV   rV      s7     0 B B 13LM N: *I&r   c            	      h    t         j                  	 	 	 ddddddt        ddffd} | t         _        y )N
middlewarer   independent_middlewareasgir/   c                     |r
 | ||      S t        j                         j                  t              }|t	               g| xs g z   }  | |      S r   )r8   r9   r:   r;   r5   )r   r   r   rD   original_prepare_middlewares       r   !sentry_patched_prepare_middlewarezD_patch_prepare_middleware.<locals>.sentry_patched_prepare_middleware   s^    
 .z;QSWXX ++-==>OP"023z7GRHJ +:7MNNr   )NFF)falcon_helpersprepare_middlewarebool)r   r   s    @r   rW   rW      sS    "0"C"C !(-OO %O O 
	O" )JN%r   r   r   zfalcon.Responsec                    t        | t        j                        xr | j                  xs dj	                  d      }t        | t        j                  t        j
                  j                  f       }|xs |xr t         xs t        |      S )NrL   5)	ro   r7   	HTTPErrorstatus
startswithhttp_status
HTTPStatusFALCON3_has_http_5xx_status)r   r   is_server_erroris_unhandled_errors       r   r}   r}      s     V%5%56 BIIO;W;W<O (
Vv11<<=  11 5+H5r   c                 8    | j                   j                  d      S )Nr   )r   r   )r   s    r   r   r      s    ??%%c**r   r   r   rM   r   falcon.Requestc                 b    |j                   |j                  d}||   | d<   dt        |   i| d<   y )NrH   transactionsourcetransaction_info)rI   rJ   r   )r   rM   r   name_for_styles       r    _set_transaction_name_and_sourcer      sD      ,,N **;<E-!)+;<M+N OE
r   c                      d fd}|S )Nc                     t        | j                         t               5  t              j	                  |        d d d        | S # 1 sw Y   | S xY wr   )r   rM   r	   r   extract_into_event)r   r{   rD   r@   s     r   event_processorz6_make_request_event_processor.<locals>.event_processor   sO    (0M0MsS(* 	B"3'::5A	B 	B s   AA)r   r   r{   zdict[str, Any]r/   r   r#   )r@   rD   r   s   `` r   r?   r?      s     r   r0   )r@   r   rD   r;   r/   r   )7r8   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   r7   r   rT   ImportErrorfalcon.app_helpersapp_helpersr   Apprj   r   falcon.api_helpersapi_helpersAPIr   __annotations__falcon.requestr   r   r5   rP   r;   rU   rV   rW   rp   r   r}   r   r\   r   r?   r#   r   r   <module>r      s    U U A = /  !7
/4''NzzG %)! (
	$	& ;:;'- 'TS S 4 $ $08(!IHJ.Y :K PT $+#4 + +PP'*P5EP	P	(;w  /
-
../  ''NzzG; ;s*   
C0 D 
D*0C>#D'&D'*D3