Skip to content

Commit 62dcf1a

Browse files
authored
Add NotFound handler (#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 <[email protected]>
1 parent a9ed1c5 commit 62dcf1a

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

integrations/links_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ func TestRedirectsNoLogin(t *testing.T) {
6161
resp := MakeRequest(t, req, http.StatusFound)
6262
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
6363
}
64+
65+
var temporaryRedirects = map[string]string{
66+
"/user2/repo1/": "/user2/repo1",
67+
}
68+
for link, redirectLink := range temporaryRedirects {
69+
req := NewRequest(t, "GET", link)
70+
resp := MakeRequest(t, req, http.StatusTemporaryRedirect)
71+
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
72+
}
73+
6474
}
6575

6676
func TestNoLoginNotExist(t *testing.T) {

integrations/signout_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestSignOut(t *testing.T) {
1818
session.MakeRequest(t, req, http.StatusFound)
1919

2020
// try to view a private repo, should fail
21-
req = NewRequest(t, "GET", "/user2/repo2/")
21+
req = NewRequest(t, "GET", "/user2/repo2")
2222
session.MakeRequest(t, req, http.StatusNotFound)
2323

2424
// invalidate cached cookies for user2, for subsequent tests

routers/web/web.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,4 +1078,14 @@ func RegisterRoutes(m *web.Route) {
10781078
if setting.API.EnableSwagger {
10791079
m.Get("/swagger.v1.json", SwaggerV1Json)
10801080
}
1081+
m.NotFound(func(w http.ResponseWriter, req *http.Request) {
1082+
escapedPath := req.URL.EscapedPath()
1083+
if len(escapedPath) > 1 && escapedPath[len(escapedPath)-1] == '/' {
1084+
http.Redirect(w, req, setting.AppSubURL+escapedPath[:len(escapedPath)-1], http.StatusTemporaryRedirect)
1085+
return
1086+
}
1087+
ctx := context.GetContext(req)
1088+
ctx.NotFound("", nil)
1089+
})
1090+
10811091
}

0 commit comments

Comments
 (0)