Ë
    RPfòH  ã                  óâ  — 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Zd dlZd dl	Z	d dl
mZ d dlmZmZmZmZmZ d dlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZm Z m!Z! ddl"m#Z# ddlm$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z* g d¢Z+ G d„ de(«      Z, G d„ d«      Z-	 	 dddddddddddedddddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z.	 d 	 	 	 	 	 	 	 d!d„Z/y)"é    )ÚannotationsN)ÚTracebackType)ÚAnyÚCallableÚOptionalÚSequenceÚType)Ú	CloseCodeé   )ÚServerExtensionFactory)Ú enable_server_permessage_deflate)Úvalidate_subprotocols)Ú
USER_AGENT)ÚRequestÚResponse)Ú
CONNECTINGÚOPENÚEvent)ÚServerProtocol)Ú
LoggerLikeÚOriginÚSubprotocolé   )Ú
Connection)ÚDeadline)ÚserveÚ
unix_serveÚServerConnectionÚWebSocketServerc                  ót   ‡ — e Zd ZdZddœ	 	 	 	 	 	 	 d	ˆ fd„Zddedf	 	 	 	 	 	 	 	 	 d
d„Zdˆ fd„Zdˆ fd„Zˆ xZ	S )r   aá  
    Threaded implementation of a WebSocket server connection.

    :class:`ServerConnection` provides :meth:`recv` and :meth:`send` methods for
    receiving and sending messages.

    It supports iteration to receive messages::

        for message in websocket:
            process(message)

    The iterator exits normally when the connection is closed with close code
    1000 (OK) or 1001 (going away) or without a close code. It raises a
    :exc:`~websockets.exceptions.ConnectionClosedError` when the connection is
    closed with any other code.

    Args:
        socket: Socket connected to a WebSocket client.
        protocol: Sans-I/O connection.
        close_timeout: Timeout for closing the connection in seconds.

    é
   ©Úclose_timeoutc               ó`   •— |  t        j                  «       | _        t        ‰| �  |||¬«       y )Nr"   )Ú	threadingr   Úrequest_rcvdÚsuperÚ__init__)ÚselfÚsocketÚprotocolr#   Ú	__class__s       €úP/var/www/fastapitest/venv/lib/python3.12/site-packages/websockets/sync/server.pyr(   zServerConnection.__init__7   s2   ø€ ñ 	Ü%ŸO™OÓ-ˆÔÜ‰ÑØØØ'ð 	õ 	
ó    Nc                ó*  — | j                   j                  |«      s5| j                  «        | j                  j	                  «        t        d«      ‚| j                  €5| j                  «        | j                  j	                  «        t        d«      ‚| j                  t        ¬«      5  d| _
        |�	  || | j                  «      | _
        | j                  €*| j                  j)                  | j                  «      | _
        |�|| j                  j*                  d	<   |�)	  || | j                  | j                  «      }|�|| _
        	 | j                  j-                  | j                  «       ddd«       | j                  j.                  t0        urO| j                  j	                  | j2                  «       | j                  «        | j                  j	                  «        | j                  j                  �| j                  j                  ‚y# t        $ rr}|| j                  _        | j                  j                  dd¬«       | j                  j!                  t"        j$                  j&                  d«      | _
        Y d}~�Œ·d}~ww xY w# t        $ rr}|| j                  _        | j                  j                  dd¬«       | j                  j!                  t"        j$                  j&                  d«      | _
        Y d}~�Œ¹d}~ww xY w# 1 sw Y   �ŒžxY w)
z1
        Perform the opening handshake.

        ztimed out during handshakeNz"connection closed during handshake)Úexpected_statezopening handshake failedT©Úexc_infozLFailed to open a WebSocket connection.
See server log for more information.
ÚServer)r&   ÚwaitÚclose_socketÚrecv_events_threadÚjoinÚTimeoutErrorÚrequestÚConnectionErrorÚsend_contextr   ÚresponseÚ	Exceptionr+   Úhandshake_excÚloggerÚerrorÚrejectÚhttpÚ
HTTPStatusÚINTERNAL_SERVER_ERRORÚacceptÚheadersÚsend_responseÚstater   r#   )r)   Úprocess_requestÚprocess_responseÚserver_headerÚtimeoutÚexcr<   s          r-   Ú	handshakezServerConnection.handshakeF   s{  € ð* × Ñ ×%Ñ% gÔ.Ø×ÑÔØ×#Ñ#×(Ñ(Ô*ÜÐ;Ó<Ð<à�<‰<ÐØ×ÑÔØ×#Ñ#×(Ñ(Ô*Ü!Ð"FÓGÐGà×Ñ¬jÐÓ9ñ (	7Ø ˆDŒMàÐ*ðÙ$3°D¸$¿,¹,Ó$G�D”Mð �}‰}Ð$Ø $§¡× 4Ñ 4°T·\±\Ó B�”àÐ(Ø2?�—‘×%Ñ% hÑ/àÐ+ð1Ù/°°d·l±lÀDÇMÁMÓR�Hð  Ð+Ø(0˜�à�M‰M×'Ñ'¨¯©Ô6÷Q(	7ðT �=‰=×Ñ¤dÑ*Ø×#Ñ#×(Ñ(¨×);Ñ);Ô<Ø×ÑÔØ×#Ñ#×(Ñ(Ô*à�=‰=×&Ñ&Ð2Ø—-‘-×-Ñ-Ð-ð 3øôS !ò 	Ø25�D—M‘MÔ/Ø—K‘K×%Ñ%Ð&@È4Ð%ÔPØ$(§M¡M×$8Ñ$8ÜŸ™×=Ñ=ðEó%�D—M’Mûð	ûô( !ò 	Ø25�D—M‘MÔ/Ø—K‘K×%Ñ%Ð&@È4Ð%ÔPØ$(§M¡M×$8Ñ$8ÜŸ™×=Ñ=ðEó%�D—M’Mûð	ú÷5(	7ñ (	7úsc   Â(
LÂ3HÃALÄJ
Ä=/LÈ	JÈA'JÉ<LÊJÊLÊ
	LÊA'L Ë:LÌ LÌLÌLc                ó¤   •— | j                   €4t        |t        «      sJ ‚|| _         | j                  j	                  «        yt
        ‰| �  |«       y)z.
        Process one incoming event.

        N)r9   Ú
isinstancer   r&   Úsetr'   Úprocess_event)r)   Úeventr,   s     €r-   rR   zServerConnection.process_event—   sG   ø€ ð �<‰<ÐÜ˜e¤WÔ-Ð-Ð-Ø ˆDŒLØ×Ñ×!Ñ!Õ#ô ‰GÑ! %Õ(r.   c                ó–   •— 	 t         ‰| �  «        | j                  j                  «        y# | j                  j                  «        w xY w)zI
        Read incoming data from the socket and process events.

        N)r'   Úrecv_eventsr&   rQ   )r)   r,   s    €r-   rU   zServerConnection.recv_events¥   s:   ø€ ð
	$Ü‰GÑÔ!ð ×Ñ×!Ñ!Õ#øˆD×Ñ×!Ñ!Õ#ús	   ƒ, ¬A)r*   úsocket.socketr+   r   r#   úOptional[float]ÚreturnÚNone)
rI   úCOptional[Callable[[ServerConnection, Request], Optional[Response]]]rJ   úMOptional[Callable[[ServerConnection, Request, Response], Optional[Response]]]rK   úOptional[str]rL   rW   rX   rY   )rS   r   rX   rY   ©rX   rY   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   r   rN   rR   rU   Ú__classcell__)r,   s   @r-   r   r      s•   ø„ ñð8 *,ñ
àð
ð !ð
ð
 'ð
ð 
õ
ð, ð Ø'1Ø#'ðO.ð
ðO.ð
ðO.ð %ðO.ð !ðO.ð  
ó!O.õb)÷	$ñ 	$r.   r   c                  ó^   — e Zd ZdZ	 d		 	 	 	 	 d
d„Zdd„Zdd„Zdd„Zdd„Z	 	 	 	 	 	 	 	 dd„Z	y)r   a  
    WebSocket server returned by :func:`serve`.

    This class mirrors the API of :class:`~socketserver.BaseServer`, notably the
    :meth:`~socketserver.BaseServer.serve_forever` and
    :meth:`~socketserver.BaseServer.shutdown` methods, as well as the context
    manager protocol.

    Args:
        socket: Server socket listening for new connections.
        handler: Handler for one connection. Receives the socket and address
            returned by :meth:`~socket.socket.accept`.
        logger: Logger for this server.

    Nc                óÆ   — || _         || _        |€t        j                  d«      }|| _        t
        j                  dk7  r"t        j                  «       \  | _	        | _
        y y )Nzwebsockets.serverÚwin32)r*   ÚhandlerÚloggingÚ	getLoggerr?   ÚsysÚplatformÚosÚpipeÚshutdown_watcherÚshutdown_notifier)r)   r*   rf   r?   s       r-   r(   zWebSocketServer.__init__Â   sV   € ð ˆŒØˆŒØˆ>Ü×&Ñ&Ð':Ó;ˆFØˆŒÜ�<‰<˜7Ò"Ü<>¿G¹G»IÑ9ˆDÔ! 4Õ#9ð #r.   c                óÜ  — t        j                  «       }|j                  | j                  t         j                  «       t
        j                  dk7  r*|j                  | j                  t         j                  «       	 |j                  «        	 | j                  j                  «       \  }}t        j                  | j                  ||f¬«      }|j                  «        Œb# t        $ r Y yw xY w)a  
        See :meth:`socketserver.BaseServer.serve_forever`.

        This method doesn't return. Calling :meth:`shutdown` from another thread
        stops the server.

        Typical use::

            with serve(...) as server:
                server.serve_forever()

        re   )ÚtargetÚargsN)Ú	selectorsÚDefaultSelectorÚregisterr*   Ú
EVENT_READri   rj   rm   ÚselectrE   ÚOSErrorr%   ÚThreadrf   Ústart)r)   ÚpollerÚsockÚaddrÚthreads        r-   Úserve_foreverzWebSocketServer.serve_foreverÐ   s·   € ô ×*Ñ*Ó,ˆØ�‰˜Ÿ™¤Y×%9Ñ%9Ô:Ü�<‰<˜7Ò"Ø�O‰O˜D×1Ñ1´9×3GÑ3GÔHàØ�M‰MŒOðð "Ÿ[™[×/Ñ/Ó1‘
��dô ×%Ñ%¨T¯\©\ÀÀtÀÔMˆFØ�L‰LŒNð øô ò Ùðús   ÂC Ã	C+Ã*C+c                ó    — | j                   j                  «        t        j                  dk7  r!t	        j
                  | j                  d«       yy)z@
        See :meth:`socketserver.BaseServer.shutdown`.

        re   ó   xN)r*   Úcloseri   rj   rk   Úwritern   ©r)   s    r-   ÚshutdownzWebSocketServer.shutdowní   s9   € ð
 	�‰×ÑÔÜ�<‰<˜7Ò"Ü�H‰H�T×+Ñ+¨TÕ2ð #r.   c                ó6   — | j                   j                  «       S )z>
        See :meth:`socketserver.BaseServer.fileno`.

        )r*   Úfilenorƒ   s    r-   r†   zWebSocketServer.filenoö   s   € ð
 �{‰{×!Ñ!Ó#Ð#r.   c                ó   — | S ©N© rƒ   s    r-   Ú	__enter__zWebSocketServer.__enter__ý   s   € Øˆr.   c                ó$   — | j                  «        y rˆ   )r„   )r)   Úexc_typeÚ	exc_valueÚ	tracebacks       r-   Ú__exit__zWebSocketServer.__exit__   s   € ð 	�‰�r.   rˆ   )r*   rV   rf   z$Callable[[socket.socket, Any], None]r?   úOptional[LoggerLike]r]   )rX   Úint)rX   r   )rŒ   zOptional[Type[BaseException]]r�   zOptional[BaseException]rŽ   zOptional[TracebackType]rX   rY   )
r^   r_   r`   ra   r(   r~   r„   r†   rŠ   r�   r‰   r.   r-   r   r   ±   sp   „ ñð( (,ð	FàðFð 6ðFð %ó	Fóó:3ó$óðà/ðð +ðð +ð	ð
 
ôr.   r   FÚdeflater!   i   )r{   Ússl_contextÚunixÚpathÚoriginsÚ
extensionsÚsubprotocolsÚselect_subprotocolrI   rJ   rK   ÚcompressionÚopen_timeoutr#   Úmax_sizer?   Úcreate_connectionc               ó²  ‡ ‡‡‡‡‡	‡
‡‡‡‡‡‡‡‡— ‰	�t        ‰	«       |dk(  rt        ‰«      Šn|�t        d|› �«      ‚‰€t        Š|€M‰r3|€t	        d«      ‚t        j                  |t
        j                  ¬«      }n%t        j                  ||f«      }n|�t	        d«      ‚‰�‰j                  |dd¬«      }d
ˆˆˆˆ ˆˆˆˆˆˆˆ
ˆˆˆ	ˆfd	„}t        ||‰«      S )aH  
    Create a WebSocket server listening on ``host`` and ``port``.

    Whenever a client connects, the server creates a :class:`ServerConnection`,
    performs the opening handshake, and delegates to the ``handler``.

    The handler receives a :class:`ServerConnection` instance, which you can use
    to send and receive messages.

    Once the handler completes, either normally or with an exception, the server
    performs the closing handshake and closes the connection.

    :class:`WebSocketServer` mirrors the API of
    :class:`~socketserver.BaseServer`. Treat it as a context manager to ensure
    that it will be closed and call the :meth:`~WebSocketServer.serve_forever`
    method to serve requests::

        def handler(websocket):
            ...

        with websockets.sync.server.serve(handler, ...) as server:
            server.serve_forever()

    Args:
        handler: Connection handler. It receives the WebSocket connection,
            which is a :class:`ServerConnection`, in argument.
        host: Network interfaces the server binds to.
            See :func:`~socket.create_server` for details.
        port: TCP port the server listens on.
            See :func:`~socket.create_server` for details.
        sock: Preexisting TCP socket. ``sock`` replaces ``host`` and ``port``.
            You may call :func:`socket.create_server` to create a suitable TCP
            socket.
        ssl_context: Configuration for enabling TLS on the connection.
        origins: Acceptable values of the ``Origin`` header, for defending
            against Cross-Site WebSocket Hijacking attacks. Include :obj:`None`
            in the list if the lack of an origin is acceptable.
        extensions: List of supported extensions, in order in which they
            should be negotiated and run.
        subprotocols: List of supported subprotocols, in order of decreasing
            preference.
        select_subprotocol: Callback for selecting a subprotocol among
            those supported by the client and the server. It receives a
            :class:`ServerConnection` (not a
            :class:`~websockets.server.ServerProtocol`!) instance and a list of
            subprotocols offered by the client. Other than the first argument,
            it has the same behavior as the
            :meth:`ServerProtocol.select_subprotocol
            <websockets.server.ServerProtocol.select_subprotocol>` method.
        process_request: Intercept the request during the opening handshake.
            Return an HTTP response to force the response or :obj:`None` to
            continue normally. When you force an HTTP 101 Continue response,
            the handshake is successful. Else, the connection is aborted.
        process_response: Intercept the response during the opening handshake.
            Return an HTTP response to force the response or :obj:`None` to
            continue normally. When you force an HTTP 101 Continue response,
            the handshake is successful. Else, the connection is aborted.
        server_header: Value of  the ``Server`` response header.
            It defaults to ``"Python/x.y.z websockets/X.Y"``. Setting it to
            :obj:`None` removes the header.
        compression: The "permessage-deflate" extension is enabled by default.
            Set ``compression`` to :obj:`None` to disable it. See the
            :doc:`compression guide <../../topics/compression>` for details.
        open_timeout: Timeout for opening connections in seconds.
            :obj:`None` disables the timeout.
        close_timeout: Timeout for closing connections in seconds.
            :obj:`None` disables the timeout.
        max_size: Maximum size of incoming messages in bytes.
            :obj:`None` disables the limit.
        logger: Logger for this server.
            It defaults to ``logging.getLogger("websockets.server")``. See the
            :doc:`logging guide <../../topics/logging>` for details.
        create_connection: Factory for the :class:`ServerConnection` managing
            the connection. Set it to a wrapper or a subclass to customize
            connection handling.
    r’   zunsupported compression: zmissing path argument)Úfamilyz(path and sock arguments are incompatibleTF)Úserver_sideÚdo_handshake_on_connectc           	     óô  •‡— t        ‰«      }	 ‰s/| j                  t        j                  t        j                  d«       ‰�\| j                  |j                  «       «       t        | t        j                  «      sJ ‚| j                  «        | j                  d «       d }‰�	 	 	 	 	 	 dˆˆfd„}t        ‰‰‰|t        ‰‰
