
    A>qj                    H   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ZddlmZmZ ddlmZmZmZ dd	gZ	 d d
lmZ d dlmZmZ dddej4                  d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ G d d	      Zy# e$ r& dddej4                  d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZY 5w xY w)    )annotationsN)Any	AwaitableCallableLiteral   )RequestResponse   )ServerServerConnectionserverouteRouter)NotFound)MapRequestRedirect)server_namesslcreate_routertask_statusc                  	
K   |dnd}|dur|||d<   |t         } || ||      
|j                  dd      		
j                  }n	 	 	 	 	 	 d		
fd}t        
j                  g|||d| d{   S 7 w)
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.trio.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.trio.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 event to exit the server
            stop = trio.Event()

            with trio.open_nursery() as nursery:
                server = await nursery.start(route, url_map, ...)
                async with server:
                    await stop.wait()

        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.
            task_status: For compatibility with :meth:`nursery.start
                <trio.Nursery.start>`.

        NwswssTr   process_requestc                   K    | |      }t        |t              r
| d {   }||S j                  | |      S 7 w)N)
isinstancer   route_request)
connectionrequestresponse_process_requestrouters      U/opt/rentech/trading_bot/.venv/lib/python3.12/site-packages/websockets/trio/router.pyr   zroute.<locals>.process_request   sI      ,J@h	2%-~H'#O++J@@  .s   ?=?)r   r   r   r   r    r	   returnzResponse | None)r   popr   r   handler)url_mapr   r   r   r   argskwargs
url_schemer   r"   r#   s            @@r$   r   r   $   s     X ![Te
d?sF5M "MwZ@ JJ($/ 	 # $$ 	A,	A 	A !	A NN

 ,#	

 
 
 	
 
s   A1A<5A:6A<c                   K   t        d      w)Nzroute() requires werkzeug)ImportError)r)   r   r   r   r   r*   r+   s          r$   r   r      s      566s   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	"    c                P    | j                   |j                  d   S | j                   S )NHost)r   headers)r3   r   r    s      r$   get_server_namezRouter.get_server_name   s)    #??6**###r6   c                ~    |j                  t        j                  j                  d|       }||j                  d<   |S )Nz	Found at Location)respondhttp
HTTPStatusFOUNDr9   )r3   r   urlr!   s       r$   redirectzRouter.redirect   s:    %%doo&;&;y=NO'*$r6   c                V    |j                  t        j                  j                  d      S )Nz	Not Found)r=   r>   r?   	NOT_FOUNDr3   r   s     r$   	not_foundzRouter.not_found   s    !!$//";";[IIr6   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urlparsepathmatchqueryr   rB   new_urlr   rF   r(   handler_kwargs)r3   r   r    url_map_adapterparsedr(   r+   rB   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                X   K    |j                   |fi |j                   d{   S 7 w)zHandle a connection.N)r(   rR   rE   s     r$   r(   zRouter.handler   s*     'Z''
Pj6O6OPPPPs   !*(*)Nr   )r)   r   r   
str | Noner,   strr&   None)r   r   r    r	   r&   rW   )r   r   rA   rW   r&   r
   )r   r   r&   r
   r%   )r   r   r&   rX   )
__name__
__module____qualname____doc__r5   r:   rB   rF   r   r(    r6   r$   r   r      sk    4
 #'	
"
"  
" 	
"
 

"$
J*5<	*Qr6   )r)   r   r*   r   r   rV   r   z,ssl_module.SSLContext | Literal[True] | Noner   ztype[Router] | Noner   ztrio.TaskStatus[Server]r+   r   r&   rX   )
__future__r   r>   r   
ssl_moduleurllib.parserK   typingr   r   r   r   triohttp11r	   r
   serverr   r   r   __all__werkzeug.exceptionsr   werkzeug.routingr   r   TASK_STATUS_IGNOREDr   r.   r   r]   r6   r$   <module>ri      s-   "    4 4  & 3 3 H
G
,5( #'<@-1/3/G/Gt
t
t
  t
 :	t

 +t
 -t
 t
 
t
n4Q 4QM  7
 #'<@-1/3/G/G	7	7	7  	7 :		7
 +	7 -	7 	7 
	77s   A6 6(B! B!