This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
body_parser.go
320 lines (291 loc) · 7.69 KB
/
body_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2018 tree xie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bodyparser
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/vicanso/elton"
"github.com/vicanso/hes"
)
const (
// ErrCategory body parser error category
ErrCategory = "elton-body-parser"
// 默认为50kb
defaultRequestBodyLimit = 50 * 1024
jsonContentType = "application/json"
formURLEncodedContentType = "application/x-www-form-urlencoded"
)
type (
// Decode body decode function
Decode func(c *elton.Context, originalData []byte) (data []byte, err error)
// Validate body content type check validate function
Validate func(c *elton.Context) bool
// Decoder decoder
Decoder struct {
Decode Decode
Validate Validate
}
// Config json parser config
Config struct {
// Limit the limit size of body
Limit int
// Decoders decode list
Decoders []*Decoder
Skipper elton.Skipper
ContentTypeValidate Validate
}
)
var (
validMethods = []string{
http.MethodPost,
http.MethodPatch,
http.MethodPut,
}
errInvalidJSON = &hes.Error{
Category: ErrCategory,
Message: "invalid json format",
StatusCode: http.StatusBadRequest,
}
jsonBytes = []byte("{}[]")
)
// AddDecoder add decoder
func (conf *Config) AddDecoder(decoder *Decoder) {
if len(conf.Decoders) == 0 {
conf.Decoders = make([]*Decoder, 0)
}
conf.Decoders = append(conf.Decoders, decoder)
}
// NewGzipDecoder new gzip decoder
func NewGzipDecoder() *Decoder {
return &Decoder{
Validate: func(c *elton.Context) bool {
encoding := c.GetRequestHeader(elton.HeaderContentEncoding)
return encoding == elton.Gzip
},
Decode: func(c *elton.Context, originalData []byte) (data []byte, err error) {
c.SetRequestHeader(elton.HeaderContentEncoding, "")
return doGunzip(originalData)
},
}
}
// NewJSONDecoder new json decoder
func NewJSONDecoder() *Decoder {
return &Decoder{
Validate: func(c *elton.Context) bool {
ct := c.GetRequestHeader(elton.HeaderContentType)
ctFields := strings.Split(ct, ";")
return ctFields[0] == jsonContentType
},
Decode: func(c *elton.Context, originalData []byte) (data []byte, err error) {
originalData = bytes.TrimSpace(originalData)
if len(originalData) == 0 {
return nil, nil
}
firstByte := originalData[0]
lastByte := originalData[len(originalData)-1]
if firstByte != jsonBytes[0] && firstByte != jsonBytes[2] {
err = errInvalidJSON
return
}
if firstByte == jsonBytes[0] && lastByte != jsonBytes[1] {
err = errInvalidJSON
return
}
if firstByte == jsonBytes[2] && lastByte != jsonBytes[3] {
err = errInvalidJSON
return
}
return originalData, nil
},
}
}
// NewFormURLEncodedDecoder new form url encode decoder
func NewFormURLEncodedDecoder() *Decoder {
return &Decoder{
Validate: func(c *elton.Context) bool {
ct := c.GetRequestHeader(elton.HeaderContentType)
ctFields := strings.Split(ct, ";")
return ctFields[0] == formURLEncodedContentType
},
Decode: func(c *elton.Context, originalData []byte) (data []byte, err error) {
urlValues, err := url.ParseQuery(string(originalData))
if err != nil {
he := hes.Wrap(err)
he.Exception = true
return nil, he
}
arr := make([]string, 0, len(urlValues))
for key, values := range urlValues {
if len(values) < 2 {
arr = append(arr, fmt.Sprintf(`"%s":"%s"`, key, values[0]))
continue
}
tmpArr := []string{}
for _, v := range values {
tmpArr = append(tmpArr, `"`+v+`"`)
}
arr = append(arr, fmt.Sprintf(`"%s":[%s]`, key, strings.Join(tmpArr, ",")))
}
data = []byte("{" + strings.Join(arr, ",") + "}")
return data, nil
},
}
}
// DefaultJSONContentTypeValidate default json content type validate
func DefaultJSONContentTypeValidate(c *elton.Context) bool {
ct := c.GetRequestHeader(elton.HeaderContentType)
return strings.HasPrefix(ct, jsonContentType)
}
// DefaultJSONAndFormContentTypeValidate default json and form content type validate
func DefaultJSONAndFormContentTypeValidate(c *elton.Context) bool {
ct := c.GetRequestHeader(elton.HeaderContentType)
if strings.HasPrefix(ct, jsonContentType) {
return true
}
return strings.HasPrefix(ct, formURLEncodedContentType)
}
// NewDefault create a default body parser, default limit and only json parser
func NewDefault() elton.Handler {
conf := Config{}
conf.AddDecoder(NewGzipDecoder())
conf.AddDecoder(NewJSONDecoder())
return New(conf)
}
// doGunzip gunzip
func doGunzip(buf []byte) ([]byte, error) {
r, err := gzip.NewReader(bytes.NewBuffer(buf))
if err != nil {
return nil, err
}
defer r.Close()
return ioutil.ReadAll(r)
}
type maxBytesReader struct {
r io.ReadCloser // underlying reader
n int64 // max bytes remaining
err error // sticky error
}
func (l *maxBytesReader) Read(p []byte) (n int, err error) {
if l.err != nil {
return 0, l.err
}
if len(p) == 0 {
return 0, nil
}
// If they asked for a 32KB byte read but only 5 bytes are
// remaining, no need to read 32KB. 6 bytes will answer the
// question of the whether we hit the limit or go past it.
if int64(len(p)) > l.n+1 {
p = p[:l.n+1]
}
n, err = l.r.Read(p)
if int64(n) <= l.n {
l.n -= int64(n)
l.err = err
return n, err
}
l.err = fmt.Errorf("request body is too large, it should be <= %d", l.n)
n = int(l.n)
l.n = 0
return n, l.err
}
func (l *maxBytesReader) Close() error {
return l.r.Close()
}
func MaxBytesReader(r io.ReadCloser, n int64) *maxBytesReader {
return &maxBytesReader{
n: n,
r: r,
}
}
// New create a body parser
func New(config Config) elton.Handler {
limit := defaultRequestBodyLimit
if config.Limit != 0 {
limit = config.Limit
}
skipper := config.Skipper
if skipper == nil {
skipper = elton.DefaultSkipper
}
contentTypeValidate := config.ContentTypeValidate
if contentTypeValidate == nil {
contentTypeValidate = DefaultJSONContentTypeValidate
}
return func(c *elton.Context) (err error) {
if skipper(c) || c.RequestBody != nil || !contentTypeValidate(c) {
return c.Next()
}
method := c.Request.Method
// 对于非提交数据的method跳过
valid := false
for _, item := range validMethods {
if item == method {
valid = true
break
}
}
if !valid {
return c.Next()
}
// 如果request body为空,则表示未读取数据
if c.RequestBody == nil {
r := c.Request.Body
if limit > 0 {
r = MaxBytesReader(r, int64(limit))
}
defer r.Close()
body, e := ioutil.ReadAll(r)
if e != nil {
// IO 读取失败的认为是 exception
err = &hes.Error{
Exception: true,
StatusCode: http.StatusInternalServerError,
Message: e.Error(),
Category: ErrCategory,
Err: e,
}
return
}
c.Request.Body.Close()
c.RequestBody = body
}
body := c.RequestBody
decodeList := make([]Decode, 0)
for _, decoder := range config.Decoders {
if decoder.Validate(c) {
decodeList = append(decodeList, decoder.Decode)
break
}
}
// 没有符合条件的解码
if len(decodeList) == 0 {
return c.Next()
}
for _, decode := range decodeList {
body, err = decode(c, body)
if err != nil {
return
}
}
c.RequestBody = body
return c.Next()
}
}