Skip to content

Commit

Permalink
🚀 refactor: improve logic method BodyParser
Browse files Browse the repository at this point in the history
  • Loading branch information
renanbastos93 committed Jul 14, 2020
1 parent aa58673 commit d61e478
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
40 changes: 19 additions & 21 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,47 +222,45 @@ func (ctx *Ctx) Body() string {
// application/json, application/xml, application/x-www-form-urlencoded, multipart/form-data
func (ctx *Ctx) BodyParser(out interface{}) error {
// TODO: Create benchmark ( Probably need a sync pool )
var schemaDecoderForm = schema.NewDecoder()
var schemaDecoderQuery = schema.NewDecoder()
schemaDecoderForm.SetAliasTag("form")
schemaDecoderForm.IgnoreUnknownKeys(true)
schemaDecoderQuery.SetAliasTag("query")
schemaDecoderQuery.IgnoreUnknownKeys(true)

// get content type
ctype := getString(ctx.Fasthttp.Request.Header.ContentType())
// application/json
if strings.HasPrefix(ctype, MIMEApplicationJSON) {
var (
schemaDecoder = schema.NewDecoder()
ctype = getString(ctx.Fasthttp.Request.Header.ContentType())
)
schemaDecoder.IgnoreUnknownKeys(true)

switch ctype {
case MIMEApplicationJSON, MIMEApplicationJSONCharsetUTF8:
return json.Unmarshal(ctx.Fasthttp.Request.Body(), out)
}
// application/xml text/xml
if strings.HasPrefix(ctype, MIMEApplicationXML) || strings.HasPrefix(ctype, MIMETextXML) {
case MIMETextXML, MIMETextXMLCharsetUTF8, MIMEApplicationXML, MIMEApplicationXMLCharsetUTF8:
return xml.Unmarshal(ctx.Fasthttp.Request.Body(), out)
}
// application/x-www-form-urlencoded
if strings.HasPrefix(ctype, MIMEApplicationForm) {
case MIMEApplicationForm: // application/x-www-form-urlencoded
schemaDecoder.SetAliasTag("form")
data, err := url.ParseQuery(getString(ctx.Fasthttp.PostBody()))
if err != nil {
return err
}
return schemaDecoderForm.Decode(out, data)
return schemaDecoder.Decode(out, data)
}
// multipart/form-data

// this case is outside switch case because it can have info additional as `boundary=something` in content-type
if strings.HasPrefix(ctype, MIMEMultipartForm) {
schemaDecoder.SetAliasTag("form")
data, err := ctx.Fasthttp.MultipartForm()
if err != nil {
return err
}
return schemaDecoderForm.Decode(out, data.Value)
return schemaDecoder.Decode(out, data.Value)
}

// query params
if ctx.Fasthttp.QueryArgs().Len() > 0 {
schemaDecoder.SetAliasTag("query")
fmt.Println("Parsing query strings using `BodyParser` is deprecated since v1.12.7, please us `ctx.QueryParser` instead")
data := make(map[string][]string)
ctx.Fasthttp.QueryArgs().VisitAll(func(key []byte, val []byte) {
data[getString(key)] = append(data[getString(key)], getString(val))
})
return schemaDecoderQuery.Decode(out, data)
return schemaDecoder.Decode(out, data)
}

return fmt.Errorf("bodyparser: cannot parse content-type: %v", ctype)
Expand Down
1 change: 1 addition & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ const (
MIMETextHTMLCharsetUTF8 = "text/html; charset=utf-8"
MIMETextPlainCharsetUTF8 = "text/plain; charset=utf-8"
MIMEApplicationXMLCharsetUTF8 = "application/xml; charset=utf-8"
MIMEApplicationJSONCharsetUTF8 = "application/json; charset=utf-8"
MIMEApplicationJavaScriptCharsetUTF8 = "application/javascript; charset=utf-8"
)

Expand Down

0 comments on commit d61e478

Please sign in to comment.