a
    ñ»jø@  ã                   @  s¾  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
mZ ddlmZmZ ddlmZmZmZmZmZmZ g d¢Ze
d	ƒZd
dœddddddœdd„Zddddœdd„Ze d¡Zddddœdd„Ze d¡Zdddddœdd„Ze d¡Ze d¡Zdddddœd d!„Z e d"¡Z!e d#¡Z"ddd$œd%d&„Z#d'dddd(d)œd*d+„Z$dddd,dœd-d.„Z%dd/d0œd1d2„Z&e d3¡Z'dddd4dœd5d6„Z(dd7d0œd8d9„Z)dddd:dœd;d<„Z*dddd=dœd>d?„Z+dd@d0œdAdB„Z,e,Z-dCdDddEœdFdG„Z.dHddIœdJdK„Z/e/Z0ddddLdœdMdN„Z1ddOd0œdPdQ„Z2e2Z3dRddSœdTdU„Z4e4Z5dRdVdSœdWdX„Z6dddYœdZd[„Z7e d\¡Z8dddddœd]d^„Z9ddddVdœd_d`„Z:ddad0œdbdc„Z;dddddœdedf„Z<dS )gé    )ÚannotationsN)ÚSequence)ÚCallableÚTypeVarÚcasté   )ÚInvalidHeaderFormatÚInvalidHeaderValue)ÚConnectionOptionÚExtensionHeaderÚExtensionNameÚExtensionParameterÚSubprotocolÚUpgradeProtocol)Ú
build_hostÚparse_connectionÚparse_upgradeÚparse_extensionÚbuild_extensionÚparse_subprotocolÚbuild_subprotocolÚvalidate_subprotocolsÚbuild_www_authenticate_basicÚparse_authorization_basicÚbuild_authorization_basicÚTF)Úalways_include_portÚstrÚintÚbool)ÚhostÚportÚsecurer   Úreturnc                C  s^   zt  | ¡}W n ty    Y n0 |jdkr8d| › d�} |sL||rFdndkrZ| › d|› �} | S )z#
    Build a ``Host`` header.

    é   ú[ú]i»  éP   ú:)Ú	ipaddressÚ
ip_addressÚ
ValueErrorÚversion)r    r!   r"   r   Úaddress© r.   ú8E:\soccer\ml_env\Lib\site-packages\websockets/headers.pyr   '   s    
r   z
str | None)ÚheaderÚposr#   c                 C  s   |t | ƒkrdS | | S )z·
    Return the next character from ``header`` at the given position.

    Return :obj:`None` at the end of ``header``.

    We never need to peek more than one character ahead.

    N)Úlen)r0   r1   r.   r.   r/   Ú
peek_aheadI   s    	r3   z[\t ]*c                 C  s    t  | |¡}|dusJ ‚| ¡ S )z±
    Parse optional whitespace from ``header`` at the given position.

    Return the new position.

    The whitespace itself isn't returned because it isn't significant.

    N)Ú_OWS_reÚmatchÚend)r0   r1   r5   r.   r.   r/   Ú	parse_OWSX   s    
r7   z[-!#$%&\'*+.^_`|~0-9a-zA-Z]+ztuple[str, int])r0   r1   Úheader_namer#   c                 C  s2   t  | |¡}|du r"t|d| |ƒ‚| ¡ | ¡ fS )z®
    Parse a token from ``header`` at the given position.

    Return the token value and the new position.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    Nzexpected token)Ú	_token_rer5   r   Úgroupr6   ©r0   r1   r8   r5   r.   r.   r/   Úparse_tokenj   s    
r<   zC"(?:[\x09\x20-\x21\x23-\x5b\x5d-\x7e]|\\[\x09\x20-\x7e\x80-\xff])*"z\\([\x09\x20-\x7e\x80-\xff])c                 C  sB   t  | |¡}|du r"t|d| |ƒ‚t d| ¡ dd… ¡| ¡ fS )z¹
    Parse a quoted string from ``header`` at the given position.

    Return the unquoted value and the new position.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    Nzexpected quoted stringz\1r   éÿÿÿÿ)Ú_quoted_string_rer5   r   Ú_unquote_reÚsubr:   r6   r;   r.   r.   r/   Úparse_quoted_string‚   s    
rA   z[\x09\x20-\x7e\x80-\xff]*z([\x22\x5c]))Úvaluer#   c                 C  s.   t  | ¡}|du rtdƒ‚dt d| ¡ d S )zh
    Format ``value`` as a quoted string.

    This is the reverse of :func:`parse_quoted_string`.

    Nz-invalid characters for quoted-string encodingú"z\\\1)Ú_quotable_reÚ	fullmatchr+   Ú	_quote_rer@   )rB   r5   r.   r.   r/   Úbuild_quoted_string˜   s    
