1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #730 from jthomas/master

Allow HTTP nodes to handle non-UTF8 content.
This commit is contained in:
Nick O'Leary 2015-10-05 20:52:35 +01:00
commit 21b25ffaee
2 changed files with 25 additions and 2 deletions

View File

@ -27,17 +27,39 @@ module.exports = function(RED) {
var jsonParser = bodyParser.json(); var jsonParser = bodyParser.json();
var urlencParser = bodyParser.urlencoded({extended:true}); var urlencParser = bodyParser.urlencoded({extended:true});
var onHeaders = require('on-headers'); var onHeaders = require('on-headers');
var typer = require('media-typer');
var isUtf8 = require('is-utf8');
function isMIMETypeText(mimeType) {
var parsedType = typer.parse(mimeType)
if (parsedType.type === "text") {
return true;
}
if (parsedType.subtype === "xml" || parsedType.suffix === "xml") {
return true;
}
return false;
}
function rawBodyParser(req, res, next) { function rawBodyParser(req, res, next) {
if (req._body) { return next(); } if (req._body) { return next(); }
req.body = ""; req.body = "";
req._body = true; req._body = true;
var textType = isMIMETypeText(req.headers['content-type'])
getBody(req, { getBody(req, {
limit: '1mb',
length: req.headers['content-length'], length: req.headers['content-length'],
encoding: 'utf8' encoding: textType ? "utf8" : null
}, function (err, buf) { }, function (err, buf) {
if (err) { return next(err); } if (err) { return next(err); }
if (!textType && isUtf8(buf)) {
buf = buf.toString()
}
req.body = buf; req.body = buf;
next(); next();
}); });

View File

@ -45,6 +45,7 @@
"uglify-js":"2.4.24", "uglify-js":"2.4.24",
"on-headers":"1.0.1", "on-headers":"1.0.1",
"is-utf8":"0.2.0", "is-utf8":"0.2.0",
"media-typer": "0.3.0",
"fs.notify":"0.0.4", "fs.notify":"0.0.4",
"passport":"0.3.0", "passport":"0.3.0",
"passport-http-bearer":"1.0.1", "passport-http-bearer":"1.0.1",