
    2Bf>                        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
mZmZmZ ddl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j4                  d      Zd,dZ ej4                  d      Zd-dZ ej4                  d      Z ej4                  d      Z d-dZ! ej4                  d      Z" ej4                  d      Z#d.dZ$	 	 	 	 	 	 	 	 	 	 d/dZ%	 	 	 	 	 	 	 	 d0dZ&d1dZ' ej4                  d      Z(	 	 	 	 	 	 	 	 d2dZ)d3dZ*	 	 	 	 	 	 	 	 d4dZ+	 	 	 	 	 	 	 	 d5dZ,d6dZ-e-Z.	 	 	 	 	 	 d7dZ/d8dZ0e0Z1	 	 	 	 	 	 	 	 d9d Z2d:d!Z3e3Z4d;d"Z5e5Z6d<d#Z7d=d$Z8 ej4                  d%      Z9d-d&Z:d>d'Z;d?d(Z<d@d)Z=y)A    )annotationsN)CallableListOptionalSequenceTupleTypeVarcast   )
exceptions)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       R/var/www/highfloat_scraper/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_aheadr2   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)r0   r1   r6   s      r-   	parse_OWSr8   Q   s,     MM&#&E99;r3   z[-!#$%&\'*+.^_`|~0-9a-zA-Z]+c                    t         j                  | |      }|t        j                  |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_rer6   r   InvalidHeaderFormatgroupr7   r0   r1   header_namer6   s       r-   parse_tokenr?   c   sI     OOFC(E},,[:JFTWXX;;=%))+%%r3   zC"(?:[\x09\x20-\x21\x23-\x5b\x5d-\x7e]|\\[\x09\x20-\x7e\x80-\xff])*"z\\([\x09\x20-\x7e\x80-\xff])c                    t         j                  | |      }|t        j                  |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_rer6   r   r;   _unquote_resubr<   r7   r=   s       r-   parse_quoted_stringrE   {   sc     ##FC0E},,163
 	
 ??5%++-""56		CCr3   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_rerD   )valuer6   s     r-   build_quoted_stringrL      s@     ""5)E}HIIw..44r3   c                   t        ||      dk(  rt        ||dz         }t        ||      dk(  rg }	  | |||      \  }}|j                  |       t        ||      }|t        |      k(  rnut        ||      dk(  rt        ||dz         }nt	        j
                  |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)r2   r8   appendr/   r   r;   )
parse_itemr0   r1   r>   itemsitems         r-   
parse_listrS      s   > VS
!S
(a( VS
!S
( E
vsK8	cT$ #f+ fc"c)FC!G,C00-vs 
 %,FC!G,C %, #f+1 8 #f+Lr3   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   r0   r1   r>   rR   s       r-   parse_connection_optionrV      s)     FC5ID# $',,r3   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)rS   rV   r0   s    r-   r   r      s     -vq,GGr3   z>[-!#$%&\'*+.^_`|~0-9a-zA-Z]+(?:/[-!#$%&\'*+.^_`|~0-9a-zA-Z]+)?c                    t         j                  | |      }|t        j                  |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_rer6   r   r;   r
   r   r<   r7   r=   s       r-   parse_upgrade_protocolr\   	  sX     vs+E},,,fc
 	
 /<<r3   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)rS   r\   rY   s    r-   r   r     s     ,faCCr3   c                Z   t        | ||      \  }}t        | |      }d}t        | |      dk(  ryt        | |dz         }t        | |      dk(  r?|}t        | ||      \  }}t        j                  |      (t        j                  |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   rG   zinvalid quoted header content)r?   r8   r2   rE   r:   rI   r   r;   )r0   r1   r>   namerK   
pos_befores         r-   parse_extension_item_paramrc   -  s     FC5ID#
FC
 CE&##%a(fc"c)J,VS+FJE3 ""5)1 44!@&*  %VS+>JE3$%=#r3   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?   r8   r2   rc   rO   r
   r   )r0   r1   r>   ra   
parameters	parameters         r-   parse_extension_itemrh   Q  s     FC5ID#
FC
 CJ
