
    \j>                     4   d dl Z d dlmZ d dlmZ d dl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 d dlmZ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 d dlmZm Z m!Z!m"Z" erId dlm#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z, d dl-m.Z.m/Z/ d dlm0Z0  e+d      Z1 e+d      Z2 e+d      Z3 G d de)      Z4 ed      Z5dZ6d4de7de7de7de7fdZ8	 d5ddde9de7fd Z: G d! d"      Z;	 d6d#dd$d%d&e7d'dd(d)ddfd*Z<d7d+Z=d8d,Z>d9d-Z? G d. d/      Z@ddde9dd0fd1ZA	 d5ddde9dd2fd3ZBy):    N)partial)TYPE_CHECKING)_get_headersget_host)continue_trace)OP)DEFAULT_HTTP_METHODS_TO_CAPTURE_filter_headersnullcontext)should_send_default_piiuse_isolation_scope)track_session)StreamedSpanSegmentSource)SpanTransactionSource)has_span_streaming_enabled)
ContextVarcapture_internal_exceptionsevent_from_exceptionreraise)
AnyCallableContextManagerDictIteratorOptionalProtocolTupleTypeVarUnion)EventEventProcessor)ExcInfoWsgiResponseIterWsgiResponseHeadersWsgiExcInfoc            	       (    e Zd Z	 d
deddddddfd	Zy)StartResponseNstatusresponse_headersr&   exc_infoOptional[WsgiExcInfo]returnr%   c                      y N )selfr*   r+   r,   s       F/root/env/lib/python3.12/site-packages/sentry_sdk/integrations/wsgi.py__call__zStartResponse.__call__0   s         r0   )__name__
__module____qualname__strr4   r1   r5   r3   r)   r)   /   s2    
 15			 4	 .		
  	r5   r)   sentry_wsgi_middleware_appliedzgeneric WSGI requestscharseterrorsr.   c                 D    | j                  d      j                  ||      S )Nlatin1)encodedecode)r;   r<   r=   s      r3   wsgi_decoding_dancerB   =   s    88H$$Wf55r5   environDict[str, str]use_x_forwarded_forc                 6   | j                  dd      j                  d      }| j                  dd      j                  d      }| d| }| j                  d      }|r| j                  d|      }|dt        | |      dt	        |      j                  d      S )zPReturn the absolute URL without query string for the given WSGI
    environment.SCRIPT_NAME /	PATH_INFOwsgi.url_schemeHTTP_X_FORWARDED_PROTOz://)getrstriplstripr   rB   )rC   rE   script_name	path_infopathschemes         r3   get_request_urlrT   A   s    
 ++mR077<KK,33C8I]!I;'D[[*+F5v> 	-.D!((- r5   c                   H    e Zd ZdZddefdddededd	d
