o
    Zh                     @  s   d dl m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gZ	e
ejdd	Ze
ejd
dZdddZedZedZd ddZd!ddZd"ddZd#ddZdS )$    )annotationsN   )Headers)SecurityErrorread_requestread_responseZWEBSOCKETS_MAX_NUM_HEADERSZ128ZWEBSOCKETS_MAX_LINE_LENGTHZ8192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   M/var/www/html/lang_env/lib/python3.10/site-packages/websockets/legacy/http.pyd   s   r   s   [-!#$%&\'*+.^_`|~0-9a-zA-Z]+s   [\x09\x20-\x7e\x80-\xff]*streamasyncio.StreamReadertuple[str, Headers]c              
     s   z	t | I 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| I dH }||fS )a  
    Read an HTTP/1.1 GET request and return ``(path, headers)``.

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

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

    :func:`read_request` 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 ``stream`` after this coroutine returns.

    Args:
        stream: Input to read the request from.

    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    r   zinvalid HTTP request line: s   GETzunsupported HTTP method:    HTTP/1.1unsupported HTTP version: asciisurrogateescape)	read_lineEOFErrorsplit
ValueErrorr   r   read_headers)r   Zrequest_lineexcmethodraw_pathversionpathheadersr   r   r   r   -   s&   
tuple[int, str, Headers]c           	   
     s   z	t | I 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	| I dH }|||fS )a  
    Read an HTTP/1.1 response and return ``(status_code, reason, headers)``.

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

    :func:`read_request` doesn't attempt to read the response body because
    WebSocket handshake responses don't have one. If the response contains a
    body, it may be read from ``stream`` after this coroutine returns.

    Args:
        stream: Input to read the response from.

    Raises:
        EOFError: If the connection is closed without a full HTTP response.
        SecurityError: If the response exceeds a security limit.
        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   r   r   r   int	_value_re	fullmatchr   r   )	r   Zstatus_liner   r"   Zraw_status_codeZ
raw_reasonstatus_codereasonr$   r   r   r   r   ^   s4   


r   c           	        s   t  }ttd D ]p}z	t| I 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)zo
    Read HTTP headers from ``stream``.

    Non-ASCII characters are represented with surrogate escapes.

       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   rangeMAX_NUM_HEADERSr   r   r   r   r   	_token_rer)   stripr(   r   r   )	r   r$   _liner   raw_nameZ	raw_valuenamer   r   r   r   r      s4   





r   c                   sB   |   I dH }t|tkrtd|dstd|dd S )z[
    Read a single line from ``stream``.

    CRLF is stripped from the return value.

    Nzline too longs   
zline without CRLF)readlinelenMAX_LINE_LENGTHr   endswithr   )r   r4   r   r   r   r      s   
r   )r   r	   r
   r   )r   r   r
   r   )r   r   r
   r%   )r   r   r
   r   )r   r   r
   r	   )
__future__r   asyncioosreZdatastructuresr   
exceptionsr   __all__r'   environgetr0   r:   r   compiler1   r(   r   r   r   r   r   r   r   r   <module>   s     




1
4(