remove superfluous ws-hostname parameter from notebook

This made the notebook server artificially and unnecessarily brittle to tunneling, and non-local hostname resolution.  The ws_url is now defined based on the Origin of the request.  This allows tunneled hosts and ports to preserve connections, as the ws_url always matches the one in use by the client.

Implemented as a property, to facilitate future cases where it might behave differently.
This commit is contained in:
MinRK 2011-10-30 22:50:46 -07:00
parent 425de1bd73
commit 00c797e2b5
2 changed files with 11 additions and 21 deletions

View File

@ -139,7 +139,15 @@ class AuthenticatedHandler(web.RequestHandler):
return True
else:
return False
@property
def ws_url(self):
"""websocket url matching the current request
turns http[s]://host[:port]/foo/bar into
ws[s]://host[:port]/foo/bar
"""
return self.request.headers.get('Origin').replace('http', 'ws', 1)
class ProjectDashboardHandler(AuthenticatedHandler):
@ -221,8 +229,7 @@ class MainKernelHandler(AuthenticatedHandler):
km = self.application.kernel_manager
notebook_id = self.get_argument('notebook', default=None)
kernel_id = km.start_kernel(notebook_id)
ws_url = self.application.ipython_app.get_ws_url()
data = {'ws_url':ws_url,'kernel_id':kernel_id}
data = {'ws_url':self.ws_url,'kernel_id':kernel_id}
self.set_header('Location', '/'+kernel_id)
self.finish(jsonapi.dumps(data))
@ -249,8 +256,7 @@ class KernelActionHandler(AuthenticatedHandler):
self.set_status(204)
if action == 'restart':
new_kernel_id = km.restart_kernel(kernel_id)
ws_url = self.application.ipython_app.get_ws_url()
data = {'ws_url':ws_url,'kernel_id':new_kernel_id}
data = {'ws_url':self.ws_url,'kernel_id':new_kernel_id}
self.set_header('Location', '/'+new_kernel_id)
self.write(jsonapi.dumps(data))
self.finish()

View File

@ -147,7 +147,6 @@ aliases.update({
'port': 'NotebookApp.port',
'keyfile': 'NotebookApp.keyfile',
'certfile': 'NotebookApp.certfile',
'ws-hostname': 'NotebookApp.ws_hostname',
'notebook-dir': 'NotebookManager.notebook_dir',
})
@ -155,7 +154,7 @@ aliases.update({
# multi-kernel evironment:
aliases.pop('f', None)
notebook_aliases = [u'port', u'ip', u'keyfile', u'certfile', u'ws-hostname',
notebook_aliases = [u'port', u'ip', u'keyfile', u'certfile',
u'notebook-dir']
#-----------------------------------------------------------------------------
@ -200,13 +199,6 @@ class NotebookApp(BaseIPythonApplication):
help="The port the notebook server will listen on."
)
ws_hostname = Unicode(LOCALHOST, config=True,
help="""The FQDN or IP for WebSocket connections. The default will work
fine when the server is listening on localhost, but this needs to
be set if the ip option is used. It will be used as the hostname part
of the WebSocket url: ws://hostname/path."""
)
certfile = Unicode(u'', config=True,
help="""The full path to an SSL/TLS certificate file."""
)
@ -226,14 +218,6 @@ class NotebookApp(BaseIPythonApplication):
help="Whether to prevent editing/execution of notebooks."
)
def get_ws_url(self):
"""Return the WebSocket URL for this server."""
if self.certfile:
prefix = u'wss://'
else:
prefix = u'ws://'
return prefix + self.ws_hostname + u':' + unicode(self.port)
def parse_command_line(self, argv=None):
super(NotebookApp, self).parse_command_line(argv)
if argv is None: