Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/plugins/bodyParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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':
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/jsonBodyParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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();
}
}
Expand Down
22 changes: 22 additions & 0 deletions lib/plugins/utils/isExtendedJson.js
Original file line number Diff line number Diff line change
@@ -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');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting to see this RegExp being rebuilt on every call to the function. I built a little test that ran this function with the RegExp declared outside the function and found it to be about 460% faster. Is there a reason to leave it declared in the function rather than as a module var?

Copy link

@mattbishop mattbishop Jun 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the test script if interested:

console.time('RegExp outside');
var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json');
for (var i = 0; i < 100000; i++ ) {
  jsonPatternMatcher.test('application/json');
}
console.timeEnd('RegExp outside');

console.time('RegExp inside');
for (var i = 0; i < 100000; i++ ) {
  var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json');
  jsonPatternMatcher.test('application/json');
}
console.timeEnd('RegExp inside');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason not to! @ifiok seems like something went awry with your branch history - hope you don't mind, but I took a stab over in #1670

return jsonPatternMatcher.test(type);
}

///--- Exports

module.exports = validateExtendedJsonContentType;