mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Refactor response writer & access logger (#33323)
And add comments & tests
This commit is contained in:
@@ -43,14 +43,18 @@ func ProtocolMiddlewares() (handlers []any) {
|
||||
|
||||
func RequestContextHandler() func(h http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
||||
profDesc := fmt.Sprintf("%s: %s", req.Method, req.RequestURI)
|
||||
return http.HandlerFunc(func(respOrig http.ResponseWriter, req *http.Request) {
|
||||
// this response writer might not be the same as the one in context.Base.Resp
|
||||
// because there might be a "gzip writer" in the middle, so the "written size" here is the compressed size
|
||||
respWriter := context.WrapResponseWriter(respOrig)
|
||||
|
||||
profDesc := fmt.Sprintf("HTTP: %s %s", req.Method, req.RequestURI)
|
||||
ctx, finished := reqctx.NewRequestContext(req.Context(), profDesc)
|
||||
defer finished()
|
||||
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
RenderPanicErrorPage(resp, req, err) // it should never panic
|
||||
RenderPanicErrorPage(respWriter, req, err) // it should never panic
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -62,7 +66,7 @@ func RequestContextHandler() func(h http.Handler) http.Handler {
|
||||
_ = req.MultipartForm.RemoveAll() // remove the temp files buffered to tmp directory
|
||||
}
|
||||
})
|
||||
next.ServeHTTP(context.WrapResponseWriter(resp), req)
|
||||
next.ServeHTTP(respWriter, req)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user