a
    ñ»jS  ã                	   @  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
 d dlmZ d dlmZmZ ddlmZmZ dd	lmZmZmZ g d
¢ZG dd„ dƒZddddœddddddddœdd„Zddddddœdd„ZdS )é    )ÚannotationsN)ÚAnyÚCallableÚLiteral)ÚNotFound)ÚMapÚRequestRedirecté   )ÚRequestÚResponseé   )ÚServerÚServerConnectionÚserve)ÚrouteÚ
unix_routeÚRouterc                   @  s|   e Zd ZdZddddddœd	d
„Zddddœdd„Zddddœdd„Zdddœdd„Zddddœdd„Zdddœdd„Z	dS )r   z*WebSocket router supporting :func:`route`.NÚwsr   ú
str | NoneÚstrÚNone)Úurl_mapÚserver_nameÚ
url_schemeÚreturnc                 C  s,   || _ || _|| _| j  ¡ D ]
}d|_qd S )NT)r   r   r   Z
iter_rulesZ	websocket)Úselfr   r   r   Úrule© r   ú<E:\soccer\ml_env\Lib\site-packages\websockets/sync/router.pyÚ__init__   s
    zRouter.__init__r   r
   ©Ú
connectionÚrequestr   c                 C  s   | j d u r|jd S | j S d S )NÚHost)r   Úheaders)r   r!   r"   r   r   r   Úget_server_name!   s    

zRouter.get_server_namer   )r!   Úurlr   c                 C  s$   |  tjjd|› �¡}||jd< |S )Nz	Found at ZLocation)ÚrespondÚhttpÚ
HTTPStatusÚFOUNDr$   )r   r!   r&   Úresponser   r   r   Úredirect'   s    
zRouter.redirect)r!   r   c                 C  s   |  tjjd¡S )Nz	Not Found)r'   r(   r)   Ú	NOT_FOUND©r   r!   r   r   r   Ú	not_found,   s    zRouter.not_foundúResponse | Nonec              
   C  s¢   | j j|  ||¡| jd�}z(tj |j¡}|j|j|j	d�\}}W nL t
yt } z|  ||j¡W  Y d}~S d}~0  tyŽ   |  |¡ Y S 0 || |_|_dS )zRoute incoming request.)r   r   )Z	path_infoZ
query_argsN)r   Úbindr%   r   ÚurllibÚparseÚurlparseÚpathÚmatchÚqueryr   r,   Únew_urlr   r/   ÚhandlerÚhandler_kwargs)r   r!   r"   Zurl_map_adapterÚparsedr9   Úkwargsr,   r   r   r   Úroute_request/   s     
þþ"zRouter.route_requestc                 C  s   |j |fi |j¤ŽS )zHandle a connection.)r9   r:   r.   r   r   r   r9   D   s    zRouter.handler)Nr   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r,   r/   r=   r9   r   r   r   r   r      s     ür   )r   ÚsslÚcreate_routerr   r   r   z,ssl_module.SSLContext | Literal[True] | Noneztype[Router] | Noner   )r   Úargsr   rB   rC   r<   r   c                  s�   |du rdnd}|dur(|dur(||d< |du r4t }|| ||ƒ‰| dd¡‰ ˆ du r\ˆj}nddd	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.sync.router import route
        from werkzeug.routing import Map, Rule

        def channel_handler(websocket, channel_id):
            ...

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

        with route(url_map, ...) as server:
            server.serve_forever()

    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 actually 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.

    Nr   ÚwssTrB   Úprocess_requestr   r
   r0   r    c                   s"   ˆ | |ƒ}|d ur|S ˆ  | |¡S )N)r=   )r!   r"   r+   ©Z_process_requestZrouterr   r   rF   ¥   s    
zroute.<locals>.process_request)r   Úpopr=   r   r9   )r   r   rB   rC   rD   r<   r   rF   r   rG   r   r   I   s    E
úýr   )r   r5   r<   r   c                 K  s   t | fd|dœ|¤ŽS )aK  
    Create a WebSocket Unix server dispatching connections to different handlers.

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

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

    T)Úunixr5   )r   )r   r5   r<   r   r   r   r   °   s    r   )N)Ú
__future__r   r(   rB   Z
ssl_moduleÚurllib.parser2   Útypingr   r   r   Zwerkzeug.exceptionsr   Zwerkzeug.routingr   r   Zhttp11r
   r   Úserverr   r   r   Ú__all__r   r   r   r   r   r   r   Ú<module>   s    :ûi þ