-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (40 loc) · 1.09 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
'use strict';
var _ = require('lodash');
var parseFormData = function (str) {
var pieces = str.split(/------WebKitFormBoundary.*/);
// remove empties
pieces = _.compact(pieces);
var parts;
var name;
var value;
var fields = {};
_.each(pieces, function (item) {
if (item.indexOf('Content-Disposition: form-data;') !== -1) {
parts = item.split(/\n/)
//["", "Content-Disposition: form-data; name="(name)"", "", "(value)", ""]
name = parts[1].split(/\"/)[1];
parts.pop();
value = parts.pop();
fields[name] = value
}
});
return fields;
}
module.exports = function (req, res, next) {
if (req.headers['content-type'] !== 'application/x-www-form-urlencoded') {
next();
return;
}
var body = '';
req.on('data', function (data) {
body += data;
// Too much POST data, kill the connection!
if (body.length > 1e6)
req.connection.destroy();
});
req.on('end', function () {
var fields = parseFormData(body);
req.body = req.body ? _.extend(req.body, fields) : fields;
next();
});
}