From 199bfd293b70ea28b7b31afd1274e88082a91de0 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Sat, 8 Oct 2022 19:22:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BD=93=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E4=BD=93=E4=B8=BA=E7=A9=BA=EF=BC=8C=E4=B8=94content-type?= =?UTF-8?q?=E5=80=BC=E4=B8=BAapplication/json=E6=97=B6=EF=BC=8C=E9=A2=9D?= =?UTF-8?q?=E5=A4=96=E5=8F=82=E6=95=B0=E4=B8=8D=E7=94=9F=E6=95=88=E5=BD=93?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/auth/jwt/factory.go | 3 - drivers/app/config.go | 1 - drivers/app/extend-param.go | 3 + drivers/plugins/extra-params/extra-params.go | 69 ++++++++++++++++---- drivers/plugins/extra-params/util.go | 44 +++++++++++-- 5 files changed, 97 insertions(+), 23 deletions(-) diff --git a/application/auth/jwt/factory.go b/application/auth/jwt/factory.go index be4727b4..716903fe 100644 --- a/application/auth/jwt/factory.go +++ b/application/auth/jwt/factory.go @@ -4,8 +4,6 @@ import ( "errors" "reflect" - "github.com/eolinker/eosc/log" - "github.com/eolinker/eosc/utils/schema" "github.com/eolinker/apinto/application" @@ -50,7 +48,6 @@ func (f *factory) Create(tokenName string, position string, rule interface{}) (a if !ok { return nil, errors.New("invalid jwt config") } - log.Debug("config type: ", reflect.TypeOf(baseConfig.Config())) cfg, ok := baseConfig.Config().(*Rule) if !ok { return nil, errors.New("invalid jwt config") diff --git a/drivers/app/config.go b/drivers/app/config.go index e88a72e4..4eaeaa09 100644 --- a/drivers/app/config.go +++ b/drivers/app/config.go @@ -47,7 +47,6 @@ func (a *Auth) Reset(originVal reflect.Value, targetVal reflect.Value, variables if err != nil { return nil, err } - log.Debug("set type: ", string(bytes)) f, err := auth.GetFactory(tmp.Type) if err != nil { diff --git a/drivers/app/extend-param.go b/drivers/app/extend-param.go index a6337449..bca655da 100644 --- a/drivers/app/extend-param.go +++ b/drivers/app/extend-param.go @@ -149,6 +149,9 @@ func parseBodyParams(ctx http_service.IHttpContext) (interface{}, map[string][]s if err != nil { return nil, nil, err } + if string(body) == "" { + body = []byte("{}") + } bodyParams, err := oj.Parse(body) return bodyParams, nil, err } diff --git a/drivers/plugins/extra-params/extra-params.go b/drivers/plugins/extra-params/extra-params.go index 32e81304..7b509f31 100644 --- a/drivers/plugins/extra-params/extra-params.go +++ b/drivers/plugins/extra-params/extra-params.go @@ -5,9 +5,12 @@ import ( "errors" "fmt" "mime" + "net/http" "strconv" "strings" + "github.com/ohler55/ojg/jp" + http_context "github.com/eolinker/apinto/node/http-context" "github.com/eolinker/eosc" "github.com/eolinker/eosc/eocontext" @@ -17,6 +20,10 @@ import ( var _ http_service.HttpFilter = (*ExtraParams)(nil) var _ eocontext.IFilter = (*ExtraParams)(nil) +var ( + errorExist = "%s: %s is already exists" +) + type ExtraParams struct { *Driver id string @@ -84,7 +91,11 @@ func (e *ExtraParams) access(ctx http_service.IHttpContext) (int, error) { } case "body": { - if strings.Contains(contentType, http_context.FormData) || strings.Contains(contentType, http_context.MultipartForm) { + if ctx.Proxy().Method() != http.MethodPost && ctx.Proxy().Method() != http.MethodPut && ctx.Proxy().Method() != http.MethodPatch { + continue + } + switch contentType { + case http_context.FormData, http_context.MultipartForm: if _, has := formParams[param.Name]; has { switch param.Conflict { case paramConvert: @@ -98,23 +109,57 @@ func (e *ExtraParams) access(ctx http_service.IHttpContext) (int, error) { } else { formParams[param.Name] = []string{paramValue.(string)} } - } else if strings.Contains(contentType, http_context.JSON) { - if _, has := bodyParams[param.Name]; has { + case http_context.JSON: + { + key := param.Name + if !strings.HasPrefix(param.Name, "$.") { + key = "$." + key + } + x, err := jp.ParseString(key) + if err != nil { + return 400, fmt.Errorf("parse key error: %v", err) + } switch param.Conflict { case paramConvert: - bodyParams[param.Name] = paramValue.(string) - case paramOrigin: - case paramError: - return clientErrStatusCode, errors.New(`[extra_params] "` + param.Name + `" has a conflict.`) - default: - bodyParams[param.Name] = paramValue + err = x.Set(bodyParams, param.Value) + if err != nil { + return 400, fmt.Errorf("set additional json param error: %v", err) + } + case paramOrigin, paramError: + { + result := x.Get(bodyParams) + if len(result) < 1 { + err = x.Set(bodyParams, param.Value) + if err != nil { + return 400, fmt.Errorf("set additional json param error: %v", err) + } + } + if param.Conflict == paramError { + return 400, fmt.Errorf(errorExist, param.Position, param.Name) + } + } } - } else { - bodyParams[param.Name] = paramValue } } - } + //if strings.Contains(contentType, http_context.FormData) || strings.Contains(contentType, http_context.MultipartForm) { + // + //} else if strings.Contains(contentType, ) { + // if _, has := bodyParams[param.Name]; has { + // switch param.Conflict { + // case paramConvert: + // bodyParams[param.Name] = paramValue.(string) + // case paramOrigin: + // case paramError: + // return clientErrStatusCode, errors.New(`[extra_params] "` + param.Name + `" has a conflict.`) + // default: + // bodyParams[param.Name] = paramValue + // } + // } else { + // bodyParams[param.Name] = paramValue + // } + //} + } } if strings.Contains(contentType, http_context.FormData) || strings.Contains(contentType, http_context.MultipartForm) { diff --git a/drivers/plugins/extra-params/util.go b/drivers/plugins/extra-params/util.go index 3bea2030..fc5b93a7 100644 --- a/drivers/plugins/extra-params/util.go +++ b/drivers/plugins/extra-params/util.go @@ -5,8 +5,11 @@ import ( "errors" "fmt" "mime" + "net/http" "strings" + "github.com/ohler55/ojg/oj" + http_context "github.com/eolinker/apinto/node/http-context" http_service "github.com/eolinker/eosc/eocontext/http-context" ) @@ -42,9 +45,11 @@ func encodeErr(ent string, origin string, statusCode int) error { return fmt.Errorf("%s statusCode: %d", origin, statusCode) } -func parseBodyParams(ctx http_service.IHttpContext) (map[string]interface{}, map[string][]string, error) { +func parseBodyParams(ctx http_service.IHttpContext) (interface{}, map[string][]string, error) { + if ctx.Proxy().Method() != http.MethodPost && ctx.Proxy().Method() != http.MethodPut && ctx.Proxy().Method() != http.MethodPatch { + return nil, nil, nil + } contentType, _, _ := mime.ParseMediaType(ctx.Proxy().Body().ContentType()) - switch contentType { case http_context.FormData, http_context.MultipartForm: formParams, err := ctx.Proxy().Body().BodyForm() @@ -57,15 +62,40 @@ func parseBodyParams(ctx http_service.IHttpContext) (map[string]interface{}, map if err != nil { return nil, nil, err } - var bodyParams map[string]interface{} - err = json.Unmarshal(body, &bodyParams) - if err != nil { - return bodyParams, nil, err + if string(body) == "" { + body = []byte("{}") } + bodyParams, err := oj.Parse(body) + return bodyParams, nil, err } - return nil, nil, errors.New("[params_transformer] unsupported content-type: " + contentType) + return nil, nil, errors.New("unsupported content-type: " + contentType) } +// +//func parseBodyParams(ctx http_service.IHttpContext) (map[string]interface{}, map[string][]string, error) { +// contentType, _, _ := mime.ParseMediaType(ctx.Proxy().Body().ContentType()) +// +// switch contentType { +// case http_context.FormData, http_context.MultipartForm: +// formParams, err := ctx.Proxy().Body().BodyForm() +// if err != nil { +// return nil, nil, err +// } +// return nil, formParams, nil +// case http_context.JSON: +// body, err := ctx.Proxy().Body().RawBody() +// if err != nil { +// return nil, nil, err +// } +// var bodyParams map[string]interface{} +// err = json.Unmarshal(body, &bodyParams) +// if err != nil { +// return bodyParams, nil, err +// } +// } +// return nil, nil, errors.New("[params_transformer] unsupported content-type: " + contentType) +//} + func getHeaderValue(headers map[string][]string, param *ExtraParam, value string) (string, error) { paramName := ConvertHeaderKey(param.Name)