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
This commit is contained in:
Gina Häußge committed 2018-04-03 16:45:14 +02:00
1 parent fcecb5c0f0
commit c1fe8f5a35
3 files changed
+19 -9

No files matched your search

+1
View File
@@ -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
+2 -9
View File
@@ -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)
+16
View File
@@ -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("</", "<\\/")
import tornado.escape
tornado.escape.json_encode = fixed_json_encode
#~~ More sensible logging