From d61e4785780fb97df19e611cc9d01f73655a3d50 Mon Sep 17 00:00:00 2001 From: renanbastos93 Date: Mon, 13 Jul 2020 23:41:37 -0300 Subject: [PATCH] :rocket: refactor: improve logic method BodyParser --- ctx.go | 40 +++++++++++++++++++--------------------- utils.go | 1 + 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/ctx.go b/ctx.go index f68eda34b2..3338748ed2 100644 --- a/ctx.go +++ b/ctx.go @@ -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) diff --git a/utils.go b/utils.go index f89c4c5cc9..869f09034f 100644 --- a/utils.go +++ b/utils.go @@ -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" )