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