a
    ñ»jø;  ã                   @  sv  d dl mZ d dl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
 ddlmZ ddlmZ ddlmZ g d	¢Zd
jejŽ Zej dde› de› �¡Zej dde› de› �¡Zeej dd¡ƒZeej dd¡ƒZeej dd¡ƒZdddœdd„Ze d¡Ze d¡Z ej!G dd„ dƒƒZ"ej!G dd„ dƒƒZ#d d!d"œd#d$„Z$d d%d"œd&d'„Z%d(d)d d d d!d*œd+d,„Z&dS )-é    )ÚannotationsN)Ú	Generator)ÚCallableé   )ÚHeaders)ÚSecurityError)Úversion)ÚSERVERÚ
USER_AGENTÚRequestÚResponsez{}.{}ZWEBSOCKETS_USER_AGENTzPython/z websockets/ZWEBSOCKETS_SERVERZWEBSOCKETS_MAX_NUM_HEADERSZ128ZWEBSOCKETS_MAX_LINE_LENGTHZ8192ZWEBSOCKETS_MAX_BODY_SIZEZ	1_048_576ÚbytesÚstr)ÚvalueÚreturnc                 C  s   | j dd�S )zG
    Decode a bytestring for interpolating into an error message.

    Úbackslashreplace©Úerrors)Údecode)r   © r   ú7E:\soccer\ml_env\Lib\site-packages\websockets/http11.pyÚd2   s    r   s   [-!#$%&\'*+.^_`|~0-9a-zA-Z]+s   [\x09\x20-\x7e\x80-\xff]*c                   @  sb   e Zd ZU dZded< ded< dZded< edd	œd
d„ƒZedddœdd„ƒZ	dd	œdd„Z
dS )r   z�
    WebSocket handshake request.

    Attributes:
        path: Request path, including optional query.
        headers: Request headers.
    r   Úpathr   ÚheadersNúException | NoneÚ
_exception©r   c                 C  s   t  dt¡ | jS )NzIRequest.exception is deprecated; use ServerProtocol.handshake_exc instead©ÚwarningsÚwarnÚDeprecationWarningr   ©Úselfr   r   r   Ú	exception^   s
    þzRequest.exceptionú-Callable[[int], Generator[None, None, bytes]]zGenerator[None, None, Request]©Ú	read_liner   c           	   
   c  sø   zt |ƒE dH }W n. ty@ } ztdƒ|‚W Y d}~n
d}~0 0 z| dd¡\}}}W n& ty~   tdt|ƒ› �ƒd‚Y n0 |dkrštdt|ƒ› �ƒ‚|dkr´td	t|ƒ› �ƒ‚| d
d¡}t|ƒE dH }d|v rÞtdƒ‚d|v rîtdƒ‚| ||ƒS )a©  
        Parse a WebSocket handshake request.

        This is a generator-based coroutine.

        The request path isn't URL-decoded or validated in any way.

        The request path and headers are expected to contain only ASCII
        characters. Other characters are represented with surrogate escapes.

        :meth:`parse` doesn't attempt to read the request body because
        WebSocket handshake requests don't have one. If the request contains a
        body, it may be read from the data stream after :meth:`parse` returns.

        Args:
            read_line: Generator-based coroutine that reads a LF-terminated
                line or raises an exception if there isn't enough data

        Raises:
            EOFError: If the connection is closed without a full HTTP request.
            SecurityError: If the request exceeds a security limit.
            ValueError: If the request isn't well formatted.

        Nz1connection closed while reading HTTP request lineó    é   zinvalid HTTP request line: ó   HTTP/1.1ú)unsupported protocol; expected HTTP/1.1: s   GETz+unsupported HTTP method; expected GET; got ÚasciiÚsurrogateescapeúTransfer-Encodingz!transfer codings aren't supportedúContent-Lengthzunsupported request body)Ú
