U
    g                     @  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ddZedZedZdddddZdddddZdddddZdddddZdS )    )annotationsN   )Headers)SecurityErrorread_requestread_responseZWEBSOCKETS_MAX_NUM_HEADERSZ128ZWEBSOCKETS_MAX_LINE_LENGTHZ8192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   :/tmp/pip-unpacked-wheel-dx_q7dq3/websockets/legacy/http.pyd   s    r   s   [-!#$%&\'*+.^_`|~0-9a-zA-Z]+s   [\x09\x20-\x7e\x80-\xff]*zasyncio.StreamReaderztuple[str, Headers])streamr   c              
     s   zt | I dH }W n, tk
r> } ztd|W 5 d}~X Y nX z|dd\}}}W n( tk
r~   tdt| dY nX |dkrtdt| |dkrt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    ztuple[int, str, Headers]c           	   
     s6  zt | I dH }W n, tk
r> } ztd|W 5 d}~X Y nX z|dd\}}}W n( tk
r~   tdt| dY nX |dkrtdt| zt|}W n( tk
r   tdt| dY nX d	|  krd
k sn tdt| t|st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   ^   s*    r   c           	        s  t  }ttd D ]}zt| I dH }W n, tk
rT } ztd|W 5 d}~X Y nX |dkrd qz|dd\}}W n( tk
r   tdt| dY nX t	|stdt| |
d}t	|std	t| |d
}|d
d}|||< qtd|S )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   Zraw_name	raw_valuenamer
   r   r   r   r      s,    




r   c                   s@   |   I dH }t|tkr"td|ds4t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   r1   r   r   r   r      s    
r   )
__future__r   ZasyncioosreZdatastructuresr   
exceptionsr   __all__r$   environgetr-   r7   r   compiler.   r%   r   r   r   r   r   r   r   r   <module>   s   

14(