¬«      }‰€J ‚ ‰| |‰¬«      Š‰j                  ‰‰‰|j                  «       «       	  ‰	‰«       ‰j                  «        y # t        $ r | j                  «        Y y w xY w# t        $ r? |j                   j#                  dd¬«       ‰j                  t$        j&                  «       Y y w xY w)NTc                ó>   •— ‰€J ‚| ‰j                   u sJ ‚ ‰‰|«      S rˆ   )r+   )r+   r˜   Ú
connectionr™   s     €€r-   Úprotocol_select_subprotocolz@serve.<locals>.conn_handler.<locals>.protocol_select_subprotocolÇ  s2   ø€ ð
 .Ð9Ð9Ð9à# z×':Ñ':Ñ:Ð:Ð:Ù-¨j¸,ÓGÐGr.   )r–   r—   r˜   r™   rH   rœ   r?   r"   zconnection handler failedr1   )r+   r   r˜   zSequence[Subprotocol]rX   zOptional[Subprotocol])r   Ú
setsockoptr*   ÚIPPROTO_TCPÚTCP_NODELAYÚ
settimeoutrL   rP   ÚsslÚ	SSLSocketÚdo_handshaker   r   rN   r=   r�   r?   r@   r
   ÚINTERNAL_ERROR)r{   r|   Údeadliner¥   r+   r¤   r#   r�   r—   rf   r?   rœ   r›   r–   rI   rJ   r™   rK   r“   r˜   r”   s        @€€€€€€€€€€€€€€€r-   Úconn_handlerzserve.<locals>.conn_handler¨  s�  ù€ ô ˜LÓ)ˆðB	ñ Ø—‘¤× 2Ñ 2´F×4FÑ4FÈÔMð Ð&Ø—‘ × 0Ñ 0Ó 2Ô3Ü! $¬¯©Ô6Ð6Ð6Ø×!Ñ!Ô#Ø—‘ Ô%ð ð (ð "Ð-ðHØ,ðHà"7ðHð +öHô &ØØ%Ø)Ø#>Ü Ø!ØôˆHð %Ð0Ð0Ð0Ù*ØØØ+ôˆJð × Ñ ØØ ØØ× Ñ Ó"ô	ð	Ù�JÔð
 ×ÑÕøô ò 	Ø�J‰JŒLÙð	ûô ò 	7Ø�O‰O×!Ñ!Ð"=ÈÐ!ÔMØ×ÑœY×5Ñ5Ö6ð	7ús%   �C'D Ã7D/ ÄD,Ä+D,Ä/AE7Å6E7)r{   rV   r|   r   rX   rY   )
r   r   Ú
ValueErrorr   Ú	TypeErrorr*   Úcreate_serverÚAF_UNIXÚwrap_socketr   )rf   ÚhostÚportr{   r“   r”   r•   r–   r—   r˜   r™   rI   rJ   rK   rš   r›   r#   rœ   r?   r�   r¯   s   `   `` ``````` ````` r-   r   r   	  sí   ÿþ€ ðv ÐÜ˜lÔ+à�iÒÜ5°jÓA‰
Ø	Ð	 ÜÐ4°[°MÐBÓCÐCàÐ Ü,Ðð €|ÙØˆ|ÜÐ 7Ó8Ð8Ü×'Ñ'¨´V·^±^ÔD‰Dä×'Ñ'¨¨t¨Ó5‰DàÐÜÐFÓGÐGð ÐØ×&Ñ&ØØà$)ð	 'ó 
ˆ÷P÷ Pó Pôh ˜4 ¨vÓ6Ð6r.   c                ó    — t        | f|ddœ|¤ŽS )aî  
    Create a WebSocket server listening on a Unix socket.

    This function is identical to :func:`serve`, except the ``host`` and
    ``port`` arguments are replaced by ``path``. It's only available on Unix.

    It's useful for deploying a server behind a reverse proxy such as nginx.

    Args:
        handler: Connection handler. It receives the WebSocket connection,
            which is a :class:`ServerConnection`, in argument.
        path: File system path to the Unix socket.

    T)r•   r”   )r   )rf   r•   Úkwargss      r-   r   r   ÿ  s   € ô& �Ð9˜t¨$Ñ9°&Ñ9Ð9r.   )NN)*rf   z"Callable[[ServerConnection], None]rµ   r\   r¶   úOptional[int]r{   zOptional[socket.socket]r“   zOptional[ssl.SSLContext]r”   Úboolr•   r\   r–   z$Optional[Sequence[Optional[Origin]]]r—   z*Optional[Sequence[ServerExtensionFactory]]r˜   zOptional[Sequence[Subprotocol]]r™   zTOptional[Callable[[ServerConnection, Sequence[Subprotocol]], Optional[Subprotocol]]]rI   rZ   rJ   r[   rK   r\   rš   r\   r›   rW   r#   rW   rœ   r¹   r?   r�   r�   z Optional[Type[ServerConnection]]rX   r   rˆ   )rf   z!Callable[[ServerConnection], Any]r•   r\   r¸   r   rX   r   )0Ú
