Skip to content

Commit

Permalink
consistent header requirements
Browse files Browse the repository at this point in the history
remove redundant test

updated comments

fix typo

support xml bindings

removed uneeded feature

removed unneeded error condition

simplified conditions

undo uneeded changes
  • Loading branch information
rg2011 committed Feb 4, 2024
1 parent da0be9b commit f6ccfa4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 54 deletions.
50 changes: 25 additions & 25 deletions lib/bindings/HTTPBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,37 @@ 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.
const text = bodyParser.text();
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.
// It is useful to remove them from the document,
// 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')
Expand All @@ -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);
};
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
27 changes: 0 additions & 27 deletions test/unit/ngsiv2/contextRequests/singleMeasuresXmlTypes.json

This file was deleted.

0 comments on commit f6ccfa4

Please sign in to comment.