o
    mh>                     @  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
 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Z-e-Z.dxdNdOZ/dydQdRZ0e0Z1dzdUdVZ2e2Z3d{dXdYZ4d|d[d\Z5ed]Z6dld^d_Z7d}d`daZ8d~dcddZ9ddgdhZ:dS )    )annotationsN)CallableSequence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,   J/var/www/html/zoom/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)r.   r/   r,   r,   r-   
peek_aheadB   s   	r2   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)r.   r/   r4   r,   r,   r-   	parse_OWSQ   s   
r6   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_rer4   r   InvalidHeaderFormatgroupr5   r.   r/   r7   r4   r,   r,   r-   parse_tokenc   s   
r>   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_rer4   r   r;   _unquote_resubr<   r5   r=   r,   r,   r-   parse_quoted_string{   s   
 rC   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_rerB   )rD   r4   r,   r,   r-   build_quoted_string   s   
rI   
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)r2   r6   appendr1   r   r;   )rJ   r.   r/   r7   itemsitemr,   r,   r-   
parse_list   s.   

rQ   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	   r.   r/   r7   rP   r,   r,   r-   parse_connection_option      rU   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)rQ   rU   r.   r,   r,   r-   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_rer4   r   r;   r   r   r<   r5   r=   r,   r,   r-   parse_upgrade_protocol	  s   r^   list[UpgradeProtocol]c                 C  rX   )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)rQ   r^   rZ   r,   r,   r-   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   rE   zinvalid quoted header content)r>   r6   r2   rC   r:   rG   r   r;   )r.   r/   r7   namerD   
pos_beforer,   r,   r-   parse_extension_item_param-  s    

re   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>   r6   r2   re   rN   r   r   )r.   r/   r7   rc   
parameters	parameterr,   r,   r-   parse_extension_itemQ  s   

rj   list[ExtensionHeader]c                 C  rX   )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)rQ   rj   rZ   r,   r,   r-   r   j  s   r   rc   r   rh   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 )Nrb   r,   ).0rc   rD   r,   r,   r-   
<listcomp>  s    z(build_extension_item.<locals>.<listcomp>)joinr   r   )rc   rh   r,   r,   r-   build_extension_item  s   	
rp   
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)rp   )rm   rc   rh   r,   r,   r-   	<genexpr>  s    
z"build_extension.<locals>.<genexpr>ro   )rq   r,   r,   r-   r     s   
r   tuple[Subprotocol, int]c                 C  rS   )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   rT   r,   r,   r-   parse_subprotocol_item  rV   rw   list[Subprotocol]c                 C  rX   )z
    Parse a ``Sec-WebSocket-Protocol`` header.

    Return a list of WebSocket subprotocols.

    Raises:
        InvalidHeaderFormat: On invalid inputs.

    r   zSec-WebSocket-Protocol)rQ   rw   rZ   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`.

    rs   ru   )ry   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:   rG   r)   )ry   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=)rI   )r   charsetr,   r,   r-   r     s   	r   z[A-Za-z0-9-._~+/]+=*c                 C  r9   )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_rer4   r   r;   r<   r5   r=   r,   r,   r-   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)r1   r   r;   )r.   r/   r7   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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)r>   lowerr   InvalidHeaderValuer2   r;   r   r   base64	b64decodeencodedecodebinasciiErrorsplitr)   )r.   schemer/   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   r/   r   r!   r0   )r.   r   r/   r   r!   r   )r.   r   r/   r   r7   r   r!   r8   )rD   r   r!   r   )
rJ   rK   r.   r   r/   r   r7   r   r!   rL   )r.   r   r/   r   r7   r   r!   rR   )r.   r   r!   rW   )r.   r   r/   r   r7   r   r!   r\   )r.   r   r!   r_   )r.   r   r/   r   r7   r   r!   ra   )r.   r   r/   r   r7   r   r!   rf   )r.   r   r!   rk   )rc   r   rh   rl   r!   r   )rq   rr   r!   r   )r.   r   r/   r   r7   r   r!   rv   )r.   r   r!   rx   )ry   rz   r!   r   )ry   rz   r!   r{   )r   r   r!   r   )r.   r   r/   r   r7   r   r!   r{   )r.   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   __all__r   r   r2   compiler3   r6   r:   r>   r@   rA   rC   rF   rH   rI   rQ   rU   r   r]   r^   r   re   rj   r   parse_extension_listrp   r   build_extension_listrw   r   parse_subprotocol_listr   build_subprotocol_listr   r   r   r   r   r   r   r,   r,   r,   r-   <module>   sf     













D




$












	0