Skip to content

Commit

Permalink
make internal extended: true array limit dynamic
Browse files Browse the repository at this point in the history
closes #42
closes #43
closes #46
closes #58
closes #60
closes #61
closes #65
  • Loading branch information
dougwilson committed Nov 22, 2014
1 parent 810c089 commit e54219e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* make internal `extended: true` array limit dynamic

1.9.3 / 2014-11-21
==================

Expand Down
32 changes: 16 additions & 16 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,35 +110,33 @@ function extendedparser(options) {
parameterLimit = parameterLimit | 0
}

var opts = {
arrayLimit: 100,
parameterLimit: parameterLimit
}

return function queryparse(body) {
if (overlimit(body, parameterLimit)) {
var paramCount = parameterCount(body, parameterLimit)

if (paramCount === undefined) {
var err = new Error('too many parameters')
err.status = 413
throw err
}

return parse(body, opts)
var arrayLimit = Math.max(100, paramCount)

return parse(body, {
arrayLimit: arrayLimit,
parameterLimit: parameterLimit
})
}
}

/**
* Determine if the parameter count is over the limit.
* Count the number of parameters, stopping once limit reached
*
* @param {string} body
* @param {number} limit
* @api private
*/

function overlimit(body, limit) {
if (limit === Infinity) {
return false
}

function parameterCount(body, limit) {
var count = 0
var index = 0

Expand All @@ -147,11 +145,11 @@ function overlimit(body, limit) {
index++

if (count === limit) {
return true
return undefined
}
}

return false
return count
}

/**
Expand Down Expand Up @@ -196,7 +194,9 @@ function simpleparser(options) {
}

return function queryparse(body) {
if (overlimit(body, parameterLimit)) {
var paramCount = parameterCount(body, parameterLimit)

if (paramCount === undefined) {
var err = new Error('too many parameters')
err.status = 413
throw err
Expand Down
28 changes: 28 additions & 0 deletions test/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ describe('bodyParser.urlencoded()', function(){
.expect(200, '{"user":{"name":{"first":"Tobi"}}}', done)
})

it('should parse array index notation', function(done){
request(server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('foo[0]=bar&foo[1]=baz')
.expect(200, '{"foo":["bar","baz"]}', done)
})

it('should parse array index notation with large array', function(done){
var str = 'f[0]=0'

for (var i = 1; i < 500; i++) {
str += '&f[' + i + ']=' + i.toString(16)
}

request(server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send(str)
.expect(function (res) {
var obj = JSON.parse(res.text)
assert.equal(Object.keys(obj).length, 1)
assert.equal(Array.isArray(obj.f), true)
assert.equal(obj.f.length, 500)
})
.expect(200, done)
})

it('should parse array of objects syntax', function(done){
request(server)
.post('/')
Expand Down

0 comments on commit e54219e

Please sign in to comment.