o
    KK&hJ                     @  s   d dl mZ d dlZd dlZd dlZddlmZmZ ddgZ	dZ
ejG dd dZdddZejG dd dZdddZdddZdS )    )annotationsN   )InvalidProxy
InvalidURI	parse_uriWebSocketURIz:/?#[]@!$&'()*+,;=c                   @  sn   e Zd ZU dZded< ded< ded< ded< ded	< d
Zded< d
Zded< edddZedddZ	d
S )r   a  
    WebSocket URI.

    Attributes:
        secure: :obj:`True` for a ``wss`` URI, :obj:`False` for a ``ws`` URI.
        host: Normalized to lower case.
        port: Always set even if it's the default.
        path: May be empty.
        query: May be empty if the URI doesn't include a query component.
        username: Available when the URI contains `User Information`_.
        password: Available when the URI contains `User Information`_.

    .. _User Information: https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1

    boolsecurestrhostintportpathqueryN
str | Noneusernamepasswordreturnc                 C  s*   | j r| j }nd}| jr|d| j 7 }|S )N/?)r   r   )selfresource_name r   A/var/www/html/venv/lib/python3.10/site-packages/websockets/uri.pyr   +   s   zWebSocketURI.resource_nametuple[str, str] | Nonec                 C  (   | j d u rd S | jd usJ | j | jfS Nr   r   r   r   r   r   	user_info5      
zWebSocketURI.user_info)r   r
   r   r   )
__name__
__module____qualname____doc____annotations__r   r   propertyr   r   r   r   r   r   r      s   
 	urir
   r   c           	      C  s4  t j| }|jdvrt| d|jdu rt| d|jdkr$t| d|jdk}|j}|jp4|r3dnd	}|j}|j	}|j
}|j}|durN|du rNt| d
z| d W n: ty   |d }t jj|td}t jj|td}|dur|dus}J t jj|td}t jj|td}Y nw t|||||||S )z
    Parse and validate a WebSocket URI.

    Args:
        uri: WebSocket URI.

    Returns:
        Parsed WebSocket URI.

    Raises:
        InvalidURI: If ``uri`` isn't a valid WebSocket URI.

    )wswsszscheme isn't ws or wssNhostname isn't provided z"fragment identifier is meaninglessr*     P   "username provided without passwordasciiidnasafe)urllibparseurlparseschemer   hostnamefragmentr   r   r   r   r   encodeUnicodeEncodeErrordecodequoteDELIMSr   )	r(   parsedr	   r   r   r   r   r   r   r   r   r   r   =   s:   







c                   @  sP   e Zd ZU dZded< ded< ded< dZded	< dZded
< edddZdS )Proxya  
    Proxy.

    Attributes:
        scheme: ``"socks5h"``, ``"socks5"``, ``"socks4a"``, ``"socks4"``,
            ``"https"``, or ``"http"``.
        host: Normalized to lower case.
        port: Always set even if it's the default.
        username: Available when the proxy address contains `User Information`_.
        password: Available when the proxy address contains `User Information`_.

    .. _User Information: https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1

    r
   r7   r   r   r   Nr   r   r   r   r   c                 C  r   r   r   r   r   r   r   r      r    zProxy.user_infor!   )	r"   r#   r$   r%   r&   r   r   r'   r   r   r   r   r   r@   o   s   
 r@   proxyc                 C  s8  t j| }|jdvrt| d|j d|jdu rt| d|jdvr)t| d|jdkr3t| d	|jdkr=t| d
|j}|j}|j	pN|jdkrMdnd}|j
}|j}|durb|du rbt| dz| d W n* ty   |d }|dur|dusJ t jj|td}t jj|td}Y nw t|||||S )z
    Parse and validate a proxy.

    Args:
        proxy: proxy.

    Returns:
        Parsed proxy.

    Raises:
        InvalidProxy: If ``proxy`` isn't a valid proxy.

    )socks5hsocks5socks4asocks4httpshttpzscheme z isn't supportedNr+   )r,   r   zpath is meaninglessr,   zquery is meaninglesszfragment is meaninglessrF   r-   r.   r/   r0   r1   r2   )r4   r5   r6   r7   r   r8   r   r   r9   r   r   r   r:   r;   r<   r=   r>   r@   )rA   r?   r7   r   r   r   r   r   r   r   parse_proxy   s:   









	rH   r   c                 C  s   t j| j d| j rdS t j }| jrg d}ng d}|D ] }||}|durB|dkr>|dr>d|dd  }|  S q"dS )	zU
    Return the proxy to use for connecting to the given WebSocket URI, if any.

    :N)r*   socksrF   )r)   rJ   rF   rG   rJ   zhttp://z
socks5h://   )	r4   requestproxy_bypassr   r   
getproxiesr	   get
startswith)r(   proxiesschemesr7   rA   r   r   r   	get_proxy   s   


rS   )r(   r
   r   r   )rA   r
   r   r@   )r(   r   r   r   )
__future__r   dataclassesurllib.parser4   urllib.request
exceptionsr   r   __all__r>   	dataclassr   r   r@   rH   rS   r   r   r   r   <module>   s    
+2
2