
    g@4                       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m	Z	 ddl
mZ ddlmZ ddlmZ g dZ d	j"                  ej$                   Zej(                  j+                  d
de de       Zej(                  j+                  dde de       Z eej(                  j+                  dd            Z eej(                  j+                  dd            Z eej(                  j+                  dd            ZddZ ej:                  d      Z ej:                  d      Zej@                   G d d             Z!ej@                   G d d             Z"	 	 	 	 ddZ#	 	 	 	 ddZ$y)     )annotationsN)Callable	Generator   )Headers)SecurityError)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    F/var/www/openai/venv/lib/python3.12/site-packages/websockets/http11.pydr   ,   s    
 <<1<22    s   [-!#$%&\'*+.^_`|~0-9a-zA-Z]+s   [\x09\x20-\x7e\x80-\xff]*c                  h    e Zd ZU dZded<   ded<   dZded<   edd	       Ze	 	 	 	 dd
       Z	ddZ
y)r   z
    WebSocket handshake request.

    Attributes:
        path: Request path, including optional query.
        headers: Request headers.
    strpathr   headersN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.exceptionX   "    7	

 r   c              #    K   	 t        |      E d{   }	 |j                  dd      \  }}}|dk7  rt        dt	        |             |dk7  rt        d	t	        |             |j                  d
d      }t        |      E d{   }d|v rt        d      d|v 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 ow)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:    HTTP/1.1)unsupported protocol; expected HTTP/1.1: s   GETz+unsupported HTTP method; expected GET; got asciisurrogateescapeTransfer-Encoding!transfer codings aren't supportedContent-Lengthzunsupported request body)
parse_lineEOFErrorsplit
ValueErrorr   r   parse_headersNotImplementedError)	cls	read_linerequest_lineexcmethodraw_pathprotocolr    r!   s	            r   parsezRequest.parsea   s-    F	Y&0&;;L	X)5););D!)D&FHh {";AlO;LM  VJ1V9+VWWw(9:*955 ')%&IJJw&7884!!5 < 	YNOUXX	Y
  	X:1\?:KLMSWW	X 6sU   C8B4 B2B4 C AC8C6+C82B4 4	C=C		CC8"C33C8c                |    d| j                    dj                         }|| j                  j                         z  }|S )z;
        Serialize a WebSocket handshake request.

        zGET z HTTP/1.1
)r    encoder!   	serialize)r*   requests     r   rG   zRequest.serialize   s;     =188:4<<))++r   returnr"   )r>   -Callable[[int], Generator[None, None, bytes]]rJ   zGenerator[None, None, Request]rJ   bytes)__name__
__module____qualname____doc____annotations__r#   propertyr+   classmethodrD   rG    r   r   r   r   H   s_     I $(J '  ="@=" 
(=" ="~	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       Z
ddZy)r   z
    WebSocket handshake response.

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

    intstatus_coder   reason_phraser   r!   Nzbytes | Nonebodyr"   r#   c                N    t        j                  dt               | j                  S )NzJResponse.exception is deprecated; use ClientProtocol.handshake_exc insteadr%   r)   s    r   r+   zResponse.exception   r,   r   c              #    K   	 t        |      E d{   }	 |j                  dd      \  }}}|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d      }
t        |      E d{   }d|v rt        d      d	|	cxk  rdk  sn |	dk(  s|	dk(  rd}nQ	 |d   }t        |      }|	  |t              E d{   }n(|t        kD  rt        d| d       ||      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 # t        $ r d}Y w xY w7 # t        $ r t        dt         d      w xY w7 w)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: r0   r1   z+invalid status code; expected integer; got d   iX  u-   invalid status code; expected 100–599; got zinvalid HTTP reason phrase: r2   r3   r4   r5         i0  r6   zbody too large: over z byteszbody too large: )r7   r8   r9   r:   r   rW   	_value_re	fullmatchr   r;   r<   KeyErrorMAX_BODY_SIZERuntimeErrorr   )r=   r>   
read_exactread_to_eofstatus_liner@   rC   raw_status_code
raw_reasonrX   reasonr!   rZ   raw_content_lengthcontent_lengths                  r   rD   zResponse.parse   s`    @	X%/	%::K	V4?4E4EdA4N1Hoz {";AkN;KL 	o.K
 k'C'?/@R?ST  "":.;AjM?KLL""7,=>*955 ')%&IJJ +##{c'9[C=OD9 &--=%>" "%%7!8%W&1-&@@D  -/#&6~6Ff$MNN",^"<<;66w ; 	XMNTWW	X
  	V9!K.9IJKQUU	V  	=a>P=QR	 6$  &!%& A# W'*?f(UVVW
 =s   G1E EE E0 G1F A0G1F:-G15F< :G1G GG $G1 G/G1E 	E-E((E--G10"FG1"F77G1<G
G1	G

G1G G,,G1c                    d| j                    d| j                   dj                         }|| j                  j	                         z  }| j
                  || j
                  z  }|S )z<
        Serialize a WebSocket handshake response.

        z	HTTP/1.1  z
)rX   rY   rF   r!   rG   rZ   )r*   responses     r   rG   zResponse.serialize(  sc     t//0$2D2D1ETJQQSDLL**,,99 		!Hr   rI   )r>   rK   re   rK   rf   rK   rJ   zGenerator[None, None, Response]rL   )rN   rO   rP   rQ   rR   rZ   r#   rS   r+   rT   rD   rG   rU   r   r   r   r      s    	 D,#'J '  [7@[7 B[7 C	[7
 
)[7 [7zr   r   c              #  p  K   t               }t        t        dz         D ]  }	 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
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.

    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 headersr      :zinvalid HTTP header line: zinvalid HTTP header name: s    	zinvalid HTTP header value: r2   r3   ztoo many HTTP headers)r   rangeMAX_NUM_HEADERSr7   r8   r9   r:   r   	_token_rera   stripr`   r   r   )	r>   r!   _liner@   raw_name	raw_valuenamer   s	            r   r;   r;   6  sF    , iG?Q&'	T(33D 3;& N#	O"&**T1"5Hi ""8,9!H+GHHOOF+	""9-:1Y<.IJJw'  *;<) (. 344+ 4 	TIJPSS	T  	O9!D'CD$N	OsO    D6C4C2C4D6DBD62C44	D=D		DD6"D33D6c              #     K   	  | t               E d{   }|j                  d      st	        d      |dd S 7 %# t        $ r t        d      w xY ww)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_LINE_LENGTHrd   r   endswithr8   )r>   rw   s     r   r7   r7   i  s\     "-#O44 ==!*++9 5 -O,,-s%   A; 9; !A; AA)r   rM   rJ   r   )r>   rK   rJ   zGenerator[None, None, Headers])r>   rK   rJ   zGenerator[None, None, bytes])%
__future__r   dataclassesosresysr&   typingr   r   datastructuresr   
exceptionsr   r	   websockets_version__all__formatversion_infoPYTHON_VERSIONenvirongetr   r
   rW   rs   r}   rc   r   compilert   r`   	dataclassr   r   r;   r7   rU   r   r   <module>r      s   "  	 	 
  & # % 2 :  !1!12 ZZ^^n\*<)=>
 
n\*<)=>
 bjjnn%A5IJ bjjnn%A6JK BJJNN#={KL3 BJJ78	 BJJ45	 a a aH E E EP0<0#0f<!r   