mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Allow HTTP nodes to handle non-UTF8 content.
Setting UTF-8 as the default body encoding stops us sending binary content, e.g. audio, which can be decoded into a Buffer. Use "Content-Type" header to decide whether to decode as UTF-8 or a Buffer.
This commit is contained in:
parent
9932d34304
commit
699a22c757
@ -27,17 +27,39 @@ module.exports = function(RED) {
|
||||
var jsonParser = express.json();
|
||||
var urlencParser = express.urlencoded();
|
||||
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();
|
||||
});
|
||||
|
@ -44,6 +44,7 @@
|
||||
"uglify-js":"2.4.24",
|
||||
"on-headers":"1.0.0",
|
||||
"is-utf8":"0.2.0",
|
||||
"media-typer": "0.3.0",
|
||||
"fs.notify":"0.0.4",
|
||||
"passport":"0.2.2",
|
||||
"passport-http-bearer":"1.0.1",
|
||||
|
Loading…
Reference in New Issue
Block a user