U
    g                  
   @   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
eedd	dZd
dgZeedddZeedddZeeedddZeee edddZdeeee eee ee eedddZdS )    N)Optional)unquoteurlparse   )WebSocketProxyException	parse_urlget_proxy_info)urlreturnc                 C   s   d| krt d| dd\}} t| dd}|jr:|j}nt dd}|jrR|j}d}|d	krh|sd
}n"|dkr~d}|sd}nt d| |jr|j}nd}|jr|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   2/tmp/pip-unpacked-wheel-0xk_xrpx/websocket/_url.pyr      s2    
	localhostz	127.0.0.1)addrr
   c                 C   s0   zt |  W n t jk
r&   Y dS X dS d S )NFT)socket	inet_atonerror)r   r   r   r   _is_ip_addressO   s
    r#   )r   r
   c                 C   sP   z4|  d\}}t|o2dt|  ko.dk n  W S  tk
rJ   Y dS X d S )Nr   r       F)r   r#   intr   )r   r   netmaskr   r   r   _is_subnet_addressX   s
    &r'   )ipnetr
   c                 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.   )r   no_proxyr
   c                    s   |s2t jdt jdddd }r2|d}|s:t}d|krFdS  |krRdS t rpt fdd	|D S d
d	 |D D ]} |r~ dS q~dS )Nr/   ZNO_PROXY  ,*Tc                    s   g | ]}t |rt |qS r   )r'   r.   ).0Zsubnetr   r   r   
<listcomp>x   s   z%_is_no_proxy_host.<locals>.<listcomp>c                 S   s   g | ]}| d r|qS ).)
startswith)r4   domainr   r   r   r6   ~   s     
 F)	osenvirongetreplacer   DEFAULT_NO_PROXY_HOSTr#   anyendswith)r   r/   vr9   r   r5   r   _is_no_proxy_hosti   s,     


rB   r   )r   r   
proxy_host
proxy_port
proxy_authr/   
proxy_typer
   c                 C   s   t | |rdS |r0|std|}|}|||fS |r8dnd}	tj|	tj|	 ddd}
|
rt|
}|jrt	|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 specifiedZhttps_proxyZ
http_proxyr0   r1   N)rB   r   r:   r;   r<   upperr=   r   usernamer   passwordr   r   )r   r   rC   rD   rE   r/   rF   r   authZ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.   listrB   r%   r   r   r   r   r   <module>   s6   .		     