o �J�h�@�@s�ddlmZddlZddlZddlZddlZddlmZddlm Z m Z m Z ddl m Z mZddlmZmZmZmZmZmZgd�Ze d �Zd d �dmdd�Zdndd�Ze�d�Zdodd�Ze�d�Zdpd!d"�Ze�d#�Ze�d$�Zdpd%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�Z4e4Z5dd\d]�Z6d�d_d`�Z7e�da�Z8dpdbdc�Z9d�ddde�Z:d�dgdh�Z;d�dkdl�Z<dS)��)� 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�returncCs^zt�|�}Wn tyYn w|jdkrd|�d�}|s&||r#dndkr-|�d|��}|S)z# Build a ``Host`` header. ��[�]i��P�:)� ipaddress� ip_address� ValueError�version)rrr!r�address�r.�PC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\websockets\headers.pyr's  �  r�header�pos� str | NonecCs|t|�krdS||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)r0r1r.r.r/� peek_aheadIs r4z[\t ]*cCs t�||�}|dus 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)r0r1r6r.r.r/� parse_OWSXs r8z[-!#$%&\'*+.^_`|~0-9a-zA-Z]+� header_name�tuple[str, int]cC�2t�||�}|durt|d||��|��|��fS)z� Parse a token from ``header`` at the given position. Return the token value and the new position. Raises: InvalidHeaderFormat: On invalid inputs. Nzexpected token)� _token_rer6r�groupr7�r0r1r9r6r.r.r/� parse_tokenj� r?zC"(?:[\x09\x20-\x21\x23-\x5b\x5d-\x7e]|\\[\x09\x20-\x7e\x80-\xff])*"z\\([\x09\x20-\x7e\x80-\xff])cCsBt�||�}|durt|d||��t�d|��dd��|��fS)z� Parse a quoted string from ``header`` at the given position. Return the unquoted value and the new position. Raises: InvalidHeaderFormat: On invalid inputs. Nzexpected quoted stringz\1r�����)�_quoted_string_rer6r� _unquote_re�subr=r7r>r.r.r/�parse_quoted_string�s  rEz[\x09\x20-\x7e\x80-\xff]*z ([\x22\x5c])�valuecCs.t�|�}|dur td��dt�d|�dS)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)rFr6r.r.r/�build_quoted_string�s rK� parse_item�(Callable[[str, int, str], tuple[T, int]]�list[T]cCs�t||�dkrt||d�}t||�dksg} ||||�\}}|�|�t||�}|t|�kr1n3t||�dkr@t||d�}nt|d||��t||�dkr\t||d�}t||�dksN|t|�krcnq|t|�kslJ�|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. �,rTzexpected comma)r4r8�appendr3r)rLr0r1r9�items�itemr.r.r/� parse_list�s*�   � �rS�tuple[ConnectionOption, int]cC�t|||�\}}tt|�|fS)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?rr �r0r1r9rRr.r.r/�parse_connection_option�� rW�list[ConnectionOption]cC�tt|dd�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)rSrW�r0r.r.r/r�� rz>[-!#$%&\'*+.^_`|~0-9a-zA-Z]+(?:/[-!#$%&\'*+.^_`|~0-9a-zA-Z]+)?�tuple[UpgradeProtocol, int]cCs8t�||�}|durt|d||��tt|���|��fS)z� Parse an Upgrade protocol from ``header`` at the given position. Return the protocol value and the new position. Raises: InvalidHeaderFormat: On invalid inputs. Nzexpected protocol)� _protocol_rer6rrrr=r7r>r.r.r/�parse_upgrade_protocol s r`�list[UpgradeProtocol]cCrZ)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)rSr`r\r.r.r/rr]r�tuple[ExtensionParameter, int]cCs�t|||�\}}t||�}d}t||�dkrJt||d�}t||�dkr=|}t|||�\}}t�|�dur<t|d||��nt|||�\}}t||�}||f|fS)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�=rrGzinvalid quoted header content)r?r8r4rEr<rIr)r0r1r9�namerFZ pos_beforer.r.r/�parse_extension_item_param.s  ��  rf�tuple[ExtensionHeader, int]cCstt|||�\}}t||�}g}t||�dkr1t||d�}t|||�\}}|�|�t||�dkstt|�|f|fS)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?r8r4rfrPrr )r0r1r9re� parameters� parameterr.r.r/�parse_extension_itemRs  �rk�list[ExtensionHeader]cCrZ)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. rzSec-WebSocket-Extensions)rSrkr\r.r.r/rksrrer ri�Sequence[ExtensionParameter]cCs d�tt|�gdd�|D��S)zc Build an extension definition. This is the reverse of :func:`parse_extension_item`. z; cSs*g|]\}}|dur |n|�d|���qS)Nrdr.)�.0rerFr.r.r/� <listcomp>�s��z(build_extension_item.<locals>.<listcomp>)�joinrr)rerir.r.r/�build_extension_item�s ���rq� extensions�Sequence[ExtensionHeader]cCsd�dd�|D��S)zl Build a ``Sec-WebSocket-Extensions`` header. This is the reverse of :func:`parse_extension`. �, css�|] \}}t||�VqdS)N)rq)rnrerir.r.r/� <genexpr>�s� �z"build_extension.<locals>.<genexpr>�rp)rrr.r.r/r�s �r�tuple[Subprotocol, int]cCrU)z� Parse a subprotocol from ``header`` at the given position. Return the subprotocol value and the new position. Raises: InvalidHeaderFormat: On invalid inputs. )r?rrrVr.r.r/�parse_subprotocol_item�rXrx�list[Subprotocol]cCrZ)z� Parse a ``Sec-WebSocket-Protocol`` header. Return a list of WebSocket subprotocols. Raises: InvalidHeaderFormat: On invalid inputs. rzSec-WebSocket-Protocol)rSrxr\r.r.r/r�s r� subprotocols�Sequence[Subprotocol]cCs d�|�S)zl Build a ``Sec-WebSocket-Protocol`` header. This is the reverse of :func:`parse_subprotocol`. rtrv)rzr.r.r/r�s r�NonecCsJt|t�s td��t|t�rtd��|D]}t�|�s"td|����qdS)zT Validate that ``subprotocols`` is suitable for :func:`build_subprotocol`. zsubprotocols must be a listz&subprotocols must be a list, not a strzinvalid subprotocol: N)� isinstancer� TypeErrorrr<rIr+)rz� subprotocolr.r.r/r�s   ��r�realmcCs t|�}td�}d|�d|��S)z� Build a ``WWW-Authenticate`` header for HTTP Basic Auth. Args: realm: Identifier of the protection space. zUTF-8z Basic realm=z , charset=)rK)r��charsetr.r.r/r�s rz[A-Za-z0-9-._~+/]+=*cCr;)z� Parse a token68 from ``header`` at the given position. Return the token value and the new position. Raises: InvalidHeaderFormat: On invalid inputs. Nzexpected token68)� _token68_rer6rr=r7r>r.r.r/� parse_token68�r@r�cCs|t|�kr t|d||��dS)z8 Check that parsing reached the end of header. z trailing dataN)r3r)r0r1r9r.r.r/� parse_ends �r��tuple[str, str]cCs�t|dd�\}}|��dkrtdd|����t||�dkr$tdd||��|d7}t||d�\}}t||d�z t�|� ��� �}Wnt j yOtdd�d �wz |� d d�\}}W||fStyjtdd �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�basiczunsupported scheme: � zexpected space after schemerz#expected base64-encoded credentialsNr(z&expected username:password credentials)r?�lowerr r4rr�r��base64� b64decode�encode�decode�binascii�Error�splitr+)r0�schemer1�basic_credentials� user_pass�username�passwordr.r.r/rsD �� ��� ����rr�r�cCs4d|vsJ�|�d|��}t�|�����}d|S)z� Build an ``Authorization`` header for HTTP Basic Auth. This is the reverse of :func:`parse_authorization_basic`. r(zBasic )r�� b64encoder�r�)r�r�r�r�r.r.r/r?s r) rrrr r!r"rr"r#r)r0rr1r r#r2)r0rr1r r#r )r0rr1r r9rr#r:)rFrr#r) rLrMr0rr1r r9rr#rN)r0rr1r r9rr#rT)r0rr#rY)r0rr1r r9rr#r^)r0rr#ra)r0rr1r r9rr#rc)r0rr1r r9rr#rg)r0rr#rl)rer rirmr#r)rrrsr#r)r0rr1r r9rr#rw)r0rr#ry)rzr{r#r)rzr{r#r|)r�rr#r)r0rr1r r9rr#r|)r0rr#r�)r�rr�rr#r)=� __future__rr�r�r)�re�collections.abcr�typingrrr� exceptionsrr r r r r rr�__all__rrr4�compiler5r8r<r?rBrCrErHrJrKrSrWrr_r`rrfrkrZparse_extension_listrqrZbuild_extension_listrxrZparse_subprotocol_listrZbuild_subprotocol_listrrr�r�r�rrr.r.r.r/�<module>sj    � "   �      B �    $            0
Memory