B
    ]b                 @  s   d dl m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gZ
d	Z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)Tuple   )Headers)SecurityErrorread_requestread_response   i  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   5lib/python3.7/site-packages/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   yt | I dH }W n, tk
r> } ztd|W dd}~X Y nX y|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: s   HTTP/1.1zunsupported HTTP version: asciisurrogateescape)	read_lineEOFErrorsplit
ValueErrorr   r   read_headers)r   Zrequest_lineexcmethodZraw_pathversionpathheadersr   r   r   r   -   s    zTuple[int, str, Headers]c       	   
     s6  yt | I dH }W n, tk
r> } ztd|W dd}~X Y nX y|dd\}}}W n( tk
r~   tdt| dY nX |dkrtdt| y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: s   HTTP/1.1zunsupported HTTP version: 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_reasonZstatus_codereasonr!   r   r   r   r   ^   s*    r   c       	        s  t  }xttd D ]}yt| I dH }W n, tk
rX } ztd|W dd}~X Y nX |dkrdP y|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W 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_HEADERSr   r   r   r   r   	_token_rer%   stripr$   r   r   )	r   r!   _liner   Zraw_nameZ	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_LINEr   endswithr   )r   r/   r   r   r   r      s    
r   )Z
__future__r   Zasyncioretypingr   Zdatastructuresr   
exceptionsr   __all__r+   r4   r   compiler,   r$   r   r   r   r   r   r   r   r   <module>   s   

14(