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 <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-11-10 08:27:07 -05:00
parent 92d1715d88
commit 5836d60d88
2 changed files with 18 additions and 2 deletions

View File

@ -270,12 +270,13 @@ class RemoteRequestHandler(AuthorizedRequestHandler):
await self._process_http_request() await self._process_http_request()
async def _process_http_request(self): async def _process_http_request(self):
conn = self.get_associated_websocket()
args = {} args = {}
if self.request.query: if self.request.query:
args = self.query_parser(self.request) args = self.query_parser(self.request)
try: try:
result = await self.server.make_request( result = await self.server.make_request(
WebRequest(self.remote_callback, args)) WebRequest(self.remote_callback, args, conn=conn))
except ServerError as e: except ServerError as e:
raise tornado.web.HTTPError( raise tornado.web.HTTPError(
e.status_code, str(e)) from e e.status_code, str(e)) from e
@ -307,12 +308,13 @@ class LocalRequestHandler(AuthorizedRequestHandler):
raise tornado.web.HTTPError(405) raise tornado.web.HTTPError(405)
async def _process_http_request(self, method): async def _process_http_request(self, method):
conn = self.get_associated_websocket()
args = {} args = {}
if self.request.query: if self.request.query:
args = self.query_parser(self.request) args = self.query_parser(self.request)
try: try:
result = await self.callback( result = await self.callback(
WebRequest(self.request.path, args, method)) WebRequest(self.request.path, args, method, conn=conn))
except ServerError as e: except ServerError as e:
raise tornado.web.HTTPError( raise tornado.web.HTTPError(
e.status_code, str(e)) from e e.status_code, str(e)) from e

View File

@ -208,6 +208,20 @@ class AuthorizedRequestHandler(tornado.web.RequestHandler):
else: else:
super(AuthorizedRequestHandler, self).options() 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 # Due to the way Python treats multiple inheritance its best
# to create a separate authorized handler for serving files # to create a separate authorized handler for serving files
class AuthorizedFileHandler(tornado.web.StaticFileHandler): class AuthorizedFileHandler(tornado.web.StaticFileHandler):