From beca264d41169d1d42e63a803f3767fcb7369759 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Mon, 4 Sep 2023 10:37:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A2=9D=E5=A4=96=E5=8F=82?= =?UTF-8?q?=E6=95=B0v2=E6=8F=92=E4=BB=B6=E8=A1=A8=E5=8D=95=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=8F=90=E4=BA=A4=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drivers/plugins/counter/separator/file.go | 2 ++ drivers/plugins/counter/separator/formdata.go | 2 ++ drivers/plugins/extra-params_v2/config.go | 18 +++++----- drivers/plugins/extra-params_v2/executor.go | 33 +++++++------------ node/http-context/context.go | 5 ++- 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/drivers/plugins/counter/separator/file.go b/drivers/plugins/counter/separator/file.go index ae83469d..f12b1553 100644 --- a/drivers/plugins/counter/separator/file.go +++ b/drivers/plugins/counter/separator/file.go @@ -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 } diff --git a/drivers/plugins/counter/separator/formdata.go b/drivers/plugins/counter/separator/formdata.go index 8665cf73..cfbb2afc 100644 --- a/drivers/plugins/counter/separator/formdata.go +++ b/drivers/plugins/counter/separator/formdata.go @@ -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 } diff --git a/drivers/plugins/extra-params_v2/config.go b/drivers/plugins/extra-params_v2/config.go index 0a690284..3046a9f5 100644 --- a/drivers/plugins/extra-params_v2/config.go +++ b/drivers/plugins/extra-params_v2/config.go @@ -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 } } diff --git a/drivers/plugins/extra-params_v2/executor.go b/drivers/plugins/extra-params_v2/executor.go index 383a0b87..7121e7ac 100644 --- a/drivers/plugins/extra-params_v2/executor.go +++ b/drivers/plugins/extra-params_v2/executor.go @@ -1,12 +1,10 @@ package extra_params_v2 import ( - "encoding/json" "fmt" "mime" "net/http" "net/textproto" - "net/url" "strconv" "strings" @@ -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 @@ -100,10 +103,9 @@ 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) @@ -111,26 +113,15 @@ func addParamToBody(ctx http_service.IHttpContext, contentType string, params [] 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 } @@ -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 @@ -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 diff --git a/node/http-context/context.go b/node/http-context/context.go index 4c43f967..1394118a 100644 --- a/node/http-context/context.go +++ b/node/http-context/context.go @@ -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 } @@ -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)