Skip to content

Commit b75474e

Browse files
authored
Fix websocket requests not mapped via mappath
This is done by instead of WebSocketHandlerMixin providing the default `get` method and delegating to `ProxyHandler.http_get`, it now provides `get_websocket` and `ProxyHandler.proxy` delegates to it. Code is also removed that was never reached since it checked for the "Upgrade" HTTP header after removing it among others defined in `hop_by_hop_headers`. Fixes jupyterhub#525
1 parent 8c5906a commit b75474e

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

jupyter_server_proxy/handlers.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from jupyter_server.base.handlers import JupyterHandler, utcnow
1717
from jupyter_server.utils import ensure_async, url_path_join
1818
from simpervisor import SupervisedProcess
19-
from tornado import httpclient, httputil, web
19+
from tornado import httpclient, httputil, web, websocket
2020
from tornado.simple_httpclient import SimpleAsyncHTTPClient
2121
from traitlets import Bytes, Dict, Instance, Integer, Unicode, Union, default, observe
2222
from traitlets.traitlets import HasTraits
@@ -171,6 +171,9 @@ async def http_get(self, host, port, proxy_path=""):
171171
raise NotImplementedError(
172172
"Subclasses of ProxyHandler should implement http_get"
173173
)
174+
175+
async def get(self, *args, **kwargs):
176+
return await self.http_get(*args, **kwargs)
174177

175178
def post(self, host, port, proxy_path=""):
176179
raise NotImplementedError(
@@ -333,6 +336,11 @@ async def proxy(self, host, port, proxied_path):
333336
"See https://jupyter-server-proxy.readthedocs.io/en/latest/arbitrary-ports-hosts.html for info.",
334337
)
335338

339+
self._record_activity()
340+
341+
if self.request.method == "GET" and self.request.headers.get("Upgrade", "").lower() == "websocket":
342+
return await ensure_async(self.get_websocket(proxied_path))
343+
336344
# Remove hop-by-hop headers that don't necessarily apply to the request we are making
337345
# to the backend. See https://github.com/jupyterhub/jupyter-server-proxy/pull/328
338346
# for more information
@@ -351,16 +359,6 @@ async def proxy(self, host, port, proxied_path):
351359
if header_to_remove in self.request.headers:
352360
del self.request.headers[header_to_remove]
353361

354-
self._record_activity()
355-
356-
if self.request.headers.get("Upgrade", "").lower() == "websocket":
357-
# We wanna websocket!
358-
# jupyterhub/jupyter-server-proxy@36b3214
359-
self.log.info(
360-
"we wanna websocket, but we don't define WebSocketProxyHandler"
361-
)
362-
self.set_status(500)
363-
364362
body = self.request.body
365363
if not body:
366364
if self.request.method in {"POST", "PUT"}:

jupyter_server_proxy/websocket.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,5 @@ def undisallow(*args2, **kwargs2):
9696
setattr(self, method, wrapper(method))
9797
nextparent.__init__(self, *args, **kwargs)
9898

99-
async def get(self, *args, **kwargs):
100-
if self.request.headers.get("Upgrade", "").lower() != "websocket":
101-
return await self.http_get(*args, **kwargs)
102-
else:
103-
await ensure_async(super().get(*args, **kwargs))
99+
async def get_websocket(self, *args, **kwargs):
100+
await ensure_async(super().get(*args, **kwargs))

0 commit comments

Comments
 (0)