Show More
@@ -1,145 +1,145 | |||
|
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 | try: |
|
20 | 20 | from urllib.parse import urlparse # Py 3 |
|
21 | 21 | except ImportError: |
|
22 | 22 | from urlparse import urlparse # Py 2 |
|
23 | 23 | |
|
24 | 24 | try: |
|
25 | 25 | from http.cookies import SimpleCookie # Py 3 |
|
26 | 26 | except ImportError: |
|
27 | 27 | from Cookie import SimpleCookie # Py 2 |
|
28 | 28 | import logging |
|
29 | 29 | from tornado import web |
|
30 | 30 | from tornado import websocket |
|
31 | 31 | |
|
32 | 32 | from zmq.utils import jsonapi |
|
33 | 33 | |
|
34 | 34 | from IPython.kernel.zmq.session import Session |
|
35 | 35 | from IPython.utils.jsonutil import date_default |
|
36 | 36 | from IPython.utils.py3compat import PY3, cast_unicode |
|
37 | 37 | |
|
38 | 38 | from .handlers import IPythonHandler |
|
39 | 39 | |
|
40 | 40 | #----------------------------------------------------------------------------- |
|
41 | 41 | # ZMQ handlers |
|
42 | 42 | #----------------------------------------------------------------------------- |
|
43 | 43 | |
|
44 | 44 | class ZMQStreamHandler(websocket.WebSocketHandler): |
|
45 | 45 | |
|
46 | 46 | def check_origin(self): |
|
47 | 47 | """Check origin from headers.""" |
|
48 | 48 | origin_header = self.request.headers["Origin"] |
|
49 | 49 | host = self.request.headers["Host"] |
|
50 | 50 | |
|
51 | 51 | parsed_origin = urlparse(origin_header) |
|
52 | 52 | origin = parsed_origin.netloc |
|
53 | 53 | |
|
54 | 54 | # Check to see that origin matches host directly, including ports |
|
55 | 55 | if origin != host: |
|
56 |
self.log. |
|
|
56 | self.log.warn("Cross Origin WebSocket Attempt.") | |
|
57 | 57 | raise web.HTTPError(404) |
|
58 | 58 | |
|
59 | 59 | |
|
60 |
def _execute(self |
|
|
60 | def _execute(self, *args, **kwargs): | |
|
61 | 61 | """Wrap all calls to make sure origin gets checked.""" |
|
62 | 62 | |
|
63 | 63 | # Check to see that origin matches host directly, including ports |
|
64 | 64 | self.check_origin() |
|
65 | 65 | |
|
66 | 66 | # Pass on the rest of the handling by the WebSocketHandler |
|
67 |
super(ZMQStreamHandler, self)._execute( |
|
|
67 | super(ZMQStreamHandler, self)._execute(*args, **kwargs) | |
|
68 | 68 | |
|
69 | 69 | def clear_cookie(self, *args, **kwargs): |
|
70 | 70 | """meaningless for websockets""" |
|
71 | 71 | pass |
|
72 | 72 | |
|
73 | 73 | def _reserialize_reply(self, msg_list): |
|
74 | 74 | """Reserialize a reply message using JSON. |
|
75 | 75 | |
|
76 | 76 | This takes the msg list from the ZMQ socket, unserializes it using |
|
77 | 77 | self.session and then serializes the result using JSON. This method |
|
78 | 78 | should be used by self._on_zmq_reply to build messages that can |
|
79 | 79 | be sent back to the browser. |
|
80 | 80 | """ |
|
81 | 81 | idents, msg_list = self.session.feed_identities(msg_list) |
|
82 | 82 | msg = self.session.unserialize(msg_list) |
|
83 | 83 | try: |
|
84 | 84 | msg['header'].pop('date') |
|
85 | 85 | except KeyError: |
|
86 | 86 | pass |
|
87 | 87 | try: |
|
88 | 88 | msg['parent_header'].pop('date') |
|
89 | 89 | except KeyError: |
|
90 | 90 | pass |
|
91 | 91 | msg.pop('buffers') |
|
92 | 92 | return jsonapi.dumps(msg, default=date_default) |
|
93 | 93 | |
|
94 | 94 | def _on_zmq_reply(self, msg_list): |
|
95 | 95 | # Sometimes this gets triggered when the on_close method is scheduled in the |
|
96 | 96 | # eventloop but hasn't been called. |
|
97 | 97 | if self.stream.closed(): return |
|
98 | 98 | try: |
|
99 | 99 | msg = self._reserialize_reply(msg_list) |
|
100 | 100 | except Exception: |
|
101 | 101 | self.log.critical("Malformed message: %r" % msg_list, exc_info=True) |
|
102 | 102 | else: |
|
103 | 103 | self.write_message(msg) |
|
104 | 104 | |
|
105 | 105 | def allow_draft76(self): |
|
106 | 106 | """Allow draft 76, until browsers such as Safari update to RFC 6455. |
|
107 | 107 | |
|
108 | 108 | This has been disabled by default in tornado in release 2.2.0, and |
|
109 | 109 | support will be removed in later versions. |
|
110 | 110 | """ |
|
111 | 111 | return True |
|
112 | 112 | |
|
113 | 113 | |
|
114 | 114 | class AuthenticatedZMQStreamHandler(ZMQStreamHandler, IPythonHandler): |
|
115 | 115 | |
|
116 | 116 | def open(self, kernel_id): |
|
117 | 117 | self.kernel_id = cast_unicode(kernel_id, 'ascii') |
|
118 | 118 | self.session = Session(config=self.config) |
|
119 | 119 | self.save_on_message = self.on_message |
|
120 | 120 | self.on_message = self.on_first_message |
|
121 | 121 | |
|
122 | 122 | def _inject_cookie_message(self, msg): |
|
123 | 123 | """Inject the first message, which is the document cookie, |
|
124 | 124 | for authentication.""" |
|
125 | 125 | if not PY3 and isinstance(msg, unicode): |
|
126 | 126 | # Cookie constructor doesn't accept unicode strings |
|
127 | 127 | # under Python 2.x for some reason |
|
128 | 128 | msg = msg.encode('utf8', 'replace') |
|
129 | 129 | try: |
|
130 | 130 | identity, msg = msg.split(':', 1) |
|
131 | 131 | self.session.session = cast_unicode(identity, 'ascii') |
|
132 | 132 | except Exception: |
|
133 | 133 | logging.error("First ws message didn't have the form 'identity:[cookie]' - %r", msg) |
|
134 | 134 | |
|
135 | 135 | try: |
|
136 | 136 | self.request._cookies = SimpleCookie(msg) |
|
137 | 137 | except: |
|
138 | 138 | self.log.warn("couldn't parse cookie string: %s",msg, exc_info=True) |
|
139 | 139 | |
|
140 | 140 | def on_first_message(self, msg): |
|
141 | 141 | self._inject_cookie_message(msg) |
|
142 | 142 | if self.get_current_user() is None: |
|
143 | 143 | self.log.warn("Couldn't authenticate WebSocket connection") |
|
144 | 144 | raise web.HTTPError(403) |
|
145 | 145 | 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