
    2Bf1                    ,   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	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Optional   )datastructures
exceptions   i    i   c                &    | j                  d      S )zG
    Decode a bytestring for interpolating into an error message.

    backslashreplace)errors)decode)values    Q/var/www/highfloat_scraper/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)Requestz
    WebSocket handshake request.

    Attributes:
        path: Request path, including optional query.
        headers: Request headers.
    strpathdatastructures.HeadersheadersNOptional[Exception]
_exceptionc                N    t        j                  dt               | j                  S )NzIRequest.exception is deprecated; use ServerProtocol.handshake_exc insteadwarningswarnDeprecationWarningr   selfs    r   	exceptionzRequest.exceptionC   "    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: s   GETzunsupported HTTP method:    HTTP/1.1unsupported HTTP version: 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versionr   r   s	            r   parsezRequest.parseL   s&    F	Y&0&;;L	X(4(:(:4(C%FHg V86DEEk!9!G*FGGw(9:*955 ')%&IJJw&7884!!3 < 	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   r>   zRequest.serialize   s;     =188:4<<))++r   returnr   )r5   -Callable[[int], Generator[None, None, bytes]]rA   zGenerator[None, None, Request]rA   bytes)__name__
__module____qualname____doc____annotations__r   propertyr"   classmethodr;   r>    r   r   r   r   3   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)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_phraser   r   NzOptional[bytes]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                         }
t        |      E d{   }d|v rt        d      d	|	cxk  rdk  sn |	dk(  s|	dk(  rd}n[	 |d   }t        |      }|	  |t              E d{   }n2|t        kD  rt        j                  d| d       ||      E d{   } | |	|
||      S 7 F# 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        j                  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: r'   r(   zinvalid HTTP status code: d   i  zunsupported HTTP status code: zinvalid HTTP reason phrase: r+   r,         i0  r-   zbody too large: over z byteszbody too large: )r.   r/   r0   r1   r   rO   	_value_re	fullmatchr   r2   r3   KeyErrorMAX_BODYRuntimeErrorr	   SecurityError)r4   r5   
read_exactread_to_eofstatus_liner7   r:   raw_status_code
raw_reasonrP   reasonr   rR   raw_content_lengthcontent_lengths                  r   r;   zResponse.parse   sb    @	X%/	%::K	V3>3D3DT13M0G_j k!9!G*FGG	o.K
 k(D(=a>P=QRSS"":.;AjM?KLL""$*955 ')%&IJJ +##{c'9[C=OD9 &--=%>" "%%7!8%&1(&;;D
  (* ..&~&6f=  #-^"<<;66y ; 	XMNTWW	X
  	V9!K.9IJKQUU	V  	,Q-?,@A	 6$  &!%& <# $22/z@  =s   HE EE E8 HF A.HG-H3G 8HG GG .HH	HE 	E5$E00E55H8"FH"F??HGHGHG 'G>>H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
)rP   rQ   r=   r   r>   rR   )r!   responses     r   r>   zResponse.serialize  sc     t//0$2D2D1ETJQQSDLL**,,99 		!Hr   r@   )r5   rB   r^   rB   r_   rB   rA   zGenerator[None, None, Response]rC   )rE   rF   rG   rH   rI   rR   r   rJ   r"   rK   r;   r>   rL   r   r   rN   rN      s    	 ## D/ &*J#*  \7@\7 B\7 C	\7
 
)\7 \7|r   rN   c              #    K   t        j                         }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        j                  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: r)   r*   ztoo many HTTP headers)r   HeadersrangeMAX_HEADERSr.   r/   r0   r1   r   	_token_rerY   striprX   r   r	   r]   )	r5   r   _liner7   raw_name	raw_valuenamer   s	            r   r2   r2   !  sY    , $$&G;?# @	T(33D 3;& N#	O"&**T1"5Hi ""8,9!H+GHHOOF+	""9-:1Y<.IJJw'  *;<)@. &&'>??+ 4 	TIJPSS	T  	O9!D'CD$N	OsQ   *E
DDD E
	D%B(E
D	D"DD""E
%"EE
c              #     K   	  | t               E d{   }|j	                  d      st        d      |dd S 7 %# t        $ r t        j                  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_LINEr\   r	   r]   endswithr/   )r5   rq   s     r   r.   r.   T  sb     "8#H-- ==!*++9 . 8&&778s%   A; 9; !A; AA)r   rD   rA   r   )r5   rB   rA   z-Generator[None, None, datastructures.Headers])r5   rB   rA   zGenerator[None, None, bytes])
__future__r   dataclassesrer   typingr   r   r    r   r	   rm   rw   r[   r   compilern   rX   	dataclassr   rN   r2   r.   rL   r   r   <module>r      s    "  	  0 0 (   3 BJJ78	 BJJ45	 ` ` `F F F FR0<020f<!r   