Websocket Adjustments #955

Merged
merged 4 commits into from Nov 11, 2011

Projects

None yet

3 participants

@minrk
Member
minrk commented Oct 31, 2011

Two principal changes:

  1. alert client on failed and lost web socket connections

A long message is given if the connection fails within 1s, which assumes the connection did not succeed. Otherwise, it is a short 'connection closed unexpectedly'.

This also means that clients are notified on server termination (for better or worse).

  1. remove superfluous ws-hostname parameter from notebook

This made the notebook server artificially and unnecessarily brittle against tunneling and explicit hostname resolution. Now, the ws_url is defined based on the Origin of the request for the url, so it always matches the http[s] url. This means that it will follow the same tunnel, and the hostname will be already resolved. Resolving the hostname twice makes no sense at all unless the websockets are going to a different server than the http requests.

Implemented as a property, so it should still be easy to change for future cases where it might behave differently (e.g. websockets on a different host, or at a non-root url).

This is one approach to closing #952, but there might be better ways to do it (e.g. dialogs that are not base alert calls, which block window interaction).

minrk added some commits Oct 31, 2011
@minrk minrk 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.
f00c904
@minrk minrk alert client on failed and lost web socket connections
A long message is given if the connection fails within 1s.  Otherwise, it is a short 'connection closed unexpectedly'.

This also means that clients are notified on server termination.
81cfbe8
@ellisonbg
Member

@minrk: removing ws-hostname is fine. Originally, I thought we might need to run the ws stuff in a different process, but that didn't pan out.

@minrk
Member
minrk commented Nov 1, 2011

I changed the error message to be a jQuery dialog rather than a plain alert, so it's less obstructive.

@minrk
Member
minrk commented Nov 1, 2011

@ellisonbg - that makes sense. It should still be easy to change, in environments where the websocket host and/or port differ from the rest.

@fperez
Member
fperez commented Nov 11, 2011

Mh, I tried running a notebook with this branch on and I got this traceback:

ERROR:root:Uncaught exception POST /kernels?notebook=69f0f1e7-5cbf-4d0c-b7fe-7807e1023886 (128.32.52.133)
HTTPRequest(protocol='https', host='longs.berkeley.edu:9999', method='POST', uri='/kernels?notebook=69f0f1e7-5cbf-4d0c-b7fe-7807e1023886', version='HTTP/1.1', remote_ip='128.32.52.133', body='', headers={'Referer': 'https://longs.berkeley.edu:9999/69f0f1e7-5cbf-4d0c-b7fe-7807e1023886', 'Content-Length': '0', 'Accept-Language': 'en-us,en;q=0.5', 'Accept-Encoding': 'gzip, deflate', 'Host': 'longs.berkeley.edu:9999', 'Accept': 'application/json, text/javascript, */*; q=0.01', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1', 'Accept-Charset': 'UTF-8,*', 'Connection': 'keep-alive', 'X-Requested-With': 'XMLHttpRequest', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'Cookie': '__utma=245566491.1143703340.1319585252.1319585252.1320105682.2; __utmz=245566491.1319585252.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); username=YWUyZGI4NjctNDQzMS00ZGIxLWI1MDItNjkwNDk2MzJmNGY1|1320968643|33efb552dd2899f965dbcf4deb1d81d612e3a06f'})
Traceback (most recent call last):
  File "/home/fperez/usr/local/lib/python2.6/site-packages/tornado-2.1-py2.6.egg/tornado/web.py", line 954, in _execute
    getattr(self, self.request.method.lower())(*args, **kwargs)
  File "/home/fperez/usr/local/lib/python2.6/site-packages/tornado-2.1-py2.6.egg/tornado/web.py", line 1667, in wrapper
    return method(self, *args, **kwargs)
  File "/home/fperez/usr/lib/python2.6/site-packages/IPython/frontend/html/notebook/handlers.py", line 232, in post
    data = {'ws_url':self.ws_url,'kernel_id':kernel_id}
  File "/home/fperez/usr/lib/python2.6/site-packages/IPython/frontend/html/notebook/handlers.py", line 150, in ws_url
    return self.request.headers.get('Origin').replace('http', 'ws', 1)
AttributeError: 'NoneType' object has no attribute 'replace'
ERROR:root:500 POST /kernels?notebook=69f0f1e7-5cbf-4d0c-b7fe-7807e1023886 (128.32.52.133) 39.75ms

I can't actually execute any cells. Do you get the same? Note that the traceback occurs on notebook connection, before any attempt to even execute code.

@minrk
Member
minrk commented Nov 11, 2011

Apparently Firefox doesn't send the Origin header, so I changed it to write protocol / host directly from the request. This will still work for tunnels, but I think it probably won't work if the notebook server is behind a reverse-proxy (e.g. if you have apache set up to forward http://server/ipnb to http://localhost:8888).

@fperez
Member
fperez commented Nov 11, 2011

Great, tested and merging now, everything seems to work. Not having to pass the websocket argument is nice.

@fperez fperez merged commit f6b3d8f into ipython:master Nov 11, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment