@@ -9,25 +9,15 @@ import (
9
9
"net/http"
10
10
"reflect"
11
11
12
- "code.gitea.io/gitea/modules/context"
13
12
"code.gitea.io/gitea/modules/log"
14
13
"code.gitea.io/gitea/modules/web/routing"
14
+ "code.gitea.io/gitea/modules/web/types"
15
15
)
16
16
17
- // ResponseStatusProvider is an interface to check whether the response has been written by the handler
18
- type ResponseStatusProvider interface {
19
- Written () bool
20
- }
21
-
22
- // TODO: decouple this from the context package, let the context package register these providers
23
- var argTypeProvider = map [reflect.Type ]func (req * http.Request ) ResponseStatusProvider {
24
- reflect .TypeOf (& context.APIContext {}): func (req * http.Request ) ResponseStatusProvider { return context .GetAPIContext (req ) },
25
- reflect .TypeOf (& context.Context {}): func (req * http.Request ) ResponseStatusProvider { return context .GetWebContext (req ) },
26
- reflect .TypeOf (& context.PrivateContext {}): func (req * http.Request ) ResponseStatusProvider { return context .GetPrivateContext (req ) },
27
- }
17
+ var responseStatusProviders = map [reflect.Type ]func (req * http.Request ) types.ResponseStatusProvider {}
28
18
29
- func RegisterHandleTypeProvider [T any ](fn func (req * http.Request ) ResponseStatusProvider ) {
30
- argTypeProvider [reflect .TypeOf ((* T )(nil )).Elem ()] = fn
19
+ func RegisterResponseStatusProvider [T any ](fn func (req * http.Request ) types. ResponseStatusProvider ) {
20
+ responseStatusProviders [reflect .TypeOf ((* T )(nil )).Elem ()] = fn
31
21
}
32
22
33
23
// responseWriter is a wrapper of http.ResponseWriter, to check whether the response has been written
@@ -36,10 +26,10 @@ type responseWriter struct {
36
26
status int
37
27
}
38
28
39
- var _ ResponseStatusProvider = (* responseWriter )(nil )
29
+ var _ types. ResponseStatusProvider = (* responseWriter )(nil )
40
30
41
- func (r * responseWriter ) Written () bool {
42
- return r .status > 0
31
+ func (r * responseWriter ) WrittenStatus () int {
32
+ return r .status
43
33
}
44
34
45
35
func (r * responseWriter ) Header () http.Header {
68
58
func preCheckHandler (fn reflect.Value , argsIn []reflect.Value ) {
69
59
hasStatusProvider := false
70
60
for _ , argIn := range argsIn {
71
- if _ , hasStatusProvider = argIn .Interface ().(ResponseStatusProvider ); hasStatusProvider {
61
+ if _ , hasStatusProvider = argIn .Interface ().(types. ResponseStatusProvider ); hasStatusProvider {
72
62
break
73
63
}
74
64
}
@@ -101,7 +91,7 @@ func prepareHandleArgsIn(resp http.ResponseWriter, req *http.Request, fn reflect
101
91
case httpReqType :
102
92
argsIn [i ] = reflect .ValueOf (req )
103
93
default :
104
- if argFn , ok := argTypeProvider [argTyp ]; ok {
94
+ if argFn , ok := responseStatusProviders [argTyp ]; ok {
105
95
if isPreCheck {
106
96
argsIn [i ] = reflect .ValueOf (& responseWriter {})
107
97
} else {
@@ -129,8 +119,8 @@ func handleResponse(fn reflect.Value, ret []reflect.Value) goctx.CancelFunc {
129
119
130
120
func hasResponseBeenWritten (argsIn []reflect.Value ) bool {
131
121
for _ , argIn := range argsIn {
132
- if statusProvider , ok := argIn .Interface ().(ResponseStatusProvider ); ok {
133
- if statusProvider .Written () {
122
+ if statusProvider , ok := argIn .Interface ().(types. ResponseStatusProvider ); ok {
123
+ if statusProvider .WrittenStatus () != 0 {
134
124
return true
135
125
}
136
126
}
@@ -161,7 +151,7 @@ func toHandlerProvider(handler any) func(next http.Handler) http.Handler {
161
151
return http .HandlerFunc (func (respOrig http.ResponseWriter , req * http.Request ) {
162
152
// wrap the response writer to check whether the response has been written
163
153
resp := respOrig
164
- if _ , ok := resp .(ResponseStatusProvider ); ! ok {
154
+ if _ , ok := resp .(types. ResponseStatusProvider ); ! ok {
165
155
resp = & responseWriter {respWriter : resp }
166
156
}
167
157
0 commit comments