pkg/web: remove Router and Logger from Context (#53765)

web.Context previously held references to the current *web.Router albeit
not using it.

It also had a log.Logger only being used once internally
This commit is contained in:
sh0rez 2022-08-16 12:25:27 +02:00 committed by GitHub
parent 47364ae11f
commit 635571db8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 4 additions and 16 deletions

View File

@ -41,8 +41,7 @@ func TestToMacaronPath(t *testing.T) {
func TestAlertingProxy_createProxyContext(t *testing.T) { func TestAlertingProxy_createProxyContext(t *testing.T) {
ctx := &models.ReqContext{ ctx := &models.ReqContext{
Context: &web.Context{ Context: &web.Context{
Router: web.NewRouter(), Req: &http.Request{},
Req: &http.Request{},
}, },
SignedInUser: &user.SignedInUser{}, SignedInUser: &user.SignedInUser{},
UserToken: &models.UserToken{}, UserToken: &models.UserToken{},
@ -92,7 +91,6 @@ func TestAlertingProxy_createProxyContext(t *testing.T) {
newCtx := proxy.createProxyContext(ctx, req, resp) newCtx := proxy.createProxyContext(ctx, req, resp)
require.NotEqual(t, ctx.Context.Resp, newCtx.Context.Resp) require.NotEqual(t, ctx.Context.Resp, newCtx.Context.Resp)
require.Equal(t, ctx.Context.Router, newCtx.Context.Router)
require.Equal(t, ctx.Context.Req, newCtx.Context.Req) require.Equal(t, ctx.Context.Req, newCtx.Context.Req)
require.NotEqual(t, 123, resp.Status()) require.NotEqual(t, 123, resp.Status())

View File

@ -22,8 +22,6 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"github.com/grafana/grafana/pkg/infra/log"
) )
// Context represents the runtime context of current request of Macaron instance. // Context represents the runtime context of current request of Macaron instance.
@ -31,11 +29,9 @@ import (
type Context struct { type Context struct {
mws []Middleware mws []Middleware
*Router
Req *http.Request Req *http.Request
Resp ResponseWriter Resp ResponseWriter
template *template.Template template *template.Template
logger log.Logger
} }
func (ctx *Context) run() { func (ctx *Context) run() {
@ -69,7 +65,6 @@ func (ctx *Context) RemoteAddr() string {
if len(addr) > 0 { if len(addr) > 0 {
if parsedIP := net.ParseIP(addr); parsedIP == nil { if parsedIP := net.ParseIP(addr); parsedIP == nil {
// if parsedIP is nil we clean addr and populate with RemoteAddr below // if parsedIP is nil we clean addr and populate with RemoteAddr below
ctx.logger.Warn("Received invalid IP address in request headers, removed for log forgery prevention")
addr = "" addr = ""
} }
} }

View File

@ -86,8 +86,7 @@ func TestContext_RemoteAddr(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ctx := &Context{ ctx := &Context{
Req: tt.fields.Req, Req: tt.fields.Req,
logger: tt.fields.logger,
} }
if got := ctx.RemoteAddr(); got != tt.want { if got := ctx.RemoteAddr(); got != tt.want {
t.Errorf("RemoteAddr() = %v, want %v", got, tt.want) t.Errorf("RemoteAddr() = %v, want %v", got, tt.want)

View File

@ -24,8 +24,6 @@ import (
"context" "context"
"net/http" "net/http"
"strings" "strings"
"github.com/grafana/grafana/pkg/infra/log"
) )
const _VERSION = "1.3.4.0805" const _VERSION = "1.3.4.0805"
@ -147,10 +145,8 @@ func mwFromHandler(handler Handler) Middleware {
func (m *Macaron) createContext(rw http.ResponseWriter, req *http.Request) *Context { func (m *Macaron) createContext(rw http.ResponseWriter, req *http.Request) *Context {
c := &Context{ c := &Context{
mws: m.mws, mws: m.mws,
Router: m.Router, Resp: NewResponseWriter(req.Method, rw),
Resp: NewResponseWriter(req.Method, rw),
logger: log.New("macaron.context"),
} }
c.Req = req.WithContext(context.WithValue(req.Context(), macaronContextKey{}, c)) c.Req = req.WithContext(context.WithValue(req.Context(), macaronContextKey{}, c))