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
Comment From: qiandu2006
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
.