Skip to content

Commit 6ac8759

Browse files
committed
Add NotFound handler
PR go-gitea#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 go-gitea#18060 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 7be82f4 commit 6ac8759

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

integrations/links_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func TestRedirectsNoLogin(t *testing.T) {
4949
defer prepareTestEnv(t)()
5050

5151
var redirects = map[string]string{
52+
"/user2/repo1/": "/user2/repo1",
5253
"/user2/repo1/commits/master": "/user2/repo1/commits/branch/master",
5354
"/user2/repo1/src/master": "/user2/repo1/src/branch/master",
5455
"/user2/repo1/src/master/file.txt": "/user2/repo1/src/branch/master/file.txt",

routers/web/web.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,4 +1078,13 @@ 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.StatusFound)
1085+
}
1086+
ctx := context.GetContext(req)
1087+
ctx.NotFound("", nil)
1088+
})
1089+
10811090
}

0 commit comments

Comments
 (0)