o
    ZhXQ                  	   @   sN  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
Z
d dlZd dlmZmZmZ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 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$ d dlm%Z% d dl&m'Z' d dl&m(Z( d dlm)Z)m*Z* e!e+ Z,zd dl-m.Z. W n e/y   d dl0m.Z. Y nw g Z1ej2dd dfej3dd dfej3dd dfej3d d d!ffD ]Z4ze15e4d  e4d  e4d" f W q e6y   Y qw G d#d$ d$e7Z8d-d%d&Z9G d'd( d(e8Z:G d)d* d*e8Z;d+d, Z<dS ).    )print_functionN)	timedelta)defaultdict)Dsnloggercapture_internal_exceptions
json_dumps)BackgroundWorker)EnvelopeItem
PayloadRefdatetime_utcnow)TYPE_CHECKING)datetime)Any)Callable)Dict)Iterable)List)Optional)Tuple)Type)Union)DefaultDict)PoolManager)ProxyManager)EventEndpointType)
getproxiesc                   C   
   t tdS )NSO_KEEPALIVEgetattrsocket r%   r%   K/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/transport.py<lambda>/      
 r'      c                   C   r    )NTCP_KEEPIDLEr"   r%   r%   r%   r&   r'   0   r(   -   c                   C   r    )NTCP_KEEPINTVLr"   r%   r%   r%   r&   r'   1   r(   
   c                   C   r    )NTCP_KEEPCNTr"   r%   r%   r%   r&   r'   2   r(         c                   @   sb   e Zd ZdZdZ	dddZdd Zdd Z	dd	d
Zdd Z			dddZ
dd Zdd ZdS )	TransportzWBaseclass for all transports.

    A transport is used to send an event to sentry.
    Nc                 C   s:   || _ |r|d d ur|d rt|d | _d S d | _d S )Ndsn)optionsr   
parsed_dsn)selfr3   r%   r%   r&   __init__D   s   
zTransport.__init__c                 C      t  )zm
        This gets invoked with the event dictionary when an event should
        be sent to sentry.
        NotImplementedErrorr5   eventr%   r%   r&   capture_eventN   s   zTransport.capture_eventc                 C   r7   )a$  
        Send an envelope to Sentry.

        Envelopes are a data container format that can hold any type of data
        submitted to Sentry. We use it for transactions and sessions, but
        regular "error" events should go through `capture_event` for backwards
        compat.
        r8   )r5   enveloper%   r%   r&   capture_envelopeX   s   zTransport.capture_envelopec                 C      dS )z=Wait `timeout` seconds for the current events to be sent out.Nr%   r5   timeoutcallbackr%   r%   r&   flushf   s   zTransport.flushc                 C   r?   )zForcefully kills the transport.Nr%   r5   r%   r%   r&   killo   s   zTransport.killc                 C   r?   )zVThis increments a counter for event loss by reason and
        data category.
        Nr%   )r5   reasondata_categoryitemr%   r%   r&   record_lost_eventt   s   
zTransport.record_lost_eventc                 C   r?   )NTr%   rD   r%   r%   r&   
is_healthy      zTransport.is_healthyc                 C   s$   z|    W d S  ty   Y d S w N)rE   	ExceptionrD   r%   r%   r&   __del__   s
   zTransport.__del__rL   NN)__name__
__module____qualname____doc__r4   r6   r<   r>   rC   rE   rI   rJ   rN   r%   r%   r%   r&   r1   <   s    



	
r1   c              
   c   s    |d u rt  }| dD ]4}z'| dd\}}}|tt|d }|r+|dp,dD ]}||fV  q-W q ttfyA   Y qw d S )N,:r0   seconds;rL   )r   splitstripr   intLookupError
