Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context usage (#33554) #33557

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,7 @@ func registerRoutes(m *web.Router) {
}

m.NotFound(func(w http.ResponseWriter, req *http.Request) {
ctx := context.GetWebContext(req)
ctx := context.GetWebContext(req.Context())
routing.UpdateFuncInfo(ctx, routing.GetFuncInfo(ctx.NotFound, "WebNotFound"))
ctx.NotFound("", nil)
})
Expand Down
2 changes: 1 addition & 1 deletion services/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore
middleware.SetLocaleCookie(resp, user.Language, 0)

// force to generate a new CSRF token
if ctx := gitea_context.GetWebContext(req); ctx != nil {
if ctx := gitea_context.GetWebContext(req.Context()); ctx != nil {
ctx.Csrf.PrepareForSessionUser(ctx)
}
}
2 changes: 1 addition & 1 deletion services/auth/sspi.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore,
store.GetData()["EnableSSPI"] = true
// in this case, the Verify function is called in Gitea's web context
// FIXME: it doesn't look good to render the page here, why not redirect?
gitea_context.GetWebContext(req).HTML(http.StatusUnauthorized, tplSignIn)
gitea_context.GetWebContext(req.Context()).HTML(http.StatusUnauthorized, tplSignIn)
return nil, err
}
if outToken != "" {
Expand Down
7 changes: 4 additions & 3 deletions services/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,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.
Expand Down Expand Up @@ -133,6 +133,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.AppendContextValue(WebContextKey, ctx)
return ctx
}

Expand Down
4 changes: 2 additions & 2 deletions services/markup/processorhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func ProcessorHelper() *markup.RenderHelperFuncs {
return false
}

giteaCtx, ok := ctx.(*gitea_context.Context)
if !ok {
giteaCtx := gitea_context.GetWebContext(ctx)
if giteaCtx == nil {
// when using general context, use user's visibility to check
return mentionedUser.Visibility.IsPublic()
}
Expand Down
4 changes: 2 additions & 2 deletions services/markup/processorhelper_codepreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func renderRepoFileCodePreview(ctx context.Context, opts markup.RenderCodePrevie
return "", err
}

webCtx, ok := ctx.Value(gitea_context.WebContextKey).(*gitea_context.Context)
if !ok {
webCtx := gitea_context.GetWebContext(ctx)
if webCtx == nil {
return "", fmt.Errorf("context is not a web context")
}
doer := webCtx.Doer
Expand Down
Loading