o
    h)                     @   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%   ^/var/www/html/pro-man-master/venv/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#   sentry_wsgi_middleware_appliedutf-8replacec                 C   s   |  d||S )Nlatin1)encodedecode)scharseterrorsr%   r%   r*   wsgi_decoding_dance4   s   r8   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.SCRIPT_NAME /	PATH_INFOz
%s://%s/%szwsgi.url_scheme)getrstriplstripr   r8   )environuse_x_forwarded_forscript_name	path_infopathr%   r%   r*   get_request_url9   s   rE   c                   @   s(   e Zd ZdZddefddZdd ZdS )	SentryWsgiMiddlewareapprA   span_originhttp_methods_to_captureFmanualc                 C   s   || _ || _|| _|| _d S r$   rG   )r&   rH   rA   rI   rJ   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)session_modewsgiREQUEST_METHODr:   zgeneric WSGI request)opnamesourceoriginwsgi_environ)custom_sampling_context)_wsgi_middleware_appliedr=   rH   set
sentry_sdkisolation_scoper   r   clear_breadcrumbs_nameadd_event_processor_make_wsgi_event_processorrA   upperrJ   r   r   HTTP_SERVERr   ROUTErI   start_transactionr   r   _sentry_start_responseBaseExceptionr   _capture_exception_ScopedResponse)r&   r@   start_responsescopemethodtransactionresponser%   r%   r*   r+   ]   sf   


	
(
zSentryWsgiMiddleware.__call__N)r,   r-   r.   	__slots__r	   rL   r+   r%   r%   r%   r*   rF   H   s    

rF   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splitset_http_status)old_start_responserj   r'   r(   r)   
status_intr%   r%   r*   rc      s   

rc   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).
    SERVER_NAMESERVER_PORTREMOTE_ADDRNr   )r@   keyskeyr%   r%   r*   _get_environ   s   
ry   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.
    HTTP_X_FORWARDED_FOR,r   HTTP_X_REAL_IPrv   )rp   stripKeyError
IndexErrorr=   )r@   r%   r%   r*   get_client_ip   s   

r   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.
    rn   )r   NrO   F)typehandled)client_options	mechanism)hint)
sysr)   
isinstance
SystemExitcoder   rY   
get_clientoptionscapture_event)r)   eshould_skip_captureeventr   r%   r%   r*   re      s   
re   c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
rf   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$   )r   r   )r&   rh   rk   r%   r%   r*   rL      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$   )	iterr   r   r   nextStopIterationrd   r   re   )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   r   r   closeAttributeErrorrd   r   re   )r&   r%   r%   r*   r     s   "z_ScopedResponse.closeN)r,   r-   r.   __doc__rl   rL   r   r   r%   r%   r%   r*   rf      s    
rf   c                    s\   t |  t| || d| dtt| ttt|  fdd}|S )NQUERY_STRINGrP   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 )	NrM   user
ip_addressurlquery_stringri   envheaders)r   
setdefaultr   )r   r   request_info	user_info	client_ipr   r   ri   r   request_urlr%   r*   event_processor#  s   

z3_make_wsgi_event_processor.<locals>.event_processor)r   rE   r=   dictry   r
   r   )r@   rA   r   r%   r   r*   r^     s   


r^   )r0   r1   )Fr$   )8r   	functoolsr   rY   sentry_sdk._werkzeugr   r   sentry_sdk.apir   sentry_sdk.constsr   sentry_sdk.scoper   $sentry_sdk.integrations._wsgi_commonr	   r
   r   sentry_sdk.sessionsr   r   sentry_sdk.tracingr   r   sentry_sdk.utilsr   r   r   r   typingr   r   r   r   r   r   r   r   r   r   sentry_sdk._typesr   r   r    r!   r"   r#   rW   r8   rE   rF   rc   ry   r   re   rf   r^   r%   r%   r%   r*   <module>   sN    

N
,