2022-01-20 05:41:25 -06:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 12:20:29 -06:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-01-20 05:41:25 -06:00
|
|
|
|
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
goctx "context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/web/routing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type wrappedHandlerFunc func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func())
|
|
|
|
|
|
|
|
func convertHandler(handler interface{}) wrappedHandlerFunc {
|
|
|
|
funcInfo := routing.GetFuncInfo(handler)
|
|
|
|
switch t := handler.(type) {
|
|
|
|
case http.HandlerFunc:
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
|
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
2022-05-31 15:27:44 -05:00
|
|
|
if _, ok := resp.(context.ResponseWriter); !ok {
|
|
|
|
resp = context.NewResponse(resp)
|
|
|
|
}
|
2022-01-20 05:41:25 -06:00
|
|
|
t(resp, req)
|
|
|
|
if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
|
|
|
|
done = true
|
|
|
|
}
|
2022-06-20 05:02:49 -05:00
|
|
|
return done, deferrable
|
2022-01-20 05:41:25 -06:00
|
|
|
}
|
|
|
|
case func(http.ResponseWriter, *http.Request):
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
|
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
|
|
|
t(resp, req)
|
|
|
|
if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
|
|
|
|
done = true
|
|
|
|
}
|
2022-06-20 05:02:49 -05:00
|
|
|
return done, deferrable
|
2022-01-20 05:41:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case func(ctx *context.Context):
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
|
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
|
|
|
ctx := context.GetContext(req)
|
|
|
|
t(ctx)
|
|
|
|
done = ctx.Written()
|
2022-06-20 05:02:49 -05:00
|
|
|
return done, deferrable
|
2022-01-20 05:41:25 -06:00
|
|
|
}
|
|
|
|
case func(ctx *context.Context) goctx.CancelFunc:
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
|
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
|
|
|
ctx := context.GetContext(req)
|
|
|
|
deferrable = t(ctx)
|
|
|
|
done = ctx.Written()
|
2022-06-20 05:02:49 -05:00
|
|
|
return done, deferrable
|
2022-01-20 05:41:25 -06:00
|
|
|
}
|
|
|
|
case func(*context.APIContext):
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
|
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
|
|
|
ctx := context.GetAPIContext(req)
|
|
|
|
t(ctx)
|
|
|
|
done = ctx.Written()
|
2022-06-20 05:02:49 -05:00
|
|
|
return done, deferrable
|
2022-01-20 05:41:25 -06:00
|
|
|
}
|
|
|
|
case func(*context.APIContext) goctx.CancelFunc:
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
|
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
|
|
|
ctx := context.GetAPIContext(req)
|
|
|
|
deferrable = t(ctx)
|
|
|
|
done = ctx.Written()
|
2022-06-20 05:02:49 -05:00
|
|
|
return done, deferrable
|
2022-01-20 05:41:25 -06:00
|
|
|
}
|
|
|
|
case func(*context.PrivateContext):
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
|
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
|
|
|
ctx := context.GetPrivateContext(req)
|
|
|
|
t(ctx)
|
|
|
|
done = ctx.Written()
|
2022-06-20 05:02:49 -05:00
|
|
|
return done, deferrable
|
2022-01-20 05:41:25 -06:00
|
|
|
}
|
|
|
|
case func(*context.PrivateContext) goctx.CancelFunc:
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
|
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
|
|
|
ctx := context.GetPrivateContext(req)
|
|
|
|
deferrable = t(ctx)
|
|
|
|
done = ctx.Written()
|
2022-06-20 05:02:49 -05:00
|
|
|
return done, deferrable
|
2022-01-20 05:41:25 -06:00
|
|
|
}
|
|
|
|
case func(http.Handler) http.Handler:
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
|
2022-01-20 11:46:10 -06:00
|
|
|
next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
|
2022-01-20 05:41:25 -06:00
|
|
|
if len(others) > 0 {
|
|
|
|
next = wrapInternal(others)
|
|
|
|
}
|
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
2022-05-31 15:27:44 -05:00
|
|
|
if _, ok := resp.(context.ResponseWriter); !ok {
|
|
|
|
resp = context.NewResponse(resp)
|
|
|
|
}
|
2022-01-20 05:41:25 -06:00
|
|
|
t(next).ServeHTTP(resp, req)
|
2022-01-22 09:05:22 -06:00
|
|
|
if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
|
|
|
|
done = true
|
|
|
|
}
|
2022-06-20 05:02:49 -05:00
|
|
|
return done, deferrable
|
2022-01-20 05:41:25 -06:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unsupported handler type: %#v", t))
|
|
|
|
}
|
|
|
|
}
|