o
    lh                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZ ddlm	Z	 	 ddgZ
ded	efd
dZddgZded	efddZded	efddZdeded	efddZdedee d	efddZ		 			d!dededee dedee dee ded	efd dZdS )"    N)Optional)unquoteurlparse   )WebSocketProxyException	parse_urlget_proxy_infourlreturnc                 C   s   d| vrt d| dd\}} t| dd}|jr|j}nt dd}|jr)|j}d}|d	kr4|s3d
}n|dkr?d}|s>d}nt d| |jrL|j}nd}|jrY|d|j 7 }||||fS )z
    parse url and the result is tuple of
    (hostname, port, resource path and the flag of secure mode)

    Parameters
    ----------
    url: str
        url string.
    :zurl is invalidr   http)schemezhostname is invalidr   FwsP   wssTi  zscheme %s is invalid/?)
ValueErrorsplitr   hostnameportpathquery)r	   r   parsedr   r   	is_secureresource r   b/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/websocket/_url.pyr      s6   
	localhostz	127.0.0.1addrc                 C   s(   zt |  W dS  t jy   Y dS w )NFT)socket	inet_atonerror)r   r   r   r   _is_ip_addressO   s   r#   r   c                 C   sL   z|  d\}}t|odt|  kodk W S   W S  ty%   Y dS w )Nr   r       F)r   r#   intr   )r   r   netmaskr   r   r   _is_subnet_addressX   s   (r'   ipnetc                 C   sZ   t dt| d }|d\}}t dt|d }ddt| > d@ }||@ |kS )Nz!Ir   r   l    r$   )structunpackr    r!   r   r%   )r(   r)   ipaddrnetaddrr&   r   r   r   _is_address_in_network`   s
   r.   no_proxyc                    s   |st jdt jdddd }r|d}|st}d|v r#dS  |v r)dS t r8t fdd	|D S d
d	 |D D ]
} |rI dS q?dS )Nr/   NO_PROXY  ,*Tc                    s   g | ]}t |rt |qS r   )r'   r.   ).0subnetr   r   r   
<listcomp>x   s    z%_is_no_proxy_host.<locals>.<listcomp>c                 S   s   g | ]	}| d r|qS ).)
startswith)r5   domainr   r   r   r8   ~   s    F)	osenvirongetreplacer   DEFAULT_NO_PROXY_HOSTr#   anyendswith)r   r/   vr;   r   r7   r   _is_no_proxy_hosti   s,   


rD   r   r   
proxy_host
proxy_port
proxy_auth
proxy_typec                 C   s   t | |rdS |r|std|}|}|||fS |rdnd}	tj|	tj|	 ddd}
|
rLt|
}|jrCt	|jt	|j
fnd}|j|j|fS dS )a  
    Try to retrieve proxy host and port from environment
    if not provided in options.
    Result is (proxy_host, proxy_port, proxy_auth).
    proxy_auth is tuple of username and password
    of proxy authentication information.

    Parameters
    ----------
    hostname: str
        Websocket server name.
    is_secure: bool
        Is the connection secure? (wss) looks for "https_proxy" in env
        instead of "http_proxy"
    proxy_host: str
        http proxy host name.
    proxy_port: str or int
        http proxy port.
    no_proxy: list
        Whitelisted host names that don't use the proxy.
    proxy_auth: tuple
        HTTP proxy auth information. Tuple of username and password. Default is None.
    proxy_type: str
        Specify the proxy protocol (http, socks4, socks4a, socks5, socks5h). Default is "http".
        Use socks4a or socks5h if you want to send DNS requests through the proxy.
    )Nr   Nz+Cannot use port 0 when proxy_host specifiedhttps_proxy
http_proxyr1   r2   N)rD   r   r<   r=   r>   upperr?   r   usernamer   passwordr   r   )r   r   rE   rF   rG   r/   rH   r   authenv_keyvalueproxyr   r   r   r      s(   
#
)Nr   NNr   )r<   r    r*   typingr   urllib.parser   r   _exceptionsr   __all__strtupler   r@   boolr#   r'   r.   listrD   r%   r   r   r   r   r   <module>   sH    .		