From c1fe8f5a359e943d6a3cc6a46f0c69dd4ea912be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 3 Apr 2018 16:45:14 +0200 Subject: [PATCH] Fix RawWebsocketTransport of sockjs Ran into issues due to session wrapping in order to support custom Json encoding. We now make sure to * only wrap non-raw sessions (since only those have send_jsonified defined which we overwrite) * overwrite tornado.escape.json_encode to use our custom encoder as fallback, since that is used for RawWebsocketTransport Fixes an issue with the /sockjs/websocket endpoint hinted at in #2529 --- src/octoprint/server/__init__.py | 1 + src/octoprint/server/util/sockjs.py | 11 ++--------- src/octoprint/server/util/tornado.py | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 6e9edcd88..296c166e7 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -223,6 +223,7 @@ class Server(object): # monkey patch a bunch of stuff util.tornado.fix_ioloop_scheduling() + util.tornado.fix_json_encode() util.flask.enable_additional_translations(additional_folders=[self._settings.getBaseFolder("translations")]) # setup app diff --git a/src/octoprint/server/util/sockjs.py b/src/octoprint/server/util/sockjs.py index 07783ffd3..84ff1f9fd 100644 --- a/src/octoprint/server/util/sockjs.py +++ b/src/octoprint/server/util/sockjs.py @@ -57,15 +57,7 @@ class ThreadSafeSession(sockjs.tornado.session.Session): class JsonEncodingSessionWrapper(wrapt.ObjectProxy): - def send_message(self, msg, stats=True, binary=False): - """Send or queue outgoing message - - `msg` - Message to send - `stats` - If set to True, will update statistics after operation completes - """ self.send_jsonified(json.dumps(sockjs.tornado.util.bytes_to_str(msg), separators=(',', ':'), default=JsonEncoding.encode), @@ -74,7 +66,8 @@ class JsonEncodingSessionWrapper(wrapt.ObjectProxy): class PrinterStateConnection(sockjs.tornado.SockJSConnection, octoprint.printer.PrinterCallback): def __init__(self, printer, fileManager, analysisQueue, userManager, eventManager, pluginManager, session): - session = JsonEncodingSessionWrapper(session) + if isinstance(session, sockjs.tornado.session.Session): + session = JsonEncodingSessionWrapper(session) sockjs.tornado.SockJSConnection.__init__(self, session) diff --git a/src/octoprint/server/util/tornado.py b/src/octoprint/server/util/tornado.py index f63a1c5f6..e8de7890b 100644 --- a/src/octoprint/server/util/tornado.py +++ b/src/octoprint/server/util/tornado.py @@ -59,6 +59,22 @@ def fix_ioloop_scheduling(): tornado.ioloop.PeriodicCallback._schedule_next = _schedule_next +def fix_json_encode(): + """ + This makes tornado.escape.json_encode use octoprint.util.JsonEncoding.encode as fallback in order to allow + serialization of globally registered types like frozendict and others. + """ + + from octoprint.util.json import JsonEncoding + import json + + def fixed_json_encode(value): + return json.dumps(value, default=JsonEncoding.encode).replace("