Skip to content

Commit

Permalink
add inflate option to parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 19, 2014
1 parent c4e0cf6 commit 09a8cde
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 4 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ unreleased
* deprecate `urlencoded()` without provided `extended` option
* parsers split into files for reduced mem usage
* support gzip and deflate bodies
- set `inflate: false` to turn off
* deps: [email protected]
- Support all encodings from `iconv-lite`

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Returns middleware that only parses `json`. This parser accepts any Unicode enco
The options are:

- `strict` - only parse objects and arrays. (default: `true`)
- `inflate` - if deflated bodies will be inflated. (default: `true`)
- `limit` - maximum request body size. (default: `<100kb>`)
- `reviver` - passed to `JSON.parse()`
- `type` - request content-type to parse (default: `json`)
Expand All @@ -71,6 +72,7 @@ Returns middleware that parses all bodies as a `Buffer`. This parser supports au

The options are:

- `inflate` - if deflated bodies will be inflated. (default: `true`)
- `limit` - maximum request body size. (default: `<100kb>`)
- `type` - request content-type to parse (default: `application/octet-stream`)
- `verify` - function to verify body content
Expand All @@ -86,6 +88,7 @@ Returns middleware that parses all bodies as a string. This parser supports auto
The options are:

- `defaultCharset` - the default charset to parse as, if not specified in content-type. (default: `utf-8`)
- `inflate` - if deflated bodies will be inflated. (default: `true`)
- `limit` - maximum request body size. (default: `<100kb>`)
- `type` - request content-type to parse (default: `text/plain`)
- `verify` - function to verify body content
Expand All @@ -101,6 +104,7 @@ Returns middleware that only parses `urlencoded` bodies. This parser accepts onl
The options are:

