fix issue in char '-' support for parameter retrieving for ghttp.Request

This commit is contained in:
John
2020-01-22 20:28:42 +08:00
parent 2e10ce421b
commit 26aab44ec8
3 changed files with 53 additions and 2 deletions

View File

@ -1,5 +1,30 @@
package main
func main() {
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/os/glog"
)
func main() {
s := g.Server()
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
group.ALL("/user/list", func(r *ghttp.Request) {
glog.Debug(r.Method, r.RequestURI)
paramKey := "X-CSRF-Token"
// // www-form or query
// glog.Debug("go:", r.Request.FormValue(paramKey))
// // post form-data
// glog.Debug("go form:", r.Request.PostFormValue(paramKey))
glog.Debug("gf GetString:", r.GetString(paramKey))
glog.Debug("gf GetFormString:", r.GetFormString(paramKey))
r.Response.Writeln("list")
})
})
s.SetPort(8199)
s.Run()
}

View File

@ -237,7 +237,8 @@ func (r *Request) parseForm() {
params := ""
for name, values := range r.PostForm {
// Invalid parameter name.
if !gregex.IsMatchString(`^[\w\[\]]+$`, name) {
// Only allow chars of: '\w', '[', ']', '-'.
if !gregex.IsMatchString(`^[\w\-\[\]]+$`, name) {
if len(r.PostForm) == 1 {
// It might be JSON/XML content.
r.bodyContent = gconv.UnsafeStrToBytes(name + strings.Join(values, " "))

View File

@ -404,6 +404,31 @@ func Test_Params_Basic(t *testing.T) {
})
}
func Test_Params_SupportChars(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
s.BindHandler("/form-value", func(r *ghttp.Request) {
r.Response.Write(r.GetQuery("test-value"))
})
s.BindHandler("/form-array", func(r *ghttp.Request) {
r.Response.Write(r.GetQuery("test-array"))
})
s.SetPort(p)
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
prefix := fmt.Sprintf("http://127.0.0.1:%d", p)
client := ghttp.NewClient()
client.SetPrefix(prefix)
gtest.Assert(client.PostContent("/form-value", "test-value=100"), "100")
gtest.Assert(client.PostContent("/form-array", "test-array[]=1&test-array[]=2"), `["1","2"]`)
})
}
func Test_Params_Priority(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)