diff --git a/src/octoprint/vendor/sockjs/tornado/session.py b/src/octoprint/vendor/sockjs/tornado/session.py index 7af5d586c..3d438c7b2 100644 --- a/src/octoprint/vendor/sockjs/tornado/session.py +++ b/src/octoprint/vendor/sockjs/tornado/session.py @@ -9,12 +9,25 @@ from __future__ import absolute_import, division, print_function, unicode_litera """ import logging +import functools from octoprint.vendor.sockjs.tornado import sessioncontainer, periodic, proto from octoprint.vendor.sockjs.tornado.util import bytes_to_str +from tornado.ioloop import IOLoop + LOG = logging.getLogger("tornado.general") +def ensure_io_loop(method): + @functools.wraps(method) + def wrapper(self, *args, **kwargs): + if IOLoop.current(False): + method(self, *args, **kwargs) + else: + def run(): + method(self, *args, **kwargs) + self.server.io_loop.add_callback(run) + return wrapper class ConnectionInfo(object): """Connection information object. @@ -293,6 +306,7 @@ class Session(BaseSession, sessioncontainer.SessionMixin): return True + @ensure_io_loop def verify_state(self): """Verify if session was not yet opened. If it is, open it and call connections `on_open`""" # If we're in CONNECTING state - send 'o' message to the client @@ -323,6 +337,7 @@ class Session(BaseSession, sessioncontainer.SessionMixin): """ self.send_jsonified(proto.json_encode(bytes_to_str(msg)), stats) + @ensure_io_loop def send_jsonified(self, msg, stats=True): """Send JSON-encoded message @@ -355,6 +370,7 @@ class Session(BaseSession, sessioncontainer.SessionMixin): if stats: self.stats.on_pack_sent(1) + @ensure_io_loop def flush(self): """Flush message queue if there's an active connection running""" self._pending_flush = False @@ -365,6 +381,7 @@ class Session(BaseSession, sessioncontainer.SessionMixin): self.handler.send_pack('a[%s]' % self.send_queue) self.send_queue = '' + @ensure_io_loop def close(self, code=3000, message='Go away!'): """Close session. @@ -401,6 +418,7 @@ class Session(BaseSession, sessioncontainer.SessionMixin): if self._heartbeat_timer is not None: self._heartbeat_timer.delay() + @ensure_io_loop def _heartbeat(self): """Heartbeat callback""" if self.handler is not None: diff --git a/src/octoprint/vendor/sockjs/tornado/transports/eventsource.py b/src/octoprint/vendor/sockjs/tornado/transports/eventsource.py index edaf29c6b..5fe312a64 100644 --- a/src/octoprint/vendor/sockjs/tornado/transports/eventsource.py +++ b/src/octoprint/vendor/sockjs/tornado/transports/eventsource.py @@ -8,15 +8,14 @@ from __future__ import absolute_import, division, print_function, unicode_litera EventSource transport implementation. """ -from tornado.gen import coroutine - from octoprint.vendor.sockjs.tornado.transports import streamingbase +from octoprint.vendor.sockjs.tornado.util import no_auto_finish class EventSourceTransport(streamingbase.StreamingTransportBase): name = 'eventsource' - @coroutine + @no_auto_finish def get(self, session_id): # Start response self.preflight() @@ -46,7 +45,7 @@ class EventSourceTransport(streamingbase.StreamingTransportBase): self.notify_sent(len(msg)) self.write(msg) - self.flush(callback=self.send_complete) + self.flush().add_done_callback(self.send_complete) except IOError: # If connection dropped, make sure we close offending session instead # of propagating error all way up. diff --git a/src/octoprint/vendor/sockjs/tornado/transports/htmlfile.py b/src/octoprint/vendor/sockjs/tornado/transports/htmlfile.py index 91a64b6a1..c57c03e74 100644 --- a/src/octoprint/vendor/sockjs/tornado/transports/htmlfile.py +++ b/src/octoprint/vendor/sockjs/tornado/transports/htmlfile.py @@ -8,10 +8,20 @@ from __future__ import absolute_import, division, print_function, unicode_litera HtmlFile transport implementation. """ -from tornado.gen import coroutine +import re from octoprint.vendor.sockjs.tornado import proto from octoprint.vendor.sockjs.tornado.transports import streamingbase +from octoprint.vendor.sockjs.tornado.util import no_auto_finish + +try: + # noinspection PyCompatibility + from html import escape +except: + # noinspection PyDeprecation + from cgi import escape + +RE = re.compile(r'[\W_]+') # HTMLFILE template HTMLFILE_HEAD = r''' @@ -38,7 +48,7 @@ class HtmlFileTransport(streamingbase.StreamingTransportBase): def initialize(self, server): super(HtmlFileTransport, self).initialize(server) - @coroutine + @no_auto_finish def get(self, session_id): # Start response self.preflight() @@ -55,7 +65,7 @@ class HtmlFileTransport(streamingbase.StreamingTransportBase): return # TODO: Fix me - use parameter - self.write(HTMLFILE_HEAD % callback) + self.write(HTMLFILE_HEAD % escape(RE.sub('', callback))) self.flush() # Now try to attach to session @@ -80,7 +90,7 @@ class HtmlFileTransport(streamingbase.StreamingTransportBase): self.notify_sent(len(message)) self.write(msg) - self.flush(callback=self.send_complete) + self.flush().add_done_callback(self.send_complete) except IOError: # If connection dropped, make sure we close offending session instead # of propagating error all way up. diff --git a/src/octoprint/vendor/sockjs/tornado/transports/jsonp.py b/src/octoprint/vendor/sockjs/tornado/transports/jsonp.py index 78828cdd8..1e5820711 100644 --- a/src/octoprint/vendor/sockjs/tornado/transports/jsonp.py +++ b/src/octoprint/vendor/sockjs/tornado/transports/jsonp.py @@ -9,18 +9,17 @@ from __future__ import absolute_import, division, print_function, unicode_litera """ import logging -from tornado.gen import coroutine - from octoprint.vendor.sockjs.tornado import proto from octoprint.vendor.sockjs.tornado.transports import pollingbase from octoprint.vendor.sockjs.tornado.util import bytes_to_str, unquote_plus +from octoprint.vendor.sockjs.tornado.util import no_auto_finish LOG = logging.getLogger("tornado.general") class JSONPTransport(pollingbase.PollingTransportBase): name = 'jsonp' - @coroutine + @no_auto_finish def get(self, session_id): # Start response self.handle_session_cookie() @@ -64,7 +63,7 @@ class JSONPTransport(pollingbase.PollingTransportBase): self.set_header('Etag', 'dummy') self.write(msg) - self.flush(callback=self.send_complete) + self.flush().add_done_callback(self.send_complete) except IOError: # If connection dropped, make sure we close offending session instead # of propagating error all way up. diff --git a/src/octoprint/vendor/sockjs/tornado/transports/pollingbase.py b/src/octoprint/vendor/sockjs/tornado/transports/pollingbase.py index 889984066..3c64818d5 100644 --- a/src/octoprint/vendor/sockjs/tornado/transports/pollingbase.py +++ b/src/octoprint/vendor/sockjs/tornado/transports/pollingbase.py @@ -66,7 +66,7 @@ class PollingTransportBase(basehandler.PreflightHandler, base.BaseTransportMixin super(PollingTransportBase, self).on_connection_close() - def send_complete(self): + def send_complete(self, f=None): self._detach() # Avoid race condition when waiting for write callback and session getting closed in between diff --git a/src/octoprint/vendor/sockjs/tornado/transports/rawwebsocket.py b/src/octoprint/vendor/sockjs/tornado/transports/rawwebsocket.py index 48db5552d..dfe0ab632 100644 --- a/src/octoprint/vendor/sockjs/tornado/transports/rawwebsocket.py +++ b/src/octoprint/vendor/sockjs/tornado/transports/rawwebsocket.py @@ -42,7 +42,7 @@ class RawWebSocketTransport(websocket.SockJSWebSocketHandler, base.BaseTransport # Disable nagle if needed if self.server.settings['disable_nagle']: - self.stream.socket.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) + self.set_nodelay(True) # Create and attach to session self.session = RawSession(self.server.get_connection_class(), self.server) diff --git a/src/octoprint/vendor/sockjs/tornado/transports/streamingbase.py b/src/octoprint/vendor/sockjs/tornado/transports/streamingbase.py index 3e688134a..f07980515 100644 --- a/src/octoprint/vendor/sockjs/tornado/transports/streamingbase.py +++ b/src/octoprint/vendor/sockjs/tornado/transports/streamingbase.py @@ -33,7 +33,7 @@ class StreamingTransportBase(pollingbase.PollingTransportBase): return False - def send_complete(self): + def send_complete(self, f=None): """ Verify if connection should be closed based on amount of data that was sent. """ diff --git a/src/octoprint/vendor/sockjs/tornado/transports/websocket.py b/src/octoprint/vendor/sockjs/tornado/transports/websocket.py index 936468182..ce66a1bfb 100644 --- a/src/octoprint/vendor/sockjs/tornado/transports/websocket.py +++ b/src/octoprint/vendor/sockjs/tornado/transports/websocket.py @@ -33,7 +33,10 @@ class WebSocketTransport(websocket.SockJSWebSocketHandler, base.BaseTransportMix # Disable nagle if self.server.settings['disable_nagle']: - self.stream.set_nodelay(True) + if hasattr(self, 'ws_connection'): + self.ws_connection.stream.set_nodelay(True) + else: + self.stream.set_nodelay(True) # Handle session self.session = self.server.create_session(session_id, register=False) diff --git a/src/octoprint/vendor/sockjs/tornado/transports/xhr.py b/src/octoprint/vendor/sockjs/tornado/transports/xhr.py index 8b65ca013..3131f6918 100644 --- a/src/octoprint/vendor/sockjs/tornado/transports/xhr.py +++ b/src/octoprint/vendor/sockjs/tornado/transports/xhr.py @@ -9,11 +9,9 @@ from __future__ import absolute_import, division, print_function, unicode_litera """ import logging -from tornado.gen import coroutine - from octoprint.vendor.sockjs.tornado import proto from octoprint.vendor.sockjs.tornado.transports import pollingbase -from octoprint.vendor.sockjs.tornado.util import bytes_to_str +from octoprint.vendor.sockjs.tornado.util import bytes_to_str, no_auto_finish LOG = logging.getLogger("tornado.general") @@ -21,7 +19,7 @@ class XhrPollingTransport(pollingbase.PollingTransportBase): """xhr-polling transport implementation""" name = 'xhr' - @coroutine + @no_auto_finish def post(self, session_id): # Start response self.preflight() @@ -51,7 +49,7 @@ class XhrPollingTransport(pollingbase.PollingTransportBase): self.set_header('Content-Type', 'application/javascript; charset=UTF-8') self.set_header('Content-Length', len(message) + 1) self.write(message + '\n') - self.flush(callback=self.send_complete) + self.flush().add_done_callback(self.send_complete) except IOError: # If connection dropped, make sure we close offending session instead # of propagating error all way up. diff --git a/src/octoprint/vendor/sockjs/tornado/transports/xhrstreaming.py b/src/octoprint/vendor/sockjs/tornado/transports/xhrstreaming.py index 6b5adfc41..305e0970f 100644 --- a/src/octoprint/vendor/sockjs/tornado/transports/xhrstreaming.py +++ b/src/octoprint/vendor/sockjs/tornado/transports/xhrstreaming.py @@ -8,15 +8,14 @@ from __future__ import absolute_import, division, print_function, unicode_litera Xhr-Streaming transport implementation """ -from tornado.gen import coroutine - from octoprint.vendor.sockjs.tornado.transports import streamingbase +from octoprint.vendor.sockjs.tornado.util import no_auto_finish class XhrStreamingTransport(streamingbase.StreamingTransportBase): name = 'xhr_streaming' - @coroutine + @no_auto_finish def post(self, session_id): # Handle cookie self.preflight() @@ -45,7 +44,7 @@ class XhrStreamingTransport(streamingbase.StreamingTransportBase): self.notify_sent(len(message)) self.write(message + '\n') - self.flush(callback=self.send_complete) + self.flush().add_done_callback(self.send_complete) except IOError: # If connection dropped, make sure we close offending session instead # of propagating error all way up. diff --git a/src/octoprint/vendor/sockjs/tornado/util.py b/src/octoprint/vendor/sockjs/tornado/util.py index 6ff64d60f..28b3469f1 100644 --- a/src/octoprint/vendor/sockjs/tornado/util.py +++ b/src/octoprint/vendor/sockjs/tornado/util.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, division, print_function, unicode_literals import sys +import functools PY3 = sys.version_info[0] == 3 @@ -53,3 +54,10 @@ else: import urllib unquote_plus = urllib.unquote_plus + +def no_auto_finish(method): + @functools.wraps(method) + def wrapper(self, *args, **kwargs): + self._auto_finish = False + return method(self, *args, **kwargs) + return wrapper