Skip to content

Commit 8fa3925

Browse files
authored
Fix context usage (#33554) (#33557)
Backport #33554
1 parent 7794ff0 commit 8fa3925

File tree

6 files changed

+11
-10
lines changed

6 files changed

+11
-10
lines changed

Diff for: routers/web/web.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ func registerRoutes(m *web.Router) {
16351635
}
16361636

16371637
m.NotFound(func(w http.ResponseWriter, req *http.Request) {
1638-
ctx := context.GetWebContext(req)
1638+
ctx := context.GetWebContext(req.Context())
16391639
routing.UpdateFuncInfo(ctx, routing.GetFuncInfo(ctx.NotFound, "WebNotFound"))
16401640
ctx.NotFound("", nil)
16411641
})

Diff for: services/auth/auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore
104104
middleware.SetLocaleCookie(resp, user.Language, 0)
105105

106106
// force to generate a new CSRF token
107-
if ctx := gitea_context.GetWebContext(req); ctx != nil {
107+
if ctx := gitea_context.GetWebContext(req.Context()); ctx != nil {
108108
ctx.Csrf.PrepareForSessionUser(ctx)
109109
}
110110
}

Diff for: services/auth/sspi.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore,
8989
store.GetData()["EnableSSPI"] = true
9090
// in this case, the Verify function is called in Gitea's web context
9191
// FIXME: it doesn't look good to render the page here, why not redirect?
92-
gitea_context.GetWebContext(req).HTML(http.StatusUnauthorized, tplSignIn)
92+
gitea_context.GetWebContext(req.Context()).HTML(http.StatusUnauthorized, tplSignIn)
9393
return nil, err
9494
}
9595
if outToken != "" {

Diff for: services/context/context.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ type webContextKeyType struct{}
7777

7878
var WebContextKey = webContextKeyType{}
7979

80-
func GetWebContext(req *http.Request) *Context {
81-
ctx, _ := req.Context().Value(WebContextKey).(*Context)
82-
return ctx
80+
func GetWebContext(ctx context.Context) *Context {
81+
webCtx, _ := ctx.Value(WebContextKey).(*Context)
82+
return webCtx
8383
}
8484

8585
// ValidateContext is a special context for form validation middleware. It may be different from other contexts.
@@ -133,6 +133,7 @@ func NewWebContext(base *Base, render Render, session session.Store) *Context {
133133
}
134134
ctx.TemplateContext = NewTemplateContextForWeb(ctx)
135135
ctx.Flash = &middleware.Flash{DataStore: ctx, Values: url.Values{}}
136+
ctx.AppendContextValue(WebContextKey, ctx)
136137
return ctx
137138
}
138139

Diff for: services/markup/processorhelper.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func ProcessorHelper() *markup.RenderHelperFuncs {
2020
return false
2121
}
2222

23-
giteaCtx, ok := ctx.(*gitea_context.Context)
24-
if !ok {
23+
giteaCtx := gitea_context.GetWebContext(ctx)
24+
if giteaCtx == nil {
2525
// when using general context, use user's visibility to check
2626
return mentionedUser.Visibility.IsPublic()
2727
}

Diff for: services/markup/processorhelper_codepreview.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func renderRepoFileCodePreview(ctx context.Context, opts markup.RenderCodePrevie
3535
return "", err
3636
}
3737

38-
webCtx, ok := ctx.Value(gitea_context.WebContextKey).(*gitea_context.Context)
39-
if !ok {
38+
webCtx := gitea_context.GetWebContext(ctx)
39+
if webCtx == nil {
4040
return "", fmt.Errorf("context is not a web context")
4141
}
4242
doer := webCtx.Doer

0 commit comments

Comments
 (0)