df
dZ	 	 	 	 	 	 ddZy)SentryWsgiMiddlewareapprE   span_originhttp_methods_to_captureFmanualrX   z3Callable[[Dict[str, str], Callable[..., Any]], Any]rE   rY   rZ   zTuple[str, ...]r.   Nc                 <    || _         || _        || _        || _        y r0   rW   )r2   rX   rE   rY   rZ   s        r3   __init__zSentryWsgiMiddleware.__init__]   s#     #6 &'>$r5   c                 `
   t         j                  d      r| j                  ||      S t        j                         }t        |j                        }t         j                  d       	 t        j                         5 }t        |d      5  t               5  |j                          d|_        |j                  t        || j                               d d d        |j                  dd      j!                         }d }|| j"                  v r|rt        j$                  j'                  t)        t+        |                   |j-                  d|i       t        j$                  j/                  t0        t2        j4                  | j6                  t8        j:                  d	
      }nSt'        |t8        j:                  t0        t<        j4                  | j6                        }t        j>                  |d|i      }|xs
 tA               }|5 }	tC        |	tD              rRt               5  tG        || j                        jI                         D ]  \  }
}|	jK                  |
|        	 d d d        	 | j                  |tM        tN        ||	            }tC        |	tD              r|	jV                  t0        k7  xrl |	jY                         j                  d      t2        jZ                  j\                  t2        j4                  j\                  t2        j^                  j\                  fv }|sHt               5  t0        |	_+        |	jK                  dt2        j4                  j\                         d d d        d d d        d d d        d d d        t         j                  d       |j                  d      rta        dd       |S tc              S # 1 sw Y   xY w# 1 sw Y   mxY w# tP        $ r tS        tU                 Y jw xY w# 1 sw Y   xY w# tC        |	tD              r|	jV                  t0        k7  xrl |	jY                         j                  d      t2        jZ                  j\                  t2        j4                  j\                  t2        j^                  j\                  fv }|sUt               5  t0        |	_+        |	jK                  dt2        j4                  j\                         d d d        w # 1 sw Y   w xY ww w xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# t         j                  d       w xY w)NFTrequest)session_modewsgiREQUEST_METHODrH   wsgi_environ)sentry.span.sourcezsentry.originz	sentry.op)name
attributes)opre   sourceorigin)custom_sampling_contextrd   zwsgi.file_wrapperfileno)2_wsgi_middleware_appliedrM   rX   
sentry_sdk
get_clientr   optionssetisolation_scoper   r   clear_breadcrumbs_nameadd_event_processor_make_wsgi_event_processorrE   upperrZ   tracesr   dictr   set_custom_sampling_context
start_span_DEFAULT_TRANSACTION_NAMEr   ROUTErY   r   HTTP_SERVERr   start_transactionr   
isinstancer   _get_request_attributesitemsset_attributer   _sentry_start_responseBaseExceptionr   _capture_exceptionre   get_attributes	COMPONENTvalueCUSTOMgetattr_ScopedResponse)r2   rC   start_responseclientspan_streamingscopemethodspan_ctxtransactionspanattrr   responsealready_sets                 r3   r4   zSentryWsgiMiddleware.__call__i   s|    $''.88G^44&&(3FNNC $$T*P	0++- M*"5yA L*46 //1&,116 ')A)A %[[)92>DDFF\`H!=!==)&--<< $\'%: ; "==~w>WX'1'8'8'C'C%>:G:M:M595E5E13," (D (H +9 '#%>>%>'8'>'>'+'7'7+K (2'C'C +9G8Q(H
  (8;=H!  *T%dL9!<!> D3J$+T-E-E4""'%'!DKD% %)$6$6tU$C!DD*'+xx ' '(>PT U(H  *$=$(II1J$J %&(,(;(;(=(A(ABV(W(5(?(?(E(E(5(;(;(A(A(5(<(<(B(B(&)& !, (3)D)F %*4M	(,(:(:,@,9,?,?,E,E)*%*7 *YL*M*^ %((/( KK+,(D1=Ouh//M ZD D  - ;#%7%9:;%* %*  *$=$(II1J$J %&(,(;(;(=(A(ABV(W(5(?(?(E(E(5(;(;(A(A(5(<(<(B(B(&)& !, (3)D)F %*4M	(,(:(:,@,9,?,?,E,E)*%* %* %* (3  >! *  *YL* L*M* M*^ %((/s   'T ;T		S<=N;D1S<S/?O	S/&!OBS/$6O5	S/"S<*T	2T ;O S<OS/O2	.P1O2	2P5O>:S/BS,	6S	S,	S'#	S,	,S//S94S<<T	T		TT T-)rC   rD   r   zCallable[..., Any]r.   r   )	r6   r7   r8   	__slots__r	   boolr9   r]   r4   r1   r5   r3   rV   rV   U   sj    I %*#5T
?B
? "
? 	
?
 "3
? 

?s0's09Ms0	s0r5   rV   old_start_responser   z#Optional[Union[Span, StreamedSpan]]r*   r+   r,   r-   c                 .   t               5  t        |j                  dd      d         }|Bt        |t              r!|dk\  rdnd|_        |j                  d|       n|j                  |       d d d        |	 | ||      S  | |||      S # 1 sw Y   xY w)N    r   i  errorokzhttp.response.status_code)r   intsplitr   r   r*   r   set_http_status)r   r   r*   r+   r,   
status_ints         r3   r   r      s     
%	& 1c1-a01
$-)3s):g""#>
K$$Z01  "&*:;;!&*:HEE1 1s   A#BBc              #   ^   K   ddg}t               r|dgz  }|D ]  }|| v s|| |   f  yw)z
    Returns our explicitly included environment variables we want to
    capture (server name, port and remote addr if pii is enabled).
    SERVER_NAMESERVER_PORTREMOTE_ADDRN)r   )rC   keyskeys      r3   _get_environr      sK     
 =)D  	 $'>ws|##$s   --c                     	 | d   j                  d      d   j                         S # t        t        f$ r Y nw xY w	 | d   S # t        $ r Y nw xY w| j	                  d      S )z
    Infer the user IP address from various headers. This cannot be used in
    security sensitive situations since the value may be forged from a client,
    but it's good enough for the event payload.
    HTTP_X_FORWARDED_FOR,r   HTTP_X_REAL_IPr   )r   stripKeyError
IndexErrorrM   )rC   s    r3   get_client_ipr     sw    -.44S9!<BBDDj! '((  ;;}%%s   $' 99A 	AAc                  
   t        j                         } | d   }t        |t              xr |j                  dv }|sGt        | t        j                         j                  ddd      \  }}t        j                  ||       | S )z
    Captures the current exception and sends it to Sentry.
    Returns the ExcInfo tuple to it can be reraised afterwards.
    r   )r   Nra   F)typehandled)client_options	mechanism)hint)
sysr,   r   
SystemExitcoder   rm   rn   ro   capture_event)r,   eshould_skip_captureeventr   s        r3   r   r     sz    
 ||~HA %Q
3K)8K*%002::%%8
t
 	  T2Or5   c                   8    e Zd ZdZdZ	 	 	 	 	 	 ddZddZd	dZy)
