mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.2 KiB
104 lines
2.2 KiB
4 years ago
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||
2 years ago
|
// SPDX-License-Identifier: MIT
|
||
4 years ago
|
|
||
|
package context
|
||
|
|
||
4 years ago
|
import (
|
||
|
"net/http"
|
||
1 year ago
|
|
||
|
web_types "code.gitea.io/gitea/modules/web/types"
|
||
4 years ago
|
)
|
||
4 years ago
|
|
||
|
// ResponseWriter represents a response writer for HTTP
|
||
|
type ResponseWriter interface {
|
||
|
http.ResponseWriter
|
||
2 years ago
|
http.Flusher
|
||
1 year ago
|
web_types.ResponseStatusProvider
|
||
|
|
||
4 years ago
|
Before(func(ResponseWriter))
|
||
1 year ago
|
|
||
|
Status() int // used by access logger template
|
||
|
Size() int // used by access logger template
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
var _ ResponseWriter = &Response{}
|
||
4 years ago
|
|
||
|
// Response represents a response
|
||
|
type Response struct {
|
||
|
http.ResponseWriter
|
||
4 years ago
|
written int
|
||
4 years ago
|
status int
|
||
|
befores []func(ResponseWriter)
|
||
|
beforeExecuted bool
|
||
4 years ago
|
}
|
||
|
|
||
|
// Write writes bytes to HTTP endpoint
|
||
|
func (r *Response) Write(bs []byte) (int, error) {
|
||
4 years ago
|
if !r.beforeExecuted {
|
||
|
for _, before := range r.befores {
|
||
|
before(r)
|
||
|
}
|
||
|
r.beforeExecuted = true
|
||
|
}
|
||
4 years ago
|
size, err := r.ResponseWriter.Write(bs)
|
||
4 years ago
|
r.written += size
|
||
4 years ago
|
if err != nil {
|
||
4 years ago
|
return size, err
|
||
4 years ago
|
}
|
||
|
if r.status == 0 {
|
||
4 years ago
|
r.status = http.StatusOK
|
||
4 years ago
|
}
|
||
|
return size, nil
|
||
|
}
|
||
|
|
||
1 year ago
|
func (r *Response) Status() int {
|
||
|
return r.status
|
||
|
}
|
||
|
|
||
2 years ago
|
func (r *Response) Size() int {
|
||
|
return r.written
|
||
|
}
|
||
|
|
||
4 years ago
|
// WriteHeader write status code
|
||
|
func (r *Response) WriteHeader(statusCode int) {
|
||
4 years ago
|
if !r.beforeExecuted {
|
||
|
for _, before := range r.befores {
|
||
|
before(r)
|
||
|
}
|
||
|
r.beforeExecuted = true
|
||
|
}
|
||
4 years ago
|
if r.status == 0 {
|
||
|
r.status = statusCode
|
||
|
r.ResponseWriter.WriteHeader(statusCode)
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
// Flush flushes cached data
|
||
4 years ago
|
func (r *Response) Flush() {
|
||
|
if f, ok := r.ResponseWriter.(http.Flusher); ok {
|
||
|
f.Flush()
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
// WrittenStatus returned status code written
|
||
|
func (r *Response) WrittenStatus() int {
|
||
4 years ago
|
return r.status
|
||
|
}
|
||
|
|
||
4 years ago
|
// 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)
|
||
|
}
|
||
|
|
||
2 years ago
|
func WrapResponseWriter(resp http.ResponseWriter) *Response {
|
||
4 years ago
|
if v, ok := resp.(*Response); ok {
|
||
|
return v
|
||
|
}
|
||
4 years ago
|
return &Response{
|
||
|
ResponseWriter: resp,
|
||
|
status: 0,
|
||
|
befores: make([]func(ResponseWriter), 0),
|
||
|
}
|
||
4 years ago
|
}
|