o
    _i=                     @  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d1ddZedZedZ ej!G dd dZ"ej!G dd  d Z#d2d$d%Z$d3d'd(Z%d4d/d0Z&dS )5    )annotationsN)	Generator)Callable   )Headers)SecurityError)version)SERVER
USER_AGENTRequestResponsez{}.{}WEBSOCKETS_USER_AGENTzPython/z websockets/WEBSOCKETS_SERVERWEBSOCKETS_MAX_NUM_HEADERS128WEBSOCKETS_MAX_LINE_LENGTH8192WEBSOCKETS_MAX_BODY_SIZE	1_048_576valuebytes | bytearrayreturnstrc                 C  s   | j ddS )zG
    Decode a bytestring for interpolating into an error message.

    backslashreplaceerrors)decode)r    r   S/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/websockets/http11.pyd2   s   r   s   [-!#$%&\'*+.^_`|~0-9a-zA-Z]+s   [\x09\x20-\x7e\x80-\xff]*c                   @  sT   e Zd ZU dZded< ded< dZded< edd
dZedddZ	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
_exceptionr   c                 C     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	read_line9Callable[[int], Generator[None, None, bytes | bytearray]]Generator[None, None, Request]c           	   
   c  s    z	t |E dH }W n ty } ztd|d}~ww z|dd\}}}W n ty8   tdt| dw |dkrFtdt| |dkrStd	t| |d
d}t|E dH }d|v rhtdd|v rxt|d dkrxt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-Lengthr   zunsupported request body)	
parse_lineEOFErrorsplit
ValueErrorr   r   parse_headersNotImplementedErrorint)	clsr,   request_lineexcmethodraw_pathprotocolr    r!   r   r   r   parsef   s4   #

zRequest.parsebytesc                 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   rG      s   zRequest.serializer   r"   )r,   r-   r   r.   r   rE   )__name__
__module____qualname____doc____annotations__r#   propertyr+   classmethodrD   rG   r   r   r   r   r   N   s   
 Br   c                   @  sl   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Ze		dd ddZ
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.

    r=   status_coder   reason_phraser   r!       r   bodyNr"   r#   r   c                 C  r$   )NzJResponse.exception is deprecated; use ClientProtocol.handshake_exc insteadr%   r)   r   r   r   r+      s
   zResponse.exceptionFr,   r-   
read_exactread_to_eofproxyboolGenerator[None, None, Response]c              
   c  sh   z	t |E dH }W n ty } ztd|d}~ww z|dd\}}}	W n ty8   tdt| dw |rI|dvrHtdt| n|dkrVtd	t| zt|}
W n tym   td
t| dw d|
  krxdk sn tdt| t|	stdt|	 |	dd}t	|E dH }|rd}nt
|
||||E dH }| |
|||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/   r0   zinvalid HTTP status line: )r1   s   HTTP/1.0z5unsupported protocol; expected HTTP/1.1 or HTTP/1.0: r1   r2   z+invalid status code; expected integer; got d   iX  u-   invalid status code; expected 100–599; got zinvalid HTTP reason phrase: r3   r4   rT   )r7   r8   r9   r:   r   r=   	_value_re	fullmatchr   r;   	read_body)r>   r,   rV   rW   rX   status_liner@   rC   raw_status_code
raw_reasonrR   reasonr!   rU   r   r   r   rD      s^   !



zResponse.parserE   c                 C  s6   d| j  d| j d }|| j 7 }|| j7 }|S )z<
        Serialize a WebSocket handshake response.

        z	HTTP/1.1  z
)rR   rS   rF   r!   rG   rU   )r*   responser   r   r   rG   !  s   
zResponse.serializerI   )F)
r,   r-   rV   r-   rW   r-   rX   rY   r   rZ   rJ   )rK   rL   rM   rN   rO   rU   r#   rP   r+   rQ   rD   rG   r   r   r   r   r      s   
 Nr   r,   r-   (Generator[None, None, bytes | bytearray]c                 c  sJ    z	| t E dH }W n ty   tdw |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   endswithr8   )r,   liner   r   r   r7   .  s   
r7   Generator[None, None, Headers]c           	      c  s    t  }ttd D ]p}z	t| E dH }W n ty& } ztd|d}~ww |dkr. |S z
|dd\}}W n tyI   tdt| dw t	|sXtdt| |
d}t	|sktd	t| |d
}|d
d}|||< q
td)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 headersrT      :zinvalid HTTP header line: zinvalid HTTP header name: s    	zinvalid HTTP header value: r3   r4   ztoo many HTTP headers)r   rangeMAX_NUM_HEADERSr7   r8   r9   r:   r   	_token_rer]   stripr\   r   r   )	r,   r!   _rk   r@   raw_name	raw_valuenamer   r   r   r   r;   I  s4   





r;   rR   r=   r!   r   rV   rW   c                 c  s   d|   krdk sn | dks| dkrdS | d }d ur|dkr+td| d	d}	 t|E d H }|ddd }t|dkrQ|jdd}	td|	 dt|d}
|
dkr[n,t||
 tkrptd|
 dt| d|||
E d H 7 }|dE d H dkrt	dq.t
|E d H  |S | d }d urt|dkrtd| dt|}|tkrtd| d||E d H S z|tE d H W S  ty   tdt dw )Nr[         i0  rT   r5   chunkedztransfer coding z isn't supportedT   ;r   r      r   r   zchunk too large: 0xz bytes   zchunk too large: z bytes after r0   rf   zchunk without CRLFr6      zbody too large: zbody too large: over )getr<   r7   r9   lenr   r   r=   MAX_BODY_SIZEr:   r;   ri   )rR   r!   r,   rV   rW   codingrU   chunk_size_lineraw_chunk_sizestr_chunk_size
chunk_sizeraw_content_lengthcontent_lengthr   r   r   r^   |  sL   &
r^   )r   r   r   r   )r,   r-   r   re   )r,   r-   r   rl   )rR   r=   r!   r   r,   r-   rV   r-   rW   r-   r   re   )'
__future__r   dataclassesosresysr&   collections.abcr   typingr   datastructuresr   
exceptionsr   r   websockets_version__all__formatversion_infoPYTHON_VERSIONenvironr}   r
   r	   r=   ro   rh   r   r   compilerp   r\   	dataclassr   r   r7   r;   r^   r   r   r   r   <module>   sD    


f
x
3