From 6ac875930c00c1cf211f49e7b2b6875450789bdc Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 21 Dec 2021 20:30:35 +0000 Subject: [PATCH] Add NotFound handler PR #17997 means that urls with terminal '/' are no longer immediately mapped to the url without a terminal slash. However, it has revealed that the NotFound handler appears to have been lost. This PR adds back in a NotFound handler that simply redirects to a path without the terminal slash or runs the NotFound handler. Fix #18060 Signed-off-by: Andrew Thornton --- integrations/links_test.go | 1 + routers/web/web.go | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/integrations/links_test.go b/integrations/links_test.go index 91166274a238..a3ae18d0a270 100644 --- a/integrations/links_test.go +++ b/integrations/links_test.go @@ -49,6 +49,7 @@ func TestRedirectsNoLogin(t *testing.T) { defer prepareTestEnv(t)() var redirects = map[string]string{ + "/user2/repo1/": "/user2/repo1", "/user2/repo1/commits/master": "/user2/repo1/commits/branch/master", "/user2/repo1/src/master": "/user2/repo1/src/branch/master", "/user2/repo1/src/master/file.txt": "/user2/repo1/src/branch/master/file.txt", diff --git a/routers/web/web.go b/routers/web/web.go index 41c4e122fb4e..99469a2e2ec2 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1078,4 +1078,13 @@ func RegisterRoutes(m *web.Route) { if setting.API.EnableSwagger { m.Get("/swagger.v1.json", SwaggerV1Json) } + m.NotFound(func(w http.ResponseWriter, req *http.Request) { + escapedPath := req.URL.EscapedPath() + if len(escapedPath) > 1 && escapedPath[len(escapedPath)-1] == '/' { + http.Redirect(w, req, setting.AppSubURL+escapedPath[:len(escapedPath)-1], http.StatusFound) + } + ctx := context.GetContext(req) + ctx.NotFound("", nil) + }) + }