o
    :ÊÆhø>  ã                   @  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mZm	Z	m
Z
mZmZmZ ddlmZ ddlmZmZmZmZmZmZ g d¢ZedƒZdidd„Zdjdd„Ze d¡Zdkdd„Ze d¡Zdldd„Ze d¡Ze d ¡Z dld!d"„Z!e d#¡Z"e d$¡Z#dmd&d'„Z$dnd+d,„Z%dod.d/„Z&dpd1d2„Z'e d3¡Z(dqd5d6„Z)drd8d9„Z*dsd;d<„Z+dtd>d?„Z,dudAdB„Z-e-Z.dvdGdH„Z/dwdKdL„Z0e0Z1dxdNdO„Z2dydQdR„Z3e3Z4dzdUdV„Z5e5Z6d{dXdY„Z7d|d[d\„Z8e d]¡Z9dld^d_„Z:d}d`da„Z;d~dcdd„Z<ddgdh„Z=dS )€é    )ÚannotationsN)ÚCallableÚListÚOptionalÚSequenceÚTupleÚTypeVarÚcasté   )Ú
exceptions)Ú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ÚTÚhostÚstrÚportÚintÚsecureÚboolÚreturnc                 C  sZ   zt  | ¡}W n	 ty   Y nw |jdkrd| › d�} ||r!dndkr+| › d|› �} | S )z#
    Build a ``Host`` header.

    é   ú[ú]i»  éP   ú:)Ú	ipaddressÚ
ip_addressÚ
ValueErrorÚversion)r   r    r"   Úaddress© r/   ú_/var/www/html/HyperOracle/crypto_trader/venv/lib/python3.10/site-packages/websockets/headers.pyr   &   s   þ
r   ÚheaderÚposúOptional[str]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)r1   r2   r/   r/   r0   Ú
peek_aheadB   s   	r5   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)r1   r2   r7   r/   r/   r0   Ú	parse_OWSQ   s   
r9   z[-!#$%&\'*+.^_`|~0-9a-zA-Z]+Úheader_nameúTuple[str, int]c                 C  ó4   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_rer7   r   ÚInvalidHeaderFormatÚgroupr8   ©r1   r2   r:   r7   r/   r/   r0   Úparse_tokenc   s   
rA   zC"(?:[\x09\x20-\x21\x23-\x5b\x5d-\x7e]|\\[\x09\x20-\x7e\x80-\xff])*"z\\([\x09\x20-\x7e\x80-\xff])c                 C  sD   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_rer7   r   r>   Ú_unquote_reÚsubr?   r8   r@   r/   r/   r0   Úparse_quoted_string{   s   
ÿ rF   z[\x09\x20-\x7e\x80-\xff]*z([\x22\x5c])Úvaluec                 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_rerE   )rG   r7   r/   r/   r0   Úbuild_quoted_string“   s   
rL   Ú
parse_itemú(Callable[[str, int, str], Tuple[T, int]]úList[T]c                 C  sÞ   t ||ƒdkrt||d ƒ}t ||ƒdksg }	 | |||ƒ\}}| |¡ t||ƒ}|t|ƒkr1n4t ||ƒdkr@t||d ƒ}nt |d||¡‚t ||ƒdkr]t||d ƒ}t ||ƒdksO|t|ƒkrdnq|t|ƒksmJ ‚|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
   Tzexpected comma)r5   r9   Úappendr4   r   r>   )rM   r1   r2   r:   ÚitemsÚitemr/   r/   r0   Ú
parse_list    s.   ÿ

ÿÿèrT   úTuple[ConnectionOption, int]c                 C  ó   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.

    )rA   r	   r   ©r1   r2   r:   rS   r/   r/   r0   Úparse_connection_optionä   ó   rX   úList[ConnectionOption]c                 C  ó   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)rT   rX   ©r1   r/   r/   r0   r   ô   ó   r   z>[-!#$%&\'*+.^_`|~0-9a-zA-Z]+(?:/[-!#$%&\'*+.^_`|~0-9a-zA-Z]+)?úTuple[UpgradeProtocol, int]c                 C  s:   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_rer7   r   r>   r	   r   r?   r8   r@   r/   r/   r0   Úparse_upgrade_protocol	  s   ÿra   úList[UpgradeProtocol]c                 C  r[   )zÆ
    Parse an ``Upgrade`` header.

    Return a list of HTTP protocols.

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

    Raises:
        InvalidHeaderFormat: on invalid inputs.

    r   ÚUpgrade)rT   ra   r]   r/   r/   r0   r     r^   r   úTuple[ExtensionParameter, int]c                 C  s¢   t | ||ƒ\}}t| |ƒ}d}t| |ƒdkrKt| |d ƒ}t| |ƒdkr>|}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
   rH   zinvalid quoted header content)rA   r9   r5   rF   r=   rJ   r   r>   )r1   r2   r:   ÚnamerG   Ú
pos_beforer/   r/   r0   Úparse_extension_item_param-  s    
ÿÿ
rh   úTuple[ExtensionHeader, int]c                 C  st   t | ||ƒ\}}t| |ƒ}g }t| |ƒdkr1t| |d ƒ}t| ||ƒ\}}| |¡ t| |ƒdks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
   )rA   r9   r5   rh   rQ   r	   r   )r1   r2   r:   rf   Ú
