2021-06-09 05:03:54 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-09 05:03:54 +05:30
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-12-01 01:36:32 +05:30
|
|
|
"code.gitea.io/gitea/modules/process"
|
2021-06-09 05:03:54 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-01-20 17:11:25 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/routing"
|
2021-06-09 05:03:54 +05:30
|
|
|
|
|
|
|
"github.com/chi-middleware/proxy"
|
2021-12-03 02:28:08 +05:30
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2021-06-09 05:03:54 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Middlewares returns common middlewares
|
|
|
|
func Middlewares() []func(http.Handler) http.Handler {
|
2022-01-20 23:16:10 +05:30
|
|
|
handlers := []func(http.Handler) http.Handler{
|
2021-06-09 05:03:54 +05:30
|
|
|
func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
2021-12-24 22:20:49 +05:30
|
|
|
// First of all escape the URL RawPath to ensure that all routing is done using a correctly escaped URL
|
|
|
|
req.URL.RawPath = req.URL.EscapedPath()
|
|
|
|
|
2022-03-31 22:31:43 +05:30
|
|
|
ctx, _, finished := process.GetManager().AddTypedContext(req.Context(), fmt.Sprintf("%s: %s", req.Method, req.RequestURI), process.RequestProcessType, true)
|
2021-12-01 01:36:32 +05:30
|
|
|
defer finished()
|
|
|
|
next.ServeHTTP(context.NewResponse(resp), req.WithContext(ctx))
|
2021-06-09 05:03:54 +05:30
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if setting.ReverseProxyLimit > 0 {
|
|
|
|
opt := proxy.NewForwardedHeadersOptions().
|
|
|
|
WithForwardLimit(setting.ReverseProxyLimit).
|
|
|
|
ClearTrustedProxies()
|
|
|
|
for _, n := range setting.ReverseProxyTrustedProxies {
|
|
|
|
if !strings.Contains(n, "/") {
|
|
|
|
opt.AddTrustedProxy(n)
|
|
|
|
} else {
|
|
|
|
opt.AddTrustedNetwork(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handlers = append(handlers, proxy.ForwardedHeaders(opt))
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers = append(handlers, middleware.StripSlashes)
|
|
|
|
|
2022-01-20 17:11:25 +05:30
|
|
|
if !setting.DisableRouterLog {
|
|
|
|
handlers = append(handlers, routing.NewLoggerHandler())
|
2021-06-09 05:03:54 +05:30
|
|
|
}
|
2022-01-20 17:11:25 +05:30
|
|
|
|
2021-06-09 05:03:54 +05:30
|
|
|
if setting.EnableAccessLog {
|
|
|
|
handlers = append(handlers, context.AccessLogger())
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers = append(handlers, func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
// Why we need this? The Recovery() will try to render a beautiful
|
|
|
|
// error page for user, but the process can still panic again, and other
|
|
|
|
// middleware like session also may panic then we have to recover twice
|
2022-01-20 17:11:25 +05:30
|
|
|
// and send a simple error page that should not panic anymore.
|
2021-06-09 05:03:54 +05:30
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2022-01-20 17:11:25 +05:30
|
|
|
routing.UpdatePanicError(req.Context(), err)
|
|
|
|
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, log.Stack(2))
|
2021-06-09 05:03:54 +05:30
|
|
|
log.Error("%v", combinedErr)
|
2021-10-20 20:07:19 +05:30
|
|
|
if setting.IsProd {
|
2022-03-23 10:24:07 +05:30
|
|
|
http.Error(resp, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-06-09 05:03:54 +05:30
|
|
|
} else {
|
2022-03-23 10:24:07 +05:30
|
|
|
http.Error(resp, combinedErr, http.StatusInternalServerError)
|
2021-06-09 05:03:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
next.ServeHTTP(resp, req)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return handlers
|
|
|
|
}
|