From c232bf5ed6ba60000a8452796fb83106d0b5d82b Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 5 Oct 2015 21:10:32 +0100 Subject: [PATCH] Avoid unnecessary isUtf8 check on HTTP payloads --- nodes/core/io/21-httpin.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/nodes/core/io/21-httpin.js b/nodes/core/io/21-httpin.js index 7ea86ac1f..b98c61153 100644 --- a/nodes/core/io/21-httpin.js +++ b/nodes/core/io/21-httpin.js @@ -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;