o
    0 i9*                     @   s  d dl 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 d dlmZmZm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mZmZ d dlmZ erd 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lm$Z$ d dlm%Z% d dlm&Z& d dl'm(Z(m)Z) e$dZ*e$dZ+e$dZ,G dd de%Z-edZ.d2dd Z/d3d"d#Z0G d$d% d%Z1	d4d&d'Z2d(d) Z3d*d+ Z4d,d- Z5G d.d/ d/Z6d0d1 Z7dS )5    N)partial)get_host_get_headers)continue_trace)OPshould_send_default_pii)DEFAULT_HTTP_METHODS_TO_CAPTURE_filter_headersnullcontext)track_session)use_isolation_scope)TransactionTransactionSource)
ContextVarcapture_internal_exceptionsevent_from_exceptionreraise)TYPE_CHECKING)Callable)Dict)Iterator)Any)Tuple)Optional)TypeVar)Protocol)ExcInfo)EventEventProcessorWsgiResponseIterWsgiResponseHeadersWsgiExcInfoc                   @   s   e Zd ZdddZdS )StartResponseNc                 C   s   d S N )selfstatusresponse_headersexc_infor%   r%   h/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/integrations/wsgi.py__call__,   s   zStartResponse.__call__r$   )__name__
__module____qualname__r+   r%   r%   r%   r*   r#   +   s    r#   Zsentry_wsgi_middleware_appliedutf-8replacec                 C   s   |  d||S )Nlatin1)encodedecode)scharseterrorsr%   r%   r*   wsgi_decoding_dance4   s   r7   Fc                 C   sV   |  ddd}|  ddd}| d| }d|  dt| |t|df S )zPReturn the absolute URL without query string for the given WSGI
    environment.ZSCRIPT_NAME /Z	PATH_INFOz
%s://%s/%szwsgi.url_scheme)getrstriplstripr   r7   )environuse_x_forwarded_forscript_nameZ	path_infopathr%   r%   r*   get_request_url9   s   rA   c                   @   s(   e Zd ZdZddefddZdd ZdS )	SentryWsgiMiddlewareappr>   span_originhttp_methods_to_captureFZmanualc                 C   s   || _ || _|| _|| _d S r$   rC   )r&   rD   r>   rE   rF   r%   r%   r*   __init__P   s   
zSentryWsgiMiddleware.__init__c                 C   s  t dr| ||S t d zt }t|dd t  |  d|_	|
t|| j W d    n1 s;w   Y  |dd }d }|| jv r[t|tjdtj| jd	}|d urhtj|d
|idnt }|# z| |tt||}W n ty   tt   Y nw W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  W t d nt d w t||S )NFTrequest)Zsession_modewsgiREQUEST_METHODr8   zgeneric WSGI request)opnamesourceoriginZwsgi_environ)Zcustom_sampling_context)_wsgi_middleware_appliedr:   rD   set
sentry_sdkZisolation_scoper   r   Zclear_breadcrumbs_nameZadd_event_processor_make_wsgi_event_processorr>   upperrF   r   r   ZHTTP_SERVERr   ZROUTErE   Zstart_transactionr   r   _sentry_start_responseBaseExceptionr   _capture_exception_ScopedResponse)r&   r=   Zstart_responsescopemethodtransactionZtransaction_contextresponser%   r%   r*   r+   ]   sh   


	
)
zSentryWsgiMiddleware.__call__N)r,   r-   r.   	__slots__r	   rG   r+   r%   r%   r%   r*   rB   H   s    

rB   c                 C   sj   t   t|ddd }|d ur|| W d    n1 s!w   Y  |d u r/| ||S | |||S )N    r   )r   intsplitZset_http_status)Zold_start_responser[   r'   r(   r)   Z
status_intr%   r%   r*   rU      s   

rU   c                 c   s>    ddg}t  r|dg7 }|D ]}|| v r|| | fV  qdS )z
    Returns our explicitly included environment variables we want to
    capture (server name, port and remote addr if pii is enabled).
    ZSERVER_NAMEZSERVER_PORTREMOTE_ADDRNr   )r=   keyskeyr%   r%   r*   _get_environ   s   
re   c              	   C   sX   z| d  dd  W S  ttfy   Y nw z| d W S  ty&   Y nw | 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.
    ZHTTP_X_FORWARDED_FOR,r   ZHTTP_X_REAL_IPrb   )ra   stripKeyError
IndexErrorr:   )r=   r%   r%   r*   get_client_ip   s   

rj   c                  C   sX   t  } | d }t|to|jdv }|s*t| t jdddd\}}tj	||d | S )z
    Captures the current exception and sends it to Sentry.
    Returns the ExcInfo tuple to it can be reraised afterwards.
    r_   )r   NrI   F)typeZhandled)Zclient_optionsZ	mechanism)hint)
sysr)   
isinstance
SystemExitcoder   rQ   Z
get_clientoptionsZcapture_event)r)   eZshould_skip_captureeventrl   r%   r%   r*   rW      s   
rW   c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
rX   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_scopec                 C   s   || _ || _d S r$   )ru   rt   )r&   rY   r\   r%   r%   r*   rG      s   
z_ScopedResponse.__init__c              	   c   s    t | j}	 t| j, zt|}W n ty#   Y W d    d S  ty0   tt   Y nw W d    n1 s;w   Y  |V  qr$   )	iterrt   r   ru   nextStopIterationrV   r   rW   )r&   iteratorchunkr%   r%   r*   __iter__   s    
z_ScopedResponse.__iter__c              	   C   s   t | j7 z| j  W n ty   Y n ty#   tt   Y nw W d    d S W d    d S W d    d S 1 s?w   Y  d S r$   )r   ru   rt   closeAttributeErrorrV   r   rW   )r&   r%   r%   r*   r|     s   "z_ScopedResponse.closeN)r,   r-   r.   __doc__r]   rG   r{   r|   r%   r%   r%   r*   rX      s    
rX   c                    s\   t |  t| || d| dtt| ttt|  fdd}|S )NZQUERY_STRINGrJ   c                    s   t  4 | di }t r| di } r|d  |d< |d< |d< |d< |d< W d    | S 1 s:w   Y  | S )	NrH   user
ip_addressurlquery_stringrZ   envheaders)r   
setdefaultr   )rs   rl   Zrequest_infoZ	user_infoZ	client_ipr   r   rZ   r   request_urlr%   r*   event_processor$  s   

z3_make_wsgi_event_processor.<locals>.event_processor)rj   rA   r:   dictre   r
   r   )r=   r>   r   r%   r   r*   rS     s   


rS   )r/   r0   )Fr$   )8rm   	functoolsr   rQ   Zsentry_sdk._werkzeugr   r   Zsentry_sdk.apir   Zsentry_sdk.constsr   Zsentry_sdk.scoper   Z$sentry_sdk.integrations._wsgi_commonr	   r
   r   Zsentry_sdk.sessionsr   r   Zsentry_sdk.tracingr   r   Zsentry_sdk.utilsr   r   r   r   typingr   r   r   r   r   r   r   r   r   r   Zsentry_sdk._typesr   r   r    r!   r"   r#   rO   r7   rA   rB   rU   re   rj   rW   rX   rS   r%   r%   r%   r*   <module>   sN    

O
,