parse_lineÚEOFErrorÚsplitÚ
ValueErrorr   r   Úparse_headersÚNotImplementedError)	Úclsr&   Zrequest_lineÚexcÚmethodÚraw_pathÚprotocolr   r   r   r   r   Úparsef   s*    # ÿzRequest.parser   c                 C  s$   d| j › d� ¡ }|| j ¡ 7 }|S )z;
        Serialize a WebSocket handshake request.

        zGET z HTTP/1.1
)r   Úencoder   Ú	serialize)r"   Úrequestr   r   r   r<   ¦   s    zRequest.serialize)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   Úpropertyr#   Úclassmethodr:   r<   r   r   r   r   r   N   s   
?r   c                   @  s~   e Zd ZU dZded< ded< ded< dZd	ed
< dZded< eddœdd„ƒZe	dddddddœdd„ƒZ
d	dœdd„ZdS )r   zÅ
    WebSocket handshake response.

    Attributes:
        status_code: Response code.
        reason_phrase: Response reason.
        headers: Response headers.
        body: Response body.

    ÚintÚstatus_coder   Úreason_phraser   r   ó    r   ÚbodyNr   r   r   c                 C  s   t  dt¡ | jS )NzJResponse.exception is deprecated; use ClientProtocol.handshake_exc insteadr   r!   r   r   r   r#   Æ   s
    ýzResponse.exceptionTr$   ÚboolzGenerator[None, None, Response])r&   Ú
read_exactÚread_to_eofÚinclude_bodyr   c              
   c  s^  zt |ƒE dH }W n. ty@ } ztdƒ|‚W Y d}~n
d}~0 0 z| dd¡\}}}	W n& ty~   tdt|ƒ› �ƒd‚Y n0 |dkrštdt|ƒ› �ƒ‚zt|ƒ}
W n& tyÌ   tdt|ƒ› �ƒd‚Y n0 d	|
  krâd
k sön tdt|ƒ› �ƒ‚t |	¡�stdt|	ƒ› �ƒ‚|	 dd¡}t	|ƒE dH }|�rLt
|
||||ƒE dH }nd}| |
|||ƒS )aÂ  
        Parse a WebSocket handshake response.

        This is a generator-based coroutine.

        The reason phrase and headers are expected to contain only ASCII
        characters. Other characters are represented with surrogate escapes.

        Args:
            read_line: Generator-based coroutine that reads a LF-terminated
                line or raises an exception if there isn't enough data.
            read_exact: Generator-based coroutine that reads the requested
                bytes or raises an exception if there isn't enough data.
            read_to_eof: Generator-based coroutine that reads until the end
                of the stream.

        Raises:
            EOFError: If the connection is closed without a full HTTP response.
            SecurityError: If the response exceeds a security limit.
            LookupError: If the response isn't well formatted.
            ValueError: If the response isn't well formatted.

        Nz0connection closed while reading HTTP status liner'   r(   zinvalid HTTP status line: r)   r*   z+invalid status code; expected integer; got éd   iX  u-   invalid status code; expected 100â€“599; got zinvalid HTTP reason phrase: r+   r,   rH   )r/   r0   r1   r2   r   rE   Ú	_value_reÚ	fullmatchr   r3   Ú	read_body)r5   r&   rK   rL   rM   Zstatus_liner6   r9   Zraw_status_codeZ
raw_reasonrF   Úreasonr   rI   r   r   r   r:   Ï   sD    ! ÿÿþÿ
ÿzResponse.parsec                 C  s6   d| j › d| j› d� ¡ }|| j ¡ 7 }|| j7 }|S )z<
        Serialize a WebSocket handshake response.

        z	HTTP/1.1 ú z
)rF   rG   r;   r   r<   rI   )r"   Úresponser   r   r   r<     s    
zResponse.serialize)T)r>   r?   r@   rA   rB   rI   r   rC   r#   rD   r:   r<   r   r   r   r   r   ²   s   
 ûFr   r$   zGenerator[None, None, bytes]r%   c                 c  sL   z| t ƒE dH }W n ty,   tdƒ‚Y n0 | d¡s@tdƒ‚|dd… S )ao  
    Parse a single line.

    CRLF is stripped from the return value.

    Args:
        read_line: Generator-based coroutine that reads a LF-terminated line
            or raises an exception if there isn't enough data.

    Raises:
        EOFError: If the connection is closed without a CRLF.
        SecurityError: If the response exceeds a security limit.

    Nzline too longó   