parametersÚ	parameterr/   r/   r0   Úparse_extension_itemQ  s   

ýrm   úList[ExtensionHeader]c                 C  r[   )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)rT   rm   r]   r/   r/   r0   r   j  s   r   rf   r   rk   úList[ExtensionParameter]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 )Nre   r/   )Ú.0rf   rG   r/   r/   r0   Ú
<listcomp>’  s    ÿþz(build_extension_item.<locals>.<listcomp>)Újoinr	   r   )rf   rk   r/   r/   r0   Úbuild_extension_item‡  s   	
ýÿÿrs   Ú
extensionsúSequence[ExtensionHeader]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)rs   )rp   rf   rk   r/   r/   r0   Ú	<genexpr>¡  s   € 
ÿz"build_extension.<locals>.<genexpr>©rr   )rt   r/   r/   r0   r   š  s   
ÿr   úTuple[Subprotocol, int]c                 C  rV   )zº
    Parse a subprotocol from ``header`` at the given position.

    Return the subprotocol value and the new position.

    Raises:
        InvalidHeaderFormat: on invalid inputs.

    )rA   r	   r   rW   r/   r/   r0   Úparse_subprotocol_item©  rY   rz   úList[Subprotocol]c                 C  r[   )z 
    Parse a ``Sec-WebSocket-Protocol`` header.

    Return a list of WebSocket subprotocols.

    Raises:
        InvalidHeaderFormat: on invalid inputs.

    r   zSec-WebSocket-Protocol)rT   rz   r]   r/   r/   r0   r   ¹  s   
r   ÚsubprotocolsúSequence[Subprotocol]c                 C  s
   d  | ¡S )zl
    Build a ``Sec-WebSocket-Protocol`` header.

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

    rv   rx   )r|   r/   r/   r0   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   r=   rJ   r,   )r|   Úsubprotocolr/   r/   r0   r   Ö  s   


ÿÿr   Úrealmc                 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=)rL   )r‚   Úcharsetr/   r/   r0   r   ä  s   	r   z[A-Za-z0-9-._~+/]+=*c                 C  r<   )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_rer7   r   r>   r?   r8   r@   r/   r/   r0   Úparse_token68õ  s   
ÿr…   c                 C  s    |t | ƒk rt |d| |¡‚dS )z8
    Check that parsing reached the end of header.

    ztrailing dataN)r4   r   r>   )r1   r2   r:   r/   r/   r0   Ú	parse_end  s   ÿr†   úTuple[str, str]c                 C  sÞ   t | ddƒ\}}| ¡ dkrt dd|› �¡‚t| |ƒdkr&t dd| |¡‚|d7 }t| |dƒ\}}t| |dƒ zt 	| 
¡ ¡ ¡ }W n tjyR   t dd¡d	‚w z| d
d¡\}}W ||fS  tyn   t dd¡d	‚w )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)rA   Úlowerr   ÚInvalidHeaderValuer5   r>   r…   r†   Úbase64Ú	b64decodeÚencodeÚdecodeÚbinasciiÚErrorÚsplitr,   )r1   Úschemer2   Úbasic_credentialsÚ	user_passÚusernameÚpasswordr/   r/   r0   r     sD   þÿþýÿúþýÿr   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 )r�   Ú	b64encoder�   r�   )r—   r˜   r–   r•   r/   r/   r0   r   @  s   r   )r   r   r    r!   r"   r#   r$   r   )r1   r   r2   r!   r$   r3   )r1   r   r2   r!   r$   r!   )r1   r   r2   r!   r:   r   r$   r;   )rG   r   r$   r   )
rM   rN   r1   r   r2   r!   r:   r   r$   rO   )r1   r   r2   r!   r:   r   r$   rU   )r1   r   r$   rZ   )r1   r   r2   r!   r:   r   r$   r_   )r1   r   r$   rb   )r1   r   r2   r!   r:   r   r$   rd   )r1   r   r2   r!   r:   r   r$   ri   )r1   r   r$   rn   )rf   r   rk   ro   r$   r   )rt   ru   r$   r   )r1   r   r2   r!   r:   r   r$   ry   )r1   r   r$   r{   )r|   r}   r$   r   )r|   r}   r$   r~   )r‚   r   r$   r   )r1   r   r2   r!   r:   r   r$   r~   )r1   r   r$   r‡   )r—   r   r˜   r   r$   r   )>Ú
__future__r   r�   r‘   r*   ÚreÚtypingr   r   r   r   r   r   r	   Ú r   r   r   r   r   r   r   Ú__all__r   r   r5   Úcompiler6   r9   r=   rA   rC   rD   rF   rI   rK   rL   rT   rX   r   r`   ra   r   rh   rm   r   Úparse_extension_listrs   r   Úbuild_extension_listrz   r   Úparse_subprotocol_listr   Úbuild_subprotocol_listr   r   r„   r…   r†   r   r   r/   r/   r/   r0   Ú<module>   sf    $ 






ÿ






D
ÿ



$












	0