From 5836d60d88fde40ee7559825eb7c81a0caaa8553 Mon Sep 17 00:00:00 2001 From: Arksine Date: Tue, 10 Nov 2020 08:27:07 -0500 Subject: [PATCH] app: allow http requests to specifiy a websocket id Some requests, such "printer/objects/subscribe", require a websocket for asynchronous updates. Clients may now specify a "connection_id" in the form data that identifies an associated websocket. Signed-off-by: Eric Callahan --- moonraker/app.py | 6 ++++-- moonraker/authorization.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/moonraker/app.py b/moonraker/app.py index fbd5acb..11b39b2 100644 --- a/moonraker/app.py +++ b/moonraker/app.py @@ -270,12 +270,13 @@ class RemoteRequestHandler(AuthorizedRequestHandler): await self._process_http_request() async def _process_http_request(self): + conn = self.get_associated_websocket() args = {} if self.request.query: args = self.query_parser(self.request) try: result = await self.server.make_request( - WebRequest(self.remote_callback, args)) + WebRequest(self.remote_callback, args, conn=conn)) except ServerError as e: raise tornado.web.HTTPError( e.status_code, str(e)) from e @@ -307,12 +308,13 @@ class LocalRequestHandler(AuthorizedRequestHandler): raise tornado.web.HTTPError(405) async def _process_http_request(self, method): + conn = self.get_associated_websocket() args = {} if self.request.query: args = self.query_parser(self.request) try: result = await self.callback( - WebRequest(self.request.path, args, method)) + WebRequest(self.request.path, args, method, conn=conn)) except ServerError as e: raise tornado.web.HTTPError( e.status_code, str(e)) from e diff --git a/moonraker/authorization.py b/moonraker/authorization.py index 087ce06..14aa76c 100644 --- a/moonraker/authorization.py +++ b/moonraker/authorization.py @@ -208,6 +208,20 @@ class AuthorizedRequestHandler(tornado.web.RequestHandler): else: super(AuthorizedRequestHandler, self).options() + def get_associated_websocket(self): + # Return associated websocket connection if an id + # was provided by the request + conn = None + conn_id = self.get_argument('connection_id', None) + if conn_id is not None: + try: + conn_id = int(conn_id) + except Exception: + pass + else: + conn = self.wsm.get_websocket(conn_id) + return conn + # Due to the way Python treats multiple inheritance its best # to create a separate authorized handler for serving files class AuthorizedFileHandler(tornado.web.StaticFileHandler):