ValueError)headernowlimitretry_after
categories_categoryr%   r%   r&   _parse_rate_limits   s   re   c                   @   s   e Zd ZdZdd Z		d.ddZdd Z			d/d
dZdd Zd0ddZ	d1ddZ
dd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Z	d2d*d+Zd,d- ZdS )3HttpTransportzThe default HTTP transport.c                 C   s  ddl m} t| | | jd usJ || _t|d d| _| jd| | _	i | _
tj | _tt| _t | _|di d}|d u rIdnt|| _|di d	}|d u r]d
nt|| _| j| j|d |d |d |d d| _ddlm} || _d S )Nr   )VERSIONZtransport_queue_size)Z
queue_sizezsentry.python/%sZ_experimentsZ transport_zlib_compression_level	   Ztransport_num_poolsr0   
http_proxyhttps_proxyca_certsproxy_headers)ri   rj   rk   rl   )Hub)Zsentry_sdk.constsrg   r1   r6   r4   r3   r	   _workerZto_auth_auth_disabled_untilurllib3utilRetry_retryr   r[   _discarded_eventstime_last_client_report_sentget_compresslevel
_num_pools
_make_pool_poolZ
sentry_sdkrm   hub_cls)r5   r3   rg   compresslevel	num_poolsrm   r%   r%   r&   r6      s6   

zHttpTransport.__init__Nc                 C   sd   | j d sd S d}|d ur|j}|dkrt| pd}n|d u r%td| j||f  |7  < d S )Nsend_client_reportsr)   
attachmentzdata category not provided)r3   rG   len	get_bytes	TypeErrorru   )r5   rF   rG   rH   quantityr%   r%   r&   rI      s   
zHttpTransport.record_lost_eventc                 C   sl   |j d}|rtd | jt| d S |jdkr4td t t	| j
|p+dd | jd < d S d S )Nzx-sentry-rate-limitsz%Rate-limited via x-sentry-rate-limits  zRate-limited via 429<   rV   )headersrx   r   warningrp   updatere   statusr   r   rt   get_retry_after)r5   responser^   r%   r%   r&   _update_rate_limits   s   


z!HttpTransport._update_rate_limitsstorec                    s   fdd}| tjjtj d zjjdtj|||d}W n ty;   	d |d  w zE
| |jdkrM	d	 n'|jd
ksW|jdk r{td|j|j 	d|j |d W |  d S W |  d S W |  d S |  w )Nc                    s8    d u rj | dd d S  jD ]	}j | |d qd S )NerrorrG   rH   )rI   items)rF   rH   r=   r5   r%   r&   record_loss   s
   
z0HttpTransport._send_request.<locals>.record_loss)z
User-AgentzX-Sentry-AuthPOST)bodyr   networkZnetwork_errorr   Z
status_429i,     z%Unexpected status code: %s (body: %s)z	status_{})r   strro   clientZ	to_headerr|   requestZget_api_urlrM   on_dropped_eventr   r   r   r   dataformatclose)r5   r   r   endpoint_typer=   r   r   r%   r   r&   _send_request   sH   	






	zHttpTransport._send_requestc                 C   s   d S rL   r%   )r5   rF   r%   r%   r&   r   &  rK   zHttpTransport.on_dropped_eventFr   c                 C   st   | j d sd S |s| jt | k sd S | j}tt| _t | _|s%d S ttt dd | D ddddS )Nr   c                 S   s    g | ]\\}}}|||d qS ))rF   rd   r   r%   ).0rd   rF   r   r%   r%   r&   
<listcomp>=  s    
z>HttpTransport._fetch_pending_client_report.<locals>.<listcomp>)	timestampdiscarded_events)jsonclient_report)type)	r3   rw   rv   ru   r   r[   r   r   r   )r5   forceintervalr   r%   r%   r&   _fetch_pending_client_report*  s&   