VS
!S
(a(3FCM	3)$ VS
!S
( %z2C77r3   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)rS   rh   rY   s    r-   r   r   j  s    . *FA7QRRr3   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)ra   rf   rK   s      r-   build_extension_itemrm     s]     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)rm   ).0ra   rf   s      r-   	<genexpr>z"build_extension.<locals>.<genexpr>  s!      3C4T:.s   rk   )
extensionss    r-   r   r     s"     99 GQ  r3   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   rU   s       r-   parse_subprotocol_itemrv     s(     FC5ID#T"C''r3   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)rS   rv   rY   s    r-   r   r     s     ,fa9QRRr3   c                $    dj                  |       S )zl
    Build a ``Sec-WebSocket-Protocol`` header.

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

    ro   rs   )subprotocolss    r-   r   r     s     99\""r3   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   	TypeErrorrl   r:   rI   r(   )ry   subprotocols     r-   r   r     sb    
 lH-566,$@AA# D"";/4[MBCCDr3   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=)rL   )realmcharsets     r-   r   r     s+      &E!'*G%
7)44r3   z[A-Za-z0-9-._~+/]+=*c                    t         j                  | |      }|t        j                  |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_rer6   r   r;   r<   r7   r=   s       r-   parse_token68r     sQ     fc*E},,+VS
 	
 ;;=%))+%%r3   c                P    |t        |       k  rt        j                  |d| |      y)z8
    Check that parsing reached the end of header.

    ztrailing dataN)r/   r   r;   )r0   r1   r>   s      r-   	parse_endr     s-    
 S[,,[/6SVWW r3   c                H   t        | dd      \  }}|j                         dk7  rt        j                  dd|       t	        | |      dk7  rt        j
                  dd| |      |dz  }t        | |d      \  }}t        | |d       	 t        j                  |j                               j                         }	 |j                  d
d      \  }}||fS # t        j                  $ r t        j                  dd      d	w xY w# t        $ r t        j                  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   InvalidHeaderValuer2   r;   r   r   base64	b64decodeencodedecodebinasciiErrorsplitr(   )r0   schemer1   basic_credentials	user_passusernamepasswords          r-   r   r     sM     fa9KFC||~ ++"6(+
 	
 &##%,,:FC
 	
 1HC*63Hsfc?+$$%6%=%=%?@GGI	&__S!4( X >> ++1
 	  ++4
 	s   1C 9D  +C= !D!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   @  sQ     h*AhZ(I(()9)9);<CCE'''r3   )r)   rl   r*   intr+   boolreturnrl   )r0   rl   r1   r   r   zOptional[str])r0   rl   r1   r   r   r   )r0   rl   r1   r   r>   rl   r   zTuple[str, int])rK   rl   r   rl   )
rP   z(Callable[[str, int, str], Tuple[T, int]]r0   rl   r1   r   r>   rl   r   zList[T])r0   rl   r1   r   r>   rl   r   zTuple[ConnectionOption, int])r0   rl   r   zList[ConnectionOption])r0   rl   r1   r   r>   rl   r   zTuple[UpgradeProtocol, int])r0   rl   r   zList[UpgradeProtocol])r0   rl   r1   r   r>   rl   r   zTuple[ExtensionParameter, int])r0   rl   r1   r   r>   rl   r   zTuple[ExtensionHeader, int])r0   rl   r   zList[ExtensionHeader])ra   r   rf   zList[ExtensionParameter]r   rl   )rt   zSequence[ExtensionHeader]r   rl   )r0   rl   r1   r   r>   rl   r   zTuple[Subprotocol, int])r0   rl   r   zList[Subprotocol])ry   Sequence[Subprotocol]r   rl   )ry   r   r   None)r   rl   r   rl   )r0   rl   r1   r   r>   rl   r   r   )r0   rl   r   zTuple[str, str])r   rl   r   rl   r   rl   )>
__future__r   r   r   r%   retypingr   r   r   r   r   r	   r
    r   r   r   r   r   r   r   __all__r   r   r2   compiler5   r8   r:   r?   rB   rC   rE   rH   rJ   rL   rS   rV   r   r[   r\   r   rc   rh   r   parse_extension_listrm   r   build_extension_listrv   r   parse_subprotocol_listr   build_subprotocol_listr   r   r   r   r   r   r    r3   r-   <module>r      sj   "    	 K K K   CL8	7 "**Y
 BJJ67	&  BJJJ 
 bjj89D$ rzz67 BJJ'	
5A8AA 
A 	A
 AH---(+-!- H  rzzE
===(+= =(D !!!(+!#!H888(+8 82S4 ' 
%=&	 ' ((((+(( 
S + # + D5 bjj01&$X-`(r3   