
    :"ri                    <   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	 d d	lmZ d d
lmZmZ dddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ G d d      Zy# e$ r& dddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZY 5w xY w)    )annotationsN)AnyCallableLiteral   )RequestResponse   )ServerServerConnectionserve)route
unix_routeRouter)NotFound)MapRequestRedirect)server_namesslcreate_routerc                  	 |dnd}|dur|||d<   |t         } || ||      	|j                  dd      	j                  }n	 	 	 	 	 	 d	fd}t        	j                  g|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_requestc                B     | |      }||S j                  | |      S N)route_request)
connectionrequestresponse_process_requestrouters      ^/var/www/fortnox.pascalinesoft.com/venv/lib/python3.12/site-packages/websockets/sync/router.pyr   zroute.<locals>.process_request   s/     ,J@'#O++J@@    r   r   r   r   returnzResponse | None)r   popr   r   handler)
url_mapr   r   r   argskwargs
url_schemer   r!   r"   s
           @@r#   r   r   (   s    J ![Te
d?sF5M "MwZ@ JJ($/ 	 # $$ A,A7>A A V^^VdVOVvVVr$   c                     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)unixpath)r   r)   r/   r+   s      r#   r   r      s      W=4d=f==r$   c                   t        d      )Nzroute() requires werkzeugImportError)r)   r   r   r   r*   r+   s         r#   r   r      s     566r$   c                    t        d      )Nzunix_route() requires werkzeugr2   r0   s      r#   r   r      s    
 :;;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   z*WebSocket router supporting :func:`route`.Nc                z    || _         || _        || _        | j                   j                         D ]	  }d|_         y )NT)r)   r   r,   
iter_rules	websocket)selfr)   r   r,   rules        r#   __init__zRouter.__init__   s=     &$LL++- 	"D!DN	"r$   c                P    | j                   |j                  d   S | j                   S )NHost)r   headers)r9   r   r   s      r#   get_server_namezRouter.get_server_name   s)    #??6**###r$   c                ~    |j                  t        j                  j                  d|       }||j                  d<   |S )Nz	Found at Location)respondhttp
HTTPStatusFOUNDr>   )r9   r   urlr    s       r#   redirectzRouter.redirect   s:    %%doo&;&;y=NO'*$r$   c                V    |j                  t        j                  j                  d      S )Nz	Not Found)rB   rC   rD   	NOT_FOUNDr9   r   s     r#   	not_foundzRouter.not_found   s    !!$//";";[IIr$   c                   | j                   j                  | j                  ||      | j                        }	 t        j
                  j                  |j                        }|j                  |j                  |j                        \  }}||c|_        |_        y# t        $ r&}| j                  ||j                        cY d}~S d}~wt        $ r | j                  |      cY S w xY w)zRoute incoming request.)r   r,   )	path_info
query_argsN)r)   bindr?   r,   urllibparseurlparser/   matchqueryr   rG   new_urlr   rK   r(   handler_kwargs)r9   r   r   url_map_adapterparsedr(   r+   rG   s           r#   r   zRouter.route_request   s     ,,++,,ZA , 
		.\\**7<<8F-33 ++!<< 4 OGV 9@5
J5  	?==X-=-=>> 	.>>*--	.s$   AB 	C&%C C&C&%C&c                <     |j                   |fi |j                  S )zHandle a connection.)r(   rV   rJ   s     r#   r(   zRouter.handler   s     !z!!*J
0I0IJJr$   )Nr   )r)   r   r   
str | Noner,   strr&   None)r   r   r   r   r&   r[   )r   r   rF   r[   r&   r	   )r   r   r&   r	   r%   )r   r   r&   r\   )
__name__
__module____qualname____doc__r;   r?   rG   rK   r   r(    r$   r#   r   r      sk    4
 #'	
"
"  
" 	
"
 

"$
J*5<	*Kr$   r   )r)   r   r*   r   r   rZ   r   z,ssl_module.SSLContext | Literal[True] | Noner   ztype[Router] | Noner+   r   r&   r   r   )r)   r   r/   rZ   r+   r   r&   r   )
__future__r   rC   r   
ssl_moduleurllib.parserP   typingr   r   r   http11r   r	   serverr   r   r   __all__werkzeug.exceptionsr   werkzeug.routingr   r   r   r   r3   r   ra   r$   r#   <module>rk      sb   "    ) ) & 3 3 ,O>,54 #'<@-1dWdWdW  dW :	dW
 +dW dW 
dWP  >>> > 
	>&4K 4K]  <
 #'<@-1777  7 :	7
 +7 7 
7  <<< < 
	<<s   A0 0(BB