
    vgV>                    x   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dQdZdRdZ ej        d          ZdSdZ ej        d          ZdTdZ ej        d          Z ej        d          ZdTdZ  ej        d          Z! ej        d           Z"dUd"Z#dVd&Z$dWd(Z%dXd*Z& ej        d+          Z'dYd-Z(dZd/Z)d[d1Z*d\d3Z+d]d5Z,e,Z-d^d:Z.d_d=Z/e/Z0d`d?Z1dadAZ2e2Z3dbdDZ4e4Z5dcdFZ6dddHZ7 ej        dI          Z8dTdJZ9dedKZ:dfdMZ;dgdPZ<dS )h    )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Thoststrportintsecureboolreturnc                    	 t          j        |           }|j        dk    rd|  d} n# t          $ r Y nw xY w||rdndk    r|  d| } | S )z#
    Build a ``Host`` header.

       []i  P   :)	ipaddress
ip_addressversion
ValueError)r   r   r!   addresss       O/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/websockets/headers.pyr   r   '   s    &t,, ?at;;;D     v%2&&Ks   ( 
55headerpos
str | Nonec                <    |t          |           k    rdn| |         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   s     r/   
peek_aheadr5   C   s"     #f++%%446#;6    z[\t ]*c                h    t                               | |          }|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.

    )_OWS_rematchend)r0   r1   r9   s      r/   	parse_OWSr;   R   s1     MM&#&&E99;;r6   z[-!#$%&\'*+.^_`|~0-9a-zA-Z]+header_nametuple[str, int]c                    t                               | |          }|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_rer9   r	   groupr:   r0   r1   r<   r9   s       r/   parse_tokenrB   d   sL     OOFC((E}!+/?MMM;;==%))++%%r6   zC"(?:[\x09\x20-\x21\x23-\x5b\x5d-\x7e]|\\[\x09\x20-\x7e\x80-\xff])*"z\\([\x09\x20-\x7e\x80-\xff])c                    t                               | |          }|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_rer9   r	   _unquote_resubr@   r:   rA   s       r/   parse_quoted_stringrH   |   sf     ##FC00E}!+/GQTUUU??5%++--""566		CCr6   z[\x09\x20-\x7e\x80-\xff]*z([\x22\x5c])valuec                    t                               |           }|t          d          dt                              d|           z   dz   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_rerG   )rI   r9   s     r/   build_quoted_stringrO      sI     ""5))E}HIIIw...44r6   
