
    A>qjEG                       d dl mZ d dlZd dl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lmZ ddlmZmZmZmZmZ ddlmZ g d	Z d
j,                  ej.                   Zej2                  j5                  dde de       Zej2                  j5                  dde de       Z eej2                  j5                  dd            Z eej2                  j5                  dd            Z eej2                  j5                  dd            Z ddZ! ejD                  d      Z# ejD                  d      Z$ejJ                   G d d             Z&ejJ                   G d d             Z'ef	 	 	 	 	 d dZ(	 	 	 	 d!dZ)	 	 	 	 	 	 	 	 	 	 	 	 d"dZ*y)#    )annotationsN)	Generator)Callable   )Headers)HeaderLineTooLongRequestLineTooLongSecurityErrorStatusLineTooLongTooManyHeaders)version)SERVER
USER_AGENTRequestResponsez{}.{}WEBSOCKETS_USER_AGENTzPython/z websockets/WEBSOCKETS_SERVERWEBSOCKETS_MAX_NUM_HEADERS128WEBSOCKETS_MAX_LINE_LENGTH8192WEBSOCKETS_MAX_BODY_SIZE	1_048_576c                &    | j                  d      S )zG
    Decode a bytestring for interpolating into an error message.

    backslashreplaceerrors)decode)values    P/opt/rentech/trading_bot/.venv/lib/python3.12/site-packages/websockets/http11.pydr!   8   s    
 <<1<22    s   [-!#$%&\'*+.^_`|~0-9a-zA-Z]+s   [\x09\x20-\x7e\x80-\xff]*c                      e Zd ZU dZded<   ded<   dZded<   dZded	<   d
Zded<   edd       Z	e
	 	 	 	 dd       ZddZy
)r   a  
    WebSocket handshake request.

    ``method`` and ``path`` must contain only ASCII characters. ``headers``
    should contain only ASCII characters; however, non-ASCII header values are
    tolerated and encoded as ISO-8859-1.

    Attributes:
        path: Request path, including optional query.
        headers: Request headers.
        method: Request method; WebSocket handshake requests use GET.
        protocol: Request protocol; WebSocket handshake requests use HTTP/1.1.
    strpathr   headersGETmethodzHTTP/1.1protocolNException | None
_exceptionc                N    t        j                  dt               | j                  S )NzIRequest.exception is deprecated; use ServerProtocol.handshake_exc insteadwarningswarnDeprecationWarningr+   selfs    r    	exceptionzRequest.exceptionn   s     W	
 r"   c              #  ,  K   	 t        |t              E d{   }	 |j                  dd      \  }}}|dvrt	        dt        |             |j                  d      }|j                  d      }|j                  d      }	t        |      E d{   }
d	|
v rt        d
      d|
v rt        |
d         dk7  rt	        d       | |	|
||      S 7 # t        $ r}t        d      |d}~ww xY w# t        $ r t	        dt        |             dw xY w7 w)a  
        Parse a WebSocket handshake request.

        This is a generator-based coroutine.

        The request method and path must contain only ASCII characters. The
        request path isn't URL-decoded or validated in any way. Request headers
        should contain only ASCII characters; however, non-ASCII header values
        are tolerated and decoded with ISO-8859-1.

        :meth:`parse` doesn't 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.
            RequestLineTooLong: If the request line is too long.
            HeaderLineTooLong: If a header line is too long.
            TooManyHeaders: If there are too many headers.
            UnicodeDecodeError: If the request method or path isn't ASCII.
            ValueError: If the request isn't well formatted.

        Nz1connection closed while reading HTTP request line       zinvalid HTTP request line:    HTTP/1.1s   HTTP/1.05unsupported protocol; expected HTTP/1.1 or HTTP/1.0: asciiTransfer-Encodingz!transfer codings aren't supportedContent-Lengthr   zunsupported request body)

parse_liner	   EOFErrorsplit
ValueErrorr!   r   parse_headersNotImplementedErrorint)cls	read_linerequest_lineexc
raw_methodraw_pathraw_protocolr(   r)   r%   r&   s              r    parsezRequest.parsev   sO    P	Y&0<N&OOL	X1=1C1CD!1L.J, 99G\?#%  ""7+&&w/ w'*955 ')%&IJJw& 7+,-2 !;<<4&(33C P 	YNOUXX	Y
  	X:1\?:KLMSWW	X 6sU   DC CC C- ADD>DC 	C*C%%C**D-"DDc                    | j                    d| j                   d| j                   d}|j                  d      }|| j                  j                         z  }|S )z;
        Serialize a WebSocket handshake request.

         