zline without CRLFéþÿÿÿ)ÚMAX_LINE_LENGTHÚRuntimeErrorr   Úendswithr0   )r&   Úliner   r   r   r/   #  s    
r/   zGenerator[None, None, Headers]c           	      c  s  t ƒ }ttd ƒD ]ð}zt| ƒE dH }W n. tyV } ztdƒ|‚W Y d}~n
d}~0 0 |dkrf �qz| dd¡\}}W n& ty    tdt|ƒ› �ƒd‚Y n0 t 	|¡s¾tdt|ƒ› �ƒ‚| 
d¡}t 	|¡sätd	t|ƒ› �ƒ‚| d
¡}| d
d¡}|||< qtdƒ‚|S )aÅ  
    Parse HTTP headers.

    Non-ASCII characters are represented with surrogate escapes.

    Args:
        read_line: Generator-based coroutine that reads a LF-terminated line
            or raises an exception if there isn't enough data.

    Raises:
        EOFError: If the connection is closed without complete headers.
        SecurityError: If the request exceeds a security limit.
        ValueError: If the request isn't well formatted.

    r   Nz,connection closed while reading HTTP headersrH   ó   :zinvalid HTTP header line: zinvalid HTTP header name: s    	zinvalid HTTP header value: r+   r,   ztoo many HTTP headers)r   ÚrangeÚMAX_NUM_HEADERSr/   r0   r1   r2   r   Ú	_token_rerP   ÚstriprO   r   r   )	r&   r   Ú_rZ   r6   Úraw_nameZ	raw_valueÚnamer   r   r   r   r3   >  s,     




r3   rE   r   )rF   r   r&   rK   rL   r   c                 c  s¼  d|   krdk s&n | dks&| dkr*dS |  d¡ }d u�r |dkrVtd|› d	�ƒ‚d}t|ƒE d H }| d
d¡d }t|ƒdkr |jdd�}	td|	› d�ƒ‚t|dƒ}
|
dkr¶�qt|ƒ|
 tkràtd|
› dt|ƒ› d�ƒ‚|||
ƒE d H 7 }|dƒE d H dkrZt	dƒ‚qZt
|ƒE d H  |S |  d¡ }d u�r‚t|ƒdk�rRtd|› d�ƒ‚t|ƒ}|tk�rttd|› d�ƒ‚||ƒE d H S z|tƒE d H W S  t�y¶   tdt› d�ƒ‚Y n0 d S )NrN   éÈ   éÌ   i0  rH   r-   Úchunkedztransfer coding z isn't supportedó   ;r   r   é   r   r   zchunk too large: 0xz bytesé   zchunk too large: z bytes after r(   rU   zchunk without CRLFr.   é   zbody too large: zbody too large: over )Úgetr4   r/   r1   Úlenr   r   rE   ÚMAX_BODY_SIZEr2   r3   rX   )rF   r   r&   rK   rL   ZcodingrI   Zchunk_size_lineZraw_chunk_sizeZstr_chunk_sizeÚ
chunk_sizeZraw_content_lengthÚcontent_lengthr   r   r   rQ   q  sD    &
ÿ

rQ   )'Ú
__future__r   ÚdataclassesÚosÚreÚsysr   Úcollections.abcr   Útypingr   Zdatastructuresr   Ú
exceptionsr   r   Zwebsockets_versionÚ__all__ÚformatÚversion_infoÚPYTHON_VERSIONÚenvironrj   r
   r	   rE   r]   rW   rl   r   Úcompiler^   rO   Ú	dataclassr   r   r/   r3   rQ   r   r   r   r   Ú<module>   sB   þþ

cp3