c572fcfe04
Rename the Go module path from code.gitea.io/gitea to git.mokoconsulting.tech/MokoConsulting/MokoGitea across the entire codebase. Scope: - go.mod module declaration - 2,235 Go source files (import paths) - Dockerfile WORKDIR and COPY paths - Swagger API templates - golangci.yml linter config External dependencies (code.gitea.io/gitea-vet, code.gitea.io/sdk/gitea, gitea.com/gitea/act, etc.) are intentionally NOT renamed — they are separate upstream modules. Closes #132 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
105 lines
2.5 KiB
Go
105 lines
2.5 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package context
|
|
|
|
import (
|
|
"bufio"
|
|
"net"
|
|
"net/http"
|
|
|
|
web_types "git.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/web/types"
|
|
)
|
|
|
|
// ResponseWriter represents a response writer for HTTP
|
|
type ResponseWriter interface {
|
|
http.ResponseWriter // provides Header/Write/WriteHeader
|
|
http.Flusher // provides Flush
|
|
web_types.ResponseStatusProvider // provides WrittenStatus
|
|
|
|
Before(fn func(ResponseWriter))
|
|
WrittenSize() int
|
|
}
|
|
|
|
var _ ResponseWriter = (*Response)(nil)
|
|
|
|
// Response represents a response
|
|
type Response struct {
|
|
http.ResponseWriter
|
|
written int
|
|
status int
|
|
beforeFuncs []func(ResponseWriter)
|
|
beforeExecuted bool
|
|
}
|
|
|
|
// Write writes bytes to HTTP endpoint
|
|
func (r *Response) Write(bs []byte) (int, error) {
|
|
if !r.beforeExecuted {
|
|
for _, before := range r.beforeFuncs {
|
|
before(r)
|
|
}
|
|
r.beforeExecuted = true
|
|
}
|
|
size, err := r.ResponseWriter.Write(bs)
|
|
r.written += size
|
|
if err != nil {
|
|
return size, err
|
|
}
|
|
if r.status == 0 {
|
|
r.status = http.StatusOK
|
|
}
|
|
return size, nil
|
|
}
|
|
|
|
func (r *Response) WrittenSize() int {
|
|
return r.written
|
|
}
|
|
|
|
// WriteHeader write status code
|
|
func (r *Response) WriteHeader(statusCode int) {
|
|
if !r.beforeExecuted {
|
|
for _, before := range r.beforeFuncs {
|
|
before(r)
|
|
}
|
|
r.beforeExecuted = true
|
|
}
|
|
if r.status == 0 {
|
|
r.status = statusCode
|
|
r.ResponseWriter.WriteHeader(statusCode)
|
|
}
|
|
}
|
|
|
|
// Hijack implements http.Hijacker, delegating to the underlying ResponseWriter.
|
|
// This is needed for WebSocket upgrades through reverse proxies.
|
|
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
if hj, ok := r.ResponseWriter.(http.Hijacker); ok {
|
|
return hj.Hijack()
|
|
}
|
|
return nil, nil, http.ErrNotSupported
|
|
}
|
|
|
|
// Flush flushes cached data
|
|
func (r *Response) Flush() {
|
|
if f, ok := r.ResponseWriter.(http.Flusher); ok {
|
|
f.Flush()
|
|
}
|
|
}
|
|
|
|
// WrittenStatus returned status code written
|
|
func (r *Response) WrittenStatus() int {
|
|
return r.status
|
|
}
|
|
|
|
// 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(fn func(ResponseWriter)) {
|
|
r.beforeFuncs = append(r.beforeFuncs, fn)
|
|
}
|
|
|
|
func WrapResponseWriter(resp http.ResponseWriter) *Response {
|
|
if v, ok := resp.(*Response); ok {
|
|
return v
|
|
}
|
|
return &Response{ResponseWriter: resp}
|
|
}
|