Skip to content

Commit

Permalink
修复额外参数v2插件表单参数提交失败的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Sep 4, 2023
1 parent ca65714 commit beca264
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 31 deletions.
2 changes: 2 additions & 0 deletions drivers/plugins/counter/separator/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func (f *FileCounter) Count(ctx http_service.IHttpContext) (int64, error) {
return int64(l / f.splitLen), nil
}
return int64(l/f.splitLen + 1), nil
case ArrayCountType:
return 1, nil
}
return splitCount(strings.Join(form.Value[f.name], f.split), f.split), nil
}
Expand Down
2 changes: 2 additions & 0 deletions drivers/plugins/counter/separator/formdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func (f *FormDataCounter) Count(ctx http_service.IHttpContext) (int64, error) {
return int64(l / f.splitLen), nil
}
return int64(l/f.splitLen + 1), nil
case ArrayCountType:
return 1, nil
}
return splitCount(u.Get(f.name), f.split), nil
}
Expand Down
18 changes: 9 additions & 9 deletions drivers/plugins/extra-params_v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,23 @@ type paramInfo struct {
conflict string
}

func (b *paramInfo) Build(ctx http_service.IHttpContext, contentType string, params interface{}) (string, interface{}, error) {
value, err := b.build(ctx, contentType, params)
if err != nil {
return "", nil, err
}
func (b *paramInfo) Build(ctx http_service.IHttpContext, contentType string, params interface{}) (string, error) {
return b.build(ctx, contentType, params)
}

func (b *paramInfo) Parse(value string) (interface{}, error) {
switch b.valueType {
case "int":
v, err := strconv.Atoi(value)
return value, v, err
return v, err
case "float":
v, err := strconv.ParseFloat(value, 64)
return value, v, err
return v, err
case "bool":
v, err := strconv.ParseBool(value)
return value, v, err
return v, err
default:
return value, nil, nil
return value, nil
}
}

Expand Down
33 changes: 12 additions & 21 deletions drivers/plugins/extra-params_v2/executor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package extra_params_v2

import (
"encoding/json"
"fmt"
"mime"
"net/http"
"net/textproto"
"net/url"
"strconv"
"strings"

Expand Down Expand Up @@ -84,12 +82,17 @@ func addParamToBody(ctx http_service.IHttpContext, contentType string, params []
continue
}
}
_, value, err := param.Build(ctx, contentType, bodyParam)
value, err := param.Build(ctx, contentType, bodyParam)
if err != nil {
log.Errorf("build param(s) error: %v", key, err)
continue
}
err = x.Set(bodyParam, value)
v, err := param.Parse(value)
if err != nil {
log.Errorf("parse param(s) error: %v", key, err)
continue
}
err = x.Set(bodyParam, v)
if err != nil {
log.Errorf("set param(s) error: %v", key, err)
continue
Expand All @@ -100,37 +103,25 @@ func addParamToBody(ctx http_service.IHttpContext, contentType string, params []
ctx.Proxy().Body().SetRaw(contentType, b)
return bodyParam, nil
} else if contentType == "application/x-www-form-urlencoded" || contentType == "multipart/form-data" {
bodyParam := make(map[string]interface{})
bodyForm, _ := ctx.Proxy().Body().BodyForm()
for _, param := range params {
_, has := bodyParam[param.name]
_, has := bodyForm[param.name]
if has {
if param.conflict == paramError {
return nil, fmt.Errorf("[extra_params] body(%s) has a conflict", param.name)
} else if param.conflict == paramOrigin {
continue
}
}
_, value, err := param.Build(ctx, contentType, nil)
value, err := param.Build(ctx, contentType, nil)
if err != nil {
log.Errorf("build param(s) error: %v", param.name, err)
continue
}
bodyParam[param.name] = value
bodyForm.Set(param.name, value)

}
ctx.Proxy().Body().SetForm(bodyForm)
} else if contentType == "application/x-www-form-urlencoded" {
body, _ := ctx.Proxy().Body().RawBody()
_, err := url.ParseQuery(string(body))
if err != nil {
return nil, err
}
var d interface{}
err = json.Unmarshal(body, &d)
if err == nil && len(body) > 0 {
return nil, fmt.Errorf("fail to parse body,body is %s", string(body))
}
}
return nil, nil
}
Expand Down Expand Up @@ -173,7 +164,7 @@ func (e *executor) access(ctx http_service.IHttpContext) (int, error) {
continue
}
}
value, _, err := param.Build(ctx, contentType, bodyParam)
value, err := param.Build(ctx, contentType, bodyParam)
if err != nil {
log.Errorf("build query extra param(%s) error: %s", param.name, err.Error())
continue
Expand All @@ -192,7 +183,7 @@ func (e *executor) access(ctx http_service.IHttpContext) (int, error) {
continue
}
}
value, _, err := param.Build(ctx, contentType, bodyParam)
value, err := param.Build(ctx, contentType, bodyParam)
if err != nil {
log.Errorf("build header extra param(%s) error: %s", name, err.Error())
continue
Expand Down
5 changes: 4 additions & 1 deletion node/http-context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ func (ctx *HttpContext) Clone() (eoscContext.EoContext, error) {
//记录请求时间
copyContext.ctx = context.WithValue(ctx.Context(), http_service.KeyCloneCtx, true)
copyContext.WithValue(ctx_key.CtxKeyRetry, 0)
copyContext.WithValue(ctx_key.CtxKeyRetry, time.Duration(0))
return copyContext, nil
}

Expand All @@ -238,6 +237,10 @@ func NewContext(ctx *fasthttp.RequestCtx, port int) *HttpContext {

// 原始请求最大读取body为8k,使用clone request
request := fasthttp.AcquireRequest()

if ctx.Request.IsBodyStream() && ctx.Request.Header.ContentLength() > 8*1024 {
ctx.Request.Body()
}
ctx.Request.CopyTo(request)
httpContext.requestReader.reset(request, remoteAddr)

Expand Down

0 comments on commit beca264

Please sign in to comment.