rG   z(Callable[[str, int, str], tuple[T, int]]zlist[T])Ú
parse_itemr0   r1   r8   r#   c                 C  sÂ   t ||ƒdkrt||d ƒ}q g }| |||ƒ\}}| |¡ t||ƒ}|t|ƒkrTq®t ||ƒdkrrt||d ƒ}nt|d||ƒ‚t ||ƒdkržt||d ƒ}q€|t|ƒkr"q®q"|t|ƒks¾J ‚|S )aö  
    Parse a comma-separated list from ``header`` at the given position.

    This is appropriate for parsing values with the following grammar:

        1#item

    ``parse_item`` parses one item.

    ``header`` is assumed not to start or end with whitespace.

    (This function is designed for parsing an entire header value and
    :func:`~websockets.http.read_headers` strips whitespace from values.)

    Return a list of items.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    ú,r   zexpected comma)r3   r7   Úappendr2   r   )rH   r0   r1   r8   ÚitemsÚitemr.   r.   r/   Ú
parse_list¥   s"    

rM   ztuple[ConnectionOption, int]c                 C  s   t | ||ƒ\}}tt|ƒ|fS )z½
    Parse a Connection option from ``header`` at the given position.

    Return the protocol value and the new position.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    )r<   r   r
   ©r0   r1   r8   rL   r.   r.   r/   Úparse_connection_optionç   s    rO   zlist[ConnectionOption])r0   r#   c                 C  s   t t| ddƒS )zÓ
    Parse a ``Connection`` header.

    Return a list of HTTP connection options.

    Args
        header: value of the ``Connection`` header.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    r   Ú
Connection)rM   rO   ©r0   r.   r.   r/   r   ÷   s    r   z>[-!#$%&\'*+.^_`|~0-9a-zA-Z]+(?:/[-!#$%&\'*+.^_`|~0-9a-zA-Z]+)?ztuple[UpgradeProtocol, int]c                 C  s8   t  | |¡}|du r"t|d| |ƒ‚tt| ¡ ƒ| ¡ fS )z½
    Parse an Upgrade protocol from ``header`` at the given position.

    Return the protocol value and the new position.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    Nzexpected protocol)Ú_protocol_rer5   r   r   r   r:   r6   r;   r.   r.   r/   Úparse_upgrade_protocol  s    rS   zlist[UpgradeProtocol]c                 C  s   t t| ddƒS )zÆ
    Parse an ``Upgrade`` header.

    Return a list of HTTP protocols.

    Args:
        header: Value of the ``Upgrade`` header.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    r   ZUpgrade)rM   rS   rQ   r.   r.   r/   r     s    r   ztuple[ExtensionParameter, int]c                 C  s    t | ||ƒ\}}t| |ƒ}d}t| |ƒdkr”t| |d ƒ}t| |ƒdkrz|}t| ||ƒ\}}t |¡du rŠt|d| |ƒ‚nt | ||ƒ\}}t| |ƒ}||f|fS )zÌ
    Parse a single extension parameter from ``header`` at the given position.

    Return a ``(name, value)`` pair and the new position.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    Nú=r   rC   zinvalid quoted header content)r<   r7   r3   rA   r9   rE   r   )r0   r1   r8   ÚnamerB   Z
pos_beforer.   r.   r/   Úparse_extension_item_param.  s    
ÿ
rV   ztuple[ExtensionHeader, int]c                 C  sh   t | ||ƒ\}}t| |ƒ}g }t| |ƒdkrVt| |d ƒ}t| ||ƒ\}}| |¡ qtt|ƒ|f|fS )a  
    Parse an extension definition from ``header`` at the given position.

    Return an ``(extension name, parameters)`` pair, where ``parameters`` is a
    list of ``(name, value)`` pairs, and the new position.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    ú;r   )r<   r7   r3   rV   rJ   r   r   )r0   r1   r8   rU   Ú
parametersZ	parameterr.   r.   r/   Úparse_extension_itemR  s    
rY   zlist[ExtensionHeader]c                 C  s   t t| ddƒS )aâ  
    Parse a ``Sec-WebSocket-Extensions`` header.

    Return a list of WebSocket extensions and their parameters in this format::

        [
            (
                'extension name',
                [
                    ('parameter name', 'parameter value'),
                    ....
                ]
            ),
            ...
        ]

    Parameter values are :obj:`None` when no value is provided.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    r   zSec-WebSocket-Extensions)rM   rY   rQ   r.   r.   r/   r   k  s    r   r   zSequence[ExtensionParameter])rU   rX   r#   c                 C  s    d  tt| ƒgdd„ |D ƒ ¡S )zc
    Build an extension definition.

    This is the reverse of :func:`parse_extension_item`.

    z; c                 S  s*   g | ]"\}}|d u r|n|› d|› �‘qS )NrT   r.   )Ú.0rU   rB   r.   r.   r/   Ú
<listcomp>“  s   ÿz(build_extension_item.<locals>.<listcomp>)Újoinr   r   )rU   rX   r.   r.   r/   Úbuild_extension_itemˆ  s    	
ýÿÿr]   zSequence[ExtensionHeader])Ú
extensionsr#   c                 C  s   d  dd„ | D ƒ¡S )zl
    Build a ``Sec-WebSocket-Extensions`` header.

    This is the reverse of :func:`parse_extension`.

    ú, c                 s  s   | ]\}}t ||ƒV  qd S )N)r]   )rZ   rU   rX   r.   r.   r/   Ú	<genexpr>¢  s   z"build_extension.<locals>.<genexpr>©r\   )r^   r.   r.   r/   r   ›  s    
