o
    [h>0                     @  s   d dl mZ d dlZd dlZd dlmZmZmZ ddlm	Z	m
Z
 dZdZdZ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ZdddZdS )     )annotationsN)Callable	GeneratorOptional   )datastructures
exceptions   i  i   valuebytesreturnstrc                 C  s   | j ddS )zG
    Decode a bytestring for interpolating into an error message.

    backslashreplace)errors)decode)r
    r   x/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/websockets/http11.pyd   s   r   s   [-!#$%&\'*+.^_`|~0-9a-zA-Z]+s   [\x09\x20-\x7e\x80-\xff]*c                   @  sF   e Zd ZU dZded< ded< dZded< edddZdddZdS )Requesta  
    WebSocket handshake request.

    Attributes:
        path: Request path, including optional query.
        headers: Request headers.
        exception: If processing the response triggers an exception,
            the exception is stored in this attribute.
    r   pathdatastructures.HeadersheadersNOptional[Exception]	exception	read_line-Callable[[int], Generator[None, None, bytes]]r   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 rpt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: s   GETzunsupported HTTP method:    HTTP/1.1unsupported HTTP version: asciisurrogateescapeTransfer-Encoding!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versionr   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)selfrequestr   r   r   r3      s   zRequest.serialize)r   r   r   r   r   r   )	__name__
__module____qualname____doc____annotations__r   classmethodr1   r3   r   r   r   r   r   4   s   
 
>r   c                   @  sZ   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dddZ	dS )ResponseaI  
    WebSocket handshake response.

    Attributes:
        status_code: Response code.
        reason_phrase: Response reason.
        headers: Response headers.
        body: Response body, if any.
        exception: if processing the response triggers an exception,
            the exception is stored in this attribute.

    intstatus_coder   reason_phraser   r   NzOptional[bytes]bodyr   r   r   r   
read_exactread_to_eofr   Generator[None, None, Response]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| zt|}	W n ty]   tdt| dw d	|	  krhd
k srn tdt| t|stdt| | }
t	|E dH }d|v rt
dd	|	  krdk sn |	dks|	dkrd}nHz|d }W n ty   d}Y nw t|}|du rz	|tE dH }W n$ ty   tdt dw |tkrtd| d||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 strem.

        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 HTTP status code: d   i  zunsupported HTTP status code: zinvalid HTTP reason phrase: r#   r$         i0  r%   zbody too large: over z byteszbody too large: )r&   r'   r(   r)   r   r>   	_value_re	fullmatchr   r*   r+   KeyErrorMAX_BODYRuntimeErrorr   SecurityError)r,   r   rB   rC   Zstatus_liner-   r0   Zraw_status_codeZ
raw_reasonr?   reasonr   rA   Zraw_content_lengthcontent_lengthr   r   r   r1      sj    

&

zResponse.parser   c                 C  s@   d| j  d| j d }|| j 7 }| jdur|| j7 }|S )z<
        Serialize a WebSocket handshake response.

        z	HTTP/1.1  z
N)r?   r@   r2   r   r3   rA   )r4   responser   r   r   r3     s
   

zResponse.serialize)r   r   rB   r   rC   r   r   rD   r6   )
r7   r8   r9   r:   r;   rA   r   r<   r1   r3   r   r   r   r   r=      s   
 ^r=   r   r   -Generator[None, None, datastructures.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yJ   tdt| dw t	
|sYtdt| |d}t
|slt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 headers       :zinvalid HTTP header line: zinvalid HTTP header name: s    	zinvalid HTTP header value: r!   r"   ztoo many HTTP headers)r   ZHeadersrangeMAX_HEADERSr&   r'   r(   r)   r   	_token_rerI   striprH   r   r   rM   )	r   r   _liner-   raw_nameZ	raw_valuenamer
   r   r   r   r*     s4   






r*   Generator[None, None, bytes]c                 c  sL    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 longs   
zline without CRLF)MAX_LINErL   r   rM   endswithr'   )r   rZ   r   r   r   r&   G  s   

r&   )r
   r   r   r   )r   r   r   rR   )r   r   r   r]   )
__future__r   dataclassesretypingr   r   r    r   r   rV   r_   rK   r   compilerW   rH   	dataclassr   r=   r*   r&   r   r   r   r   <module>   s$    


\ 
3