o
    ii                     @   s   d dl Z d dlmZ d dlmZ d dlmZ er0d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d	d
 Zdd Zdd Zdd Zdd ZdS )    N)_should_send_default_pii)_filter_headers)TYPE_CHECKING)Any)Dict)Optional)Literalc                 C   sT   i }| d D ]!\}}| d}| d}||v r#|| d | ||< q|||< q|S )z^
    Extract headers from the ASGI scope, in the format that the Sentry protocol expects.
    headerslatin-1z, )decode)
asgi_scoper	   raw_key	raw_valuekeyvalue r   c/var/www/html/pca-backend/venv/lib/python3.10/site-packages/sentry_sdk/integrations/_asgi_common.py_get_headers   s   


r   c                 C   s   |  d|}|  dd}|  dd|  dd }|r!d|||f S |durF|\}}dd	dd	d
 |}||kr?d||||f S d|||f S |S )zR
    Extract URL from the ASGI scope, without also including the querystring.
    schemeserverN	root_path pathz	%s://%s%sP   i  )httphttpswswssz%s://%s:%s%s)get)r   default_schemehostr   r   r   portdefault_portr   r   r   _get_url   s   r#   c                 C   s$   |  d}|s	dS tj|dS )zb
    Extract querystring from the ASGI scope, in the format that the Sentry protocol expects.
    query_stringNr
   )r   urllibparseunquoter   )r   qsr   r   r   
_get_query5   s   
r)   c              	   C   sd   t | }z|d dd  W S  ttfy   Y nw z|d W S  ty*   Y nw | dd S )zh
    Extract IP Address from the ASGI scope based on request headers with fallback to scope client.
    zx-forwarded-for,r   z	x-real-ipclient)r   splitstripKeyError
IndexErrorr   )r   r	   r   r   r   _get_ip@   s   
r0   c                 C   s   i }| d }|dv r2|  d|d< tt|  |d< }t| |d< t| |dkr)dnd| d|d	< |  d
}|rDt rDdt| i|d< |S )zG
    Returns data related to the HTTP request from the ASGI scope.
    type)r   	websocketmethodr	   r$   r   r   r    urlr+   REMOTE_ADDRenv)r   r   r   r)   r#   r   r0   )r   request_datatyr	   r+   r   r   r   _get_request_dataS   s   

r9   )r%   sentry_sdk.hubr   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk._typesr   typingr   r   r   typing_extensionsr   r   r#   r)   r0   r9   r   r   r   r   <module>   s    