B
    &dD              
   @   s   d dl Z d dlZd dlZd dlmZ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dddZdeeeeeeeedddZdS )    N)unquoteurlparse	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 invalid   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   [/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/websocket/_url.pyr      s2    
	localhostz	127.0.0.1)addrr   c             C   s.   yt |  W n t jk
r$   dS X dS d S )NFT)socket	inet_atonerror)r   r   r   r   _is_ip_addressN   s
    r!   )r   r   c             C   sL   y2|  d\}}t|o2dt|  ko.dk S   S  tk
rF   dS X d S )Nr   r       F)r   r!   intr   )r   r   netmaskr   r   r   _is_subnet_addressW   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 x$d
d	 |D D ]} |rdS qW dS )Nr-   ZNO_PROXY  ,*Tc                s   g | ]}t |rt |qS r   )r%   r,   ).0Zsubnet)r   r   r   
<listcomp>u   s    z%_is_no_proxy_host.<locals>.<listcomp>c             S   s   g | ]}| d r|qS ).)
startswith)r2   domainr   r   r   r3   v   s    F)	osenvirongetreplacer   DEFAULT_NO_PROXY_HOSTr!   anyendswith)r   r-   vr6   r   )r   r   _is_no_proxy_hosth   s      

r?   r
   )r   r   
proxy_host
proxy_port
proxy_authr-   
proxy_typer   c             C   s   t | |rdS |r$|}|}|||fS dg}	|r:|	dd xh|	D ]`}
tj|
tj|
 ddd}|r@t|}|jrt	|jt	|j
fnd}|j|j|fS q@W 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
        before falling back to "http_proxy"
    proxy_host: str
        http proxy host name.
    http_proxy_port: str or int
        http proxy port.
    http_no_proxy: list
        Whitelisted host names that don't use the proxy.
    http_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
http_proxyr   Zhttps_proxyr.   r/   N)r?   insertr7   r8   r9   upperr:   r   usernamer   passwordr   r   )r   r   r@   rA   rB   r-   rC   r   authZenv_keyskeyvalueproxyr   r   r   r   |   s     


$)Nr   NNr
   )r7   r   r(   urllib.parser   r   __all__strtupler   r;   boolr!   r%   r,   listr?   r#   r   r   r   r   r   <module>   s   .		 