o
    Ð­j`  ã                   @  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
mZ ddlmZmZ ddlmZmZmZ g d¢Zzd d	lmZ d d
lmZmZ W n ey^   ddddœd"dd„Z	d#d$dd„ZY nw ddddœd"dd„Z	d#d$dd„ZG d d!„ d!ƒZdS )%é    )ÚannotationsN)ÚAnyÚ	AwaitableÚCallableÚLiteralé   )ÚRequestÚResponseé   )ÚServerÚServerConnectionÚserve)ÚrouteÚ
unix_routeÚRouter)ÚNotFound)ÚMapÚRequestRedirect)Úserver_nameÚsslÚcreate_routerÚurl_mapr   Úargsr   r   ú
str | Noner   ú,ssl_module.SSLContext | Literal[True] | Noner   útype[Router] | NoneÚkwargsÚreturnúAwaitable[Server]c                O  ó   t dƒ‚)Nzroute() requires werkzeug©ÚImportError)r   r   r   r   r   r   © r"   úV/var/www/html/CropPilot/venv/lib/python3.10/site-packages/websockets/asyncio/router.pyr      s   r   Úpathc                 K  r   )Nzunix_route() requires werkzeugr    ©r   r$   r   r"   r"   r#   r      s   r   c                  sˆ   |du rdnd}|dur|dur||d< |du rt }|| ||ƒ‰| dd¡‰ ˆ du r.ˆj}nd‡ ‡fdd„}tˆjg|¢R d|i|¤ŽS )aÐ	  
        Create a WebSocket server dispatching connections to different handlers.

        This feature requires the third-party library `werkzeug`_:

        .. code-block:: console

            $ pip install werkzeug

        .. _werkzeug: https://werkzeug.palletsprojects.com/

        :func:`route` accepts the same arguments as
        :func:`~websockets.sync.server.serve`, except as described below.

        The first argument is a :class:`werkzeug.routing.Map` that maps URL patterns
        to connection handlers. In addition to the connection, handlers receive
        parameters captured in the URL as keyword arguments.

        Here's an example::


            from websockets.asyncio.router import route
            from werkzeug.routing import Map, Rule

            async def channel_handler(websocket, channel_id):
                ...

            url_map = Map([
                Rule("/channel/<uuid:channel_id>", endpoint=channel_handler),
                ...
            ])

            # set this future to exit the server
            stop = asyncio.get_running_loop().create_future()

            async with route(url_map, ...) as server:
                await stop


        Refer to the documentation of :mod:`werkzeug.routing` for details.

        If you define redirects with ``Rule(..., redirect_to=...)`` in the URL map,
        when the server runs behind a reverse proxy that modifies the ``Host``
        header or terminates TLS, you need additional configuration:

        * Set ``server_name`` to the name of the server as seen by clients. When
          not provided, websockets uses the value of the ``Host`` header.

        * Set ``ssl=True`` to generate ``wss://`` URIs without enabling TLS.
          Under the hood, this bind the URL map with a ``url_scheme`` of
          ``wss://`` instead of ``ws://``.

        There is no need to specify ``websocket=True`` in each rule. It is added
        automatically.

        Args:
            url_map: Mapping of URL patterns to connection handlers.
            server_name: Name of the server as seen by clients. If :obj:`None`,
                websockets uses the value of the ``Host`` header.
            ssl: Configuration for enabling TLS on the connection. Set it to
                :obj:`True` if a reverse proxy terminates TLS connections.
            create_router: Factory for the :class:`Router` dispatching requests to
                handlers. Set it to a wrapper or a subclass to customize routing.

        NÚwsÚwssTr   Úprocess_requestÚ
connectionr   Úrequestr   r   úResponse | Nonec                 “  s8   �ˆ | |ƒ}t |tƒr|I d H }|d ur|S ˆ | |¡S ©N)Ú
isinstancer   Úroute_request)r)   r*   Úresponse©Ú_process_requestÚrouterr"   r#   r(   ˆ   s   €


zroute.<locals>.process_request©r)   r   r*   r   r   r+   )r   Úpopr.   r   Úhandler)r   r   r   r   r   r   Ú
url_schemer(   r"   r0   r#   r   (   s   I
úý
c                 K  s   t | fd|dœ|¤ŽS )aj  
        Create a WebSocket Unix server dispatching connections to different handlers.

        :func:`unix_route` combines the behaviors of :func:`route` and
        :func:`~websockets.asyncio.server.unix_serve`.

        Args:
            url_map: Mapping of URL patterns to connection handlers.
            path: File system path to the Unix socket.

        T)Úunixr$   )r   r%   r"   r"   r#   r   ”   s   c                   @  sR   e Zd ZdZ		dd dd„Zd!dd„Zd"dd„Zd#dd„Zd$dd„Zd%dd„Z	dS )&r   z*WebSocket router supporting :func:`route`.Nr&   r   r   r   r   r6   Ústrr   ÚNonec                 C  s,   || _ || _|| _| j  ¡ D ]}d|_qd S )NT)r   r   r6   Ú
iter_rulesÚ	websocket)Úselfr   r   r6   Úruler"   r"   r#   Ú__init__ª   s   ÿzRouter.__init__r)   r   r*   r   c                 C  s   | j d u r
|jd S | j S )NÚHost)r   Úheaders)r<   r)   r*   r"   r"   r#   Úget_server_name¶   s   

zRouter.get_server_nameÚurlr	   c                 C  s$   |  tjjd|› �¡}||jd< |S )Nz	Found at ÚLocation)ÚrespondÚhttpÚ
HTTPStatusÚFOUNDr@   )r<   r)   rB   r/   r"   r"   r#   Úredirect¼   s   
zRouter.redirectc                 C  s   |  tjjd¡S )Nz	Not Found)rD   rE   rF   Ú	NOT_FOUND©r<   r)   r"   r"   r#   Ú	not_foundÁ   s   zRouter.not_foundr+   c              
   C  s¢   | j j|  ||¡| jd�}ztj |j¡}|j|j|j	d�\}}W n& t
y: } z|  ||j¡W  Y d}~S d}~w tyG   |  |¡ Y S w |||_|_dS )zRoute incoming request.)r   r6   )Ú	path_infoÚ
query_argsN)r   ÚbindrA   r6   ÚurllibÚparseÚurlparser$   ÚmatchÚqueryr   rH   Únew_urlr   rK   r5   Úhandler_kwargs)r<   r)   r*   Úurl_map_adapterÚparsedr5   r   rH   r"   r"   r#   r.   Ä   s$   
þþ€ÿzRouter.route_requestc                 Ã  s   �|j |fi |j¤ŽI dH S )zHandle a connection.N)r5   rU   rJ   r"   r"   r#   r5   Ù   s   €zRouter.handler)Nr&   )r   r   r   r   r6   r8   r   r9   )r)   r   r*   r   r   r8   )r)   r   rB   r8   r   r	   )r)   r   r   r	   r3   )r)   r   r   r9   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r>   rA   rH   rK   r.   r5   r"   r"   r"   r#   r   §   s    ü



r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r,   )r   r   r$   r   r   r   r   r   )Ú
__future__r   rE   r   Ú
ssl_moduleÚurllib.parserO   Útypingr   r   r   r   Úhttp11r   r	   Úserverr   r   r   Ú__all__Úwerkzeug.exceptionsr   Úwerkzeug.routingr   r   r!   r   r   r   r"   r"   r"   r#   Ú<module>   s4    ûþôûnþ