diff --git a/nodes/core/io/21-httpin.js b/nodes/core/io/21-httpin.js index 731bbc969..7ea86ac1f 100644 --- a/nodes/core/io/21-httpin.js +++ b/nodes/core/io/21-httpin.js @@ -27,17 +27,39 @@ module.exports = function(RED) { var jsonParser = bodyParser.json(); var urlencParser = bodyParser.urlencoded({extended:true}); 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) { if (req._body) { return next(); } req.body = ""; req._body = true; + + var textType = isMIMETypeText(req.headers['content-type']) + getBody(req, { - limit: '1mb', length: req.headers['content-length'], - encoding: 'utf8' + encoding: textType ? "utf8" : null }, function (err, buf) { if (err) { return next(err); } + if (!textType && isUtf8(buf)) { + buf = buf.toString() + } + req.body = buf; next(); }); diff --git a/package.json b/package.json index ff9d2940e..3758b8f56 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "uglify-js":"2.4.24", "on-headers":"1.0.1", "is-utf8":"0.2.0", + "media-typer": "0.3.0", "fs.notify":"0.0.4", "passport":"0.3.0", "passport-http-bearer":"1.0.1",