U
    6
[c                     @   sn   d Z ddlZddlZddlZddlmZ ddgZdd ZddgZd	d
 Z	dd Z
dd Zdd ZdddZdS )aL  
websocket - WebSocket client library for Python

Copyright (C) 2010 Hiroki Ohtani(liris)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1335  USA

    N)urlparse	parse_urlget_proxy_infoc                 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)

    url: url string.
    :zurl is invalid   Zws)schemezhostname is invalidr   FP   ZwssTi  zscheme %s is invalid/?)
ValueErrorsplitr   hostnameportpathZquery)Zurlr   Zparsedr   r   	is_secureresource r   0/usr/lib/python3/dist-packages/websocket/_url.pyr   !   s2    Z	localhostz	127.0.0.1c                 C   s0   zt |  W n t jk
r&   Y dS X dS d S )NFT)socket	inet_atonerror)addrr   r   r   _is_ip_addressO   s
    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   c                 C   sZ   t dt| d }|d\}}t dt|d dt|d > d @ }||@ |kS )NIr   r	      r   )structZunpackr   r   r   r   )ZipZnetZipaddrZnetaddrbitsr   r   r   r   _is_address_in_network`   s    *r!   c                    sZ   |s$t jdddd}|d}|s,t} |kr8dS t rVt fdd|D S dS )	Nno_proxy  ,Tc                    s   g | ]}t |rt |qS r   )r   r!   ).0Zsubnetr   r   r   
<listcomp>q   s      z%_is_no_proxy_host.<locals>.<listcomp>F)osenvirongetreplacer   DEFAULT_NO_PROXY_HOSTr   any)r   r"   vr   r'   r   _is_no_proxy_hostg   s    
r0   httpc                 C   s   t | |rdS |r$|}|}|||fS dg}	|r:|	dd |	D ]F}
tj|
d}|r>t|}|jrn|j|jfnd}|j|j	|f  S q>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.

    hostname: websocket server name.

    is_secure:  is the connection secure? (wss)
                looks for "https_proxy" in env
                before falling back to "http_proxy"

    options:    "http_proxy_host" - http proxy host name.
                "http_proxy_port" - http proxy port.
                "http_no_proxy"   - host names, which doesn't use proxy.
                "http_proxy_auth" - http proxy auth information.
                                    tuple of username and password.
                                    default is None
                "proxy_type"      - if set to "socks5" PySocks wrapper
                                    will be used in place of a http proxy.
                                    default is "http"
    )Nr   NZ
http_proxyr   Zhttps_proxyN)
r0   insertr)   r*   r+   r   ZusernameZpasswordr   r   )r   r   Z
proxy_hostZ
proxy_portZ
proxy_authr"   Z
proxy_typer   ZauthZenv_keyskeyvalueproxyr   r   r   r   v   s     

)Nr   NNr1   )__doc__r)   r   r   Zsix.moves.urllib.parser   __all__r   r-   r   r   r!   r0   r   r   r   r   r   <module>   s"   +	        