
    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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	Zd+d
Z ej0                  d      Zd,dZ ej0                  d      Zd-dZ ej0                  d      Z ej0                  d      Zd-dZ ej0                  d      Z  ej0                  d      Z!d.dZ"	 	 	 	 	 	 	 	 	 	 d/dZ#	 	 	 	 	 	 	 	 d0dZ$d1dZ% ej0                  d      Z&	 	 	 	 	 	 	 	 d2dZ'd3dZ(	 	 	 	 	 	 	 	 d4dZ)	 	 	 	 	 	 	 	 d5dZ*d6dZ+e+Z,	 	 	 	 	 	 d7dZ-d8dZ.e.Z/	 	 	 	 	 	 	 	 d9d Z0d:d!Z1e1Z2d;d"Z3e3Z4d<d#Z5d=d$Z6 ej0                  d%      Z7d-d&Z8d>d'Z9d?d(Z:d@d)Z;y)A    )annotationsN)CallableSequence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Tc                    	 t        j                  |       }|j                  dk(  rd|  d} 	 ||rdndk7  r|  d| } | S # t        $ r Y w xY w)z#
    Build a ``Host`` header.

       []i  P   :)	ipaddress
ip_addressversion
ValueError)hostportsecureaddresss       G/var/www/openai/venv/lib/python3.12/site-packages/websockets/headers.pyr   r   &   sg    &&t, ??atfA;Dv2&qK  s   ? 	A
Ac                ,    |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)headerposs     r+   
peek_aheadr0   B   s     #f+%466#;6    z[\t ]*c                V    t         j                  | |      }|J |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)r.   r/   r4   s      r+   	parse_OWSr6   Q   s,     MM&#&E99;r1   z[-!#$%&\'*+.^_`|~0-9a-zA-Z]+c                    t         j                  | |      }|t        |d| |      |j                         |j	                         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.

    zexpected token)	_token_rer4   r	   groupr5   r.   r/   header_namer4   s       r+   parse_tokenr<   c   sB     OOFC(E}!+/?MM;;=%))+%%r1   zC"(?:[\x09\x20-\x21\x23-\x5b\x5d-\x7e]|\\[\x09\x20-\x7e\x80-\xff])*"z\\([\x09\x20-\x7e\x80-\xff])c                    t         j                  | |      }|t        |d| |      t        j	                  d|j                         dd       |j                         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.

    zexpected quoted stringz\1r   )_quoted_string_rer4   r	   _unquote_resubr9   r5   r:   s       r+   parse_quoted_stringrB   {   sW     ##FC0E}!+/GQTUU??5%++-""56		CCr1   z[\x09\x20-\x7e\x80-\xff]*z([\x22\x5c])c                ~    t         j                  |       }|t        d      dt        j	                  d|       z   dz   S )zh
    Format ``value`` as a quoted string.

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

    z-invalid characters for quoted-string encoding"z\\\1)_quotable_re	fullmatchr&   	_quote_rerA   )valuer4   s     r+   build_quoted_stringrI      s@     ""5)E}HIIw..44r1   c                   t        ||      dk(  rt        ||dz         }t        ||      dk(  rg }	  | |||      \  }}|j                  |       t        ||      }|t        |      k(  rnkt        ||      dk(  rt        ||dz         }nt	        |d||      t        ||      dk(  rt        ||dz         }t        ||      dk(  r|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   zexpected comma)r0   r6   appendr-   r	   )
parse_itemr.   r/   r;   itemsitems         r+   
parse_listrP      s   > VS
!S
(a( VS
!S
( E
vsK8	cT$ #f+ fc"c)FC!G,C%k3CVSQQ %,FC!G,C %, #f+- 4 #f+Lr1   c                F    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/   r;   rO   s       r+   parse_connection_optionrS      s)     FC5ID# $',,r1   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)rP   rS   r.   s    r+   r   r      s     -vq,GGr1   z>[-!#$%&\'*+.^_`|~0-9a-zA-Z]+(?:/[-!#$%&\'*+.^_`|~0-9a-zA-Z]+)?c                    t         j                  | |      }|t        |d| |      t        t        |j                               |j                         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.

    zexpected protocol)_protocol_rer4   r	   r   r   r9   r5   r:   s       r+   parse_upgrade_protocolrY     sK     vs+E}!+/BFCPP/<<r1   c                &    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)rP   rY   rV   s    r+   r   r     s     ,faCCr1   c                F   t        | ||      \  }}t        | |      }d}t        | |      dk(  rot        | |dz         }t        | |      dk(  r5|}t        | ||      \  }}t        j                  |      t        |d| |      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   rD   zinvalid quoted header content)r<   r6   r0   rB   r8   rF   r	   )r.   r/   r;   namerH   
pos_befores         r+   parse_extension_item_paramr`   '  s     FC5ID#
FC
 CE&##%a(fc"c)J,VS+FJE3 ""5)1)!@&*  %VS+>JE3$%=#r1   c                   t        | ||      \  }}t        | |      }g }t        | |      dk(  r@t        | |dz         }t        | ||      \  }}|j	                  |       t        | |      dk(  r@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   r0   r`   rL   r   r   )r.   r/   r;   r^   
parameters	parameters         r+   parse_extension_itemre   K  s     FC5ID#
FC
 CJ
