diff --git a/README.md b/README.md index cfc162b..d6944a5 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# cod-body-parser +# elton-body-parser -[![Build Status](https://img.shields.io/travis/vicanso/cod-body-parser.svg?label=linux+build)](https://travis-ci.org/vicanso/cod-body-parser) +[![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 cod. It support `application/json` and `application/x-www-form-urlencoded` type, but `NewDefault` just support `application/json`. +Body parser for elton. It support `application/json` and `application/x-www-form-urleneltoned` type, but `NewDefault` just support `application/json`. ```go package main @@ -10,16 +10,16 @@ package main import ( "bytes" - "github.com/vicanso/cod" - bodyparser "github.com/vicanso/cod-body-parser" + "github.com/vicanso/elton" + bodyparser "github.com/vicanso/elton-body-parser" ) func main() { - d := cod.New() + d := elton.New() d.Use(bodyparser.NewDefault()) - d.POST("/user/login", func(c *cod.Context) (err error) { + d.POST("/user/login", func(c *elton.Context) (err error) { c.BodyBuffer = bytes.NewBuffer(c.RequestBody) return }) diff --git a/body_parser.go b/body_parser.go index 633ac8d..8fb365a 100644 --- a/body_parser.go +++ b/body_parser.go @@ -23,33 +23,33 @@ import ( "net/url" "strings" - "github.com/vicanso/cod" + "github.com/vicanso/elton" "github.com/vicanso/hes" ) const ( // ErrCategory body parser error category - ErrCategory = "cod-body-parser" + ErrCategory = "elton-body-parser" // 默认为50kb - defaultRequestBodyLimit = 50 * 1024 - jsonContentType = "application/json" - formURLEncodedContentType = "application/x-www-form-urlencoded" + defaultRequestBodyLimit = 50 * 1024 + jsonContentType = "application/json" + formURLEneltonedContentType = "application/x-www-form-urleneltoned" ) type ( - // Decode body decode function - Decode func(c *cod.Context, originalData []byte) (data []byte, err error) + // Deeltone body deeltone function + Deeltone 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 - // IgnoreFormURLEncoded ignore form url encoded type - IgnoreFormURLEncoded bool - // Decode decode function - Decode Decode - Skipper cod.Skipper + // IgnoreFormURLEneltoned ignore form url eneltoned type + IgnoreFormURLEneltoned bool + // Deeltone deeltone function + Deeltone Deeltone + Skipper elton.Skipper } ) @@ -61,21 +61,21 @@ var ( } ) -// DefaultDecode default decode -func DefaultDecode(c *cod.Context, data []byte) ([]byte, error) { - encoding := c.GetRequestHeader(cod.HeaderContentEncoding) - if encoding == cod.Gzip { - c.SetRequestHeader(cod.HeaderContentEncoding, "") +// DefaultDeeltone default deeltone +func DefaultDeeltone(c *elton.Context, data []byte) ([]byte, error) { + eneltoning := c.GetRequestHeader(elton.HeaderContentEncoding) + if eneltoning == elton.Gzip { + c.SetRequestHeader(elton.HeaderContentEncoding, "") return doGunzip(data) } return data, nil } // NewDefault create a default body parser, default limit and only json parser -func NewDefault() cod.Handler { +func NewDefault() elton.Handler { return New(Config{ - IgnoreFormURLEncoded: true, - Decode: DefaultDecode, + IgnoreFormURLEneltoned: true, + Deeltone: DefaultDeeltone, }) } @@ -90,16 +90,16 @@ func doGunzip(buf []byte) ([]byte, error) { } // New create a body parser -func New(config Config) cod.Handler { +func New(config Config) elton.Handler { limit := defaultRequestBodyLimit if config.Limit != 0 { limit = config.Limit } skipper := config.Skipper if skipper == nil { - skipper = cod.DefaultSkipper + skipper = elton.DefaultSkipper } - return func(c *cod.Context) (err error) { + return func(c *elton.Context) (err error) { if skipper(c) || c.RequestBody != nil { return c.Next() } @@ -116,14 +116,14 @@ func New(config Config) cod.Handler { if !valid { return c.Next() } - ct := c.GetRequestHeader(cod.HeaderContentType) + ct := c.GetRequestHeader(elton.HeaderContentType) ctFields := strings.Split(ct, ";") // 非json则跳过 isJSON := ctFields[0] == jsonContentType - isFormURLEncoded := ctFields[0] == formURLEncodedContentType + isFormURLEneltoned := ctFields[0] == formURLEneltonedContentType - // 如果不是 json 也不是 form url encoded,则跳过 - if !isJSON && !isFormURLEncoded { + // 如果不是 json 也不是 form url eneltoned,则跳过 + if !isJSON && !isFormURLEneltoned { return c.Next() } // 如果数据类型json,而且中间件不处理,则跳过 @@ -131,8 +131,8 @@ func New(config Config) cod.Handler { return c.Next() } - // 如果数据类型form url encoded,而且中间件不处理,则跳过 - if isFormURLEncoded && config.IgnoreFormURLEncoded { + // 如果数据类型form url eneltoned,而且中间件不处理,则跳过 + if isFormURLEneltoned && config.IgnoreFormURLEneltoned { return c.Next() } @@ -157,15 +157,15 @@ func New(config Config) cod.Handler { } return } - if config.Decode != nil { - // 对提交数据做decode处理(如编码转换等) - body, err = config.Decode(c, body) + if config.Deeltone != nil { + // 对提交数据做deeltone处理(如编码转换等) + body, err = config.Deeltone(c, body) if err != nil { return } } - // 将form url encoded 数据转化为json - if isFormURLEncoded { + // 将form url eneltoned 数据转化为json + if isFormURLEneltoned { data, err := url.ParseQuery(string(body)) if err != nil { he := hes.Wrap(err) diff --git a/body_parser_test.go b/body_parser_test.go index b218ed8..8a61c52 100644 --- a/body_parser_test.go +++ b/body_parser_test.go @@ -12,7 +12,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/vicanso/cod" + "github.com/vicanso/elton" "github.com/vicanso/hes" ) @@ -40,7 +40,7 @@ func NewErrorReadCloser(err error) io.ReadCloser { return r } -func TestDefaultDecode(t *testing.T) { +func TestDefaultDeeltone(t *testing.T) { assert := assert.New(t) originalBuf := []byte("abcdabcdabcd") var b bytes.Buffer @@ -49,14 +49,14 @@ func TestDefaultDecode(t *testing.T) { assert.Nil(err) w.Close() - c := cod.NewContext(httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil)) + c := elton.NewContext(httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil)) - buf, err := DefaultDecode(c, b.Bytes()) + buf, err := DefaultDeeltone(c, b.Bytes()) assert.Nil(err) assert.Equal(b.Bytes(), buf) - c.SetRequestHeader(cod.HeaderContentEncoding, cod.Gzip) - buf, err = DefaultDecode(c, b.Bytes()) + c.SetRequestHeader(elton.HeaderContentEncoding, elton.Gzip) + buf, err = DefaultDeeltone(c, b.Bytes()) assert.Nil(err) assert.Equal(originalBuf, buf) } @@ -65,15 +65,15 @@ func TestBodyParser(t *testing.T) { t.Run("skip", func(t *testing.T) { assert := assert.New(t) bodyParser := New(Config{ - Skipper: func(c *cod.Context) bool { + Skipper: func(c *elton.Context) bool { return true }, }) body := `{"name": "tree.xie"}` req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader(body)) - req.Header.Set(cod.HeaderContentType, "application/json") - c := cod.NewContext(nil, req) + req.Header.Set(elton.HeaderContentType, "application/json") + c := elton.NewContext(nil, req) done := false c.Next = func() error { done = true @@ -91,8 +91,8 @@ func TestBodyParser(t *testing.T) { body := `{"name": "tree.xie"}` req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader(body)) - req.Header.Set(cod.HeaderContentType, "application/json") - c := cod.NewContext(nil, req) + req.Header.Set(elton.HeaderContentType, "application/json") + c := elton.NewContext(nil, req) done := false c.Next = func() error { done = true @@ -110,7 +110,7 @@ func TestBodyParser(t *testing.T) { assert := assert.New(t) bodyParser := New(Config{}) req := httptest.NewRequest("GET", "https://aslant.site/", nil) - c := cod.NewContext(nil, req) + c := elton.NewContext(nil, req) done := false c.Next = func() error { done = true @@ -125,7 +125,7 @@ func TestBodyParser(t *testing.T) { assert := assert.New(t) bodyParser := New(Config{}) req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader("abc")) - c := cod.NewContext(nil, req) + c := elton.NewContext(nil, req) done := false c.Next = func() error { done = true @@ -140,11 +140,11 @@ func TestBodyParser(t *testing.T) { assert := assert.New(t) bodyParser := New(Config{}) req := httptest.NewRequest("POST", "https://aslant.site/", NewErrorReadCloser(hes.New("abc"))) - req.Header.Set(cod.HeaderContentType, "application/json") - c := cod.NewContext(nil, req) + req.Header.Set(elton.HeaderContentType, "application/json") + c := elton.NewContext(nil, req) err := bodyParser(c) assert.NotNil(err) - assert.Equal(err.Error(), "category=cod-body-parser, message=message=abc") + assert.Equal(err.Error(), "category=elton-body-parser, message=message=abc") }) t.Run("body over limit size", func(t *testing.T) { @@ -153,11 +153,11 @@ func TestBodyParser(t *testing.T) { Limit: 1, }) req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader("abc")) - req.Header.Set(cod.HeaderContentType, "application/json") - c := cod.NewContext(nil, req) + req.Header.Set(elton.HeaderContentType, "application/json") + c := elton.NewContext(nil, req) err := bodyParser(c) assert.NotNil(err) - assert.Equal(err.Error(), "category=cod-body-parser, message=request body is 3 bytes, it should be <= 1") + assert.Equal(err.Error(), "category=elton-body-parser, message=request body is 3 bytes, it should be <= 1") }) t.Run("ignore json and content type is json", func(t *testing.T) { @@ -166,8 +166,8 @@ func TestBodyParser(t *testing.T) { IgnoreJSON: true, }) req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader("abc")) - req.Header.Set(cod.HeaderContentType, "application/json") - c := cod.NewContext(nil, req) + req.Header.Set(elton.HeaderContentType, "application/json") + c := elton.NewContext(nil, req) done := false c.Next = func() error { done = true @@ -179,15 +179,15 @@ func TestBodyParser(t *testing.T) { assert.Equal(len(c.RequestBody), 0) }) - t.Run("ignore form url encoded and content type is form url encoded", func(t *testing.T) { + t.Run("ignore form url eneltoned and content type is form url eneltoned", func(t *testing.T) { assert := assert.New(t) bodyParser := New(Config{ - IgnoreFormURLEncoded: true, + IgnoreFormURLEneltoned: true, }) body := `name=tree.xie&type=1` req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader(body)) - req.Header.Set(cod.HeaderContentType, "application/x-www-form-urlencoded") - c := cod.NewContext(nil, req) + req.Header.Set(elton.HeaderContentType, "application/x-www-form-urleneltoned") + c := elton.NewContext(nil, req) done := false c.Next = func() error { done = true @@ -204,8 +204,8 @@ func TestBodyParser(t *testing.T) { bodyParser := New(Config{}) body := `{"name": "tree.xie"}` req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader(body)) - req.Header.Set(cod.HeaderContentType, "application/json") - c := cod.NewContext(nil, req) + req.Header.Set(elton.HeaderContentType, "application/json") + c := elton.NewContext(nil, req) done := false c.Next = func() error { done = true @@ -219,11 +219,11 @@ func TestBodyParser(t *testing.T) { assert.True(done) }) - t.Run("decode data success", func(t *testing.T) { + t.Run("deeltone data success", func(t *testing.T) { assert := assert.New(t) bodyParser := New(Config{ - Decode: func(c *cod.Context, data []byte) ([]byte, error) { - if strings.HasSuffix(c.GetRequestHeader(cod.HeaderContentType), "charset=base64") { + Deeltone: func(c *elton.Context, data []byte) ([]byte, error) { + if strings.HasSuffix(c.GetRequestHeader(elton.HeaderContentType), "charset=base64") { return base64.RawStdEncoding.DecodeString(string(data)) } return data, nil @@ -232,8 +232,8 @@ func TestBodyParser(t *testing.T) { body := `{"name": "tree.xie"}` b64 := base64.RawStdEncoding.EncodeToString([]byte(body)) req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader(b64)) - req.Header.Set(cod.HeaderContentType, "application/json;charset=base64") - c := cod.NewContext(nil, req) + req.Header.Set(elton.HeaderContentType, "application/json;charset=base64") + c := elton.NewContext(nil, req) done := false c.Next = func() error { done = true @@ -247,13 +247,13 @@ func TestBodyParser(t *testing.T) { assert.True(done) }) - t.Run("parse form url encoded success", func(t *testing.T) { + t.Run("parse form url eneltoned 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(cod.HeaderContentType, "application/x-www-form-urlencoded") - c := cod.NewContext(nil, req) + req.Header.Set(elton.HeaderContentType, "application/x-www-form-urleneltoned") + c := elton.NewContext(nil, req) done := false c.Next = func() error { done = true diff --git a/go.mod b/go.mod index 0e8be16..76701da 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,6 @@ go 1.12 require ( github.com/stretchr/testify v1.3.0 - github.com/vicanso/cod v0.1.1 - github.com/vicanso/hes v0.1.4 + github.com/vicanso/elton v0.2.0 + github.com/vicanso/hes v0.2.1 ) diff --git a/go.sum b/go.sum index 4bc57c8..7232154 100644 --- a/go.sum +++ b/go.sum @@ -7,9 +7,13 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/vicanso/cod v0.1.1 h1:l4dbZSGhGRVnrxLbArC/60GXNxDo0O/RlY5z4iHSa7I= -github.com/vicanso/cod v0.1.1/go.mod h1:T5GOazXuYrwwE1qMA0G3zka7NVwwkoL2fYIWNfeEpJw= +github.com/vicanso/elton v0.1.1 h1:l4dbZSGhGRVnrxLbArC/60GXNxDo0O/RlY5z4iHSa7I= +github.com/vicanso/elton v0.1.1/go.mod h1:T5GOazXuYrwwE1qMA0G3zka7NVwwkoL2fYIWNfeEpJw= +github.com/vicanso/elton v0.2.0 h1:QlXgmq6m+9wZN7FeLD25/EhBkl8blzppviVY5U5PTm0= +github.com/vicanso/elton v0.2.0/go.mod h1:ynAUOSkZQ+pFaUsxlG5hYnJFjPpMwz8YyEBPzNh0pSg= github.com/vicanso/hes v0.1.4 h1:n8kG8krvNJF4Sj1PvZOEUzdUsmDSbCcGr8C1nYnoP+o= github.com/vicanso/hes v0.1.4/go.mod h1:bG0UJ3EihDKObFcLLwJYjxHHr9saFllsFcepyDIvFlo= +github.com/vicanso/hes v0.2.1 h1:jRFEADmiQ30koVY/sKwlkhyXM5B3QbVVizLqrjNJgPw= +github.com/vicanso/hes v0.2.1/go.mod h1:QcxOFmFfBQMhASTaLgnFayXYCgevdSeBVprt+o+3eKo= github.com/vicanso/keygrip v0.1.0 h1:/zYzoVIbREAvaxSM7bo3/oSXuuYztaP71dPBfhRoNkM= github.com/vicanso/keygrip v0.1.0/go.mod h1:cI05iOjY00NJ7oH2Z9Zdm9eJPUkpoex3XnEubK78nho=