diff options
author | zeripath | 2021-12-22 15:26:37 +0000 |
---|---|---|
committer | GitHub | 2021-12-22 15:26:37 +0000 |
commit | 6100935a777daf764547b4825e956330cd42ef43 (patch) | |
tree | cf4266aa5cfac0457c306eaf3d5cdd57a485ea65 | |
parent | 6de75224de3c8c9bd75e8e4b9e78c22a3cf468e1 (diff) |
Add NotFound handler (#18062) (#18067)
Backport #18062
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 <art27@cantab.net>
-rw-r--r-- | integrations/links_test.go | 10 | ||||
-rw-r--r-- | integrations/signout_test.go | 2 | ||||
-rw-r--r-- | routers/web/web.go | 10 |
3 files changed, 21 insertions, 1 deletions
diff --git a/integrations/links_test.go b/integrations/links_test.go index 03229e10e..2b8bbde08 100644 --- a/integrations/links_test.go +++ b/integrations/links_test.go @@ -61,6 +61,16 @@ func TestRedirectsNoLogin(t *testing.T) { resp := MakeRequest(t, req, http.StatusFound) assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp)) } + + var temporaryRedirects = map[string]string{ + "/user2/repo1/": "/user2/repo1", + } + for link, redirectLink := range temporaryRedirects { + req := NewRequest(t, "GET", link) + resp := MakeRequest(t, req, http.StatusTemporaryRedirect) + assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp)) + } + } func TestNoLoginNotExist(t *testing.T) { diff --git a/integrations/signout_test.go b/integrations/signout_test.go index c31c91307..b54e7ee9e 100644 --- a/integrations/signout_test.go +++ b/integrations/signout_test.go @@ -18,7 +18,7 @@ func TestSignOut(t *testing.T) { session.MakeRequest(t, req, http.StatusFound) // try to view a private repo, should fail - req = NewRequest(t, "GET", "/user2/repo2/") + req = NewRequest(t, "GET", "/user2/repo2") session.MakeRequest(t, req, http.StatusNotFound) // invalidate cached cookies for user2, for subsequent tests diff --git a/routers/web/web.go b/routers/web/web.go index 82d5669e8..45cf536bc 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1036,4 +1036,14 @@ 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.StatusTemporaryRedirect) + return + } + ctx := context.GetContext(req) + ctx.NotFound("", nil) + }) + } |