Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
fix: fix form urlencoded
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 18, 2019
1 parent 3178de8 commit cfd1fcb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://img.shields.io/travis/vicanso/elton-body-parser.svg?label=linux+build)](https://travis-ci.org/vicanso/elton-body-parser)

Body parser for elton. It support `application/json` and `application/x-www-form-urleneltoned` type, but `NewDefault` just support `application/json`.
Body parser for elton. It support `application/json` and `application/x-www-form-urlencoded` type, but `NewDefault` just support `application/json`.

```go
package main
Expand Down
52 changes: 26 additions & 26 deletions body_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ const (
// ErrCategory body parser error category
ErrCategory = "elton-body-parser"
// 默认为50kb
defaultRequestBodyLimit = 50 * 1024
jsonContentType = "application/json"
formURLEneltonedContentType = "application/x-www-form-urleneltoned"
defaultRequestBodyLimit = 50 * 1024
jsonContentType = "application/json"
formURLEncodedContentType = "application/x-www-form-urlencoded"
)

type (
// Deeltone body deeltone function
Deeltone func(c *elton.Context, originalData []byte) (data []byte, err error)
// Decode body decode function
Decode func(c *elton.Context, originalData []byte) (data []byte, err error)
// Config json parser config
Config struct {
// Limit the limit size of body
Limit int
// IgnoreJSON ignore json type
IgnoreJSON bool
// IgnoreFormURLEneltoned ignore form url eneltoned type
IgnoreFormURLEneltoned bool
// Deeltone deeltone function
Deeltone Deeltone
Skipper elton.Skipper
// IgnoreFormURLEncoded ignore form url encoded type
IgnoreFormURLEncoded bool
// Decode decode function
Decode Decode
Skipper elton.Skipper
}
)

Expand All @@ -61,10 +61,10 @@ var (
}
)

// DefaultDeeltone default deeltone
func DefaultDeeltone(c *elton.Context, data []byte) ([]byte, error) {
eneltoning := c.GetRequestHeader(elton.HeaderContentEncoding)
if eneltoning == elton.Gzip {
// DefaultDcode default decode
func DefaultDcode(c *elton.Context, data []byte) ([]byte, error) {
encoding := c.GetRequestHeader(elton.HeaderContentEncoding)
if encoding == elton.Gzip {
c.SetRequestHeader(elton.HeaderContentEncoding, "")
return doGunzip(data)
}
Expand All @@ -74,8 +74,8 @@ func DefaultDeeltone(c *elton.Context, data []byte) ([]byte, error) {
// NewDefault create a default body parser, default limit and only json parser
func NewDefault() elton.Handler {
return New(Config{
IgnoreFormURLEneltoned: true,
Deeltone: DefaultDeeltone,
IgnoreFormURLEncoded: true,
Decode: DefaultDcode,
})
}

Expand Down Expand Up @@ -120,19 +120,19 @@ func New(config Config) elton.Handler {
ctFields := strings.Split(ct, ";")
// 非json则跳过
isJSON := ctFields[0] == jsonContentType
isFormURLEneltoned := ctFields[0] == formURLEneltonedContentType
isFormURLEncoded := ctFields[0] == formURLEncodedContentType

// 如果不是 json 也不是 form url eneltoned,则跳过
if !isJSON && !isFormURLEneltoned {
// 如果不是 json 也不是 form url encoded,则跳过
if !isJSON && !isFormURLEncoded {
return c.Next()
}
// 如果数据类型json,而且中间件不处理,则跳过
if isJSON && config.IgnoreJSON {
return c.Next()
}

// 如果数据类型form url eneltoned,而且中间件不处理,则跳过
if isFormURLEneltoned && config.IgnoreFormURLEneltoned {
// 如果数据类型form url encoded,而且中间件不处理,则跳过
if isFormURLEncoded && config.IgnoreFormURLEncoded {
return c.Next()
}

Expand All @@ -157,15 +157,15 @@ func New(config Config) elton.Handler {
}
return
}
if config.Deeltone != nil {
// 对提交数据做deeltone处理(如编码转换等
body, err = config.Deeltone(c, body)
if config.Decode != nil {
// 对提交数据做decode处理(如解压,编码转换等
body, err = config.Decode(c, body)
if err != nil {
return
}
}
// 将form url eneltoned 数据转化为json
if isFormURLEneltoned {
// 将form url encoded 数据转化为json
if isFormURLEncoded {
data, err := url.ParseQuery(string(body))
if err != nil {
he := hes.Wrap(err)
Expand Down
20 changes: 10 additions & 10 deletions body_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewErrorReadCloser(err error) io.ReadCloser {
return r
}

func TestDefaultDeeltone(t *testing.T) {
func TestDefaultDecoded(t *testing.T) {
assert := assert.New(t)
originalBuf := []byte("abcdabcdabcd")
var b bytes.Buffer
Expand All @@ -51,12 +51,12 @@ func TestDefaultDeeltone(t *testing.T) {

c := elton.NewContext(httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil))

buf, err := DefaultDeeltone(c, b.Bytes())
buf, err := DefaultDcode(c, b.Bytes())
assert.Nil(err)
assert.Equal(b.Bytes(), buf)

c.SetRequestHeader(elton.HeaderContentEncoding, elton.Gzip)
buf, err = DefaultDeeltone(c, b.Bytes())
buf, err = DefaultDcode(c, b.Bytes())
assert.Nil(err)
assert.Equal(originalBuf, buf)
}
Expand Down Expand Up @@ -179,14 +179,14 @@ func TestBodyParser(t *testing.T) {
assert.Equal(len(c.RequestBody), 0)
})

t.Run("ignore form url eneltoned and content type is form url eneltoned", func(t *testing.T) {
t.Run("ignore form url encoded and content type is form url encoded", func(t *testing.T) {
assert := assert.New(t)
bodyParser := New(Config{
IgnoreFormURLEneltoned: true,
IgnoreFormURLEncoded: true,
})
body := `name=tree.xie&type=1`
req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader(body))
req.Header.Set(elton.HeaderContentType, "application/x-www-form-urleneltoned")
req.Header.Set(elton.HeaderContentType, "application/x-www-form-urlencoded")
c := elton.NewContext(nil, req)
done := false
c.Next = func() error {
Expand Down Expand Up @@ -219,10 +219,10 @@ func TestBodyParser(t *testing.T) {
assert.True(done)
})

t.Run("deeltone data success", func(t *testing.T) {
t.Run("decode data success", func(t *testing.T) {
assert := assert.New(t)
bodyParser := New(Config{
Deeltone: func(c *elton.Context, data []byte) ([]byte, error) {
Decode: func(c *elton.Context, data []byte) ([]byte, error) {
if strings.HasSuffix(c.GetRequestHeader(elton.HeaderContentType), "charset=base64") {
return base64.RawStdEncoding.DecodeString(string(data))
}
Expand All @@ -247,12 +247,12 @@ func TestBodyParser(t *testing.T) {
assert.True(done)
})

t.Run("parse form url eneltoned success", func(t *testing.T) {
t.Run("parse form url encoded success", func(t *testing.T) {
assert := assert.New(t)
bodyParser := New(Config{})
body := `name=tree.xie&type=1&type=2`
req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader(body))
req.Header.Set(elton.HeaderContentType, "application/x-www-form-urleneltoned")
req.Header.Set(elton.HeaderContentType, "application/x-www-form-urlencoded")
c := elton.NewContext(nil, req)
done := false
c.Next = func() error {
Expand Down

0 comments on commit cfd1fcb

Please sign in to comment.