aboutsummaryrefslogtreecommitdiffstats
path: root/services/context
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2025-02-11 16:46:03 +0800
committerGitHub <noreply@github.com>2025-02-11 16:46:03 +0800
commit245ac321c3169babe70a700108ea91fe5149c326 (patch)
tree52e2e66d1b69df7b3d72d1d0169f965cece100c4 /services/context
parente9b98aef447686f918ce4922ccadfed54c3f1634 (diff)
downloadgitea-245ac321c3169babe70a700108ea91fe5149c326.tar.gz
gitea-245ac321c3169babe70a700108ea91fe5149c326.zip
Fix context usage (#33554)
Some old code use direct type-casting to get context, it causes problems. This PR fixes all legacy problems and use correct `ctx.Value` to get low-level contexts. Fix #33518
Diffstat (limited to 'services/context')
-rw-r--r--services/context/context.go9
-rw-r--r--services/context/package.go2
2 files changed, 6 insertions, 5 deletions
diff --git a/services/context/context.go b/services/context/context.go
index 7aeb0de7ab..ffce1d617a 100644
--- a/services/context/context.go
+++ b/services/context/context.go
@@ -79,9 +79,9 @@ type webContextKeyType struct{}
var WebContextKey = webContextKeyType{}
-func GetWebContext(req *http.Request) *Context {
- ctx, _ := req.Context().Value(WebContextKey).(*Context)
- return ctx
+func GetWebContext(ctx context.Context) *Context {
+ webCtx, _ := ctx.Value(WebContextKey).(*Context)
+ return webCtx
}
// ValidateContext is a special context for form validation middleware. It may be different from other contexts.
@@ -135,6 +135,7 @@ func NewWebContext(base *Base, render Render, session session.Store) *Context {
}
ctx.TemplateContext = NewTemplateContextForWeb(ctx)
ctx.Flash = &middleware.Flash{DataStore: ctx, Values: url.Values{}}
+ ctx.SetContextValue(WebContextKey, ctx)
return ctx
}
@@ -165,7 +166,7 @@ func Contexter() func(next http.Handler) http.Handler {
ctx.PageData = map[string]any{}
ctx.Data["PageData"] = ctx.PageData
- ctx.Base.SetContextValue(WebContextKey, ctx)
+ ctx.Base.SetContextValue(WebContextKey, ctx) // FIXME: this should be removed because NewWebContext should already set it
ctx.Csrf = NewCSRFProtector(csrfOpts)
// get the last flash message from cookie
diff --git a/services/context/package.go b/services/context/package.go
index e98e01acbb..e566b7e532 100644
--- a/services/context/package.go
+++ b/services/context/package.go
@@ -156,7 +156,7 @@ func PackageContexter() func(next http.Handler) http.Handler {
base := NewBaseContext(resp, req)
// it is still needed when rendering 500 page in a package handler
ctx := NewWebContext(base, renderer, nil)
- ctx.SetContextValue(WebContextKey, ctx)
+ ctx.SetContextValue(WebContextKey, ctx) // FIXME: this should be removed because NewWebContext should already set it
next.ServeHTTP(ctx.Resp, ctx.Req)
})
}