mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
pkg/web: restrict handler types (#48495)
Makes `pkg/web` only accept handles from the following set: ```go handlerStd = func(http.ResponseWriter, *http.Request) handlerStdCtx = func(http.ResponseWriter, *http.Request, *web.Context) handlerStdReqCtx = func(http.ResponseWriter, *http.Request, *models.ReqContext) handlerReqCtx = func(*models.ReqContext) handlerReqCtxRes = func(*models.ReqContext) Response handlerCtx = func(*web.Context) ``` This is a first step to reducing above set to only `http.Handler`. --- Due to a cyclic import situation between `pkg/models` and `pkg/web`, parts of this PR were put into `pkg/api/response`, even though they definitely do not belong there. This however is _temporary_ until we untangle `models.ReqContext`.
This commit is contained in:
62
pkg/api/response/web_hack.go
Normal file
62
pkg/api/response/web_hack.go
Normal file
@@ -0,0 +1,62 @@
|
||||
//nolint:unused,deadcode
|
||||
package response
|
||||
|
||||
//NOTE: This file belongs into pkg/web, but due to cyclic imports that are hard to resolve at the current time, it temporarily lives here.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/contexthandler/ctxkey"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
type (
|
||||
handlerStd = func(http.ResponseWriter, *http.Request)
|
||||
handlerStdCtx = func(http.ResponseWriter, *http.Request, *web.Context)
|
||||
handlerStdReqCtx = func(http.ResponseWriter, *http.Request, *models.ReqContext)
|
||||
handlerReqCtx = func(*models.ReqContext)
|
||||
handlerReqCtxRes = func(*models.ReqContext) Response
|
||||
handlerCtx = func(*web.Context)
|
||||
)
|
||||
|
||||
func wrap_handler(h web.Handler) http.HandlerFunc {
|
||||
switch handle := h.(type) {
|
||||
case handlerStd:
|
||||
return handle
|
||||
case handlerStdCtx:
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
handle(w, r, web.FromContext(r.Context()))
|
||||
}
|
||||
case handlerStdReqCtx:
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
handle(w, r, getReqCtx(r.Context()))
|
||||
}
|
||||
case handlerReqCtx:
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
handle(getReqCtx(r.Context()))
|
||||
}
|
||||
case handlerReqCtxRes:
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := getReqCtx(r.Context())
|
||||
res := handle(ctx)
|
||||
res.WriteTo(ctx)
|
||||
}
|
||||
case handlerCtx:
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
handle(web.FromContext(r.Context()))
|
||||
}
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unexpected handler type: %T", h))
|
||||
}
|
||||
|
||||
func getReqCtx(ctx context.Context) *models.ReqContext {
|
||||
reqCtx, ok := ctx.Value(ctxkey.Key{}).(*models.ReqContext)
|
||||
if !ok {
|
||||
panic("no *models.ReqContext found")
|
||||
}
|
||||
return reqCtx
|
||||
}
|
||||
Reference in New Issue
Block a user