From f6ccfa41a8cfcbd80659e4c2603dbfa4388b0674 Mon Sep 17 00:00:00 2001 From: rg2011 <52279456+rg2011@users.noreply.github.com> Date: Sun, 4 Feb 2024 09:28:40 +0100 Subject: [PATCH] consistent header requirements remove redundant test updated comments fix typo support xml bindings removed uneeded feature removed unneeded error condition simplified conditions undo uneeded changes --- lib/bindings/HTTPBinding.js | 50 +++++++++---------- package.json | 3 +- .../singleMeasuresXmlTypes.json | 27 ---------- 3 files changed, 26 insertions(+), 54 deletions(-) delete mode 100644 test/unit/ngsiv2/contextRequests/singleMeasuresXmlTypes.json diff --git a/lib/bindings/HTTPBinding.js b/lib/bindings/HTTPBinding.js index d2382375..4e29ac7e 100644 --- a/lib/bindings/HTTPBinding.js +++ b/lib/bindings/HTTPBinding.js @@ -52,7 +52,7 @@ const transport = 'HTTP'; const xml2js = require('xml2js'); const xmlStripPrefix = xml2js.processors.stripPrefix; -const typeis = require('type-is').is; +const typeis = require('type-is'); function parserBody() { const json = bodyParser.json({ strict: false }); // accept anything JSON.parse accepts. @@ -60,8 +60,8 @@ function parserBody() { const raw = bodyParser.raw(); const xml = bodyParser.xml({ // Tell bodyparser-xml to not check the Content-Type header, - // we will check it ourselves. This fixes some upstream bugs, - // see https://github.com/jshttp/type-is/issues/52 + // we will check it ourselves. This works around upstream bug + // https://github.com/jshttp/type-is/issues/52 type: () => true, xmlParseOptions: { // XML namespaces might change from one request to the next. @@ -69,20 +69,20 @@ function parserBody() { // to be able to refer to tags later in JEXL transformations. // See https://github.com/Leonidas-from-XIV/node-xml2js/issues/87 tagNameProcessors: [xmlStripPrefix], - attrNameProcessors: [xmlStripPrefix], - // XML might consider whitespace significant info, - // for json it is just inconvenient. - trim: true + attrNameProcessors: [xmlStripPrefix] } }) // generic bodyParser return function (req, res, next) { + // use typeis.is on a trimmed header, instead of + // req.is on the request, to work around + // https://github.com/jshttp/type-is/issues/52 const contentType = req.get('content-type').split(';').unshift().trim(); - if (typeis(contentType, ['text/plain'])) { + if (typeis.is(contentType, ['text/plain'])) { text(req, res, next) - } else if (typeis(contentType, ['application/octet-stream'])) { + } else if (typeis.is(contentType, ['application/octet-stream'])) { raw(req, res, next) - } else if (typeis(contentType, ['application/soap+xml'])) { + } else if (typeis.is(contentType, ['application/soap+xml'])) { xml(req, res, next) } else { // req.is('json') @@ -91,13 +91,17 @@ function parserBody() { }; } -function checkContentType(...mimeTypes) { +function checkPostContentType(...mimeTypes) { return function (req, res, next) { + let err + // use typeis.is on a trimmed header, instead of + // req.is on the request, to work around + // https://github.com/jshttp/type-is/issues/52 const contentType = req.get('content-type').split(';').unshift().trim(); - if (req.method === 'POST' && !typeis(contentType, mimeTypes)) { - throw new errors.UnsupportedType(mimeTypes.join(', ')); + if (!typeis.hasBody(req) || !typeis.is(contentType, mimeTypes)) { + err = new errors.UnsupportedType(mimeTypes.join(', ')); } - next(); + next(err); }; } @@ -686,9 +690,8 @@ function start(callback) { httpBindingServer.router.post( config.getConfig().iota.defaultResource || constants.HTTP_MEASURE_PATH, - // For historical reasons, this endpoint will accept - // any of the Content-Type headers below - checkContentType('application/json', 'text/plain', 'application/octet-stream'), + // text and octet-stream allowed for backward compatibility + checkPostContentType('application/json', 'text/plain', 'application/octet-stream'), bodyParser.json({ strict: false }), // accept anything JSON.parse accepts checkMandatoryParams(false), parseDataMultipleMeasure, @@ -702,8 +705,7 @@ function start(callback) { '/' + constants.MEASURES_SUFIX + '/:attrValue', - // This is the only method that supports xml payloads too - checkContentType('application/json', 'text/plain', 'application/octet-stream', '*/xml', '+xml'), + checkPostContentType('application/json', 'text/plain', 'application/octet-stream', 'application/soap+xml'), parserBody(), checkMandatoryParams(false), parseData, // non multiple measures are expected in this route @@ -714,9 +716,8 @@ function start(callback) { httpBindingServer.router.post( (config.getConfig().iota.defaultResource || constants.HTTP_MEASURE_PATH) + constants.HTTP_COMMANDS_PATH, - // For historical reasons, this endpoint will accept - // any of the Content-Type headers below - checkContentType('application/json', 'text/plain', 'application/octet-stream'), + // text and octet-stream allowed for backward compatibility + checkPostContentType('application/json', 'text/plain', 'application/octet-stream'), bodyParser.json({ strict: false }), // accept anything JSON.parse accepts. checkMandatoryParams(false), parseData, @@ -728,9 +729,8 @@ function start(callback) { httpBindingServer.router.post( (config.getConfig().iota.defaultResource || constants.HTTP_MEASURE_PATH) + constants.HTTP_CONFIGURATION_PATH, - // For historical reasons, this endpoint will accept - // any of the Content-Type headers below - checkContentType('application/json', 'text/plain', 'application/octet-stream'), + // text and octet-stream allowed for backward compatibility + checkPostContentType('application/json', 'text/plain', 'application/octet-stream'), bodyParser.json({ strict: false }), // accept anything JSON.parse accepts. checkMandatoryParams(false), parseData, diff --git a/package.json b/package.json index 00fa5487..4bab8063 100644 --- a/package.json +++ b/package.json @@ -72,8 +72,7 @@ "logops": "2.1.2", "mqtt": "4.3.7", "sinon": "~6.1.0", - "underscore": "1.12.1", - "xml2js": "0.6.2" + "underscore": "1.12.1" }, "husky": { "hooks": { diff --git a/test/unit/ngsiv2/contextRequests/singleMeasuresXmlTypes.json b/test/unit/ngsiv2/contextRequests/singleMeasuresXmlTypes.json deleted file mode 100644 index cd1f11f8..00000000 --- a/test/unit/ngsiv2/contextRequests/singleMeasuresXmlTypes.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "id": "Second MQTT Device", - "type": "AnMQTTDevice", - "configuration": { - "type": "Object", - "value": { - "settings": { - "$": { - "xmlns:ns": "http://strip.ns" - }, - "single": "value1", - "list": { - "item": [ - "item1", - "item2" - ] - }, - "with": { - "$": { - "attr": "value2" - }, - "_": "and text" - } - } - } - } -} \ No newline at end of file