Merge pull request #5033 from manics/urlpathjoin-trailing-empty

url_path_join: handle empty trailing components
This commit is contained in:
Min RK
2025-03-31 10:07:57 +02:00
committed by GitHub
4 changed files with 37 additions and 5 deletions

View File

@@ -1692,7 +1692,11 @@ class JupyterHub(Application):
"""add a url prefix to handlers"""
for i, tup in enumerate(handlers):
lis = list(tup)
lis[0] = url_path_join(prefix, tup[0])
if tup[0]:
lis[0] = url_path_join(prefix, tup[0])
else:
# the '' route should match /prefix not /prefix/
lis[0] = prefix.rstrip("/")
handlers[i] = tuple(lis)
return handlers

View File

@@ -100,6 +100,29 @@ async def test_tornado_coroutines():
assert (await t.tornado_coroutine()) == "gen.coroutine"
@pytest.mark.parametrize(
"pieces, expected",
[
(("/"), "/"),
(("/", "/"), "/"),
(("/base", ""), "/base"),
(("/base/", ""), "/base/"),
(("/base", "abc", "def"), "/base/abc/def"),
(("/base/", "/abc/", "/def/"), "/base/abc/def/"),
(("/base", "", "/", ""), "/base/"),
((""), ""),
(("", ""), ""),
(("", "part", ""), "part"),
(("", "/part"), "part"),
(("", "part", "", "after"), "part/after"),
(("", "part", "", "after/", "", ""), "part/after/"),
(("abc", "def"), "abc/def"),
],
)
def test_url_path_join(pieces, expected):
assert utils.url_path_join(*pieces) == expected
@pytest.mark.parametrize(
"forwarded, x_scheme, x_forwarded_proto, expected",
[

View File

@@ -786,7 +786,7 @@ class User:
if handler:
await self.refresh_auth(handler)
base_url = url_path_join(self.base_url, url_escape_path(server_name)) + '/'
base_url = url_path_join(self.base_url, url_escape_path(server_name), "/")
orm_server = orm.Server(base_url=base_url)
db.add(orm_server)
@@ -877,8 +877,7 @@ class User:
api_token,
url_path_join(self.url, url_escape_path(server_name), 'oauth_callback'),
allowed_scopes=allowed_scopes,
description="Server at %s"
% (url_path_join(self.base_url, server_name) + '/'),
description=f"Server at {url_path_join(self.base_url, server_name, '/')}",
)
spawner.orm_spawner.oauth_client = oauth_client
db.commit()

View File

@@ -471,9 +471,15 @@ def url_path_join(*pieces):
Use to prevent double slash when joining subpath. This will leave the
initial and final / in place.
Empty trailing items are ignored.
Copied from `notebook.utils.url_path_join`.
Based on `notebook.utils.url_path_join`.
"""
pieces = list(pieces)
while pieces and not pieces[-1]:
del pieces[-1]
if not pieces:
return ""
initial = pieces[0].startswith('/')
final = pieces[-1].endswith('/')
stripped = [s.strip('/') for s in pieces]