o
    Yi                     @  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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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returnr
   c                O     t d)Nzroute() requires werkzeugImportError)r   r   r   r   r   r    r    P/home/kim/smarthome/.venv/lib/python3.10/site-packages/websockets/sync/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.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 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                   s"    | |}|d ur|S  | |S N)route_request)r'   r(   responseZ_process_requestZ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    r-   r!   r   (   s   E
c                 K  s   t | fd|d|S )ag  
        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)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   r1   strr   Nonec                 C  s,   || _ || _|| _| j  D ]}d|_qd S )NT)r   r   r1   Z
iter_rulesZ	websocket)selfr   r   r1   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)r5   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 ZLocation)respondhttp
HTTPStatusFOUNDr9   )r5   r'   r;   r,   r    r    r!   redirect   s   
zRouter.redirectc                 C  s   | tjjdS )Nz	Not Found)r<   r=   r>   	NOT_FOUNDr5   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   r1   )Z	path_infoZ
query_argsN)r   bindr:   r1   urllibparseurlparser"   matchqueryr   r@   new_urlr   rC   r0   handler_kwargs)r5   r'   r(   Zurl_map_adapterparsedr0   r   r@   r    r    r!   r+      s$   
zRouter.route_requestc                 C  s   |j |fi |jS )zHandle a connection.)r0   rK   rB   r    r    r!   r0      s   zRouter.handler)Nr$   )r   r   r   r   r1   r3   r   r4   )r'   r   r(   r   r   r3   )r'   r   r;   r3   r   r   )r'   r   r   r   r.   )r'   r   r   r4   )
__name__
__module____qualname____doc__r7   r:   r@   rC   r+   r0   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   r=   r   Z
ssl_moduleurllib.parserE   typingr   r   r   Zhttp11r   r   serverr
   r   r   __all__Zwerkzeug.exceptionsr   Zwerkzeug.routingr   r   r   r   r   r   r    r    r    r!   <module>   s4    h