B
    ]b1                 @  s   d dl mZ d dl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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ddZdddddZdS )    )annotationsN)Callable	GeneratorOptional   )datastructures
exceptions   i  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   0lib/python3.7/site-packages/websockets/http11.pyd   s    r   s   [-!#$%&\'*+.^_`|~0-9a-zA-Z]+s   [\x09\x20-\x7e\x80-\xff]*c               @  sb   e Zd ZU dZded< ded< dZded< edd	d
dZedddddZ	dd	ddZ
dS )Requestz
    WebSocket handshake request.

    Attributes:
        path: Request path, including optional query.
        headers: Request headers.
    r   pathzdatastructures.HeadersheadersNzOptional[Exception]
_exception)r   c             C  s   t dt | jS )NzKRequest.exception is deprecated; use ServerConnection.handshake_exc instead)warningswarnDeprecationWarningr   )selfr   r   r   	exceptionE   s    zRequest.exceptionz-Callable[[int], Generator[None, None, bytes]]zGenerator[None, None, Request])	read_liner   c       	   
   c  s   yt |E 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|E dH }d|krtdd|krt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: s   HTTP/1.1zunsupported HTTP version: asciisurrogateescapezTransfer-Encodingz!transfer codings aren't supportedzContent-Lengthzunsupported request body)
parse_lineEOFErrorsplit
ValueErrorr   r   parse_headersNotImplementedError)	clsr   Zrequest_lineexcmethodZraw_pathversionr   r   r   r   r   parseN   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)r   Zrequestr   r   r   r.      s    zRequest.serialize)__name__
__module____qualname____doc____annotations__r   propertyr   classmethodr,   r.   r   r   r   r   r   5   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dZe	dddddddZ
ddddZdS )Responsez
    WebSocket handshake response.

    Attributes:
        status_code: Response code.
        reason_phrase: Response reason.
        headers: Response headers.
        body: Response body, if any.

    intstatus_coder   reason_phrasezdatastructures.Headersr   NzOptional[bytes]bodyzOptional[Exception]r   )r   c             C  s   t dt | jS )NzLResponse.exception is deprecated; use ClientConnection.handshake_exc instead)r   r   r   r   )r   r   r   r   r      s    zResponse.exceptionz-Callable[[int], Generator[None, None, bytes]]zGenerator[None, None, Response])r   
read_exactread_to_eofr   c          
   c  s$  yt |E 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	|E dH }d|kr>t
dd	|	  krVdk sln |	dksl|	dkrrd}ny|d }W n tk
r   d}Y n
X t|}|dkry|tE dH }W n( tk
r   tdt dY nX n,|tkrtd| dn||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   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: zTransfer-Encodingz!transfer codings aren't supported      i0  zContent-Lengthzbody too large: over z byteszbody too large: )r"   r#   r$   r%   r   r7   	_value_re	fullmatchr   r&   r'   KeyErrorMAX_BODYRuntimeErrorr   SecurityError)r(   r   r;   r<   Zstatus_liner)   r+   Zraw_status_codeZ
raw_reasonr8   reasonr   r:   Zraw_content_lengthZcontent_lengthr   r   r   r,      sT     

.


zResponse.parser
   c             C  s@   d| j  d| j d }|| j 7 }| jdk	r<|| j7 }|S )z<
        Serialize a WebSocket handshake response.

        z	HTTP/1.1  z
N)r8   r9   r-   r   r.   r:   )r   Zresponser   r   r   r.     s
    

zResponse.serialize)r/   r0   r1   r2   r3   r:   r   r4   r   r5   r,   r.   r   r   r   r   r6      s   
	_r6   z-Callable[[int], Generator[None, None, bytes]]z-Generator[None, None, datastructures.Headers])r   r   c       	      c  s  t  }xttd D ]}yt| E dH }W n, tk
rZ } ztd|W dd}~X Y nX |dkrfP 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 )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_rerA   stripr@   r   r   rE   )	r   r   _liner)   Zraw_nameZ	raw_valuenamer   r   r   r   r&   #  s,    




r&   zGenerator[None, None, bytes]c             c  sP   y| t E dH }W n tk
r0   tdY nX |dsDt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_LINErD   r   rE   endswithr#   )r   rO   r   r   r   r"   V  s    
r"   )Z
__future__r   Zdataclassesrer   typingr   r   r    r   r   rK   rR   rC   r   compilerL   r@   Z	dataclassr   r6   r&   r"   r   r   r   r   <module>   s    

d 3