forked from expressjs/body-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (152 loc) · 3.78 KB
/
index.js
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
var bytes = require('bytes');
var getBody = require('raw-body');
var typeis = require('type-is');
var http = require('http');
var qs = require('qs');
var firstcharRegExp = /^\s*(.)/
exports = module.exports = bodyParser;
exports.json = json;
exports.urlencoded = urlencoded;
function bodyParser(options){
var opts = {}
options = options || {}
// exclude type option
for (var prop in options) {
if ('type' !== prop) {
opts[prop] = options[prop]
}
}
var _urlencoded = urlencoded(opts)
var _json = json(opts)
return function bodyParser(req, res, next) {
_json(req, res, function(err){
if (err) return next(err);
_urlencoded(req, res, next);
});
}
}
function json(options){
options = options || {};
var limit = typeof options.limit !== 'number'
? bytes(options.limit || '100kb')
: options.limit;
var reviver = options.reviver
var strict = options.strict !== false;
var type = options.type || 'json';
var verify = options.verify || false
if (verify !== false && typeof verify !== 'function') {
throw new TypeError('option verify must be function')
}
function parse(str) {
if (0 === str.length) {
throw new Error('invalid json, empty body')
}
if (strict) {
var first = firstchar(str)
if (first !== '{' && first !== '[') {
throw new Error('invalid json')
}
}
return JSON.parse(str, reviver)
}
return function jsonParser(req, res, next) {
if (req._body) return next();
req.body = req.body || {}
if (!typeis(req, type)) return next();
// read
read(req, res, next, parse, {
limit: limit,
verify: verify
})
}
}
function urlencoded(options){
options = options || {};
var limit = typeof options.limit !== 'number'
? bytes(options.limit || '100kb')
: options.limit;
var type = options.type || 'urlencoded';
var verify = options.verify || false;
if (verify !== false && typeof verify !== 'function') {
throw new TypeError('option verify must be function')
}
function parse(str) {
return str.length
? qs.parse(str)
: {}
}
return function urlencodedParser(req, res, next) {
if (req._body) return next();
req.body = req.body || {}
if (!typeis(req, type)) return next();
// read
read(req, res, next, parse, {
limit: limit,
verify: verify
})
}
}
function firstchar(str) {
if (!str) return ''
var match = firstcharRegExp.exec(str)
return match ? match[1] : ''
}
function read(req, res, next, parse, options) {
var length = req.headers['content-length']
var waitend = true
// flag as parsed
req._body = true
options = options || {}
options.length = length
var encoding = options.encoding || 'utf-8'
var verify = options.verify
options.encoding = verify
? null
: encoding
req.on('aborted', cleanup)
req.on('end', cleanup)
req.on('error', cleanup)
// read body
getBody(req, options, function (err, body) {
if (err && waitend && req.readable) {
// read off entire request
req.resume()
req.once('end', function onEnd() {
next(err)
})
return
}
if (err) {
next(err)
return
}
var str
// verify
if (verify) {
try {
verify(req, res, body, encoding)
} catch (err) {
if (!err.status) err.status = 403
return next(err)
}
}
// parse
try {
str = typeof body !== 'string'
? body.toString(encoding)
: body
req.body = parse(str)
} catch (err){
err.body = str
err.status = 400
return next(err)
}
next()
})
function cleanup() {
waitend = false
req.removeListener('aborted', cleanup)
req.removeListener('end', cleanup)
req.removeListener('error', cleanup)
}
}