
    vKgV>                    2   S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKJ	r	J
r
Jr  SSKJrJr  SSKJrJrJrJrJrJr  / SQr\
" S	5      rS+S
 jrS,S jr\R2                  " S5      rS-S jr\R2                  " S5      rS.S jr\R2                  " S5      r\R2                  " S5      rS.S jr \R2                  " S5      r!\R2                  " S5      r"S/S jr#          S0S jr$        S1S jr%S2S jr&\R2                  " S5      r'        S3S jr(S4S jr)        S5S jr*        S6S jr+S7S jr,\,r-      S8S jr.S9S  jr/\/r0        S:S! jr1S;S" jr2\2r3S<S# jr4\4r5S=S$ jr6S>S% jr7\R2                  " S&5      r8S.S' jr9S?S( jr:S@S) jr;SAS* jr<g)B    )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Tc                     [         R                  " U 5      nUR                  S:X  a  SU  S3n X(       a  SOS:w  a  U  SU 3n U $ ! [         a     N#f = f)z
Build a ``Host`` header.

   []i  P   :)	ipaddress
ip_addressversion
ValueError)hostportsecureaddresss       R/var/www/highfloat_scraper/venv/lib/python3.13/site-packages/websockets/headers.pyr   r   '   sg    &&t, ??atfA;Dv2&qK  s   A 
AAc                ,    U[        U 5      :X  a  S$ X   $ )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   C   s     #f+%466;6    z[\t ]*c                V    [         R                  X5      nUc   eUR                  5       $ )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   R   s*     MM&&E99;r1   z[-!#$%&\'*+.^_`|~0-9a-zA-Z]+c                    [         R                  X5      nUc  [        USX5      eUR                  5       UR	                  5       4$ )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<   d   s>     OOF(E}!+/?MM;;=%))+%%r1   zC"(?:[\x09\x20-\x21\x23-\x5b\x5d-\x7e]|\\[\x09\x20-\x7e\x80-\xff])*"z\\([\x09\x20-\x7e\x80-\xff])c                    [         R                  X5      nUc  [        USX5      e[        R	                  SUR                  5       SS 5      UR                  5       4$ )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   |   sR     ##F0E}!+/GUU??5%++-""56		CCr1   z[\x09\x20-\x7e\x80-\xff]*z([\x22\x5c])c                    [         R                  U 5      nUc  [        S5      eS[        R	                  SU 5      -   S-   $ )z\
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                   [        X5      S:X  a  [        XS-   5      n[        X5      S:X  a  M  / n U " XU5      u  pRUR                  U5        [        X5      nU[        U5      :X  a  Ok[        X5      S:X  a  [        XS-   5      nO[	        USX5      e[        X5      S:X  a  [        XS-   5      n[        X5      S:X  a  M  U[        U5      :X  a  OM  U[        U5      :X  d   eU$ )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    > V
!S
(a( V
!S
( E
vK8	T$ #f+ f"c)F!G,C%k3CVQQ %,F!G,C %, #f+- 4 #f+Lr1   c                B    [        XU5      u  p1[        [        U5      U4$ )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%     F5ID $',,r1   c                &    [        [        U SS5      $ )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                    [         R                  X5      nUc  [        USX5      e[        [        UR                  5       5      UR                  5       4$ )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     sG     v+E}!+/BFPP/<<r1   c                &    [        [        U SS5      $ )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                4   [        XU5      u  p1[        X5      nSn[        X5      S:X  aj  [        XS-   5      n[        X5      S:X  a4  Un[        XU5      u  pA[        R                  U5      c  [        USX5      eO[        XU5      u  pA[        X5      nX44U4$ )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     F5ID
F
 CE&#%a(f"c)J,V+FJE ""5)1)!@&  2
 %V+>JE$=#r1   c                    [        XU5      u  p1[        X5      n/ n[        X5      S:X  a>  [        XS-   5      n[        XU5      u  pQUR	                  U5        [        X5      S:X  a  M>  [        [        U5      U4U4$ )z
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   L  s     F5ID
F
 CJ
V
!S
(a(3FM	)$ V
!S
( %z2C77r1   c                &    [        [        U SS5      $ )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   e  s    . *FA7QRRr1   c           
         SR                  [        [        U 5      /U V Vs/ sH  u  pUc  U OU  SU 3PM     snn -   5      $ s  snn f )zW
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     s`     99	c4  *
  * MD$q'88)
	
 
s   Ac                2    SR                  S U  5       5      $ )z`
Build a ``Sec-WebSocket-Extensions`` header.

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

, c              3  :   #    U H  u  p[        X5      v   M     g 7f)N)rj   ).0r^   rc   s      r+   	<genexpr>"build_extension.<locals>.<genexpr>  s      GQ3C4T..zs   rh   )
extensionss    r+   r   r     s"     99 GQ  r1   c                B    [        XU5      u  p1[        [        U5      U4$ )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_itemrt     s$     F5IDT"C''r1   c                &    [        [        U SS5      $ )z
Parse a ``Sec-WebSocket-Protocol`` header.

Return a list of WebSocket subprotocols.

Raises:
    InvalidHeaderFormat: On invalid inputs.

r   zSec-WebSocket-Protocol)rP   rt   rV   s    r+   r   r     s     ,fa9QRRr1   c                $    SR                  U 5      $ )z`
Build a ``Sec-WebSocket-Protocol`` header.

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

rl   rq   )subprotocolss    r+   r   r     s     99\""r1   c                    [        U [        5      (       d  [        S5      e[        U [        5      (       a  [        S5      eU  H+  n[        R                  U5      (       a  M  [        SU 35      e   g)zL
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&   )rw   subprotocols     r+   r   r     sd    
 lH--566,$$@AA#"";//4[MBCC $r1   c                >    [        U 5      n [        S5      nSU  SU 3$ )zq
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                    [         R                  X5      nUc  [        USX5      eUR                  5       UR	                  5       4$ )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     s@     f*E}!+/A6OO;;=%))+%%r1   c                <    U[        U 5      :  a  [        USX5      eg)z0
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                   [        U SS5      u  pUR                  5       S:w  a  [        SSU 35      e[        X5      S:w  a  [	        SSX5      eUS-  n[        XS5      u  p2[        XS5         [        R                  " UR                  5       5      R                  5       n UR                  S
S5      u  pVXV4$ ! [        R                   a    [        SS5      S	ef = f! [         a    [        SS5      S	ef = f)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   	  s%     fa9KF||~  "6(+
 	
 &#%!:F
 	
 1HC*6Hf?+$$%6%=%=%?@GGI	&__S!4  >>  1
 	   4
 	s   /2B9 "C 9"CC6c                    SU ;  d   eU  SU 3n[         R                  " UR                  5       5      R                  5       nSU-   $ )zt
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  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   )rr   zSequence[ExtensionHeader]r   ri   )r.   ri   r/   r   r;   ri   r   ztuple[Subprotocol, int])r.   ri   r   zlist[Subprotocol])rw   Sequence[Subprotocol]r   ri   )rw   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collections.abcr   typingr   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_listrt   r   parse_subprotocol_listr   build_subprotocol_listr   r   r   r   r   r   r    r1   r+   <module>r      sT   "    	 $ * * ?  CL8	7 **Y
 JJ67	&  JJJ 
 jj89D  zz67 JJ'	
5?8?? 
? 	?
 ?D---(+-!- H  zzE
===(+= =$D !!!(+!#!H888(+8 82S4 ' 
%=&	 ' ((((+(( 
S + # + D5 jj01& M-`(r1   