Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
Update handler form data
Browse files Browse the repository at this point in the history
  • Loading branch information
soulbalz committed Jan 21, 2021
1 parent 736cd70 commit 70b9cc5
Showing 1 changed file with 53 additions and 54 deletions.
107 changes: 53 additions & 54 deletions body_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ResponseError struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Status int `json:"status,omitempty"`
Raw string `json:"raw,omitempty"`
}

// Config the plugin configuration.
Expand All @@ -31,14 +32,6 @@ type Config struct {
Response ResponseError
}

// BodyMatch demonstrates a BodyMatch plugin.
type BodyMatch struct {
name string
next http.Handler
body []SingleBody
response ResponseError
}

// MatchType defines an enum which can be used to specify the match type for the 'contains' config.
type MatchType string

Expand All @@ -52,13 +45,17 @@ const (
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
Body: []SingleBody{},
Response: ResponseError{},
Body: []SingleBody{},
Response: ResponseError{
Code: "400",
Message: "Invalid Request.",
Status: http.StatusBadRequest,
},
}
}

// New created a new BodyMatch plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
func New(ctx context.Context, next http.Handler, config *Config, _ string) (http.Handler, error) {
if len(config.Body) == 0 {
return nil, fmt.Errorf("configuration incorrect, missing body")
}
Expand Down Expand Up @@ -87,59 +84,61 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
}
}

if config.Response.Code == "" {
config.Response.Code = "1034"
}

if config.Response.Message == "" {
config.Response.Message = "Invalid request."
}

if config.Response.Status == 0 {
config.Response.Status = http.StatusBadRequest
}
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
bodyValid := true

return &BodyMatch{
name: name,
next: next,
body: config.Body,
response: config.Response,
}, nil
}
var reqBody map[string]string
err := json.NewDecoder(r.Body).Decode(&reqBody)

func (a *BodyMatch) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
bodyValid := true
if err == nil {
for _, vBody := range config.Body {
reqBodyVal := reqBody[vBody.Name]

var reqBody map[string]string
json.NewDecoder(req.Body).Decode(&reqBody)

for _, vBody := range a.body {
reqBodyVal := reqBody[vBody.Name]
if vBody.IsContains() && reqBodyVal != "" {
bodyValid = checkContains(&reqBodyVal, &vBody)
} else {
bodyValid = checkRequired(&reqBodyVal, &vBody)
}

if vBody.IsContains() && reqBodyVal != "" {
bodyValid = checkContains(&reqBodyVal, &vBody)
if !bodyValid {
break
}
}
} else {
bodyValid = checkRequired(&reqBodyVal, &vBody)
}
r.ParseForm() // Parses the request body
for _, vBody := range config.Body {
reqBodyVal := r.Form.Get(vBody.Name)
if vBody.IsContains() && reqBodyVal != "" {
bodyValid = checkContains(&reqBodyVal, &vBody)
} else {
bodyValid = checkRequired(&reqBodyVal, &vBody)
}

if !bodyValid {
break
if !bodyValid {
break
}
}
}
}

if bodyValid {
a.next.ServeHTTP(rw, req)
} else {
s := fmt.Sprintf(`{
"data": null,
"error": {
"code": "%s",
"message": "%s"
if bodyValid {
next.ServeHTTP(rw, r)
} else {
var s string
if config.Response.Raw == "" {
s = fmt.Sprintf(`{
"data": null,
"error": {
"code": "%s",
"message": "%s"
}
}`, config.Response.Code, config.Response.Message)
} else {
s = config.Response.Raw
}
}`, a.response.Code, a.response.Message)

http.Error(rw, s, a.response.Status)
}
http.Error(rw, s, config.Response.Status)
}
}), nil
}

func checkContains(requestValue *string, vBody *SingleBody) bool {
Expand Down

0 comments on commit 70b9cc5

Please sign in to comment.