ÿr   ztuple[Subprotocol, int]c                 C  s   t | ||ƒ\}}tt|ƒ|fS )zº
    Parse a subprotocol from ``header`` at the given position.

    Return the subprotocol value and the new position.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    )r<   r   r   rN   r.   r.   r/   Úparse_subprotocol_itemª  s    rb   zlist[Subprotocol]c                 C  s   t t| ddƒS )z 
    Parse a ``Sec-WebSocket-Protocol`` header.

    Return a list of WebSocket subprotocols.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    r   zSec-WebSocket-Protocol)rM   rb   rQ   r.   r.   r/   r   º  s    
r   zSequence[Subprotocol])Úsubprotocolsr#   c                 C  s
   d  | ¡S )zl
    Build a ``Sec-WebSocket-Protocol`` header.

    This is the reverse of :func:`parse_subprotocol`.

    r_   ra   )rc   r.   r.   r/   r   Ê  s    r   ÚNonec                 C  sJ   t | tƒstdƒ‚t | tƒr$tdƒ‚| D ]}t |¡s(td|› �ƒ‚q(dS )zT
    Validate that ``subprotocols`` is suitable for :func:`build_subprotocol`.

    zsubprotocols must be a listz&subprotocols must be a list, not a strzinvalid subprotocol: N)Ú
isinstancer   Ú	TypeErrorr   r9   rE   r+   )rc   Zsubprotocolr.   r.   r/   r   ×  s    


r   )Úrealmr#   c                 C  s    t | ƒ} t dƒ}d| › d|› �S )z�
    Build a ``WWW-Authenticate`` header for HTTP Basic Auth.

    Args:
        realm: Identifier of the protection space.

    zUTF-8zBasic realm=z
, charset=)rG   )rg   Úcharsetr.   r.   r/   r   å  s    	r   z[A-Za-z0-9-._~+/]+=*c                 C  s2   t  | |¡}|du r"t|d| |ƒ‚| ¡ | ¡ fS )z°
    Parse a token68 from ``header`` at the given position.

    Return the token value and the new position.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    Nzexpected token68)Ú_token68_rer5   r   r:   r6   r;   r.   r.   r/   Úparse_token68ö  s    
rj   c                 C  s   |t | ƒk rt|d| |ƒ‚dS )z8
    Check that parsing reached the end of header.

    ztrailing dataN)r2   r   )r0   r1   r8   r.   r.   r/   Ú	parse_end  s    rk   ztuple[str, str]c                 C  sà   t | ddƒ\}}| ¡ dkr,tdd|› �ƒ‚t| |ƒdkrHtdd| |ƒ‚|d7 }t| |dƒ\}}t| |dƒ zt | 	¡ ¡ 
¡ }W n  tjy¢   tddƒd	‚Y n0 z| d
d¡\}}W n tyÖ   tddƒd	‚Y n0 ||fS )a!  
    Parse an ``Authorization`` header for HTTP Basic Auth.

    Return a ``(username, password)`` tuple.

    Args:
        header: Value of the ``Authorization`` header.

    Raises:
        InvalidHeaderFormat: On invalid inputs.
        InvalidHeaderValue: On unsupported inputs.

    r   ÚAuthorizationÚbasiczunsupported scheme: ú zexpected space after schemer   z#expected base64-encoded credentialsNr(   z&expected username:password credentials)r<   Úlowerr	   r3   r   rj   rk   Úbase64Ú	b64decodeÚencodeÚdecodeÚbinasciiÚErrorÚsplitr+   )r0   Úschemer1   Úbasic_credentialsÚ	user_passÚusernameÚpasswordr.   r.   r/   r     s@    þÿþýþýr   )rz   r{   r#   c                 C  s4   d| vsJ ‚| › d|› �}t  | ¡ ¡ ¡ }d| S )z€
    Build an ``Authorization`` header for HTTP Basic Auth.

    This is the reverse of :func:`parse_authorization_basic`.

    r(   zBasic )rp   Ú	b64encoderr   rs   )rz   r{   ry   rx   r.   r.   r/   r   ?  s    r   )=Ú
__future__r   rp   rt   r)   ÚreÚcollections.abcr   Útypingr   r   r   Ú
exceptionsr   r	   r
   r   r   r   r   r   Ú__all__r   r   r3   Úcompiler4   r7   r9   r<   r>   r?   rA   rD   rF   rG   rM   rO   r   rR   rS   r   rV   rY   r   Zparse_extension_listr]   r   Zbuild_extension_listrb   r   Zparse_subprotocol_listr   Zbuild_subprotocol_listr   r   ri   rj   rk   r   r   r.   r.   r.   r/   Ú<module>   sh    
û"

ÿ


Bÿ$

	0