File tree Expand file tree Collapse file tree 3 files changed +27
-11
lines changed Expand file tree Collapse file tree 3 files changed +27
-11
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ var jsonParser = require('./jsonBodyParser');
1010var formParser = require ( './formBodyParser' ) ;
1111var multipartParser = require ( './multipartBodyParser' ) ;
1212var fieldedTextParser = require ( './fieldedTextBodyParser.js' ) ;
13+ var regex = require ( './utils/regex' ) ;
1314
1415///--- Globals
1516
@@ -162,12 +163,6 @@ function bodyParser(options) {
162163 var parser ;
163164 var type = req . contentType ( ) . toLowerCase ( ) ;
164165
165- var jsonPatternMatcher = new RegExp ( '^application/[a-zA-Z.]+\\+json' ) ;
166- // map any +json to application/json
167- if ( jsonPatternMatcher . test ( type ) ) {
168- type = 'application/json' ;
169- }
170-
171166 switch ( type ) {
172167 case 'application/json' :
173168 parser = parseJson [ 0 ] ;
@@ -192,6 +187,18 @@ function bodyParser(options) {
192187 break ;
193188 }
194189
190+ // if we find no matches from the direct string comparisons, perform
191+ // more expensive regex matches. map any +json to application/json.
192+ // theoretically these could be mapped to application/json prior to the
193+ // switch statement, but putting it here allows us to skip the regex
194+ // entirely unless absolutely necessary. additional types could be
195+ // added later at some point.
196+ if ( ! parser ) {
197+ if ( regex . jsonContentType . test ( type ) ) {
198+ parser = parseJson [ 0 ] ;
199+ }
200+ }
201+
195202 if ( parser ) {
196203 parser ( req , res , next ) ;
197204 } else if ( opts && opts . rejectUnknown ) {
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ var assert = require('assert-plus');
66var errors = require ( 'restify-errors' ) ;
77
88var bodyReader = require ( './bodyReader' ) ;
9+ var regex = require ( './utils/regex' ) ;
910
1011///--- API
1112
@@ -28,13 +29,16 @@ function jsonBodyParser(options) {
2829 // save original body on req.rawBody and req._body
2930 req . rawBody = req . _body = req . body ;
3031
31- var jsonPatternMatcher = new RegExp ( '^application/[a-zA-Z.]+\\+json' ) ;
3232 var contentType = req . getContentType ( ) ;
3333
34- if ( contentType !== 'application/json' || ! req . body ) {
35- if ( ! jsonPatternMatcher . test ( contentType ) ) {
36- return next ( ) ;
37- }
34+ // check for empty body first, don't pay regex tax unless necessary.
35+ // for content type, check for exact match and any of the *+json types
36+ if (
37+ ! req . body ||
38+ ( contentType !== 'application/json' &&
39+ ! regex . jsonContentType . test ( contentType ) )
40+ ) {
41+ return next ( ) ;
3842 }
3943
4044 var params ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ module . exports = {
4+ jsonContentType : new RegExp ( '^application/[a-zA-Z.]+\\+json' )
5+ } ;
You can’t perform that action at this time.
0 commit comments