diff --git a/routers/common/middleware.go b/routers/common/middleware.go index 4f9d43c362182..2765577b021ea 100644 --- a/routers/common/middleware.go +++ b/routers/common/middleware.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/web/routing" "github.com/chi-middleware/proxy" + "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" ) @@ -50,6 +51,27 @@ func Middlewares() []func(http.Handler) http.Handler { handlers = append(handlers, middleware.StripSlashes) + handlers = append(handlers, func(next http.Handler) http.Handler { + return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { + var path string + rctx := chi.RouteContext(req.Context()) + if rctx != nil && rctx.RoutePath != "" { + path = rctx.RoutePath + } else { + path = req.URL.Path + } + if len(path) > 1 && strings.HasPrefix(path, "//") { + newPath := path[1:] + if rctx == nil { + req.URL.Path = newPath + } else { + rctx.RoutePath = newPath + } + } + next.ServeHTTP(resp, req) + }) + }) + if !setting.Log.DisableRouterLog { handlers = append(handlers, routing.NewLoggerHandler()) }