o
    hm%                     @   s  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
 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 d d
lmZmZ d dl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' e$dZ(e$dZ)e$dZ*G dd de%Z+edZ,erd3dd Z-nd3d!d Z-d4d#d$Z.G d%d& d&e/Z0	d5d'd(Z1d)d* Z2d+d, Z3d-d. Z4G d/d0 d0e/Z5d1d2 Z6dS )6    N)PY2reraise)partial)TYPE_CHECKING)get_host_get_headers)continue_trace)OP)Hub_should_send_default_pii)
ContextVarcapture_internal_exceptionsevent_from_exception)TransactionTRANSACTION_SOURCE_ROUTE)auto_session_tracking)_filter_headers)Callable)Dict)Iterator)Any)Tuple)Optional)TypeVar)Protocol)ExcInfo)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/aiguide_backend/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   |  ||S r!   )decodescharseterrorsr"   r"   r'   wsgi_decoding_dance/   s   r4   c                 C   s   |  d||S )Nlatin1)encoder/   r0   r"   r"   r'   r4   5   s   Fc                 C   s.   d|  dt| |t|  dpddf S )zPReturn the absolute URL without query string for the given WSGI
    environment.z
%s://%s/%szwsgi.url_scheme	PATH_INFO /)getr   r4   lstrip)environuse_x_forwarded_forr"   r"   r'   get_request_url:   s
   r>   c                   @   s"   e Zd ZdZdddZdd ZdS )	SentryWsgiMiddlewareappr=   Fc                 C      || _ || _d S r!   r@   )r#   rA   r=   r"   r"   r'   __init__H      
zSentryWsgiMiddleware.__init__c                 C   s  t dr| ||S t d zttj}t|dd |} t , | }|	  d|_
|t|| j W d    n1 sCw   Y  W d    n1 sRw   Y  t|tjdtd}|j|d|id	$ 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wsgizgeneric WSGI request)opnamesourcewsgi_environ)custom_sampling_context)_wsgi_middleware_appliedr:   rA   setr
   currentr   r   configure_scopeclear_breadcrumbs_nameadd_event_processor_make_wsgi_event_processorr=   r   r	   HTTP_SERVERr   start_transactionr   _sentry_start_responseBaseExceptionr   _capture_exception_ScopedResponse)r#   r<   start_responsehubscopetransactionrvr"   r"   r'   r(   M   s^   




 
zSentryWsgiMiddleware.__call__NF)r)   r*   r+   	__slots__rC   r(   r"   r"   r"   r'   r?   E   s    
r?   c                 C   sb   t   t|ddd }|| W d    n1 sw   Y  |d u r+| ||S | |||S )N    r   )r   intsplitset_http_status)old_start_responser^   r$   r%   r&   
status_intr"   r"   r'   rW   z   s   
rW   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   
rn   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_IPrk   )re   stripKeyError
IndexErrorr:   )r<   r"   r"   r'   get_client_ip   s   

ru   c                 C   s`   t  }| jd ur.|d }t|to|jdv }|s.t|| jjdddd\}}| j||d |S )Nrc   )r   NrG   F)typehandled)client_options	mechanism)hint)	sysr&   client
isinstance
SystemExitcoder   optionscapture_event)r\   r&   eshould_skip_captureeventrz   r"   r"   r'   rY      s   

rY   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	rZ   )	_response_hubc                 C   rB   r!   )r   r   )r#   r\   responser"   r"   r'   rC      rD   z_ScopedResponse.__init__c              	   c   s    t | j}	 | j. zt|}W n ty!   Y W d    d S  ty0   tt| j  Y nw W d    n1 s;w   Y  |V  qr!   )iterr   r   nextStopIterationrX   r   rY   )r#   iteratorchunkr"   r"   r'   __iter__   s    
z_ScopedResponse.__iter__c              	   C   s   | j 9 z| j  W n ty   Y n ty#   tt| j   Y nw W d    d S W d    d S W d    d S 1 s?w   Y  d S r!   )r   r   closeAttributeErrorrX   r   rY   )r#   r"   r"   r'   r      s   "z_ScopedResponse.closeN)r)   r*   r+   ra   rC   r   r   r"   r"   r"   r'   rZ      s
    rZ   c                    s\   t |  t| || d| dtt| ttt|  fdd}|S )NQUERY_STRINGREQUEST_METHODc                    s   t  4 | di }t r| di } r|d  |d< |d< |d< |d< |d< W d    | S 1 s:w   Y  | S )	NrE   user
ip_addressurlquery_stringmethodenvheaders)r   
setdefaultr   )r   rz   request_info	user_info	client_ipr   r   r   r   request_urlr"   r'   event_processor   s   

z3_make_wsgi_event_processor.<locals>.event_processor)ru   r>   r:   dictrn   r   r   )r<   r=   r   r"   r   r'   rT      s   


rT   )r-   r.   r`   r!   )7r{   sentry_sdk._compatr   r   sentry_sdk._functoolsr   sentry_sdk._typesr   sentry_sdk._werkzeugr   r   sentry_sdk.apir   sentry_sdk.constsr	   sentry_sdk.hubr
   r   sentry_sdk.utilsr   r   r   sentry_sdk.tracingr   r   sentry_sdk.sessionsr   $sentry_sdk.integrations._wsgi_commonr   typingr   r   r   r   r   r   r   r   r   r   r   r   r   r    rM   r4   r>   objectr?   rW   rn   ru   rY   rZ   rT   r"   r"   r"   r'   <module>   sP    

:
"