Skip to content

Commit

Permalink
Merge remote-tracking branch 'gitlab/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Oct 9, 2022
2 parents 2047f67 + d2304b2 commit e0835f3
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 23 deletions.
3 changes: 0 additions & 3 deletions application/auth/jwt/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"errors"
"reflect"

"github.com/eolinker/eosc/log"

"github.com/eolinker/eosc/utils/schema"

"github.com/eolinker/apinto/application"
Expand Down Expand Up @@ -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")
Expand Down
1 change: 0 additions & 1 deletion drivers/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions drivers/app/extend-param.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
69 changes: 57 additions & 12 deletions drivers/plugins/extra-params/extra-params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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) {
Expand Down
44 changes: 37 additions & 7 deletions drivers/plugins/extra-params/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand All @@ -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)

Expand Down

0 comments on commit e0835f3

Please sign in to comment.