summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-01-20 09:47:43 +0800
committerGitHub <noreply@github.com>2021-01-19 20:47:43 -0500
commit135b0e502d93c591b21083794dc100b53e520477 (patch)
tree591f5034aa021f97e71be94d97f58a45c014ddf7 /modules
parent41e19b93a27318a1f8b88ee76837a30969ccb767 (diff)
downloadgitea-135b0e502d93c591b21083794dc100b53e520477.tar.gz
gitea-135b0e502d93c591b21083794dc100b53e520477.zip
Fix log http status is always zero (#14400)
* Fix log http status is always zero * Fix lint Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'modules')
-rw-r--r--modules/context/response.go62
1 files changed, 62 insertions, 0 deletions
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}
+}