o
    Ð­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œdmdd„Zdndd„Ze d¡Zdodd„Ze d¡Zdpd!d"„Ze d#¡Ze d$¡Zdpd%d&„Z e d'¡Z!e d(¡Z"dqd*d+„Z#drd/d0„Z$dsd2d3„Z%dtd5d6„Z&e d7¡Z'dud9d:„Z(dvd<d=„Z)dwd?d@„Z*dxdBdC„Z+dydEdF„Z,e,Z-dzdKdL„Z.d{dOdP„Z/e/Z0d|dRdS„Z1d}dUdV„Z2e2Z3d~dYdZ„Z4e4Z5dd\d]„Z6d€d_d`„Z7e da¡Z8dpdbdc„Z9d�ddde„Z:d‚dgdh„Z;dƒdkdl„Z<dS )„é    )Ú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ÚhostÚstrÚportÚintÚsecureÚboolr   Úreturnc                C  s^   zt  | ¡}W n	 ty   Y nw |jdkrd| › d�} |s&||r#dndkr-| › d|› �} | S )z#
    Build a ``Host`` header.

    é   ú[ú]i»  éP   ú:)Ú	ipaddressÚ
ip_addressÚ
ValueErrorÚversion)r   r   r!   r   Úaddress© r.   úO/var/www/html/CropPilot/venv/lib/python3.10/site-packages/websockets/headers.pyr   '   s   þ
r   ÚheaderÚposú
str | Nonec                 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   	r4   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   r6   r.   r.   r/   Ú	parse_OWSX   s   
r8   z[-!#$%&\'*+.^_`|~0-9a-zA-Z]+Úheader_nameútuple[str, int]c                 C  ó2   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_rer6   r   Úgroupr7   ©r0   r1   r9   r6   r.   r.   r/   Úparse_tokenj   ó   
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_rer6   r   Ú_unquote_reÚsubr=   r7   r>   r.   r.   r/   Úparse_quoted_string‚   s   
 rE   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_rerD   )rF   r6   r.   r.   r/   Úbuild_quoted_string˜   s   
rK   Ú
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r1n3t ||ƒdkr@t||d ƒ}nt|d||ƒ‚t ||ƒdkr\t||d ƒ}t ||ƒdksN|t|ƒkrcnq|t|ƒkslJ ‚|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)r4   r8   Úappendr3   r   )rL   r0   r1   r9   ÚitemsÚitemr.   r.   r/   Ú
parse_list¥   s*   ÿ

ÿêrS   ú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.

    )r?   r   r
   ©r0   r1   r9   rR   r.   r.   r/   Úparse_connection_optionç   ó   rW   ú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)rS   rW   ©r0   r.   r.   r/   r   ÷   ó   r   z>[-!#$%&\'*+.^_`|~0-9a-zA-Z]+(?:/[-!#$%&\'*+.^_`|~0-9a-zA-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_rer6   r   r   r   r=   r7   r>   r.   r.   r/   Úparse_upgrade_protocol  s   r`   úlist[UpgradeProtocol]c                 C  rZ   )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)rS   r`   r\   r.   r.   r/   r     r]   r   útuple[ExtensionParameter, int]c                 C  s    t | ||ƒ\}}t| |ƒ}d}t| |ƒdkrJt| |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   rG   zinvalid quoted header content)r?   r8   r4   rE   r<   rI   r   )r0   r1   r9   ÚnamerF   Ú
pos_beforer.   r.   r/   Úparse_extension_item_param.  s    
ÿÿ
rg   ú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   )r?   r8   r4   rg   rP   r   r   )r0   r1   r9   re   Ú
parametersÚ	parameterr.   r.   r/   Úparse_extension_itemR  s   

ýrl   úlist[ExtensionHeader]c                 C  rZ   )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)rS   rl   r\   r.   r.   r/   r   k  s   r   re   r   rj   úSequence[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 )Nrd   r.   )Ú.0re   rF   r.   r.   r/   Ú
<listcomp>“  s    ÿþz(build_extension_item.<locals>.<listcomp>)Újoinr   r   )re   rj   r.   r.   r/   Úbuild_extension_itemˆ  s   	
ýÿÿrr   Ú
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)rr   )ro   re   rj   r.   r.   r/   Ú	<genexpr>¢  s   € 
ÿz"build_extension.<locals>.<genexpr>©rq   )rs   r.   r.   r/   r   ›  s   
ÿr   útuple[Subprotocol, int]c                 C  rU   )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   rV   r.   r.   r/   Úparse_subprotocol_itemª  rX   ry   úlist[Subprotocol]c                 C  rZ   )z 
    Parse a ``Sec-WebSocket-Protocol`` header.

    Return a list of WebSocket subprotocols.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    r   zSec-WebSocket-Protocol)rS   ry   r\   r.   r.   r/   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`.

    ru   rw   )r{   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   r<   rI   r+   )r{   Úsubprotocolr.   r.   r/   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=)rK   )r�   Úcharsetr.   r.   r/   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_rer6   r   r=   r7   r>   r.   r.   r/   Úparse_token68ö  r@   r„   c                 C  s   |t | ƒk rt|d| |ƒ‚dS )z8
    Check that parsing reached the end of header.

    ztrailing dataN)r3   r   )r0   r1   r9   r.   r.   r/   Ú	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yO   tddƒd	‚w z| d
d¡\}}W ||fS  tyj   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)r?   Úlowerr	   r4   r   r„   r…   Úbase64Ú	b64decodeÚencodeÚdecodeÚbinasciiÚErrorÚsplitr+   )r0   Úschemer1   Úbasic_credentialsÚ	user_passÚusernameÚpasswordr.   r.   r/   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.   r/   r   ?  s   r   )
r   r   r   r    r!   r"   r   r"   r#   r   )r0   r   r1   r    r#   r2   )r0   r   r1   r    r#   r    )r0   r   r1   r    r9   r   r#   r:   )rF   r   r#   r   )
rL   rM   r0   r   r1   r    r9   r   r#   rN   )r0   r   r1   r    r9   r   r#   rT   )r0   r   r#   rY   )r0   r   r1   r    r9   r   r#   r^   )r0   r   r#   ra   )r0   r   r1   r    r9   r   r#   rc   )r0   r   r1   r    r9   r   r#   rh   )r0   r   r#   rm   )re   r   rj   rn   r#   r   )rs   rt   r#   r   )r0   r   r1   r    r9   r   r#   rx   )r0   r   r#   rz   )r{   r|   r#   r   )r{   r|   r#   r}   )r�   r   r#   r   )r0   r   r1   r    r9   r   r#   r}   )r0   r   r#   r†   )r•   r   r–   r   r#   r   )=Ú
__future__r   r‹   r�   r)   ÚreÚcollections.abcr   Útypingr   r   r   Ú
exceptionsr   r	   r
   r   r   r   r   r   Ú__all__r   r   r4   Úcompiler5   r8   r<   r?   rB   rC   rE   rH   rJ   rK   rS   rW   r   r_   r`   r   rg   rl   r   Úparse_extension_listrr   r   Úbuild_extension_listry   r   Úparse_subprotocol_listr   Úbuild_subprotocol_listr   r   rƒ   r„   r…   r   r   r.   r.   r.   r/   Ú<module>   sj     
û
"



ÿ






B
ÿ



$












	0