
    &th>                    x   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dPdZdQdZ ej        d          ZdRdZ ej        d          ZdSdZ ej        d          Z ej        d          Z dSdZ! ej        d          Z" ej        d          Z#dTd!Z$dUd%Z%dVd'Z&dWd)Z' ej        d*          Z(dXd,Z)dYd.Z*dZd0Z+d[d2Z,d\d4Z-e-Z.d]d9Z/d^d<Z0e0Z1d_d>Z2d`d@Z3e3Z4dadCZ5e5Z6dbdEZ7dcdGZ8 ej        dH          Z9dSdIZ:dddJZ;dedLZ<dfdOZ=dS )g    )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Thoststrportintsecureboolreturnc                    	 t          j        |           }|j        dk    rd|  d} n# t          $ r Y nw xY w||rdndk    r|  d| } | S )z#
    Build a ``Host`` header.

       []i  P   :)	ipaddress
ip_addressversion
ValueError)r   r!   r#   addresss       f/var/www/html/mycamper/aliexpress-site/backend/venv/lib/python3.11/site-packages/websockets/headers.pyr   r   &   s    &t,, ?at;;;D     v%2&&Ks   ( 
55headerposOptional[str]c                <    |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)r2   r3   s     r1   
peek_aheadr7   B   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)r2   r3   r;   s      r1   	parse_OWSr=   Q   s1     MM&#&&E99;;r8   z[-!#$%&\'*+.^_`|~0-9a-zA-Z]+header_nameTuple[str, int]c                    t                               | |          }|t          j        |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   InvalidHeaderFormatgroupr<   r2   r3   r>   r;   s       r1   parse_tokenrE   c   sP     OOFC((E},[:JFTWXXX;;==%))++%%r8   zC"(?:[\x09\x20-\x21\x23-\x5b\x5d-\x7e]|\\[\x09\x20-\x7e\x80-\xff])*"z\\([\x09\x20-\x7e\x80-\xff])c                    t                               | |          }|t          j        |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   rB   _unquote_resubrC   r<   rD   s       r1   parse_quoted_stringrK   {   sq     ##FC00E},163
 
 	
 ??5%++--""566		CCr8   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_rerJ   )rL   r;   s     r1   build_quoted_stringrR      sI     ""5))E}HIIIw...44r8   
