From 0c84b0dcf67fd07ef89c0254b254bb9b3dc0e5c3 2015-02-13 00:20:50 From: Min RK Date: 2015-02-13 00:20:50 Subject: [PATCH] backport WebSocket.send_error from tornado 4.1 raising an exception in a websocket results in "Method not supported" on tornado 4.0 --- diff --git a/IPython/html/base/zmqhandlers.py b/IPython/html/base/zmqhandlers.py index 8d7a2dc..4ddcb5e 100644 --- a/IPython/html/base/zmqhandlers.py +++ b/IPython/html/base/zmqhandlers.py @@ -94,6 +94,19 @@ if os.environ.get('IPYTHON_ALLOW_DRAFT_WEBSOCKETS_FOR_PHANTOMJS', False): class ZMQStreamHandler(WebSocketHandler): + if tornado.version_info < (4,1): + """Backport send_error from tornado 4.1 to 4.0""" + def send_error(self, *args, **kwargs): + if self.stream is None: + super(WebSocketHandler, self).send_error(*args, **kwargs) + else: + # If we get an uncaught exception during the handshake, + # we have no choice but to abruptly close the connection. + # TODO: for uncaught exceptions after the handshake, + # we can close the connection more gracefully. + self.stream.close() + + def check_origin(self, origin): """Check Origin == Host or Access-Control-Allow-Origin.