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

Feature request: User-configurable querycache & formcache #3639

Open
WayneJz opened this issue Jun 7, 2023 · 1 comment
Open

Feature request: User-configurable querycache & formcache #3639

WayneJz opened this issue Jun 7, 2023 · 1 comment

Comments

@WayneJz
Copy link

WayneJz commented Jun 7, 2023

Description

Allow user to configure querycache & formcache settings (enable/disable or reset them)

How to reproduce

func ChangeUrlParam() gin.HandlerFunc {
	return func(c *gin.Context) {
		params, _ := url.ParseQuery(c.Request.URL.RawQuery)
		params.Set("xxx", "yyy")
		c.Request.URL.RawQuery = params.Encode()
	}
}

Suppose the original url query is xxx=iii, the middleware func above would change it to xxx=yyy, but it does not work.
The downstream func calls c.Query("xxx"), and still receives iii.

This is because when c.Query("xxx") is called, it uses querycache first. But querycache is immutable after created. As:

// GetQueryArray returns a slice of strings for a given query key, plus
// a boolean value whether at least one value exists for the given key.
func (c *Context) GetQueryArray(key string) (values []string, ok bool) {
	c.initQueryCache()
	values, ok = c.queryCache[key]
	return
}


func (c *Context) initQueryCache() {
	if c.queryCache == nil {
		if c.Request != nil {
			c.queryCache = c.Request.URL.Query()
		} else {
			c.queryCache = url.Values{}
		}
	}
}

Formcache has the same issue as querycache's.

Expectations

  • Above middleware func will output the correct result.
  • Gin provides an exportable func, such that user can enable, disable or reset querycache or formcache.

Actual result

Not working as query cache is not mutable. The query parameters are not successfully modified.

Environment

  • go version: 1.16
  • gin version (or commit ref): github.com/gin-gonic/gin v1.8.1
  • operating system: Linux/AMD64
@qiandu2006
Copy link

queryCache becomes immutable after first initializing, but sometimes middlewares may change RawQuery (at least modification is allowed), so there is inconsistency between queryCache and RawQuery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants