diff --git a/lib/plugins/bodyParser.js b/lib/plugins/bodyParser.js index e42331fed..753929ce0 100644 --- a/lib/plugins/bodyParser.js +++ b/lib/plugins/bodyParser.js @@ -10,6 +10,7 @@ var jsonParser = require('./jsonBodyParser'); var formParser = require('./formBodyParser'); var multipartParser = require('./multipartBodyParser'); var fieldedTextParser = require('./fieldedTextBodyParser.js'); +var isExtendedJsonContentType = require('../plugins/utils/isExtendedJson'); ///--- Globals @@ -162,11 +163,7 @@ function bodyParser(options) { var parser; var type = req.contentType().toLowerCase(); - var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); - // map any +json to application/json - if (jsonPatternMatcher.test(type)) { - type = 'application/json'; - } + if (isExtendedJsonContentType(type)) type = 'application/json'; switch (type) { case 'application/json': diff --git a/lib/plugins/jsonBodyParser.js b/lib/plugins/jsonBodyParser.js index 97b20eaed..9450f139a 100644 --- a/lib/plugins/jsonBodyParser.js +++ b/lib/plugins/jsonBodyParser.js @@ -6,6 +6,7 @@ var assert = require('assert-plus'); var errors = require('restify-errors'); var bodyReader = require('./bodyReader'); +var isExtendedJsonContentType = require('../plugins/utils/isExtendedJson'); ///--- API @@ -28,11 +29,10 @@ function jsonBodyParser(options) { // save original body on req.rawBody and req._body req.rawBody = req._body = req.body; - var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); var contentType = req.getContentType(); if (contentType !== 'application/json' || !req.body) { - if (!jsonPatternMatcher.test(contentType)) { + if (!isExtendedJsonContentType(contentType)) { return next(); } } diff --git a/lib/plugins/utils/isExtendedJson.js b/lib/plugins/utils/isExtendedJson.js new file mode 100644 index 000000000..6f4193291 --- /dev/null +++ b/lib/plugins/utils/isExtendedJson.js @@ -0,0 +1,22 @@ +// Copyright 2018 Ifiok Idiang. All rights reserved. + +'use strict'; + +/** + * Return the stripped content type string if it is a valid JSON extended type + * + * @public + * @function validateExtendedJsonContentType + * @param {String} contentType - the content type string to validate + * @returns {Boolean} validity flag + */ +function validateExtendedJsonContentType(contentType) { + var type = contentType.toLowerCase(); + // map any +json to application/json + var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); + return jsonPatternMatcher.test(type); +} + +///--- Exports + +module.exports = validateExtendedJsonContentType;