
    ,h>                       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	 	 	 	 	 	 	 	 	 d-dZd.dZ ej2                  d      Zd/dZ ej2                  d      Zd0dZ ej2                  d      Z ej2                  d      Zd0dZ  ej2                  d      Z! ej2                  d      Z"d1dZ#	 	 	 	 	 	 	 	 	 	 d2dZ$	 	 	 	 	 	 	 	 d3dZ%d4dZ& ej2                  d      Z'	 	 	 	 	 	 	 	 d5dZ(d6dZ)	 	 	 	 	 	 	 	 d7dZ*	 	 	 	 	 	 	 	 d8dZ+d9d Z,e,Z-	 	 	 	 	 	 d:d!Z.d;d"Z/e/Z0	 	 	 	 	 	 	 	 d<d#Z1d=d$Z2e2Z3d>d%Z4e4Z5d?d&Z6d@d'Z7 ej2                  d(      Z8d0d)Z9dAd*Z:dBd+Z;dCd,Z<y)D    )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_portc                   	 t        j                  |       }|j                  dk(  rd|  d} 	 |s	||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securer   addresss        T/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/websockets/headers.pyr   r   '   si    &&t, ??atfA;Ddfs"=qK  s   A 	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_aheadr1   I   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/   r0   r5   s      r,   	parse_OWSr7   X   s)     MM&#&E99;r2   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_rer5   r	   groupr6   r/   r0   header_namer5   s       r,   parse_tokenr=   j   sB     OOFC(E}!+/?MM;;=%))+%%r2   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_rer5   r	   _unquote_resubr:   r6   r;   s       r,   parse_quoted_stringrC      sW     ##FC0E}!+/GQTUU??5%++-""56		CCr2   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_rerB   )valuer5   s     r,   build_quoted_stringrJ      s@     ""5)E}HIIw..44r2   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)r1   r7   appendr.   r	   )
parse_itemr/   r0   r<   itemsitems         r,   
parse_listrQ      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+Lr2   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/   r0   r<   rP   s       r,   parse_connection_optionrT      s)     FC5ID# $',,r2   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)rQ   rT   r/   s    r,   r   r      s     -vq,GGr2   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_rer5   r	   r   r   r:   r6   r;   s       r,   parse_upgrade_protocolrZ     sK     vs+E}!+/BFCPP/<<r2   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)rQ   rZ   rW   s    r,   r   r     s     ,faCCr2   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   rE   zinvalid quoted header content)r=   r7   r1   rC   r9   rG   r	   )r/   r0   r<   namerI   
pos_befores         r,   parse_extension_item_paramra   .  s     FC5ID#
FC
 CE&##%a(fc"c)J,VS+FJE3 ""5)1)!@&*  %VS+>JE3$%=#r2   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=   r7   r1   ra   rM   r   r   )r/   r0   r<   r_   
parameters	parameters         r,   parse_extension_itemrf   R  s     FC5ID#
FC
 CJ
VS
!S
(a(3FCM	3)$ VS
!S
( %z2C77r2   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)rQ   rf   rW   s    r,   r   r   k  s    . *FA7QRRr2   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_   rd   rI   s      r,   build_extension_itemrk     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)rk   ).0r_   rd   s      r,   	<genexpr>z"build_extension.<locals>.<genexpr>  s!      3C4T:.s   ri   )
extensionss    r,   r   r     s"     99 GQ  r2   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   rS   s       r,   parse_subprotocol_itemrt     s(     FC5ID#T"C''r2   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)rQ   rt   rW   s    r,   r   r     s     ,fa9QRRr2   c                $    dj                  |       S )zl
    Build a ``Sec-WebSocket-Protocol`` header.

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

    rm   rq   )subprotocolss    r,   r   r     s     99\""r2   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   	TypeErrorrj   r9   rG   r'   )rw   subprotocols     r,   r   r     sb    
 lH-566,$@AA# D"";/4[MBCCDr2   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=)rJ   )realmcharsets     r,   r   r     s+      &E!'*G%
7)44r2   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_rer5   r	   r:   r6   r;   s       r,   parse_token68r     sD     fc*E}!+/A63OO;;=%))+%%r2   c                <    |t        |       k  rt        |d| |      y)z8
    Check that parsing reached the end of header.

    ztrailing dataN)r.   r	   )r/   r0   r<   s      r,   	parse_endr     s&    
 S[!+LL r2   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
   r1   r	   r   r   base64	b64decodeencodedecodebinasciiErrorsplitr'   )r/   schemer0   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   ?  sN     h*AhZ(I(()9)9);<CCE'''r2   )
r(   rj   r)   intr*   boolr   r   returnrj   )r/   rj   r0   r   r   z
str | None)r/   rj   r0   r   r   r   )r/   rj   r0   r   r<   rj   r   ztuple[str, int])rI   rj   r   rj   )
rN   z(Callable[[str, int, str], tuple[T, int]]r/   rj   r0   r   r<   rj   r   zlist[T])r/   rj   r0   r   r<   rj   r   ztuple[ConnectionOption, int])r/   rj   r   zlist[ConnectionOption])r/   rj   r0   r   r<   rj   r   ztuple[UpgradeProtocol, int])r/   rj   r   zlist[UpgradeProtocol])r/   rj   r0   r   r<   rj   r   ztuple[ExtensionParameter, int])r/   rj   r0   r   r<   rj   r   ztuple[ExtensionHeader, int])r/   rj   r   zlist[ExtensionHeader])r_   r   rd   zSequence[ExtensionParameter]r   rj   )rr   zSequence[ExtensionHeader]r   rj   )r/   rj   r0   r   r<   rj   r   ztuple[Subprotocol, int])r/   rj   r   zlist[Subprotocol])rw   Sequence[Subprotocol]r   rj   )rw   r   r   None)r}   rj   r   rj   )r/   rj   r0   r   r<   rj   r   r   )r/   rj   r   ztuple[str, str])r   rj   r   rj   r   rj   )=
__future__r   r   r   r$   recollections.abcr   typingr   r   r   
exceptionsr	   r
   r   r   r   r   r   r   __all__r   r   r1   compiler4   r7   r9   r=   r@   rA   rC   rF   rH   rJ   rQ   rT   r   rY   rZ   r   ra   rf   r   parse_extension_listrk   r   build_extension_listrt   r   parse_subprotocol_listr   build_subprotocol_listr   r   r   r   r   r   r    r2   r,   <module>r      s   "    	 $ * * ?  CL !&

 
  	D	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 ' 
%A&	 ' ((((+(( 
S + # + D5 bjj01& M-`(r2   