mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Make web context initialize correctly for different cases (#26726)
The web context (modules/context.Context) is quite complex, it's difficult for the callers to initialize correctly. This PR introduces a `NewWebContext` function, to make sure the web context have the same behavior for different cases.
This commit is contained in:
		@@ -107,6 +107,29 @@ func GetValidateContext(req *http.Request) (ctx *ValidateContext) {
 | 
			
		||||
	return ctx
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewTemplateContextForWeb(ctx *Context) TemplateContext {
 | 
			
		||||
	tmplCtx := NewTemplateContext(ctx)
 | 
			
		||||
	tmplCtx["Locale"] = ctx.Base.Locale
 | 
			
		||||
	tmplCtx["AvatarUtils"] = templates.NewAvatarUtils(ctx)
 | 
			
		||||
	return tmplCtx
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewWebContext(base *Base, render Render, session session.Store) *Context {
 | 
			
		||||
	ctx := &Context{
 | 
			
		||||
		Base:    base,
 | 
			
		||||
		Render:  render,
 | 
			
		||||
		Session: session,
 | 
			
		||||
 | 
			
		||||
		Cache: mc.GetCache(),
 | 
			
		||||
		Link:  setting.AppSubURL + strings.TrimSuffix(base.Req.URL.EscapedPath(), "/"),
 | 
			
		||||
		Repo:  &Repository{PullRequest: &PullRequest{}},
 | 
			
		||||
		Org:   &Organization{},
 | 
			
		||||
	}
 | 
			
		||||
	ctx.TemplateContext = NewTemplateContextForWeb(ctx)
 | 
			
		||||
	ctx.Flash = &middleware.Flash{DataStore: ctx, Values: url.Values{}}
 | 
			
		||||
	return ctx
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Contexter initializes a classic context for a request.
 | 
			
		||||
func Contexter() func(next http.Handler) http.Handler {
 | 
			
		||||
	rnd := templates.HTMLRenderer()
 | 
			
		||||
@@ -127,21 +150,8 @@ func Contexter() func(next http.Handler) http.Handler {
 | 
			
		||||
	return func(next http.Handler) http.Handler {
 | 
			
		||||
		return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
 | 
			
		||||
			base, baseCleanUp := NewBaseContext(resp, req)
 | 
			
		||||
			ctx := &Context{
 | 
			
		||||
				Base:    base,
 | 
			
		||||
				Cache:   mc.GetCache(),
 | 
			
		||||
				Link:    setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/"),
 | 
			
		||||
				Render:  rnd,
 | 
			
		||||
				Session: session.GetSession(req),
 | 
			
		||||
				Repo:    &Repository{PullRequest: &PullRequest{}},
 | 
			
		||||
				Org:     &Organization{},
 | 
			
		||||
			}
 | 
			
		||||
			defer baseCleanUp()
 | 
			
		||||
 | 
			
		||||
			// TODO: "install.go" also shares the same logic, which should be refactored to a general function
 | 
			
		||||
			ctx.TemplateContext = NewTemplateContext(ctx)
 | 
			
		||||
			ctx.TemplateContext["Locale"] = ctx.Locale
 | 
			
		||||
			ctx.TemplateContext["AvatarUtils"] = templates.NewAvatarUtils(ctx)
 | 
			
		||||
			ctx := NewWebContext(base, rnd, session.GetSession(req))
 | 
			
		||||
 | 
			
		||||
			ctx.Data.MergeFrom(middleware.CommonTemplateContextData())
 | 
			
		||||
			ctx.Data["Context"] = ctx // TODO: use "ctx" in template and remove this
 | 
			
		||||
@@ -172,8 +182,7 @@ func Contexter() func(next http.Handler) http.Handler {
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// prepare an empty Flash message for current request
 | 
			
		||||
			ctx.Flash = &middleware.Flash{DataStore: ctx, Values: url.Values{}}
 | 
			
		||||
			// if there are new messages in the ctx.Flash, write them into cookie
 | 
			
		||||
			ctx.Resp.Before(func(resp ResponseWriter) {
 | 
			
		||||
				if val := ctx.Flash.Encode(); val != "" {
 | 
			
		||||
					middleware.SetSiteCookie(ctx.Resp, CookieNameFlash, val, 0)
 | 
			
		||||
 
 | 
			
		||||
@@ -154,12 +154,10 @@ func PackageContexter() func(next http.Handler) http.Handler {
 | 
			
		||||
	return func(next http.Handler) http.Handler {
 | 
			
		||||
		return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
 | 
			
		||||
			base, baseCleanUp := NewBaseContext(resp, req)
 | 
			
		||||
			ctx := &Context{
 | 
			
		||||
				Base:   base,
 | 
			
		||||
				Render: renderer, // it is still needed when rendering 500 page in a package handler
 | 
			
		||||
			}
 | 
			
		||||
			defer baseCleanUp()
 | 
			
		||||
 | 
			
		||||
			// it is still needed when rendering 500 page in a package handler
 | 
			
		||||
			ctx := NewWebContext(base, renderer, nil)
 | 
			
		||||
			ctx.Base.AppendContextValue(WebContextKey, ctx)
 | 
			
		||||
			next.ServeHTTP(ctx.Resp, ctx.Req)
 | 
			
		||||
		})
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user