Skip to content

Commit

Permalink
Add NotFound handler (#18062)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
zeripath authored Dec 22, 2021
1 parent a9ed1c5 commit 62dcf1a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions integrations/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion integrations/signout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1078,4 +1078,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)
})

}

0 comments on commit 62dcf1a

Please sign in to comment.