mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Move middlewares to web/middleware (#14480)
Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
		@@ -17,8 +17,8 @@ import (
 | 
			
		||||
	"code.gitea.io/gitea/modules/auth/sso"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	"code.gitea.io/gitea/modules/middlewares"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	"code.gitea.io/gitea/modules/web/middleware"
 | 
			
		||||
 | 
			
		||||
	"gitea.com/go-chi/session"
 | 
			
		||||
)
 | 
			
		||||
@@ -224,7 +224,7 @@ func APIContexter() func(http.Handler) http.Handler {
 | 
			
		||||
	return func(next http.Handler) http.Handler {
 | 
			
		||||
 | 
			
		||||
		return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 | 
			
		||||
			var locale = middlewares.Locale(w, req)
 | 
			
		||||
			var locale = middleware.Locale(w, req)
 | 
			
		||||
			var ctx = APIContext{
 | 
			
		||||
				Context: &Context{
 | 
			
		||||
					Resp:    NewResponse(w),
 | 
			
		||||
 
 | 
			
		||||
@@ -26,11 +26,11 @@ import (
 | 
			
		||||
	"code.gitea.io/gitea/modules/base"
 | 
			
		||||
	mc "code.gitea.io/gitea/modules/cache"
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	"code.gitea.io/gitea/modules/middlewares"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	"code.gitea.io/gitea/modules/templates"
 | 
			
		||||
	"code.gitea.io/gitea/modules/translation"
 | 
			
		||||
	"code.gitea.io/gitea/modules/util"
 | 
			
		||||
	"code.gitea.io/gitea/modules/web/middleware"
 | 
			
		||||
 | 
			
		||||
	"gitea.com/go-chi/cache"
 | 
			
		||||
	"gitea.com/go-chi/session"
 | 
			
		||||
@@ -56,7 +56,7 @@ type Context struct {
 | 
			
		||||
	translation.Locale
 | 
			
		||||
	Cache   cache.Cache
 | 
			
		||||
	csrf    CSRF
 | 
			
		||||
	Flash   *middlewares.Flash
 | 
			
		||||
	Flash   *middleware.Flash
 | 
			
		||||
	Session session.Store
 | 
			
		||||
 | 
			
		||||
	Link        string // current request URL
 | 
			
		||||
@@ -206,7 +206,7 @@ func (ctx *Context) HTMLString(name string, data interface{}) (string, error) {
 | 
			
		||||
// RenderWithErr used for page has form validation but need to prompt error to users.
 | 
			
		||||
func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form interface{}) {
 | 
			
		||||
	if form != nil {
 | 
			
		||||
		middlewares.AssignForm(form, ctx.Data)
 | 
			
		||||
		middleware.AssignForm(form, ctx.Data)
 | 
			
		||||
	}
 | 
			
		||||
	ctx.Flash.ErrorMsg = msg
 | 
			
		||||
	ctx.Data["Flash"] = ctx.Flash
 | 
			
		||||
@@ -384,12 +384,12 @@ func (ctx *Context) Redirect(location string, status ...int) {
 | 
			
		||||
 | 
			
		||||
// SetCookie set cookies to web browser
 | 
			
		||||
func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
 | 
			
		||||
	middlewares.SetCookie(ctx.Resp, name, value, others...)
 | 
			
		||||
	middleware.SetCookie(ctx.Resp, name, value, others...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetCookie returns given cookie value from request header.
 | 
			
		||||
func (ctx *Context) GetCookie(name string) string {
 | 
			
		||||
	return middlewares.GetCookie(ctx.Req, name)
 | 
			
		||||
	return middleware.GetCookie(ctx.Req, name)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetSuperSecureCookie returns given cookie value from request header with secret string.
 | 
			
		||||
@@ -496,10 +496,10 @@ func GetContext(req *http.Request) *Context {
 | 
			
		||||
 | 
			
		||||
// SignedUserName returns signed user's name via context
 | 
			
		||||
func SignedUserName(req *http.Request) string {
 | 
			
		||||
	if middlewares.IsInternalPath(req) {
 | 
			
		||||
	if middleware.IsInternalPath(req) {
 | 
			
		||||
		return ""
 | 
			
		||||
	}
 | 
			
		||||
	if middlewares.IsAPIPath(req) {
 | 
			
		||||
	if middleware.IsAPIPath(req) {
 | 
			
		||||
		ctx, ok := req.Context().Value(apiContextKey).(*APIContext)
 | 
			
		||||
		if ok {
 | 
			
		||||
			v := ctx.Data["SignedUserName"]
 | 
			
		||||
@@ -539,7 +539,7 @@ 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) {
 | 
			
		||||
			var locale = middlewares.Locale(resp, req)
 | 
			
		||||
			var locale = middleware.Locale(resp, req)
 | 
			
		||||
			var startTime = time.Now()
 | 
			
		||||
			var link = setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/")
 | 
			
		||||
			var ctx = Context{
 | 
			
		||||
@@ -567,7 +567,7 @@ func Contexter() func(next http.Handler) http.Handler {
 | 
			
		||||
			flashCookie := ctx.GetCookie("macaron_flash")
 | 
			
		||||
			vals, _ := url.ParseQuery(flashCookie)
 | 
			
		||||
			if len(vals) > 0 {
 | 
			
		||||
				f := &middlewares.Flash{
 | 
			
		||||
				f := &middleware.Flash{
 | 
			
		||||
					DataStore:  &ctx,
 | 
			
		||||
					Values:     vals,
 | 
			
		||||
					ErrorMsg:   vals.Get("error"),
 | 
			
		||||
@@ -578,7 +578,7 @@ func Contexter() func(next http.Handler) http.Handler {
 | 
			
		||||
				ctx.Data["Flash"] = f
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			f := &middlewares.Flash{
 | 
			
		||||
			f := &middleware.Flash{
 | 
			
		||||
				DataStore:  &ctx,
 | 
			
		||||
				Values:     url.Values{},
 | 
			
		||||
				ErrorMsg:   "",
 | 
			
		||||
@@ -588,11 +588,11 @@ func Contexter() func(next http.Handler) http.Handler {
 | 
			
		||||
			}
 | 
			
		||||
			ctx.Resp.Before(func(resp ResponseWriter) {
 | 
			
		||||
				if flash := f.Encode(); len(flash) > 0 {
 | 
			
		||||
					middlewares.SetCookie(resp, "macaron_flash", flash, 0,
 | 
			
		||||
					middleware.SetCookie(resp, "macaron_flash", flash, 0,
 | 
			
		||||
						setting.SessionConfig.CookiePath,
 | 
			
		||||
						middlewares.Domain(setting.SessionConfig.Domain),
 | 
			
		||||
						middlewares.HTTPOnly(true),
 | 
			
		||||
						middlewares.Secure(setting.SessionConfig.Secure),
 | 
			
		||||
						middleware.Domain(setting.SessionConfig.Domain),
 | 
			
		||||
						middleware.HTTPOnly(true),
 | 
			
		||||
						middleware.Secure(setting.SessionConfig.Secure),
 | 
			
		||||
						//middlewares.SameSite(opt.SameSite), FIXME: we need a samesite config
 | 
			
		||||
					)
 | 
			
		||||
					return
 | 
			
		||||
@@ -600,10 +600,10 @@ func Contexter() func(next http.Handler) http.Handler {
 | 
			
		||||
 | 
			
		||||
				ctx.SetCookie("macaron_flash", "", -1,
 | 
			
		||||
					setting.SessionConfig.CookiePath,
 | 
			
		||||
					middlewares.Domain(setting.SessionConfig.Domain),
 | 
			
		||||
					middlewares.HTTPOnly(true),
 | 
			
		||||
					middlewares.Secure(setting.SessionConfig.Secure),
 | 
			
		||||
					//middlewares.SameSite(), FIXME: we need a samesite config
 | 
			
		||||
					middleware.Domain(setting.SessionConfig.Domain),
 | 
			
		||||
					middleware.HTTPOnly(true),
 | 
			
		||||
					middleware.Secure(setting.SessionConfig.Secure),
 | 
			
		||||
					//middleware.SameSite(), FIXME: we need a samesite config
 | 
			
		||||
				)
 | 
			
		||||
			})
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user