parse_item(Callable[[str, int, str], tuple[T, int]]list[T]c                R   t          ||          dk    r't          ||dz             }t          ||          dk    'g }	  | |||          \  }}|                    |           t          ||          }|t          |          k    rnt          ||          dk    rt          ||dz             }nt	          |d||          t          ||          dk    r't          ||dz             }t          ||          dk    '|t          |          k    rn|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   Tzexpected comma)r5   r;   appendr4   r	   )rP   r0   r1   r<   itemsitems         r/   
parse_listrX      sX   > VS
!
!S
(
(a(( VS
!
!S
(
( EJvsK88	cT$$ #f++ fc""c))FC!G,,CC%k3CVSQQQ %%,,FC!G,,C %%,, #f++-4 #f++Lr6   tuple[ConnectionOption, int]c                X    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.

    )rB   r   r   r0   r1   r<   rW   s       r/   parse_connection_optionr\      s/     FC55ID# $'',,r6   list[ConnectionOption]c                0    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)rX   r\   r0   s    r/   r   r      s     -vq,GGGr6   z>[-!#$%&\'*+.^_`|~0-9a-zA-Z]+(?:/[-!#$%&\'*+.^_`|~0-9a-zA-Z]+)?tuple[UpgradeProtocol, int]c                    t                               | |          }|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_rer9   r	   r   r   r@   r:   rA   s       r/   parse_upgrade_protocolrd     sX     vs++E}!+/BFCPPP//<<r6   list[UpgradeProtocol]c                0    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   Upgrade)rX   rd   r`   s    r/   r   r     s     ,faCCCr6   tuple[ExtensionParameter, int]c                   t          | ||          \  }}t          | |          }d}t          | |          dk    rt          | |dz             }t          | |          dk    rC|}t          | ||          \  }}t                              |          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   rK   zinvalid quoted header content)rB   r;   r5   rH   r?   rM   r	   )r0   r1   r<   namerI   
pos_befores         r/   parse_extension_item_paramrm   (  s     FC55ID#
FC
 
 CE&##%%a((fc""c))J,VS+FFJE3 ""5))1)!@&*   2
 %VS+>>JE3$$%=#r6   tuple[ExtensionHeader, int]c                H   t          | ||          \  }}t          | |          }g }t          | |          dk    rPt          | |dz             }t          | ||          \  }}|                    |           t          | |          dk    Pt          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   )rB   r;   r5   rm   rU   r   r   )r0   r1   r<   rk   
parameters	parameters         r/   parse_extension_itemrs   L  s     FC55ID#
FC
 
 CJ
VS
!
!S
(
(a((3FCMM	3)$$$ VS
!
!S
(
( %%z2C77r6   list[ExtensionHeader]c                0    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)rX   rs   r`   s    r/   r   r   e  s    . *FA7QRRRr6   rk   r   rq   list[ExtensionParameter]c                n    d                     t          t          |           gd |D             z             S )zc
    Build an extension definition.

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

    z; c                *    g | ]\  }}||n| d| S )Nrj    ).0rk   rI   s      r/   
<listcomp>z(build_extension_item.<locals>.<listcomp>  sD     
 
 
 e MDD$'8'8'8'8
 
 
r6   )joinr   r   )rk   rq   s     r/   build_extension_itemr}     sK     99	c4
 
  *
 
 
	
  r6   
extensionsSequence[ExtensionHeader]c                @    d                     d | D                       S )zl
    Build a ``Sec-WebSocket-Extensions`` header.

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

    , c              3  <   K   | ]\  }}t          ||          V  d S )N)r}   )rz   rk   rq   s      r/   	<genexpr>z"build_extension.<locals>.<genexpr>  sB        3C4T:..     r6   r|   )r~   s    r/   r   r     s6     99  GQ     r6   tuple[Subprotocol, int]c                X    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.

    )rB   r   r   r[   s       r/   parse_subprotocol_itemr     s.     FC55ID#T""C''r6   list[Subprotocol]c                0    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)rX   r   r`   s    r/   r   r     s     ,fa9QRRRr6   subprotocolsSequence[Subprotocol]c                ,    d                     |           S )zl
    Build a ``Sec-WebSocket-Protocol`` header.

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

    r   r   )r   s    r/   r   r     s     99\"""r6   Nonec                    t          | t                    st          d          t          | t                    rt          d          | D ].}t                              |          st          d|           /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?   rM   r-   )r   subprotocols     r/   r   r     s    
 lH-- 75666,$$ B@AAA# D D"";// 	DB[BBCCC	DD Dr6   realmc                N    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=)rO   )r   charsets     r/   r   r     s4      &&E!'**G4%447444r6   z[A-Za-z0-9-._~+/]+=*c                    t                               | |          }|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_rer9   r	   r@   r:   rA   s       r/   parse_token68r     sN     fc**E}!+/A63OOO;;==%))++%%r6   c                P    |t          |           k     rt          |d| |          dS )z8
    Check that parsing reached the end of header.

    ztrailing dataN)r4   r	   )r0   r1   r<   s      r/   	parse_endr      s1    
 S[[!+LLL r6   tuple[str, str]c                X   t          | dd          \  }}|                                dk    rt          dd|           t          | |          dk    rt	          dd| |          |dz  }t          | |d          \  }}t          | |d           	 t          j        |	                                          
                                }n$# t          j        $ r t          dd          d	w xY w	 |                    d
d          \  }}n# t          $ r t          dd          d	w xY w||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)rB   lowerr
   r5   r	   r   r   base64	b64decodeencodedecodebinasciiErrorsplitr-   )r0   schemer1   basic_credentials	user_passusernamepasswords          r/   r   r   	  s     fa99KFC||~~   +6++
 
 	
 &##%%!:FC
 
 	
 1HC*63HHsfc?+++$%6%=%=%?%?@@GGII		>    1
 
 	
&__S!44((    4
 
 	 Xs   8C
 
!C+/D	 	D%r   r   c                    d| vsJ |  d| }t          j        |                                                                          }d|z   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   s       r/   r   r   9  s]     h((h((I()9)9););<<CCEE'''r6   )r   r   r   r    r!   r"   r#   r   )r0   r   r1   r    r#   r2   )r0   r   r1   r    r#   r    )r0   r   r1   r    r<   r   r#   r=   )rI   r   r#   r   )
rP   rQ   r0   r   r1   r    r<   r   r#   rR   )r0   r   r1   r    r<   r   r#   rY   )r0   r   r#   r]   )r0   r   r1   r    r<   r   r#   ra   )r0   r   r#   re   )r0   r   r1   r    r<   r   r#   rh   )r0   r   r1   r    r<   r   r#   rn   )r0   r   r#   rt   )rk   r   rq   rv   r#   r   )r~   r   r#   r   )r0   r   r1   r    r<   r   r#   r   )r0   r   r#   r   )r   r   r#   r   )r   r   r#   r   )r   r   r#   r   )r0   r   r1   r    r<   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   r5   compiler8   r;   r?   rB   rE   rF   rH   rL   rN   rO   rX   r\   r   rc   rd   r   rm   rs   r   parse_extension_listr}   r   build_extension_listr   r   parse_subprotocol_listr   build_subprotocol_listr   r   r   r   r   r   r   ry   r6   r/   <module>r      s   " " " " " "       				 $ $ $ $ $ $ * * * * * * * * * * ? ? ? ? ? ? ? ?                  GCLL   8	7 	7 	7 	7 "*Y

    BJ677	& & & &  BJJ  
 bj899D D D D  rz677 BJ''	
5 
5 
5 
5? ? ? ?D- - - - H H H H  rzE 
= = = =$D D D D ! ! ! !H8 8 8 82S S S S4 '    &	 	 	 	 ' ( ( ( ( 
S 
S 
S 
S + # # # # + D D D D5 5 5 5 bj011& & & & M M M M- - - -`( ( ( ( ( (r6   