2021-01-20 07:17:43 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-20 07:17:43 +05:30
|
|
|
|
|
|
|
package context
|
|
|
|
|
2021-04-14 18:27:18 +05:30
|
|
|
import (
|
|
|
|
"net/http"
|
2023-06-18 13:29:09 +05:30
|
|
|
|
|
|
|
web_types "code.gitea.io/gitea/modules/web/types"
|
2021-04-14 18:27:18 +05:30
|
|
|
)
|
2021-01-20 07:17:43 +05:30
|
|
|
|
|
|
|
// ResponseWriter represents a response writer for HTTP
|
|
|
|
type ResponseWriter interface {
|
|
|
|
http.ResponseWriter
|
2023-05-21 07:20:53 +05:30
|
|
|
http.Flusher
|
2023-06-18 13:29:09 +05:30
|
|
|
web_types.ResponseStatusProvider
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
Before(func(ResponseWriter))
|
2023-06-18 13:29:09 +05:30
|
|
|
|
|
|
|
Status() int // used by access logger template
|
|
|
|
Size() int // used by access logger template
|
2021-01-20 07:17:43 +05:30
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
var _ ResponseWriter = &Response{}
|
2021-01-20 07:17:43 +05:30
|
|
|
|
|
|
|
// Response represents a response
|
|
|
|
type Response struct {
|
|
|
|
http.ResponseWriter
|
2021-01-27 23:16:35 +05:30
|
|
|
written int
|
2021-01-26 21:06:53 +05:30
|
|
|
status int
|
|
|
|
befores []func(ResponseWriter)
|
|
|
|
beforeExecuted bool
|
2021-01-20 07:17:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes bytes to HTTP endpoint
|
|
|
|
func (r *Response) Write(bs []byte) (int, error) {
|
2021-01-26 21:06:53 +05:30
|
|
|
if !r.beforeExecuted {
|
|
|
|
for _, before := range r.befores {
|
|
|
|
before(r)
|
|
|
|
}
|
|
|
|
r.beforeExecuted = true
|
|
|
|
}
|
2021-01-20 07:17:43 +05:30
|
|
|
size, err := r.ResponseWriter.Write(bs)
|
2021-01-27 23:16:35 +05:30
|
|
|
r.written += size
|
2021-01-20 07:17:43 +05:30
|
|
|
if err != nil {
|
2021-01-27 23:16:35 +05:30
|
|
|
return size, err
|
2021-01-20 07:17:43 +05:30
|
|
|
}
|
|
|
|
if r.status == 0 {
|
2021-05-14 13:35:50 +05:30
|
|
|
r.status = http.StatusOK
|
2021-01-20 07:17:43 +05:30
|
|
|
}
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
2023-06-18 13:29:09 +05:30
|
|
|
func (r *Response) Status() int {
|
|
|
|
return r.status
|
|
|
|
}
|
|
|
|
|
2023-05-22 07:08:38 +05:30
|
|
|
func (r *Response) Size() int {
|
|
|
|
return r.written
|
|
|
|
}
|
|
|
|
|
2021-01-20 07:17:43 +05:30
|
|
|
// WriteHeader write status code
|
|
|
|
func (r *Response) WriteHeader(statusCode int) {
|
2021-01-26 21:06:53 +05:30
|
|
|
if !r.beforeExecuted {
|
|
|
|
for _, before := range r.befores {
|
|
|
|
before(r)
|
|
|
|
}
|
|
|
|
r.beforeExecuted = true
|
|
|
|
}
|
2021-04-14 18:27:18 +05:30
|
|
|
if r.status == 0 {
|
|
|
|
r.status = statusCode
|
|
|
|
r.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
}
|
2021-01-20 07:17:43 +05:30
|
|
|
}
|
|
|
|
|
2023-05-21 07:20:53 +05:30
|
|
|
// Flush flushes cached data
|
2021-01-20 07:17:43 +05:30
|
|
|
func (r *Response) Flush() {
|
|
|
|
if f, ok := r.ResponseWriter.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 13:29:09 +05:30
|
|
|
// WrittenStatus returned status code written
|
|
|
|
func (r *Response) WrittenStatus() int {
|
2021-01-20 07:17:43 +05:30
|
|
|
return r.status
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
// Before allows for a function to be called before the ResponseWriter has been written to. This is
|
|
|
|
// useful for setting headers or any other operations that must happen before a response has been written.
|
|
|
|
func (r *Response) Before(f func(ResponseWriter)) {
|
|
|
|
r.befores = append(r.befores, f)
|
|
|
|
}
|
|
|
|
|
2023-05-21 07:20:53 +05:30
|
|
|
func WrapResponseWriter(resp http.ResponseWriter) *Response {
|
2021-01-20 07:17:43 +05:30
|
|
|
if v, ok := resp.(*Response); ok {
|
|
|
|
return v
|
|
|
|
}
|
2021-01-26 21:06:53 +05:30
|
|
|
return &Response{
|
|
|
|
ResponseWriter: resp,
|
|
|
|
status: 0,
|
|
|
|
befores: make([]func(ResponseWriter), 0),
|
|
|
|
}
|
2021-01-20 07:17:43 +05:30
|
|
|
}
|