1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var raw = require('raw-body');
|
7 | var inflate = require('inflation');
|
8 | var qs = require('qs');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | module.exports = function(req, opts){
|
23 | req = req.req || req;
|
24 | opts = opts || {};
|
25 | var queryString = opts.queryString || {};
|
26 |
|
27 |
|
28 | if (queryString.allowDots === undefined) queryString.allowDots = true;
|
29 |
|
30 |
|
31 | var len = req.headers['content-length'];
|
32 | var encoding = req.headers['content-encoding'] || 'identity';
|
33 | if (len && encoding === 'identity') opts.length = ~~len;
|
34 | opts.encoding = opts.encoding || 'utf8';
|
35 | opts.limit = opts.limit || '56kb';
|
36 | opts.qs = opts.qs || qs;
|
37 |
|
38 |
|
39 | return Promise.resolve()
|
40 | .then(function() {
|
41 | return raw(inflate(req), opts);
|
42 | })
|
43 | .then(function(str){
|
44 | try {
|
45 | return opts.qs.parse(str, queryString);
|
46 | } catch (err) {
|
47 | err.status = 400;
|
48 | err.body = str;
|
49 | throw err;
|
50 | }
|
51 | });
|
52 | };
|