K iddlmZddlZddlZddlZddlmZm Z m Z ddl m Z ddl mZmZddlmZmZdd lmZmZmZgd ZGd d Zdddd  ddZ d ddZy)) annotationsN)AnyCallableLiteral)NotFound)MapRequestRedirect)RequestResponse)ServerServerConnectionserve)route unix_routeRouterc`eZdZdZ d d dZd dZd dZd dZ ddZddZ y)rz*WebSocket router supporting :func:`route`.Ncz||_||_||_|jjD] }d|_ y)NT)url_map server_name url_scheme iter_rules websocket)selfrrrrules \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/websockets/sync/router.py__init__zRouter.__init__s=  &$LL++- "D!DN "cP|j|jdS|jS)NHost)rheaders)r connectionrequests rget_server_namezRouter.get_server_name!s)    #??6* *## #rc~|jtjjd|}||jd<|S)Nz Found at Location)respondhttp HTTPStatusFOUNDr")rr#urlresponses rredirectzRouter.redirect's:%%doo&;&;y=NO'*$rcV|jtjjdS)Nz Not Found)r(r)r* NOT_FOUNDrr#s r not_foundzRouter.not_found,s!!$//";";[IIrc|jj|j|||j} tj j |j}|j|j|j\}}||c|_|_y#t$r&}|j||jcYd}~Sd}~wt$r|j|cYSwxYw)zRoute incoming request.)rr) path_info query_argsN)rbindr%rurllibparseurlparsepathmatchqueryr r.new_urlrr2handlerhandler_kwargs)rr#r$url_map_adapterparsedr>kwargsr.s r route_requestzRouter.route_request/s,,++,,ZA,  .\\**7<<8F-33 ++!<<4OGV9@5 J5  ?==X-=-=> > .>>*- - .s$AB C&%CC&C&%C&c<|j|fi|jS)zHandle a connection.)r>r?r1s rr>zRouter.handlerDs !z!!*J 0I0IJJr)Nws)rrr str | NonerstrreturnNone)r#rr$r rHrG)r#rr,rGrHr )r#rrHr r#rr$r rHzResponse | None)r#rrHrI) __name__ __module__ __qualname____doc__rr%r.r2rCr>rrrrsk4 #' " "  " "  "$  J*5< *Krr)rssl create_routerc |dnd}|dur|||d<|t}|||| |jdd j}n d fd }t jg|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/", 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. NrEwssTrPprocess_requestcB||}||Sj||SN)rC)r#r$r-_process_requestrouters rrTzroute..process_requests/( G) rrrPrQargsrBrrTrWrXs @@rrrIsJ%J $3?u  7K rfs" ))(1&33 ,4K4Kt#8<)- dS dS dSdS 6 dS ' dS  dS dSR: : :: :r