Skip to content

Commit

Permalink
feat: support custom formvalue function
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Dec 15, 2022
1 parent f6aac90 commit faf8119
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
63 changes: 49 additions & 14 deletions server.go
Expand Up @@ -412,6 +412,7 @@ type Server struct {
concurrencyCh chan struct{}
perIPConnCounter perIPConnCounter
serverName atomic.Value
FormValueFunc FormValueFunc

ctxPool sync.Pool
readerPool sync.Pool
Expand Down Expand Up @@ -604,6 +605,7 @@ type RequestCtx struct {

hijackHandler HijackHandler
hijackNoResponse bool
formValueFunc FormValueFunc
}

// HijackHandler must process the hijacked connection c.
Expand Down Expand Up @@ -1108,23 +1110,54 @@ func SaveMultipartFile(fh *multipart.FileHeader, path string) (err error) {
//
// The returned value is valid until your request handler returns.
func (ctx *RequestCtx) FormValue(key string) []byte {
v := ctx.QueryArgs().Peek(key)
if len(v) > 0 {
return v
if ctx.formValueFunc != nil {
return ctx.formValueFunc(ctx, key)
}
v = ctx.PostArgs().Peek(key)
if len(v) > 0 {
return v
return defaultFormValue(ctx, key)
}

type FormValueFunc func(*RequestCtx, string) []byte

var (
defaultFormValue = func(ctx *RequestCtx, key string) []byte {
v := ctx.QueryArgs().Peek(key)
if len(v) > 0 {
return v
}
v = ctx.PostArgs().Peek(key)
if len(v) > 0 {
return v
}
mf, err := ctx.MultipartForm()
if err == nil && mf.Value != nil {
vv := mf.Value[key]
if len(vv) > 0 {
return []byte(vv[0])
}
}
return nil
}
mf, err := ctx.MultipartForm()
if err == nil && mf.Value != nil {
vv := mf.Value[key]
if len(vv) > 0 {
return []byte(vv[0])

// NetHttpFormValueFunc Consistent behaviour with net/http. POST and PUT body parameters take precedence over URL query string values.
NetHttpFormValueFunc = func(ctx *RequestCtx, key string) []byte {
v := ctx.PostArgs().Peek(key)
if len(v) > 0 {
return v
}
mf, err := ctx.MultipartForm()
if err == nil && mf.Value != nil {
vv := mf.Value[key]
if len(vv) > 0 {
return []byte(vv[0])
}
}
v = ctx.QueryArgs().Peek(key)
if len(v) > 0 {
return v
}
return nil
}
return nil
}
)

// IsGet returns true if request method is GET.
func (ctx *RequestCtx) IsGet() bool {
Expand Down Expand Up @@ -2638,7 +2671,9 @@ func (s *Server) acquireCtx(c net.Conn) (ctx *RequestCtx) {
} else {
ctx = v.(*RequestCtx)
}

if s.FormValueFunc != nil {
ctx.formValueFunc = s.FormValueFunc
}
ctx.c = c

return ctx
Expand Down
14 changes: 14 additions & 0 deletions server_test.go
Expand Up @@ -1713,6 +1713,20 @@ func TestRequestCtxFormValue(t *testing.T) {
}
}

func TestSetStandardFormValueFunc(t *testing.T) {
t.Parallel()
var ctx RequestCtx
var req Request
req.SetRequestURI("/foo/bar?aaa=bbb")
req.SetBodyString("aaa=port")
req.Header.SetContentType("application/x-www-form-urlencoded")
ctx.Init(&req, nil, nil)
ctx.formValueFunc = NetHttpFormValueFunc
v := ctx.FormValue("aaa")
if string(v) != "port" {
t.Fatalf("unexpected value %q. Expecting %q", v, "port")
}
}
func TestRequestCtxUserValue(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit faf8119

Please sign in to comment.