Skip to content

Commit debe0a3

Browse files
committed
Fix rebase
1 parent c166e6d commit debe0a3

File tree

1 file changed

+11
-87
lines changed

1 file changed

+11
-87
lines changed

lib/types/urlencoded.js

Lines changed: 11 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@
1515
var createError = require('http-errors')
1616
var debug = require('debug')('body-parser:urlencoded')
1717
var genericParser = require('../..').generic
18+
var qs = require('qs')
1819

1920
/**
2021
* Module exports.
2122
*/
2223

2324
module.exports = urlencoded
2425

25-
/**
26-
* Cache of parser modules.
27-
*/
28-
29-
var parsers = Object.create(null)
30-
3126
/**
3227
* Create a middleware to parse urlencoded bodies.
3328
*
@@ -42,19 +37,15 @@ function urlencoded (options) {
4237
var type = opts.type || 'application/x-www-form-urlencoded'
4338
var charset = opts.charset || 'utf-8'
4439

45-
var queryparse = opts.parser || (
46-
extended
47-
? extendedparser(opts)
48-
: simpleparser(opts)
49-
)
40+
var queryparse = opts.parser || createQueryParser(opts, extended)
5041

5142
return genericParser(opts, {
5243
type: type,
5344
charset: charset,
5445

55-
parse: function parse (buf) {
56-
return buf.length
57-
? queryparse(buf)
46+
parse: function parse (body, encoding) {
47+
return body.length
48+
? queryparse(body, encoding)
5849
: {}
5950
}
6051
})
@@ -66,17 +57,18 @@ function urlencoded (options) {
6657
* @param {object} options
6758
*/
6859

69-
function extendedparser (options) {
60+
function createQueryParser (options, extended) {
7061
var parameterLimit = options.parameterLimit !== undefined
7162
? options.parameterLimit
7263
: 1000
7364
var charsetSentinel = options.charsetSentinel
7465
var interpretNumericEntities = options.interpretNumericEntities
75-
var depth = (options.depth !== undefined ? options.depth : 32)
66+
var depth = extended ? (options.depth !== undefined ? options.depth : 32) : 0
7667

7768
if (isNaN(parameterLimit) || parameterLimit < 1) {
7869
throw new TypeError('option parameterLimit must be a positive number')
7970
}
71+
8072
if (isNaN(depth) || depth < 0) {
8173
throw new TypeError('option depth must be a zero or a positive number')
8274
}
@@ -85,8 +77,6 @@ function extendedparser (options) {
8577
parameterLimit = parameterLimit | 0
8678
}
8779

88-
var parse = parser('qs')
89-
9080
return function queryparse (body, encoding) {
9181
var paramCount = parameterCount(body, parameterLimit)
9282

@@ -97,11 +87,11 @@ function extendedparser (options) {
9787
})
9888
}
9989

100-
var arrayLimit = Math.max(100, paramCount)
90+
var arrayLimit = extended ? Math.max(100, paramCount) : 0
10191

102-
debug('parse extended urlencoding')
92+
debug('parse ' + (extended ? 'extended ' : '') + 'urlencoding')
10393
try {
104-
return parse(body, {
94+
return qs.parse(body, {
10595
allowPrototypes: true,
10696
arrayLimit: arrayLimit,
10797
depth: depth,
@@ -146,69 +136,3 @@ function parameterCount (body, limit) {
146136

147137
return count
148138
}
149-
150-
/**
151-
* Get parser for module name dynamically.
152-
*
153-
* @param {string} name
154-
* @return {function}
155-
* @api private
156-
*/
157-
158-
function parser (name) {
159-
var mod = parsers[name]
160-
161-
if (mod !== undefined) {
162-
return mod.parse
163-
}
164-
165-
// this uses a switch for static require analysis
166-
switch (name) {
167-
case 'qs':
168-
mod = require('qs')
169-
break
170-
case 'querystring':
171-
mod = require('querystring')
172-
break
173-
}
174-
175-
// store to prevent invoking require()
176-
parsers[name] = mod
177-
178-
return mod.parse
179-
}
180-
181-
/**
182-
* Get the simple query parser.
183-
*
184-
* @param {object} options
185-
*/
186-
187-
function simpleparser (options) {
188-
var parameterLimit = options.parameterLimit !== undefined
189-
? options.parameterLimit
190-
: 1000
191-
var parse = parser('querystring')
192-
193-
if (isNaN(parameterLimit) || parameterLimit < 1) {
194-
throw new TypeError('option parameterLimit must be a positive number')
195-
}
196-
197-
if (isFinite(parameterLimit)) {
198-
parameterLimit = parameterLimit | 0
199-
}
200-
201-
return function queryparse (body) {
202-
var paramCount = parameterCount(body, parameterLimit)
203-
204-
if (paramCount === undefined) {
205-
debug('too many parameters')
206-
throw createError(413, 'too many parameters', {
207-
type: 'parameters.too.many'
208-
})
209-
}
210-
211-
debug('parse urlencoding')
212-
return parse(body, undefined, undefined, { maxKeys: parameterLimit })
213-
}
214-
}

0 commit comments

Comments
 (0)