##// END OF EJS Templates
don't use parent=self in handlers...
MinRK -
Show More
@@ -1,114 +1,114 b''
1 """Tornado handlers for WebSocket <-> ZMQ sockets.
1 """Tornado handlers for WebSocket <-> ZMQ sockets.
2
2
3 Authors:
3 Authors:
4
4
5 * Brian Granger
5 * Brian Granger
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2008-2011 The IPython Development Team
9 # Copyright (C) 2008-2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 import Cookie
19 import Cookie
20 import logging
20 import logging
21 from tornado import web
21 from tornado import web
22 from tornado import websocket
22 from tornado import websocket
23
23
24 from zmq.utils import jsonapi
24 from zmq.utils import jsonapi
25
25
26 from IPython.kernel.zmq.session import Session
26 from IPython.kernel.zmq.session import Session
27 from IPython.utils.jsonutil import date_default
27 from IPython.utils.jsonutil import date_default
28 from IPython.utils.py3compat import PY3
28 from IPython.utils.py3compat import PY3
29
29
30 from .handlers import IPythonHandler
30 from .handlers import IPythonHandler
31
31
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33 # ZMQ handlers
33 # ZMQ handlers
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35
35
36 class ZMQStreamHandler(websocket.WebSocketHandler):
36 class ZMQStreamHandler(websocket.WebSocketHandler):
37
37
38 def clear_cookie(self, *args, **kwargs):
38 def clear_cookie(self, *args, **kwargs):
39 """meaningless for websockets"""
39 """meaningless for websockets"""
40 pass
40 pass
41
41
42 def _reserialize_reply(self, msg_list):
42 def _reserialize_reply(self, msg_list):
43 """Reserialize a reply message using JSON.
43 """Reserialize a reply message using JSON.
44
44
45 This takes the msg list from the ZMQ socket, unserializes it using
45 This takes the msg list from the ZMQ socket, unserializes it using
46 self.session and then serializes the result using JSON. This method
46 self.session and then serializes the result using JSON. This method
47 should be used by self._on_zmq_reply to build messages that can
47 should be used by self._on_zmq_reply to build messages that can
48 be sent back to the browser.
48 be sent back to the browser.
49 """
49 """
50 idents, msg_list = self.session.feed_identities(msg_list)
50 idents, msg_list = self.session.feed_identities(msg_list)
51 msg = self.session.unserialize(msg_list)
51 msg = self.session.unserialize(msg_list)
52 try:
52 try:
53 msg['header'].pop('date')
53 msg['header'].pop('date')
54 except KeyError:
54 except KeyError:
55 pass
55 pass
56 try:
56 try:
57 msg['parent_header'].pop('date')
57 msg['parent_header'].pop('date')
58 except KeyError:
58 except KeyError:
59 pass
59 pass
60 msg.pop('buffers')
60 msg.pop('buffers')
61 return jsonapi.dumps(msg, default=date_default)
61 return jsonapi.dumps(msg, default=date_default)
62
62
63 def _on_zmq_reply(self, msg_list):
63 def _on_zmq_reply(self, msg_list):
64 # Sometimes this gets triggered when the on_close method is scheduled in the
64 # Sometimes this gets triggered when the on_close method is scheduled in the
65 # eventloop but hasn't been called.
65 # eventloop but hasn't been called.
66 if self.stream.closed(): return
66 if self.stream.closed(): return
67 try:
67 try:
68 msg = self._reserialize_reply(msg_list)
68 msg = self._reserialize_reply(msg_list)
69 except Exception:
69 except Exception:
70 self.log.critical("Malformed message: %r" % msg_list, exc_info=True)
70 self.log.critical("Malformed message: %r" % msg_list, exc_info=True)
71 else:
71 else:
72 self.write_message(msg)
72 self.write_message(msg)
73
73
74 def allow_draft76(self):
74 def allow_draft76(self):
75 """Allow draft 76, until browsers such as Safari update to RFC 6455.
75 """Allow draft 76, until browsers such as Safari update to RFC 6455.
76
76
77 This has been disabled by default in tornado in release 2.2.0, and
77 This has been disabled by default in tornado in release 2.2.0, and
78 support will be removed in later versions.
78 support will be removed in later versions.
79 """
79 """
80 return True
80 return True
81
81
82
82
83 class AuthenticatedZMQStreamHandler(ZMQStreamHandler, IPythonHandler):
83 class AuthenticatedZMQStreamHandler(ZMQStreamHandler, IPythonHandler):
84
84
85 def open(self, kernel_id):
85 def open(self, kernel_id):
86 self.kernel_id = kernel_id.decode('ascii')
86 self.kernel_id = kernel_id.decode('ascii')
87 self.session = Session(parent=self)
87 self.session = Session(config=self.config)
88 self.save_on_message = self.on_message
88 self.save_on_message = self.on_message
89 self.on_message = self.on_first_message
89 self.on_message = self.on_first_message
90
90
91 def _inject_cookie_message(self, msg):
91 def _inject_cookie_message(self, msg):
92 """Inject the first message, which is the document cookie,
92 """Inject the first message, which is the document cookie,
93 for authentication."""
93 for authentication."""
94 if not PY3 and isinstance(msg, unicode):
94 if not PY3 and isinstance(msg, unicode):
95 # Cookie constructor doesn't accept unicode strings
95 # Cookie constructor doesn't accept unicode strings
96 # under Python 2.x for some reason
96 # under Python 2.x for some reason
97 msg = msg.encode('utf8', 'replace')
97 msg = msg.encode('utf8', 'replace')
98 try:
98 try:
99 identity, msg = msg.split(':', 1)
99 identity, msg = msg.split(':', 1)
100 self.session.session = identity.decode('ascii')
100 self.session.session = identity.decode('ascii')
101 except Exception:
101 except Exception:
102 logging.error("First ws message didn't have the form 'identity:[cookie]' - %r", msg)
102 logging.error("First ws message didn't have the form 'identity:[cookie]' - %r", msg)
103
103
104 try:
104 try:
105 self.request._cookies = Cookie.SimpleCookie(msg)
105 self.request._cookies = Cookie.SimpleCookie(msg)
106 except:
106 except:
107 self.log.warn("couldn't parse cookie string: %s",msg, exc_info=True)
107 self.log.warn("couldn't parse cookie string: %s",msg, exc_info=True)
108
108
109 def on_first_message(self, msg):
109 def on_first_message(self, msg):
110 self._inject_cookie_message(msg)
110 self._inject_cookie_message(msg)
111 if self.get_current_user() is None:
111 if self.get_current_user() is None:
112 self.log.warn("Couldn't authenticate WebSocket connection")
112 self.log.warn("Couldn't authenticate WebSocket connection")
113 raise web.HTTPError(403)
113 raise web.HTTPError(403)
114 self.on_message = self.save_on_message No newline at end of file
114 self.on_message = self.save_on_message
General Comments 0
You need to be logged in to leave comments. Login now