From 135b0e502d93c591b21083794dc100b53e520477 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 20 Jan 2021 09:47:43 +0800 Subject: [PATCH] Fix log http status is always zero (#14400) * Fix log http status is always zero * Fix lint Co-authored-by: 6543 <6543@obermui.de> --- modules/context/response.go | 62 +++++++++++++++++++++++++++++++++++++ routers/routes/chi.go | 12 +++++-- 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 modules/context/response.go diff --git a/modules/context/response.go b/modules/context/response.go new file mode 100644 index 0000000000..549bd30ee0 --- /dev/null +++ b/modules/context/response.go @@ -0,0 +1,62 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package context + +import "net/http" + +// ResponseWriter represents a response writer for HTTP +type ResponseWriter interface { + http.ResponseWriter + Flush() + Status() int +} + +var ( + _ ResponseWriter = &Response{} +) + +// Response represents a response +type Response struct { + http.ResponseWriter + status int +} + +// Write writes bytes to HTTP endpoint +func (r *Response) Write(bs []byte) (int, error) { + size, err := r.ResponseWriter.Write(bs) + if err != nil { + return 0, err + } + if r.status == 0 { + r.WriteHeader(200) + } + return size, nil +} + +// WriteHeader write status code +func (r *Response) WriteHeader(statusCode int) { + r.status = statusCode + r.ResponseWriter.WriteHeader(statusCode) +} + +// Flush flush cached data +func (r *Response) Flush() { + if f, ok := r.ResponseWriter.(http.Flusher); ok { + f.Flush() + } +} + +// Status returned status code written +func (r *Response) Status() int { + return r.status +} + +// NewResponse creates a response +func NewResponse(resp http.ResponseWriter) *Response { + if v, ok := resp.(*Response); ok { + return v + } + return &Response{resp, 0} +} diff --git a/routers/routes/chi.go b/routers/routes/chi.go index 16fd6ea905..2400140ae6 100644 --- a/routers/routes/chi.go +++ b/routers/routes/chi.go @@ -16,6 +16,7 @@ import ( "text/template" "time" + "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/metrics" @@ -90,9 +91,11 @@ func LoggerHandler(level log.Level) func(next http.Handler) http.Handler { next.ServeHTTP(w, req) - ww := middleware.NewWrapResponseWriter(w, req.ProtoMajor) + var status int + if v, ok := w.(context.ResponseWriter); ok { + status = v.Status() + } - status := ww.Status() _ = log.GetLogger("router").Log(0, level, "Completed %s %s %v %s in %v", log.ColoredMethod(req.Method), req.URL.RequestURI(), log.ColoredStatus(status), log.ColoredStatus(status, http.StatusText(status)), log.ColoredTime(time.Since(start))) }) } @@ -183,6 +186,11 @@ var ( // NewChi creates a chi Router func NewChi() chi.Router { c := chi.NewRouter() + c.Use(func(next http.Handler) http.Handler { + return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { + next.ServeHTTP(context.NewResponse(resp), req) + }) + }) c.Use(middleware.RealIP) if !setting.DisableRouterLog && setting.RouterLogLevel != log.NONE { if log.GetLogger("router").GetLevel() <= setting.RouterLogLevel {