web: Redirect on incomplete URLs

Examples:
/linux          -> /linux/{latest_tag}/source
/linux/{tag}    -> /linux/{tag}/source
This commit is contained in:
Franciszek Stachura 2025-02-06 01:01:27 +01:00 committed by Théo Lebrun
parent e8823db052
commit f6b2d46a0c

View file

@ -343,6 +343,22 @@ class IdentWithoutFamilyResource(IdentResource):
def on_get(self, req, resp, project: str, version: str, ident: str):
super().on_get(req, resp, project, version, 'C', ident)
# Handles /{project}/{version} URL, without path
class IncompleteURLRedirectResource:
def on_get(self, req, resp, project: str, version: str = "latest"):
ctx = req.context
query = get_query(ctx.config.project_dir, project)
if not query:
raise ElixirProjectError('Error', f'Unknown default project: {project}',
status=falcon.HTTP_INTERNAL_SERVER_ERROR)
if version == 'latest' or len(version) == 0:
version = parse.quote(query.query('latest'))
resp.status = falcon.HTTP_FOUND
resp.location = stringify_source_path(project, version, '/')
# File families available in the dropdown next to search input in the topbar
TOPBAR_FAMILIES = {
@ -789,6 +805,10 @@ def get_application():
app.add_route('/acp', AutocompleteResource())
app.add_route('/api/ident/{project:project}/{ident:ident}', ApiIdentGetterResource())
app.add_route('/{project}', IncompleteURLRedirectResource())
app.add_route('/{project}/{version}', IncompleteURLRedirectResource())
app.add_route('/{project}/{version}/', IncompleteURLRedirectResource())
return app
application = get_application()