r   aY  
    Users a separate scope for each response chunk.

    This will make WSGI apps more tolerant against:
    - WSGI servers streaming responses from a different thread/from
      different threads than the one that called start_response
    - close() not being called
    - WSGI servers streaming responses interleaved from the same thread
    )	_response_scopeNc                      || _         || _        y r0   )r   r   )r2   r   r   s      r3   r]   z_ScopedResponse.__init__=  s     !r5   c              #     K   t        | j                        }	 t        | j                        5  	 t	        |      }d d d         /# t
        $ r Y d d d        y t        $ r t        t                 Y :w xY w# 1 sw Y   ?xY wwr0   )	iterr   r   r   nextStopIterationr   r   r   )r2   iteratorchunks      r3   __iter__z_ScopedResponse.__iter__C  s     '$T[[1 33 NE3 K  % 	3 3
 % 3/1233 3sJ   ,BA9AB	A6A9	BA63A95A66A99B>Bc                     t        | j                        5  	 | j                  j                          d d d        y # t        $ r Y t
        $ r t        t                 Y .w xY w# 1 sw Y   y xY wr0   )r   r   r   closeAttributeErrorr   r   r   )r2   s    r3   r   z_ScopedResponse.closeQ  se     - 	//$$&	/ 	/ "   /+-./	/ 	/s2   A$;	A!A$A!A$ A!!A$$A-)r   zsentry_sdk.scope.Scoper   Iterator[bytes]r.   N)r.   r   )r.   N)r6   r7   r8   __doc__r   r]   r   r   r1   r5   r3   r   r   0  s5     (I"-"9J"	"/r5   r   r#   c                     t        |       t        | |      | j                  d      | j                  d      t        t	        |             t        t        t        |                   dfd}|S )NQUERY_STRINGrb   c                     t               5  | j                  di       }t               r&| j                  di       }r|j                  d       	|d<   |d<   |d<   |d<   |d<   d d d        | S # 1 sw Y   | S xY w)	Nr_   user
ip_addressurlquery_stringr   envheaders)r   
setdefaultr   )
r   r   request_info	user_info	client_ipr   r   r   r   request_urls
       r3   event_processorz3_make_wsgi_event_processor.<locals>.event_processorq  s    (* 	. ++Ir:L&(!,,VR8	((yA"-L+7L(%+L""%L&-L#	. 	. s   AA22A<)r   r"   r   Dict[str, Any]r.   r"   )r   rT   rM   rx   r   r
   r   )	rC   rE   r   r   r   r   r   r   r   s	      @@@@@@r3   ru   ru   [  sn     g&I!'+>?K;;~.L[[)*F
|G$
%Cd<#89:G $ r5   r   c                 B   i }| j                  d      }|r|j                         |d<   t        t        t	        |             d      }|j                         D ]  \  }}||d|j                          <    | j                  d      }|r||d<   t        | |      |d<   | j                  d	      }|r||d
<   | j                  d      }	|	r|	|d<   | j                  d      }
|
r	 t        |
      |d<   t               rt        |       }|r
||d<   ||d<   |S # t        $ r Y .w xY w)zS
    Return span attributes related to the HTTP request from the WSGI environ.
    rb   zhttp.request.methodF)use_annotated_valuezhttp.request.header.r   z
http.queryzurl.fullrK   znetwork.protocol.namer   zserver.addressr   zserver.portzclient.addresszuser.ip_address)rM   rv   r
   rx   r   r   lowerrT   r   
ValueErrorr   r   )rC   rE   rf   r   r   headerr   r   
url_schemeserver_nameserver_portr   s               r3   r   r     sH    $&J[[)*F,2LLN
()d<#89uUG  D>C
)&,,.)9:;D ;;~.L#/
< ,W6IJJz./J.8
*+++m,K'2
#$++m,K	(+K(8J}%  !'*	+4J'(,5J()  		s   !D 	DD)zutf-8replace)Fr0   )rC   rD   r.   zIterator[Tuple[str, str]])rC   rD   r.   zOptional[Any])r.   r$   )Cr   	functoolsr   typingr   rm   sentry_sdk._werkzeugr   r   sentry_sdk.apir   sentry_sdk.constsr   $sentry_sdk.integrations._wsgi_commonr	   r
   r   sentry_sdk.scoper   r   sentry_sdk.sessionsr   sentry_sdk.tracesr   r   sentry_sdk.tracingr   r   sentry_sdk.tracing_utilsr   sentry_sdk.utilsr   r   r   r   r   r   r   r   r   r   r   r   r    r!   sentry_sdk._typesr"   r#   r$   r%   r&   r'   r)   rl   r{   r9   rB   r   rT   rV   r   r   r   r   r   ru   r   r1   r5   r3   <module>r      s   
     7 )   
 J - 9 6 ?     8(12!"78-(K  &&FG 2 63 6 6 6TW 6
 <A48(G0 G0^ )-F'F
/F F ,	F
 &F F2$ &&*(/ (/V((48((Z !&,,, ,r5   