o
    i)                     @   s  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mZmZ erd dl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m)Z) e%dZ*e%dZ+e%dZ,G dd de#Z-edZ.d<de/de/de/de/fddZ0	d=ddd e1de/fd!d"Z2G d#d$ d$Z3	d>d%dd&d'd(e/d)dd*d+ddfd,d-Z4d?d/d0Z5d@d2d3Z6dAd5d6Z7G d7d8 d8Z8ddd e1dd9fd:d;Z9dS )B    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)TransactionTransactionSource)
ContextVarcapture_internal_exceptionsevent_from_exceptionreraise)AnyCallableDictIteratorOptionalProtocolTupleTypeVar)EventEventProcessor)ExcInfoWsgiResponseIterWsgiResponseHeadersWsgiExcInfoc                	   @   s*   e Zd Z	ddeddddddfd	d
ZdS )StartResponseNstatusresponse_headersr    exc_infoOptional[WsgiExcInfo]returnr   c                 C   s   d S N )selfr#   r$   r%   r)   r)   Y/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/sentry_sdk/integrations/wsgi.py__call__#   s   zStartResponse.__call__r(   )__name__
__module____qualname__strr,   r)   r)   r)   r+   r"   "   s    r"   sentry_wsgi_middleware_appliedutf-8replacescharseterrorsr'   c                 C   s   |  d||S )Nlatin1)encodedecode)r4   r5   r6   r)   r)   r+   wsgi_decoding_dance/   s   r:   FenvironDict[str, str]use_x_forwarded_forc                 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   r:   )r;   r=   script_name	path_infopathr)   r)   r+   get_request_url3   s   rH   c                   @   sL   e Zd ZdZddefdddededd	d
df
ddZ					
	dddZdS )SentryWsgiMiddlewareappr=   span_originhttp_methods_to_captureFmanualrK   z3Callable[[Dict[str, str], Callable[..., Any]], Any]r=   rL   rM   zTuple[str, ...]r'   Nc                 C   s   || _ || _|| _|| _d S r(   rJ   )r*   rK   r=   rL   rM   r)   r)   r+   __init__K   s   
zSentryWsgiMiddleware.__init__r;   r<   start_responseCallable[..., Any]_ScopedResponsec                 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_appliedrB   rK   set
sentry_sdkisolation_scoper   r   clear_breadcrumbs_nameadd_event_processor_make_wsgi_event_processorr=   upperrM   r   r   HTTP_SERVERr   ROUTErL   start_transactionr
   r   _sentry_start_responseBaseExceptionr   _capture_exceptionrR   )r*   r;   rP   scopemethodtransactiontransaction_contextresponser)   r)   r+   r,   W   sh   


	
)
zSentryWsgiMiddleware.__call__)r;   r<   rP   rQ   r'   rR   )	r-   r.   r/   	__slots__r   boolr0   rO   r,   r)   r)   r)   r+   rI   C   s.    

rI   old_start_responsern   zOptional[Transaction]r#   r$   r%   r&   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)rs   rn   r#   r$   r%   
status_intr)   r)   r+   ri      s   

ri   Iterator[Tuple[str, str]]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_ADDRN)r   )r;   keyskeyr)   r)   r+   _get_environ   s   
r   Optional[Any]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_IPr}   )rw   stripKeyError
IndexErrorrB   )r;   r)   r)   r+   get_client_ip   s   

r   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.
    ru   )r   NrU   F)typehandled)client_options	mechanism)hint)
sysr%   
isinstance
SystemExitcoder   r_   
get_clientoptionscapture_event)r%   eshould_skip_captureeventr   r)   r)   r+   rk      s   
rk   c                   @   s>   e Zd ZdZdZ						dd	d
ZdddZdddZdS )rR   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_scoperl   sentry_sdk.scope.Scoperp   Iterator[bytes]r'   Nc                 C   s   || _ || _d S r(   )r   r   )r*   rl   rp   r)   r)   r+   rO      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StopIterationrj   r   rk   )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AttributeErrorrj   r   rk   )r*   r)   r)   r+   r      s   "z_ScopedResponse.close)rl   r   rp   r   r'   N)r'   r   )r'   N)r-   r.   r/   __doc__rq   rO   r   r   r)   r)   r)   r+   rR      s    


rR   r   c                    s^   t |  t| || d| dtt| ttt| d
 fdd	}|S )NQUERY_STRINGrV   r   r   r   Dict[str, Any]r'   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 )	NrS   user
ip_addressurlquery_stringrm   envheaders)r   
setdefaultr   )r   r   request_info	user_info	client_ipr   r   rm   r   request_urlr)   r+   event_processor  s   

z3_make_wsgi_event_processor.<locals>.event_processor)r   r   r   r   r'   r   )r   rH   rB   dictr   r	   r   )r;   r=   r   r)   r   r+   rd     s   


rd   )r2   r3   )Fr(   )r;   r<   r'   rz   )r;   r<   r'   r   )r'   r   ):r   	functoolsr   typingr   r_   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.tracingr   r   sentry_sdk.utilsr   r   r   r   r   r   r   r   r   r   r   r   sentry_sdk._typesr   r   r   r   r    r!   r"   r]   r0   r:   rr   rH   rI   ri   r   r   rk   rR   rd   r)   r)   r)   r+   <module>   sp    (

O



+