parse_item(Callable[[str, int, str], Tuple[T, int]]List[T]c                \   t          ||          dk    r't          ||dz             }t          ||          dk    'g }	  | |||          \  }}|                    |           t          ||          }|t          |          k    rnt          ||          dk    rt          ||dz             }nt	          j        |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)r7   r=   appendr6   r   rB   )rS   r2   r3   r>   itemsitems         r1   
parse_listr[      sb   > VS
!
!S
(
(a(( VS
!
!S
(
( EJvsK88	cT$$ #f++ fc""c))FC!G,,CC0-vs  
 %%,,FC!G,,C %%,, #f++18 #f++Lr8   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.

    )rE   r
   r   r2   r3   r>   rZ   s       r1   parse_connection_optionr_      s/     FC55ID# $'',,r8   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)r[   r_   r2   s    r1   r   r      s     -vq,GGGr8   z>[-!#$%&\'*+.^_`|~0-9a-zA-Z]+(?:/[-!#$%&\'*+.^_`|~0-9a-zA-Z]+)?Tuple[UpgradeProtocol, int]c                    t                               | |          }|t          j        |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   rB   r
   r   rC   r<   rD   s       r1   parse_upgrade_protocolrg   	  sd     vs++E},,fc
 
 	
 //<<r8   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)r[   rg   rc   s    r1   r   r     s     ,faCCCr8   Tuple[ExtensionParameter, int]c                   t          | ||          \  }}t          | |          }d}t          | |          dk    rt          | |dz             }t          | |          dk    rH|}t          | ||          \  }}t                              |          t          j        |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   rN   zinvalid quoted header content)rE   r=   r7   rK   rA   rP   r   rB   )r2   r3   r>   namerL   
pos_befores         r1   parse_extension_item_paramrp   -  s     FC55ID#
FC
 
 CE&##%%a((fc""c))J,VS+FFJE3 ""5))1 4!@&*   2
 %VS+>>JE3$$%=#r8   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   )rE   r=   r7   rp   rX   r
   r   )r2   r3   r>   rn   
parameters	parameters         r1   parse_extension_itemrv   Q  s     FC55ID#
FC
 
 CJ
VS
!
!S
(
(a((3FCMM	3)$$$ VS
!
!S
(
( %%z2C77r8   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)r[   rv   rc   s    r1   r   r   j  s    . *FA7QRRRr8   rn   r   rt   List[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 )Nrm    ).0rn   rL   s      r1   
<listcomp>z(build_extension_item.<locals>.<listcomp>  sD     
 
 
 e MDD$'8'8'8'8
 
 
r8   )joinr
   r    )rn   rt   s     r1   build_extension_itemr     sK     99	c4
 
  *
 
 
	
  r8   
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}   rn   rt   s      r1   	<genexpr>z"build_extension.<locals>.<genexpr>  sB        3C4T:..     r8   r   )r   s    r1   r   r     s6     99  GQ     r8   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.

    )rE   r
   r   r^   s       r1   parse_subprotocol_itemr     s.     FC55ID#T""C''r8   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)r[   r   rc   s    r1   r   r     s     ,fa9QRRRr8   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    r1   r   r     s     99\"""r8   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    rA   rP   r/   )r   subprotocols     r1   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r8   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=)rR   )r   charsets     r1   r   r     s4      &&E!'**G4%447444r8   z[A-Za-z0-9-._~+/]+=*c                    t                               | |          }|t          j        |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   rB   rC   r<   rD   s       r1   parse_token68r     sZ     fc**E},+VS
 
 	
 ;;==%))++%%r8   c                Z    |t          |           k     rt          j        |d| |          dS )z8
    Check that parsing reached the end of header.

    ztrailing dataN)r6   r   rB   )r2   r3   r>   s      r1   	parse_endr     s5    
 S[[,[/6SVWWW r8   Tuple[str, str]c                   t          | dd          \  }}|                                dk    rt          j        dd|           t	          | |          dk    rt          j        dd| |          |dz  }t          | |d          \  }}t          | |d           	 t          j	        |
                                                                          }n)# t          j        $ r t          j        dd          d	w xY w	 |                    d
d          \  }}n$# t          $ r t          j        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)rE   lowerr   InvalidHeaderValuer7   rB   r   r   base64	b64decodeencodedecodebinasciiErrorsplitr/   )r2   schemer3   basic_credentials	user_passusernamepasswords          r1   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 !D9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       r1   r   r   @  s]     h((h((I()9)9););<<CCEE'''r8   )r   r    r!   r"   r#   r$   r%   r    )r2   r    r3   r"   r%   r4   )r2   r    r3   r"   r%   r"   )r2   r    r3   r"   r>   r    r%   r?   )rL   r    r%   r    )
rS   rT   r2   r    r3   r"   r>   r    r%   rU   )r2   r    r3   r"   r>   r    r%   r\   )r2   r    r%   r`   )r2   r    r3   r"   r>   r    r%   rd   )r2   r    r%   rh   )r2   r    r3   r"   r>   r    r%   rk   )r2   r    r3   r"   r>   r    r%   rq   )r2   r    r%   rw   )rn   r   rt   ry   r%   r    )r   r   r%   r    )r2   r    r3   r"   r>   r    r%   r   )r2   r    r%   r   )r   r   r%   r    )r   r   r%   r   )r   r    r%   r    )r2   r    r3   r"   r>   r    r%   r   )r2   r    r%   r   )r   r    r   r    r%   r    )>
__future__r   r   r   r,   retypingr   r   r   r   r   r	   r
    r   r   r   r   r   r   r   __all__r   r   r7   compiler:   r=   rA   rE   rH   rI   rK   rO   rQ   rR   r[   r_   r   rf   rg   r   rp   rv   r   parse_extension_listr   r   build_extension_listr   r   parse_subprotocol_listr   build_subprotocol_listr   r   r   r   r   r   r   r|   r8   r1   <module>r      s   " " " " " "       				 K K K K K K K K K K K K K K K K K K                        GCLL   8	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A A A AH- - - - 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& & & &$X X X X- - - -`( ( ( ( ( (r8   