mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-05 01:40:04 +00:00
894d9b2836
Since `modules/context` has to depend on `models` and many other packages, it should be moved from `modules/context` to `services/context` according to design principles. There is no logic code change on this PR, only move packages. - Move `code.gitea.io/gitea/modules/context` to `code.gitea.io/gitea/services/context` - Move `code.gitea.io/gitea/modules/contexttest` to `code.gitea.io/gitea/services/contexttest` because of depending on context - Move `code.gitea.io/gitea/modules/upload` to `code.gitea.io/gitea/services/context/upload` because of depending on context (cherry picked from commit 29f149bd9f517225a3c9f1ca3fb0a7b5325af696) Conflicts: routers/api/packages/alpine/alpine.go routers/api/v1/repo/issue_reaction.go routers/install/install.go routers/web/admin/config.go routers/web/passkey.go routers/web/repo/search.go routers/web/repo/setting/default_branch.go routers/web/user/home.go routers/web/user/profile.go tests/integration/editor_test.go tests/integration/integration_test.go tests/integration/mirror_push_test.go trivial context conflicts also modified all other occurrences in Forgejo specific files
104 lines
2.2 KiB
Go
104 lines
2.2 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package context
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
web_types "code.gitea.io/gitea/modules/web/types"
|
|
)
|
|
|
|
// ResponseWriter represents a response writer for HTTP
|
|
type ResponseWriter interface {
|
|
http.ResponseWriter
|
|
http.Flusher
|
|
web_types.ResponseStatusProvider
|
|
|
|
Before(func(ResponseWriter))
|
|
|
|
Status() int // used by access logger template
|
|
Size() int // used by access logger template
|
|
}
|
|
|
|
var _ ResponseWriter = &Response{}
|
|
|
|
// Response represents a response
|
|
type Response struct {
|
|
http.ResponseWriter
|
|
written int
|
|
status int
|
|
befores []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.befores {
|
|
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) Status() int {
|
|
return r.status
|
|
}
|
|
|
|
func (r *Response) Size() int {
|
|
return r.written
|
|
}
|
|
|
|
// WriteHeader write status code
|
|
func (r *Response) WriteHeader(statusCode int) {
|
|
if !r.beforeExecuted {
|
|
for _, before := range r.befores {
|
|
before(r)
|
|
}
|
|
r.beforeExecuted = true
|
|
}
|
|
if r.status == 0 {
|
|
r.status = statusCode
|
|
r.ResponseWriter.WriteHeader(statusCode)
|
|
}
|
|
}
|
|
|
|
// 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(f func(ResponseWriter)) {
|
|
r.befores = append(r.befores, f)
|
|
}
|
|
|
|
func WrapResponseWriter(resp http.ResponseWriter) *Response {
|
|
if v, ok := resp.(*Response); ok {
|
|
return v
|
|
}
|
|
return &Response{
|
|
ResponseWriter: resp,
|
|
status: 0,
|
|
befores: make([]func(ResponseWriter), 0),
|
|
}
|
|
}
|