__future__r   rB   rg   rk   rr   r*   rª   ri   r%   Útypesr   Útypingr   r   r   r   r	   Úwebsockets.framesr
   Úextensions.baser   Úextensions.permessage_deflater   rF   r   r   Úhttp11r   r   r+   r   r   r   Úserverr   r   r   r   r¤   r   Úutilsr   Ú__all__r   r   r   r   r‰   r.   r-   ú<module>rÅ      sæ  ðÝ "ã Û Û 	Û Û Û 
Û 
Û Ý ß :Õ :å 'å 4Ý LÝ +Ý ß &ß .Ñ .Ý #ß 4Ñ 4Ý "Ý ò I€ôO$�zô O$÷dUñ Uðt Øðs7ð %)Ø,0ØØà48Ø=AØ48ð 	ð 	ð 	Ø#-Ø!*à$&Ø%'à#à#'à:>ñUs7Ø/ðs7à
ðs7ð ðs7ð "ðs7ð *ðs7ð ðs7ð ðs7ð 2ðs7ð ;ðs7ð 2ðs7ððs7ð(ð)s7ð4ð5s7ð@ !ðAs7ðB ðCs7ðF "ðGs7ðH #ðIs7ðL ðMs7ðP !ðQs7ðT 8ðUs7ðV óWs7ðp ð:Ø.ð:à
ð:ð ð:ð ô	:r.   