r:   )r(   r%   r)   encoder&   	serialize)r2   rF   requests      r    rP   zRequest.serialize   sT     ++a		{!DMM?$G%%g.4<<))++r"   returnr*   )rE   JCallable[[int, type[Exception]], Generator[None, None, bytes | bytearray]]rS   zGenerator[None, None, Request]rS   bytes)__name__
__module____qualname____doc____annotations__r(   r)   r+   propertyr3   classmethodrK   rP    r"   r    r   r   T   s{     I FCHc $(J '  I4
I4
 
(I4 I4V
r"   r   c                      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       Ze		 d	 	 	 	 	 	 	 	 	 dd       Z
ddZy)r   aw  
    WebSocket handshake response.

    ``reason_phrase`` and ``headers`` should contain only ASCII characters;
    however, non-ASCII reason phrases and header values are tolerated and
    encoded as ISO-8859-1.

    Attributes:
        status_code: Response code.
        reason_phrase: Response reason.
        headers: Response headers.
        body: Response body.

    rC   status_coder$   reason_phraser   r&   r"   bytes | bytearraybodyNr*   r+   c                N    t        j                  dt               | j                  S )NzJResponse.exception is deprecated; use ClientProtocol.handshake_exc insteadr-   r1   s    r    r3   zResponse.exception   s"    7	

 r"   c              #    K   	 t        |t              E d{   }	 |j                  dd      \  }}}	|r|dvr3t	        dt        |             |dk7  rt	        d	t        |             	 t        |      }
d|
cxk  rdk  sn t	        dt        |             t        j                  |	      st	        dt        |	             |	j                  d      }t        |      E d{   }|rd}nt        |
||||      E d{   } | |
|||      S 7 # t        $ r}t        d      |d}~ww xY w# t        $ r t	        dt        |             dw xY w# t        $ r t	        d
t        |             dw xY w7 7 zw)a  
        Parse a WebSocket handshake response.

        This is a generator-based coroutine.

        The reason phrase and headers should contain only ASCII characters;
        however, non-ASCII reason phrases and header values are tolerated and
        decoded as ISO-8859-1.

        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.
            StatusLineTooLong: If the status line is too long.
            HeaderLineTooLong: If a header line is too long.
            TooManyHeaders: If there are too many headers.
            SecurityError: If the response body 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 liner5   r6   zinvalid HTTP status line: r7   r9   r8   z)unsupported protocol; expected HTTP/1.1: z+invalid status code; expected integer; got d   iX  u-   invalid status code; expected 100–599; got zinvalid HTTP reason phrase: 