z*HttpTransport._fetch_pending_client_reportc                 C   s0   | j |dd}|d ur| t|gd d S d S )Nr   )r   r   )r   )r   r>   r
   )r5   r   r   r%   r%   r&   _flush_client_reportsI  s   z#HttpTransport._flush_client_reportsc                    s    fdd}||p|d S )Nc                    s    j | }|d uo|t kS rL   )rp   rx   r   )ZbuckettsrD   r%   r&   	_disabledQ  s   z0HttpTransport._check_disabled.<locals>._disabledr%   )r5   rd   r   r%   rD   r&   _check_disabledO  s   zHttpTransport._check_disabledc                 C   s   t dd | j D S )Nc                 s   s    | ]}|t  kV  qd S rL   r   )r   r   r%   r%   r&   	<genexpr>Z  s    z1HttpTransport._is_rate_limited.<locals>.<genexpr>)anyrp   valuesrD   r%   r%   r&   _is_rate_limitedX  s   zHttpTransport._is_rate_limitedc                 C   s
   | j  S rL   )rn   fullrD   r%   r%   r&   _is_worker_full\  s   
zHttpTransport._is_worker_fullc                 C   s   |   p|   S rL   )r   r   rD   r%   r%   r&   rJ   `  s   zHttpTransport.is_healthyc                 C   s  |  dr| d | jddd d S t }| jdkr$|t| n tj	|d| jd}|t| W d    n1 s?w   Y  | j
d usKJ td|d	pTd
|dpZd
|dp`d
| j
j| j
jf  ddi}| jdkrwd|d< | j| |d d S )Nr   self_rate_limitsratelimit_backoffr   r   wfileobjmoder~   z>Sending event, type:%s level:%s event_id:%s project:%s host:%sr   nulllevelZevent_idContent-Typezapplication/jsongzipContent-Encoding)r   )r   r   rI   ioBytesIOry   writer   r   GzipFiler4   r   debugrx   
project_idhostr   getvalue)r5   r;   r   fr   r%   r%   r&   _send_eventd  s<   



zHttpTransport._send_eventc                 C   s8  g }|j D ]}| |jr|jdv r| d | jd|d q|| qt|j|d}|j s1d S | jdd}|d urA|j | t	
 }| jdkrP|| ntj|d	| jd
}|| W d    n1 siw   Y  | jd usuJ td|j| jj| jj ddi}| jdkrd|d< | j| |d|d d S )N)Ztransactionr   defaultr   r   r   )r   r      )r   r   r   r   z(Sending envelope [%s] project:%s host:%sr   zapplication/x-sentry-enveloper   r   r=   )r   r   r=   )r   r   rG   r   rI   appendr
   r   r   r   r   ry   Zserialize_intor   r   r4   r   r   descriptionr   r   r   r   )r5   r=   Z	new_itemsrH   Zclient_report_itemr   r   r   r%   r%   r&   _send_envelope  sP   




zHttpTransport._send_envelopec                 C   s   | j d|pt d}d }| jd d ur| jd }| jd r?|d u r$g }dd |D }tD ]}|d |d f|vr>|| q-|d urG||d< |S )	NCERT_REQUIRED)r   	cert_reqsrk   socket_options
keep_alivec                 S   s   h | ]
}|d  |d fqS )r   r)   r%   )r   or%   r%   r&   	<setcomp>  s    z2HttpTransport._get_pool_options.<locals>.<setcomp>r   r)   )rz   certifiwherer3   KEEP_ALIVE_SOCKET_OPTIONSr   )r5   rk   r3   r   Zused_optionsZdefault_optionr%   r%   r&   _get_pool_options  s$   



zHttpTransport._get_pool_optionsc                 C   sN   t  d}|s
dS |dD ]}| }|j|s!|j|r$ dS qdS )NnoFrT   T)r   rx   rY   rZ   r   endswithnetloc)r5   r4   no_proxyr   r%   r%   r&   _in_no_proxy  s   zHttpTransport._in_no_proxyc                 C   s   d }|  |}|jdkr|dkr|p| ot d}|s,|dkr,|p+| o+t d}| |}|ru|r9||d< |drld}	zddlm}
 W n tyY   d	}	t	
