From 126c0115ec0e716ac25d040664b1a11c0808886e Mon Sep 17 00:00:00 2001 From: Alex Liu Date: Sun, 27 May 2018 16:28:14 -0700 Subject: [PATCH 1/2] chore: lift json regex to top of file --- lib/plugins/bodyParser.js | 15 +++++++++------ lib/plugins/jsonBodyParser.js | 16 +++++++++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/plugins/bodyParser.js b/lib/plugins/bodyParser.js index e42331fed..7b20eea2e 100644 --- a/lib/plugins/bodyParser.js +++ b/lib/plugins/bodyParser.js @@ -14,6 +14,7 @@ var fieldedTextParser = require('./fieldedTextBodyParser.js'); ///--- Globals var UnsupportedMediaTypeError = errors.UnsupportedMediaTypeError; +var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); ///--- API @@ -162,12 +163,6 @@ 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'; - } - switch (type) { case 'application/json': parser = parseJson[0]; @@ -192,6 +187,14 @@ function bodyParser(options) { break; } + // if still no match, do more expensive regex matches. map any +json to + // application/json. + if (!parser) { + if (jsonPatternMatcher.test(type)) { + parser = parseJson[0]; + } + } + if (parser) { parser(req, res, next); } else if (opts && opts.rejectUnknown) { diff --git a/lib/plugins/jsonBodyParser.js b/lib/plugins/jsonBodyParser.js index 97b20eaed..da1b42084 100644 --- a/lib/plugins/jsonBodyParser.js +++ b/lib/plugins/jsonBodyParser.js @@ -7,6 +7,9 @@ var errors = require('restify-errors'); var bodyReader = require('./bodyReader'); +///---local globals +var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); + ///--- API /** @@ -28,13 +31,16 @@ 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)) { - return next(); - } + // check for empty body first, don't pay regex tax unless necessary. + // for content type, check for exact match and any of the *+json types + if ( + !req.body || + (contentType !== 'application/json' && + !jsonPatternMatcher.test(contentType)) + ) { + return next(); } var params; From 160c2659bec2f07e3315cda661e0fad5f9b16d75 Mon Sep 17 00:00:00 2001 From: Alex Liu Date: Tue, 5 Jun 2018 17:28:43 -0700 Subject: [PATCH 2/2] fix: pr comments --- lib/plugins/bodyParser.js | 12 ++++++++---- lib/plugins/jsonBodyParser.js | 6 ++---- lib/plugins/utils/regex.js | 5 +++++ 3 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 lib/plugins/utils/regex.js diff --git a/lib/plugins/bodyParser.js b/lib/plugins/bodyParser.js index 7b20eea2e..aaf4d5fac 100644 --- a/lib/plugins/bodyParser.js +++ b/lib/plugins/bodyParser.js @@ -10,11 +10,11 @@ var jsonParser = require('./jsonBodyParser'); var formParser = require('./formBodyParser'); var multipartParser = require('./multipartBodyParser'); var fieldedTextParser = require('./fieldedTextBodyParser.js'); +var regex = require('./utils/regex'); ///--- Globals var UnsupportedMediaTypeError = errors.UnsupportedMediaTypeError; -var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); ///--- API @@ -187,10 +187,14 @@ function bodyParser(options) { break; } - // if still no match, do more expensive regex matches. map any +json to - // application/json. + // if we find no matches from the direct string comparisons, perform + // more expensive regex matches. map any +json to application/json. + // theoretically these could be mapped to application/json prior to the + // switch statement, but putting it here allows us to skip the regex + // entirely unless absolutely necessary. additional types could be + // added later at some point. if (!parser) { - if (jsonPatternMatcher.test(type)) { + if (regex.jsonContentType.test(type)) { parser = parseJson[0]; } } diff --git a/lib/plugins/jsonBodyParser.js b/lib/plugins/jsonBodyParser.js index da1b42084..ebc7f8a8b 100644 --- a/lib/plugins/jsonBodyParser.js +++ b/lib/plugins/jsonBodyParser.js @@ -6,9 +6,7 @@ var assert = require('assert-plus'); var errors = require('restify-errors'); var bodyReader = require('./bodyReader'); - -///---local globals -var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); +var regex = require('./utils/regex'); ///--- API @@ -38,7 +36,7 @@ function jsonBodyParser(options) { if ( !req.body || (contentType !== 'application/json' && - !jsonPatternMatcher.test(contentType)) + !regex.jsonContentType.test(contentType)) ) { return next(); } diff --git a/lib/plugins/utils/regex.js b/lib/plugins/utils/regex.js new file mode 100644 index 000000000..13aa439b7 --- /dev/null +++ b/lib/plugins/utils/regex.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = { + jsonContentType: new RegExp('^application/[a-zA-Z.]+\\+json') +};