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

Avoid unnecessary isUtf8 check on HTTP payloads

This commit is contained in:
Nick O'Leary 2015-10-05 21:10:32 +01:00
parent 21b25ffaee
commit c232bf5ed6

View File

@ -30,34 +30,34 @@ module.exports = function(RED) {
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'])
var isText = true;
var checkUTF = false;
if (req.headers['content-type']) {
var parsedType = typer.parse(req.headers['content-type'])
if (parsedType.type === "text") {
isText = true;
} else if (parsedType.subtype === "xml" || parsedType.suffix === "xml") {
isText = true;
} else if (parsedType.type !== "application") {
isText = false;
} else if (parsedType.subtype !== "octet-stream") {
checkUTF = true;
}
}
getBody(req, {
length: req.headers['content-length'],
encoding: textType ? "utf8" : null
encoding: isText ? "utf8" : null
}, function (err, buf) {
if (err) { return next(err); }
if (!textType && isUtf8(buf)) {
buf = buf.toString()
if (!isText && checkUTF && isUtf8(buf)) {
buf = buf.toString()
}
req.body = buf;