Skip to content

Fix websocket requests not mapped via mappath #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions jupyter_server_proxy/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ async def http_get(self, host, port, proxy_path=""):
"Subclasses of ProxyHandler should implement http_get"
)

async def get(self, *args, **kwargs):
return await self.http_get(*args, **kwargs)

def post(self, host, port, proxy_path=""):
raise NotImplementedError(
"Subclasses of ProxyHandler should implement this post"
Expand Down Expand Up @@ -333,6 +336,14 @@ async def proxy(self, host, port, proxied_path):
"See https://jupyter-server-proxy.readthedocs.io/en/latest/arbitrary-ports-hosts.html for info.",
)

self._record_activity()

if (
self.request.method == "GET"
and self.request.headers.get("Upgrade", "").lower() == "websocket"
):
return await ensure_async(self.get_websocket(proxied_path))

# Remove hop-by-hop headers that don't necessarily apply to the request we are making
# to the backend. See https://github.com/jupyterhub/jupyter-server-proxy/pull/328
# for more information
Expand All @@ -351,16 +362,6 @@ async def proxy(self, host, port, proxied_path):
if header_to_remove in self.request.headers:
del self.request.headers[header_to_remove]

self._record_activity()

if self.request.headers.get("Upgrade", "").lower() == "websocket":
# We wanna websocket!
# jupyterhub/jupyter-server-proxy@36b3214
self.log.info(
"we wanna websocket, but we don't define WebSocketProxyHandler"
)
self.set_status(500)

body = self.request.body
if not body:
if self.request.method in {"POST", "PUT"}:
Expand Down
6 changes: 6 additions & 0 deletions jupyter_server_proxy/rawsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ def _create_ws_connection(self, proto: asyncio.BaseProtocol):
return loop.create_connection(proto, "localhost", self.port)

async def proxy(self, port, path):
if (
self.request.method == "GET"
and self.request.headers.get("Upgrade", "").lower() == "websocket"
):
return await super().proxy(port, path)

raise web.HTTPError(
405, "this raw_socket_proxy backend only supports websocket connections"
)
Expand Down
7 changes: 2 additions & 5 deletions jupyter_server_proxy/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,5 @@ def undisallow(*args2, **kwargs2):
setattr(self, method, wrapper(method))
nextparent.__init__(self, *args, **kwargs)

async def get(self, *args, **kwargs):
if self.request.headers.get("Upgrade", "").lower() != "websocket":
return await self.http_get(*args, **kwargs)
else:
await ensure_async(super().get(*args, **kwargs))
async def get_websocket(self, *args, **kwargs):
await ensure_async(super().get(*args, **kwargs))
8 changes: 8 additions & 0 deletions tests/resources/jupyter_server_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def mappathf(path):
return p


def mappathf_socket(path):
return path + "socket"


def translate_ciao(path, host, response, orig_response, port):
# Assume that the body has not been modified by any previous rewrite
assert response.body == orig_response.body
Expand Down Expand Up @@ -83,6 +87,10 @@ def my_env():
"X-Custom-Header": "pytest-23456",
},
},
"python-websocket-mappathf_socket": {
"command": [sys.executable, _get_path("websocket.py"), "--port={port}"],
"mappath": mappathf_socket,
},
"python-eventstream": {
"command": [sys.executable, _get_path("eventstream.py"), "--port={port}"]
},
Expand Down
13 changes: 13 additions & 0 deletions tests/test_proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,19 @@ async def test_server_proxy_websocket_headers(a_server_port_and_token: Tuple[int
assert headers["X-Custom-Header"] == "pytest-23456"


async def test_server_proxy_websocket_messages_mappath(
a_server_port_and_token: Tuple[int, str]
) -> None:
PORT, TOKEN = a_server_port_and_token
# Mappath is configured to add "socket" to websocket paths
url = f"ws://{LOCALHOST}:{PORT}/python-websocket-mappathf_socket/echo?token={TOKEN}"
conn = await websocket_connect(url)
expected_msg = "Hello, world!"
await conn.write_message(expected_msg)
msg = await conn.read_message()
assert msg == expected_msg


@pytest.mark.parametrize(
"client_requested,server_received,server_responded,proxy_responded",
[
Expand Down