
    g>                    ~   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dTdZdUdZ ej        d          ZdVdZ ej        d          ZdWdZ ej        d          Z ej        d           ZdWd!Z  ej        d"          Z! ej        d#          Z"dXd%Z#dYd)Z$dZd+Z%d[d-Z& ej        d.          Z'd\d0Z(d]d2Z)d^d4Z*d_d6Z+d`d8Z,e,Z-dad=Z.dbd@Z/e/Z0dcdBZ1dddDZ2e2Z3dedGZ4e4Z5dfdIZ6dgdKZ7 ej        dL          Z8dWdMZ9dhdNZ:didPZ;djdSZ<dS )k    )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                   	 t          j        |           }|j        dk    rd|  d} n# t          $ r Y nw xY w|s
||rdndk    r|  d| } | S )z#
    Build a ``Host`` header.

       []i  P   :)	ipaddress
ip_addressversion
ValueError)r   r    r"   r   addresss        ^/var/www/html/nodeJS/PythonScripts/venv3.11/lib/python3.11/site-packages/websockets/headers.pyr   r   '   s    &t,, ?at;;;D       df'<ss"==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)r1   r2   s     r0   
peek_aheadr6   I   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)r1   r2   r:   s      r0   	parse_OWSr<   X   s1     MM&#&&E99;;r7   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_rer:   r	   groupr;   r1   r2   r=   r:   s       r0   parse_tokenrC   j   sL     OOFC((E}!+/?MMM;;==%))++%%r7   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_rer:   r	   _unquote_resubrA   r;   rB   s       r0   parse_quoted_stringrI      sf     ##FC00E}!+/GQTUUU??5%++--""566		CCr7   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_rerH   )rJ   r:   s     r0   build_quoted_stringrP      sI     ""5))E}HIIIw...44r7   
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)r6   r<   appendr5   r	   )rQ   r1   r2   r=   itemsitems         r0   
parse_listrY      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++Lr7   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.

    )rC   r   r   r1   r2   r=   rX   s       r0   parse_connection_optionr]      s/     FC55ID# $'',,r7   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)rY   r]   r1   s    r0   r   r      s     -vq,GGGr7   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_rer:   r	   r   r   rA   r;   rB   s       r0   parse_upgrade_protocolre     sX     vs++E}!+/BFCPPP//<<r7   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)rY   re   ra   s    r0   r   r     s     ,faCCCr7   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   rL   zinvalid quoted header content)rC   r<   r6   rI   r@   rN   r	   )r1   r2   r=   namerJ   
pos_befores         r0   parse_extension_item_paramrn   .  s     FC55ID#
FC
 
 CE&##%%a((fc""c))J,VS+FFJE3 ""5))1)!@&*   2
 %VS+>>JE3$$%=#r7   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   )rC   r<   r6   rn   rV   r   r   )r1   r2   r=   rl   
parameters	parameters         r0   parse_extension_itemrt   R  s     FC55ID#
FC
 
 CJ
VS
!
!S
(
(a((3FCMM	3)$$$ VS
!
!S
(
( %%z2C77r7   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)rY   rt   ra   s    r0   r   r   k  s    . *FA7QRRRr7   rl   r   rr   Sequence[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 )Nrk    ).0rl   rJ   s      r0   
<listcomp>z(build_extension_item.<locals>.<listcomp>  sD     
 
 
 e MDD$'8'8'8'8
 
 
r7   )joinr   r   )rl   rr   s     r0   build_extension_itemr~     sK     99	c4
 
  *
 
 
	
  r7   
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~   )r{   rl   rr   s      r0   	<genexpr>z"build_extension.<locals>.<genexpr>  sB        3C4T:..     r7   r}   )r   s    r0   r   r     s6     99  GQ     r7   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.

    )rC   r   r   r\   s       r0   parse_subprotocol_itemr     s.     FC55ID#T""C''r7   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)rY   r   ra   s    r0   r   r     s     ,fa9QRRRr7   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    r0   r   r     s     99\"""r7   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@   rN   r.   )r   subprotocols     r0   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r7   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=)rP   )r   charsets     r0   r   r     s4      &&E!'**G4%447444r7   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_rer:   r	   rA   r;   rB   s       r0   parse_token68r     sN     fc**E}!+/A63OOO;;==%))++%%r7   c                P    |t          |           k     rt          |d| |          dS )z8
    Check that parsing reached the end of header.

    ztrailing dataN)r5   r	   )r1   r2   r=   s      r0   	parse_endr     s1    
 S[[!+LLL r7   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)rC   lowerr
   r6   r	   r   r   base64	b64decodeencodedecodebinasciiErrorsplitr.   )r1   schemer2   basic_credentials	user_passusernamepasswords          r0   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       r0   r   r   ?  s]     h((h((I()9)9););<<CCEE'''r7   )
r   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>   )rJ   r   r$   r   )
rQ   rR   r1   r   r2   r!   r=   r   r$   rS   )r1   r   r2   r!   r=   r   r$   rZ   )r1   r   r$   r^   )r1   r   r2   r!   r=   r   r$   rb   )r1   r   r$   rf   )r1   r   r2   r!   r=   r   r$   ri   )r1   r   r2   r!   r=   r   r$   ro   )r1   r   r$   ru   )rl   r   rr   rw   r$   r   )r   r   r$   r   )r1   r   r2   r!   r=   r   r$   r   )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collections.abcr   typingr   r   r   
exceptionsr	   r
   r   r   r   r   r   r   __all__r   r   r6   compiler9   r<   r@   rC   rF   rG   rI   rM   rO   rP   rY   r]   r   rd   re   r   rn   rt   r   parse_extension_listr~   r   build_extension_listr   r   parse_subprotocol_listr   build_subprotocol_listr   r   r   r   r   r   r   rz   r7   r0   <module>r      s   " " " " " "       				 $ $ $ $ $ $ * * * * * * * * * * ? ? ? ? ? ? ? ?                  GCLL !&     D	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- - - -`( ( ( ( ( (r7   