iso-8859-1r"   )r=   r   r>   r?   r@   r!   rC   	_value_re	fullmatchr   rA   	read_body)rD   rE   
read_exactread_to_eofproxystatus_linerG   r)   raw_status_code
raw_reasonr`   reasonr&   rc   s                 r    rK   zResponse.parse   s    R	X%/	;L%MMK	V4?4E4EdA4N1Hoz 99 K~&( 
 ;& ?+?OP 	o.K
 k'C'?/@R?ST  "":.;AjM?KLL ""<0*955 D'Wi[ D ;66] N 	XMNTWW	X
  	V9!K.9IJKQUU	V  	=a>P=QR	 6s   F D DD D2 9F -E 8A/F 'E<(F E>F D 	D/D**D//F 2"EF "E99F >F c                    d| j                    d| j                   d}|j                  d      }|| j                  j	                         z  }|| j
                  z  }|S )z<
        Serialize a WebSocket handshake response.

        z	HTTP/1.1 rM   rN   rg   )r`   ra   rO   r&   rP   rc   )r2   rn   responses      r    rP   zResponse.serializeJ  s`     "$"2"2!31T5G5G4HM%%l3DLL**,,DIIr"   rR   )F)
rE   rT   rk   9Callable[[int], Generator[None, None, bytes | bytearray]]rl   rT   rm   boolrS   zGenerator[None, None, Response]rU   )rW   rX   rY   rZ   r[   rc   r+   r\   r3   r]   rK   rP   r^   r"   r    r   r      s     !D
!#'J '   W7
W7
 NW7
W7 W7 
)W7 W7r
r"   r   c              #  z   K    | t         |      E d{   }|j                  d      st        d      |dd S 7 %w)a  
    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.
        too_long_exc_type: exception to raise if the line is too long;
            defaults to :exc:`SecurityError`.

    Raises:
        EOFError: If the connection is closed without a CRLF.
        SecurityError: If the response exceeds a security limit.

    N   
zline without CRLF)MAX_LINE_LENGTHendswithr>   )rE   too_long_exc_typelines      r    r=   r=   W  sB     ,  1BCCD==!*++9	 Ds   ;9&;c              #    K   t               }t        t        dz         D ]  }	 t        | t              E d{   }|dk(  r |S 	 |j                  dd      \  }}t        j                  |      st        dt        |             |j                  d      }t        j                  |      st        d	t        |             |j                  d
      }|j                  d      }|j                  ||        t        dt         d      7 # t
        $ r}t        d      |d}~ww xY w# t        $ r t        dt        |             dw xY ww)a-  
    Parse HTTP headers.

    Headers should contain only ASCII characters; however, non-ASCII values are
    tolerated and decoded as ISO-8859-1.

    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.
        HeaderLineTooLong: If a header line is too long.
        TooManyHeaders: If there are too many headers.
        ValueError: If the request isn't well formatted.

    r   Nz,connection closed while reading HTTP headersr"      :zinvalid HTTP header line: zinvalid HTTP header name: s    	zinvalid HTTP header value: r:   rg   zexpected no more than z headers)r   rangeMAX_NUM_HEADERSr=   r   r>   r?   r@   r!   	_token_reri   striprh   r   set_insecurer   )	rE   r&   _r|   rG   raw_name	raw_valuenamer   s	            r    rA   rA   t  sc    4 iG?Q&' Q	T(4EFFD 3;4 N1	O"&**T1"5Hi ""8,9!H+GHHOOF+	""9-:1Y<.IJJw'   . 	T5)7Q< 5o5FhOPP9 G 	TIJPSS	T  	O9!D'CD$N	OsP    EDDDED*B2ED	D'D""D''E*"EEc              #  j  K   d| cxk  rdk  sn | dk(  s| dk(  ry|j                  d      x}|dk7  rt        d| d	      d}	 t        |t              E d {   }|j	                  d
d      d   }t        |      dkD  r!|j                  d      }	t        d|	 d      t        |d      }
|
dk(  rnbt        |      |
z   t        kD  rt        d|
 dt        |       d      | ||
      E d {   z  } |d      E d {   dk7  rt        d      t        |      E d {    |S |j                  d      x}Pt        |      dkD  rt        d| d      t        |      }|t        kD  rt        d| d       ||      E d {   S  |t        t              E d {   S 7 N7 7 7 7 &7 w)Nrf         i0  r"   r;   chunkedztransfer coding z isn't supported   ;r   r      r   r   zchunk too large: 0xz bytes   zchunk too large: z bytes after r6   rw   zchunk without CRLFr<      zbody too large: )getrB   r=   r
   r?   lenr   rC   MAX_BODY_SIZEr@   rA   )r`   r&   rE   rk   rl   codingrc   chunk_size_lineraw_chunk_sizestr_chunk_size
chunk_sizeraw_content_lengthcontent_lengths                r    rj   rj     s     kC;##59K KK 34
4&	AY%(8@P&QRR)3I})MMO,224;A>N>"R'!/!6!6>P!6!Q#&9.9I$PQQ^R0JQ4y:%5#'
|=T6R  z*555D%a=(W4 !566! $ !+++ ',< =
=
	J!"R'"23E2Ff MNN/0M)"2>2B& IJJ%n556 '}mDDE= N 6( 	, 6 Esn   AF3F&BF3/F)0F3F+!F3#F-$A$F3F/	F3!F1"F3)F3+F3-F3/F31F3)r   rb   rS   r$   )rE   rT   r{   ztype[Exception]rS   (Generator[None, None, bytes | bytearray])rE   rT   rS   zGenerator[None, None, Headers])r`   rC   r&   r   rE   rT   rk   rt   rl   rT   rS   r   )+
__future__r   dataclassesosresysr.   collections.abcr   typingr   datastructuresr   
exceptionsr   r	   r
   r   r   r   websockets_version__all__formatversion_infoPYTHON_VERSIONenvironr   r   r   rC   r   ry   r   r!   compiler   rh   	dataclassr   r   r=   rA   rj   r^   r"   r    <module>r      s   "  	 	 
  %  #  3  !1!12 ZZ^^n\*<)=>
 
n\*<)=>
 bjjnn%A5IJ bjjnn%A6JK BJJNN#={KL3 BJJ78	 BJJ45	 w w wt D D DV *7	 '	
 .:;; $	;|;F;F;F;F J;F;F .;Fr"   