def on_open(self, info: ConnectionInfo) -> None: log_data = dict(extra='[transport=%s]' % (self.session.transport_name,)) record_request_start_data(log_data) ioloop = tornado.ioloop.IOLoop.instance() self.authenticated = False self.session.user_profile = None self.close_info = None # type: Optional[CloseErrorInfo] self.did_close = False try: self.browser_session_id = info.get_cookie(settings.SESSION_COOKIE_NAME).value self.csrf_token = info.get_cookie(settings.CSRF_COOKIE_NAME).value except AttributeError: # The request didn't contain the necessary cookie values. We can't # close immediately because sockjs-tornado doesn't expect a close # inside on_open(), so do it on the next tick. self.close_info = CloseErrorInfo(403, "Initial cookie lacked required values") ioloop.add_callback(self.close) return def auth_timeout() -> None: self.close_info = CloseErrorInfo(408, "Timeout while waiting for authentication") self.close() self.timeout_handle = ioloop.call_later(10, auth_timeout) write_log_line(log_data, path='/socket/open', method='SOCKET', remote_ip=info.ip, email='unknown', client_name='?')
def test_same_origin(self): settings.DRAGON_URL = self.host settings.SWAMP_DRAGON_SAME_ORIGIN = True response = self.fetch('/settings.js') cookie = SimpleCookie(response.headers['Set-Cookie']) request = ConnectionInfo('127.0.0.1', cookies=cookie, arguments={}, headers={}, path='/data/983/9cz4ridg/websocket') session = TestSession() self.connection = SubscriberConnection(session) self.connection.on_open(request) self.assertFalse(self.connection.is_closed)
def test_on_close(self): """ Closing a connection should automatically unsubscribe from all channels """ request = ConnectionInfo('127.0.0.1', cookies={}, arguments={}, headers={}, path='/data/983/9cz4ridg/websocket') self.connection.on_open(request) self.connection.pub_sub.subscribe(['test-channel'], self.connection) self.assertIn( self.connection, self.connection.pub_sub.publisher.subscribers['test-channel']) self.connection.on_close() self.assertNotIn('test-channel', self.connection.pub_sub.publisher.subscribers)