VS
!S
(a(3FCM	3)$ VS
!S
( %z2C77r1   c                &    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)rP   re   rV   s    r+   r   r   d  s    . *FA7QRRr1   c           
         dj                  t        t        |       g|D  cg c]  \  } }|| n|  d|  c}} z         S c c}} w )zc
    Build an extension definition.

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

    z; r]   )joinr   str)r^   rc   rH   s      r+   build_extension_itemrj     sb     99	c4  *
  *e MD$q'88)
	
 
s   A c                2    dj                  d | D              S )zl
    Build a ``Sec-WebSocket-Extensions`` header.

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

    , c              3  :   K   | ]  \  }}t        ||        y w)N)rj   ).0r^   rc   s      r+   	<genexpr>z"build_extension.<locals>.<genexpr>  s!      GQ3C4T:.zs   rh   )
extensionss    r+   r   r     s"     99 GQ  r1   c                F    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.

    )r<   r   r   rR   s       r+   parse_subprotocol_itemrs     s(     FC5ID#T"C''r1   c                &    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)rP   rs   rV   s    r+   r   r     s     ,fa9QRRr1   c                $    dj                  |       S )zl
    Build a ``Sec-WebSocket-Protocol`` header.

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

    rl   rp   )subprotocolss    r+   r   r     s     99\""r1   c                    t        | t              st        d      t        | t              rt        d      | D ]%  }t        j                  |      rt        d|        y)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   	TypeErrorri   r8   rF   r&   )rv   subprotocols     r+   r   r     s[    
 lH-566,$@AA#"";/4[MBCC $r1   c                >    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   )realmcharsets     r+   r   r     s+      &E!'*G%
7)44r1   z[A-Za-z0-9-._~+/]+=*c                    t         j                  | |      }|t        |d| |      |j                         |j	                         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.

    zexpected token68)_token68_rer4   r	   r9   r5   r:   s       r+   parse_token68r     sD     fc*E}!+/A63OO;;=%))+%%r1   c                <    |t        |       k  rt        |d| |      y)z8
    Check that parsing reached the end of header.

    ztrailing dataN)r-   r	   )r.   r/   r;   s      r+   	parse_endr     s&    
 S[!+LL r1   c                   t        | dd      \  }}|j                         dk7  rt        dd|       t        | |      dk7  rt	        dd| |      |dz  }t        | |d      \  }}t        | |d       	 t        j                  |j                               j                         }	 |j                  d
d      \  }}||fS # t        j                  $ r t        dd      d	w xY w# t        $ r t        dd      d	w xY 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
   r0   r	   r   r   base64	b64decodeencodedecodebinasciiErrorsplitr&   )r.   schemer/   basic_credentials	user_passusernamepasswords          r+   r   r     s5     fa9KFC||~  "6(+
 	
 &##%!:FC
 	
 1HC*63Hsfc?+$$%6%=%=%?@GGI	&__S!4( X >>  1
 	   4
 	s   31B> %C" >!C"C9c                    d| vsJ |  d| }t        j                  |j                               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   8  sQ     h*AhZ(I(()9)9);<CCE'''r1   )r'   ri   r(   intr)   boolreturnri   )r.   ri   r/   r   r   z
str | None)r.   ri   r/   r   r   r   )r.   ri   r/   r   r;   ri   r   ztuple[str, int])rH   ri   r   ri   )
rM   z(Callable[[str, int, str], tuple[T, int]]r.   ri   r/   r   r;   ri   r   zlist[T])r.   ri   r/   r   r;   ri   r   ztuple[ConnectionOption, int])r.   ri   r   zlist[ConnectionOption])r.   ri   r/   r   r;   ri   r   ztuple[UpgradeProtocol, int])r.   ri   r   zlist[UpgradeProtocol])r.   ri   r/   r   r;   ri   r   ztuple[ExtensionParameter, int])r.   ri   r/   r   r;   ri   r   ztuple[ExtensionHeader, int])r.   ri   r   zlist[ExtensionHeader])r^   r   rc   zlist[ExtensionParameter]r   ri   )rq   zSequence[ExtensionHeader]r   ri   )r.   ri   r/   r   r;   ri   r   ztuple[Subprotocol, int])r.   ri   r   zlist[Subprotocol])rv   Sequence[Subprotocol]r   ri   )rv   r   r   None)r|   ri   r   ri   )r.   ri   r/   r   r;   ri   r   r   )r.   ri   r   ztuple[str, str])r   ri   r   ri   r   ri   )<
__future__r   r   r   r#   retypingr   r   r   r   
exceptionsr	   r
   r   r   r   r   r   r   __all__r   r   r0   compiler3   r6   r8   r<   r?   r@   rB   rE   rG   rI   rP   rS   r   rX   rY   r   r`   re   r   parse_extension_listrj   r   build_extension_listrs   r   parse_subprotocol_listr   build_subprotocol_listr   r   r   r   r   r   r    r1   r+   <module>r      sa   "    	 4 4 ?  CL8	7 "**Y
 BJJ67	&  BJJJ 
 bjj89D  rzz67 BJJ'	
5?8?? 
? 	?
 ?D---(+-!- H  rzzE
===(+= =$D !!!(+!#!H888(+8 82S4 ' 
%=&	 ' ((((+(( 
S + # + D5 bjj01& M-`(r1   