
    2Bfg                        d dl mZ d dlZd dlZd dlmZ ddlmZ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 dd	lmZmZ g d
ZddZddZddZddZy)    )annotationsN)List   )HeadersMultipleValuesError)InvalidHeaderInvalidHeaderValueInvalidUpgrade)parse_connectionparse_upgrade)ConnectionOptionUpgradeProtocol)
accept_keygenerate_key)build_requestcheck_requestbuild_responsecheck_responsec                B    t               }d| d<   d| d<   || d<   d| d<   |S )z
    Build a handshake request to send to the server.

    Update request headers passed in argument.

    Args:
        headers: Handshake request headers.

    Returns:
        str: ``key`` that must be passed to :func:`check_response`.

    	websocketUpgrade
ConnectionSec-WebSocket-Key13Sec-WebSocket-Version)r   headerskeys     [/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/websockets/legacy/handshake.pyr   r      s:     .C$GI%GL#&G '+G#$J    c                   t        | j                  d      D cg c]  }t        |       c}g       }t        d |D              st	        ddj                  |            t        | j                  d      D cg c]  }t        |       c}g       }t        |      dk(  r|d   j                         dk(  st	        ddj                  |            	 | d   }	 t        j                  |j                         d      }t        |      dk7  rt#        d|      	 | d   }|dk7  rt#        d|      |S c c}w c c}w # t        $ r}t        d      |d	}~wt        $ r}t        dd
      |d	}~ww xY w# t        j                   $ r}t#        d|      |d	}~ww xY w# t        $ r}t        d      |d	}~wt        $ r}t        dd      |d	}~ww xY w)ag  
    Check a handshake request received from the client.

    This function doesn't verify that the request is an HTTP/1.1 or higher GET
    request and doesn't perform ``Host`` and ``Origin`` checks. These controls
    are usually performed earlier in the HTTP request handling code. They're
    the responsibility of the caller.

    Args:
        headers: Handshake request headers.

    Returns:
        str: ``key`` that must be passed to :func:`build_response`.

    Raises:
        InvalidHandshake: If the handshake request is invalid.
            Then, the server must return a 400 Bad Request error.

    r   c              3  B   K   | ]  }|j                         d k(    ywupgradeNlower.0values     r   	<genexpr>z check_request.<locals>.<genexpr>>        Beu{{}	)B   , r      r   r   r   Nz,more than one Sec-WebSocket-Key header foundT)validate   r   z0more than one Sec-WebSocket-Version header foundr   )sumget_allr   anyr
   joinr   lenr&   KeyErrorr   r   base64	b64decodeencodebinasciiErrorr	   )r   r)   
connectionr$   s_w_keyexcraw_keys_w_versions           r   r   r   &   s   ( *-.5ool.KLU	%	 Lb*J BzBB\499Z+@AA%(+2??9+EF%u	F&G LA'!*"2"2"4"CY		'(:;;-.H""7>>#3dC 7|r !4g>>56 d !8+FFNY 	M 	G  :/0c9 !O
	 >> H !4g>CGH  >34#= #%W
	sk   D08D5D: %E0 F :	E-EE-E((E-0FFF	G!F--G9GGc                4    d| d<   d| d<   t        |      | d<   y)z
    Build a handshake response to send to the client.

    Update response headers passed in argument.

    Args:
        headers: Handshake response headers.
        key: Returned by :func:`check_request`.

    r   r   r   Sec-WebSocket-AcceptN)acceptr   s     r   r   r   j   s'     %GI%GL&,SkG"#r    c                d   t        | j                  d      D cg c]  }t        |       c}g       }t        d |D              st	        ddj                  |            t        | j                  d      D cg c]  }t        |       c}g       }t        |      dk(  r|d   j                         dk(  st	        ddj                  |            	 | d	   }|t        |      k7  rt        d	|      y
c c}w c c}w # t        $ r}t        d	      |d
}~wt        $ r}t        d	d      |d
}~ww xY w)a  
    Check a handshake response received from the server.

    This function doesn't verify that the response is an HTTP/1.1 or higher
    response with a 101 status code. These controls are the responsibility of
    the caller.

    Args:
        headers: Handshake response headers.
        key: Returned by :func:`build_request`.

    Raises:
        InvalidHandshake: If the handshake response is invalid.

    r   c              3  B   K   | ]  }|j                         d k(    ywr#   r%   r'   s     r   r*   z!check_response.<locals>.<genexpr>   r+   r,    r   r.   r   r   r-   rB   Nz/more than one Sec-WebSocket-Accept header found)r1   r2   r   r3   r
   r4   r   r5   r&   r6   r   r   rC   r	   )r   r   r)   r<   r$   
s_w_acceptr>   s          r   r   r   z   s/     *-.5ool.KLU	%	 Lb*J BzBB\388J+?@@%(+2??9+EF%u	F&G LA'!*"2"2"4"CY		'(:;;34
 VC[  !7DD !3 	M 	G  =23< "$U
	s/   C28C7C< <	D/DD/D**D/)r   r   returnstr)r   r   r   rI   rH   None)
__future__r   r7   r:   typingr   datastructuresr   r   
exceptionsr   r	   r
   r   r   r   r   r   utilsr   rC   r   __all__r   r   r   r    r    r   <module>rR      sB    "    9 J J 5 6 6 Q*AH2 +Er    