- `extended` - parse extended syntax with the [qs](https://www.npmjs.org/package/qs#readme) module. (default: `true`)
- `inflate` - if deflated bodies will be inflated. (default: `true`)
- `limit` - maximum request body size. (default: `<100kb>`)
- `type` - request content-type to parse (default: `urlencoded`)
- `verify` - function to verify body content
Expand Down
16 changes: 12 additions & 4 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function read(req, res, next, parse, options) {
req._body = true

try {
stream = contentstream(req)
stream = contentstream(req, options.inflate)
length = stream.length
delete stream.length
} catch (err) {
Expand Down Expand Up @@ -112,18 +112,26 @@ function read(req, res, next, parse, options) {
}

/**
* Get the inflated content stream of the request.
* Get the content stream of the request.
*
* @param {object} req
* @param {boolean} [inflate=true]
* @return {object}
* @api private
*/

function contentstream(req) {
function contentstream(req, inflate) {
var encoding = req.headers['content-encoding'] || 'identity'
var err
var length = req.headers['content-length']
var stream

if (inflate === false && encoding !== 'identity') {
err = new Error('content encoding unsupported')
err.status = 415
throw err
}

switch (encoding) {
case 'deflate':
stream = zlib.createInflate()
Expand All @@ -138,7 +146,7 @@ function contentstream(req) {
stream.length = length
break
default:
var err = new Error('unsupported content encoding')
err = new Error('unsupported content encoding')
err.status = 415
throw err
}
Expand Down
2 changes: 2 additions & 0 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function json(options) {
var limit = typeof options.limit !== 'number'
? bytes(options.limit || '100kb')
: options.limit
var inflate = options.inflate !== false
var reviver = options.reviver
var strict = options.strict !== false
var type = options.type || 'json'
Expand Down Expand Up @@ -81,6 +82,7 @@ function json(options) {
// read
read(req, res, next, parse, {
encoding: charset,
inflate: inflate,
limit: limit,
verify: verify
})
Expand Down
2 changes: 2 additions & 0 deletions lib/types/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = raw
function raw(options) {
options = options || {};

var inflate = options.inflate !== false
var limit = typeof options.limit !== 'number'
? bytes(options.limit || '100kb')
: options.limit
Expand All @@ -51,6 +52,7 @@ function raw(options) {
// read
read(req, res, next, parse, {
encoding: null,
inflate: inflate,
limit: limit,
verify: verify
})
Expand Down
2 changes: 2 additions & 0 deletions lib/types/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function text(options) {
options = options || {};

var defaultCharset = options.defaultCharset || 'utf-8'
var inflate = options.inflate !== false
var limit = typeof options.limit !== 'number'
? bytes(options.limit || '100kb')
: options.limit
Expand All @@ -56,6 +57,7 @@ function text(options) {
// read
read(req, res, next, parse, {
encoding: charset,
inflate: inflate,
limit: limit,
verify: verify
})
Expand Down
2 changes: 2 additions & 0 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function urlencoded(options){
}

var extended = options.extended !== false
var inflate = options.inflate !== false
var limit = typeof options.limit !== 'number'
? bytes(options.limit || '100kb')
: options.limit
Expand Down Expand Up @@ -75,6 +76,7 @@ function urlencoded(options){
// read
read(req, res, next, parse, {
encoding: charset,
inflate: inflate,
limit: limit,
verify: verify
})
Expand Down
32 changes: 32 additions & 0 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,38 @@ describe('bodyParser.json()', function(){
})
})

describe('with inflate option', function(){
describe('when false', function(){
var server;
before(function(){
server = createServer({ inflate: false })
})

it('should not accept content-encoding', function(done){
var test = request(server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/json')
test.write(new Buffer('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))
test.expect(415, 'content encoding unsupported', done)
})
})

describe('when true', function(){
var server;
before(function(){
server = createServer({ inflate: true })
})

it('should accept content-encoding', function(done){
var test = request(server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/json')
test.write(new Buffer('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))
test.expect(200, '{"name":"论"}', done)
})
})
})

describe('with type option', function(){
var server;
before(function(){
Expand Down
32 changes: 32 additions & 0 deletions test/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,38 @@ describe('bodyParser.raw()', function(){
})
})

describe('with inflate option', function(){
describe('when false', function(){
var server;
before(function(){
server = createServer({ inflate: false })
})

it('should not accept content-encoding', function(done){
var test = request(server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/octet-stream')
test.write(new Buffer('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))
test.expect(415, 'content encoding unsupported', done)
})
})

describe('when true', function(){
var server;
before(function(){
server = createServer({ inflate: true })
})

it('should accept content-encoding', function(done){
var test = request(server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/octet-stream')
test.write(new Buffer('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))
test.expect(200, 'buf:6e616d653de8aeba', done)
})
})
})

describe('with type option', function(){
var server;
before(function(){
Expand Down
32 changes: 32 additions & 0 deletions test/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,38 @@ describe('bodyParser.text()', function(){
})
})

describe('with inflate option', function(){
describe('when false', function(){
var server;
before(function(){
server = createServer({ inflate: false })
})

it('should not accept content-encoding', function(done){
var test = request(server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'text/plain')
test.write(new Buffer('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))
test.expect(415, 'content encoding unsupported', done)
})
})

describe('when true', function(){
var server;
before(function(){
server = createServer({ inflate: true })
})

it('should accept content-encoding', function(done){
var test = request(server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'text/plain')
test.write(new Buffer('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))
test.expect(200, '"name is 论"', done)
})
})
})

describe('with type option', function(){
var server;
before(function(){
Expand Down
32 changes: 32 additions & 0 deletions test/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,38 @@ describe('bodyParser.urlencoded()', function(){
})
})

describe('with inflate option', function(){
describe('when false', function(){
var server;
before(function(){
server = createServer({ inflate: false })
})

it('should not accept content-encoding', function(done){
var test = request(server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/x-www-form-urlencoded')
test.write(new Buffer('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))
test.expect(415, 'content encoding unsupported', done)
})
})

describe('when true', function(){
var server;
before(function(){
server = createServer({ inflate: true })
})

it('should accept content-encoding', function(done){
var test = request(server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/x-www-form-urlencoded')
test.write(new Buffer('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))
test.expect(200, '{"name":"论"}', done)
})
})
})

describe('with limit option', function(){
it('should 413 when over limit with Content-Length', function(done){
var buf = new Buffer(1024)
Expand Down

0 comments on commit 09a8cde

Please sign in to comment.