
    Ji`                    R   S SK Jr  S SKrS SKrS SKrS SKJrJ	r	J
r
Jr  SSKJrJr  SSKJrJrJr  / SQr S S	KJr  S S
KJrJr  SSSS.             SS jjr S       SS jjr " S S5      rg! \ a*    SSSS.             SS jjr S       SS jjr N:f = f)    )annotationsN)Any	AwaitableCallableLiteral   )RequestResponse   )ServerServerConnectionserve)route
unix_routeRouter)NotFound)MapRequestRedirect)server_namesslcreate_routerc                  ^^	 Uc  SOSnUSLa  Ub  X%S'   Uc  [         nU" XU5      m	UR                  SS5      mTc  T	R                  nO      SUU	4S jjn[        T	R                  /UQ7SU0UD6$ )	ax  
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_requestc                   >#    T" X5      n[        U[        5      (       a
  UI S h  vN nUb  U$ TR                  X5      $  N7fN)
isinstancer   route_request)
connectionrequestresponse_process_requestrouters      [/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/websockets/asyncio/router.pyr   route.<locals>.process_request   sG      ,J@h	22%-~H'#O++J@@  .s   #AAA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    R ![Te
d?s5M "MwZ@ JJ($/ 	 # $$ A,A7>A A A V^^VdVOVvVV    c                     [        U 4SUS.UD6$ )a2  
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)unixpath)r   r+   r2   r-   s      r%   r   r      s      W=4d=f==r/   c                   [        S5      e)Nzroute() requires werkzeugImportError)r+   r   r   r   r,   r-   s         r%   r   r      s     566r/   c                    [        S5      e)Nzunix_route() requires werkzeugr5   r3   s      r%   r   r      s    
 :;;r/   c                  v    \ rS rSrSr  S       SS jjrSS jrSS jrSS jr      SS jr	SS	 jr
S
rg)r      z*WebSocket router supporting :func:`route`.Nc                x    Xl         X l        X0l        U R                   R                  5        H
  nSUl        M     g )NT)r+   r   r.   
iter_rules	websocket)selfr+   r   r.   rules        r%   __init__Router.__init__   s2     &$LL++-D!DN .r/   c                R    U R                   c  UR                  S   $ U R                   $ )NHost)r   headers)r=   r    r!   s      r%   get_server_nameRouter.get_server_name   s)    #??6**###r/   c                |    UR                  [        R                  R                  SU 35      nX#R                  S'   U$ )Nz	Found at Location)respondhttp
HTTPStatusFOUNDrC   )r=   r    urlr"   s       r%   redirectRouter.redirect   s8    %%doo&;&;y=NO'*$r/   c                V    UR                  [        R                  R                  S5      $ )Nz	Not Found)rH   rI   rJ   	NOT_FOUNDr=   r    s     r%   	not_foundRouter.not_found   s    !!$//";";[IIr/   c                   U R                   R                  U R                  X5      U R                  S9n [        R
                  R                  UR                  5      nUR                  UR                  UR                  S9u  pVXVsUl        Ul        g! [         a%  nU R                  XR                  5      s SnA$ SnAf[         a    U R                  U5      s $ f = f)zRoute incoming request.)r   r.   )	path_info
query_argsN)r+   bindrD   r.   urllibparseurlparser2   matchqueryr   rM   new_urlr   rR   r*   handler_kwargs)r=   r    r!   url_map_adapterparsedr*   r-   rM   s           r%   r   Router.route_request   s     ,,++,,ZA , 
		.\\**7<<8F-33 ++!<< 4 OG 9@5
J5  	?==-=-=>> 	.>>*--	.s$   AB 
CB=7C=CCc                X   #    UR                   " U40 UR                  D6I Sh  vN $  N7f)zHandle a connection.N)r*   r^   rQ   s     r%   r*   Router.handler   s'     ''
Pj6O6OPPPPs   !*(*)r   r+   r.   )Nr   )r+   r   r   
str | Noner.   strr(   None)r    r   r!   r	   r(   re   )r    r   rL   re   r(   r
   )r    r   r(   r
   r'   )r    r   r(   rf   )__name__
__module____qualname____firstlineno____doc__r?   rD   rM   rR   r   r*   __static_attributes__ r/   r%   r   r      sk    4
 #'	
"
"  
" 	
"
 

"$
J*5<	*Qr/   r   )r+   r   r,   r   r   rd   r   z,ssl_module.SSLContext | Literal[True] | Noner   ztype[Router] | Noner-   r   r(   Awaitable[Server]r   )r+   r   r2   rd   r-   r   r(   rn   )
__future__r   rI   r   
ssl_moduleurllib.parserX   typingr   r   r   r   http11r	   r
   serverr   r   r   __all__werkzeug.exceptionsr   werkzeug.routingr   r   r   r   r6   r   rm   r/   r%   <module>rx      sb   "    4 4 & 3 3 ,U>,54 #'<@-1jWjWjW  jW :	jW
 +jW jW 
jW\  >>> > 
	>&4Q 4Qi  <
 #'<@-1777  7 :	7
 +7 7 
7  <<< < 
	<<s   A6 6-B&%B&