d
| Y nw |	rd|
|fi |S tjdi |S tj|fi |S tjdi |S )Nhttps httprl   socksTr   )SOCKSProxyManagerFzYou have configured a SOCKS proxy (%s) but support for SOCKS proxies is not installed. Disabling proxy support. Please add `PySocks` (or `urllib3` with the `[socks]` extra) to your dependencies.r%   )r   schemer   rx   r   
startswithZurllib3.contrib.socksr   ImportErrorr   r   rq   r   r   )r5   r4   ri   rj   rk   rl   proxyr   optsZuse_socks_proxyr   r%   r%   r&   r{     s4   	


zHttpTransport._make_poolc                    sD   j j fdd}j|s d jddd d S d S )Nc                	      r   - t       W d    n1 sw   Y  W d    d S W d    d S 1 s2w   Y  d S rL   )r   r   r   r%   r;   hubr5   r%   r&   send_event_wrapper%     

"z7HttpTransport.capture_event.<locals>.send_event_wrapper
full_queuequeue_overflowr   r   )r}   currentrn   submitr   rI   )r5   r;   r   r%   r   r&   r<     s   
zHttpTransport.capture_eventc                    sP   j j fdd}j|s$d  jD ]}jd|d qd S d S )Nc                	      r   rL   )r   r   r   r%   r=   r   r5   r%   r&   send_envelope_wrapper6  r   z=HttpTransport.capture_envelope.<locals>.send_envelope_wrapperr   r   r   )r}   r   rn   r   r   r   rI   )r5   r=   r   rH   r%   r   r&   r>   0  s   

zHttpTransport.capture_envelopec                    s<   t d |dkr j fdd  j|| d S d S )NzFlushing HTTP transportr   c                      s    j ddS )NT)r   )r   r%   rD   r%   r&   r'   K  s    z%HttpTransport.flush.<locals>.<lambda>)r   r   rn   r   rC   r@   r%   rD   r&   rC   B  s
   
zHttpTransport.flushc                 C   s   t d | j  d S )NzKilling HTTP transport)r   r   rn   rE   rD   r%   r%   r&   rE   N  s   
zHttpTransport.killrO   )r   N)Fr   )FrL   )rP   rQ   rR   rS   r6   rI   r   r   r   r   r   r   r   r   rJ   r   r   r   r   r{   r<   r>   rC   rE   r%   r%   r%   r&   rf      s6    )

9

	(>/
rf   c                   @   s   e Zd Zdd Zdd ZdS )_FunctionTransportc                 C   s   t |  || _d S rL   )r1   r6   _func)r5   funcr%   r%   r&   r6   U  s   

z_FunctionTransport.__init__c                 C   s   |  | d S rL   )r   r:   r%   r%   r&   r<   \  s   
z _FunctionTransport.capture_eventN)rP   rQ   rR   r6   r<   r%   r%   r%   r&   r   T  s    r   c                 C   sb   | d }|d u rt }nt|tr|S t|trt|tr|}nt|r't|S | d r/|| S d S )N	transportr2   )rf   
isinstancer1   r   
issubclasscallabler   )r3   Zref_transportZtransport_clsr%   r%   r&   make_transportd  s   
r   rL   )=
__future__r   r   r   r$   rv   r   r   collectionsr   rq   r   Zsentry_sdk.utilsr   r   r   r   Zsentry_sdk.workerr	   Zsentry_sdk.enveloper
   r   r   Zsentry_sdk._compatr   Zsentry_sdk._typesr   typingr   r   r   r   r   r   r   r   r   r   Zurllib3.poolmanagerr   r   r   r   r   ZDataCategoryurllib.requestr   r   urllibr   
SOL_SOCKETSOL_TCPoptionr   AttributeErrorobjectr1   re   rf   r   r   r%   r%   r%   r&   <module>   sl    "
P   <