]> source.dussan.org Git - gitea.git/commitdiff
Add NotFound handler (#18062)
authorzeripath <art27@cantab.net>
Wed, 22 Dec 2021 10:39:28 +0000 (10:39 +0000)
committerGitHub <noreply@github.com>
Wed, 22 Dec 2021 10:39:28 +0000 (10:39 +0000)
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>
integrations/links_test.go
integrations/signout_test.go
routers/web/web.go

index 91166274a238197d051335747e81876369cccba5..872234e61fcc9d55d67c7a7b2fe5bb2eec1cd76f 100644 (file)
@@ -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) {
index c31c913070f5dce9a753a1c180233fa890175387..b54e7ee9eeec46fee6a4de45de2ae445f0d64aab 100644 (file)
@@ -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
index 41c4e122fb4e91f80db3bcbe6712b288540e9359..ebd0995df837919275ab21789de75d2ee1faa350 100644 (file)
@@ -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)
+       })
+
 }