Skip to content
Myr

Match routes iteratively to prevent stack overflows

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fixes #2412

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 5312a99 · Browse files · 3 files+112 added−64 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+112 added−64 removed

  • tests:+15 added−0 removed
  • other:+97 added−64 removed

3 files+112 added−64 removed

modified:lib/router/index.js+96 added-64 removed
167 unmodified lines
168 var layerError = err === 'route'169 ? null170 : err;171172 var layer = stack[idx++];173172 // remove added slash173 if (slashAdded) {174 req.url = req.url.substr(1);175 slashAdded = false;176 }177178 // restore altered req.url179 if (removed.length !== 0) {180 req.baseUrl = parentUrl;181 req.url = protohost + removed + req.url.substr(protohost.length);182 removed = '';183 }184185 if (!layer) {185 // no more matching layers186 if (idx >= stack.length) {187 setImmediate(done, layerError);188 return;189 }190190 self.match_layer(layer, req, res, function (err, path) {191 if (err || path === undefined) {192 return next(layerError || err);191 // get pathname of request192 var path = getPathname(req);193194 if (path == null) {195 return done(layerError);196 }197198 // find next matching layer199 var layer;200 var match;201 var route;202203 while (match !== true && idx < stack.length) {204 layer = stack[idx++];205 match = matchLayer(layer, path);206 route = layer.route;207208 if (typeof match !== 'boolean') {209 // hold on to layerError210 layerError = layerError || match;211 }212195 // route object and not middleware196 var route = layer.route;213 if (match !== true) {214 continue;215 }216198 // if final route, then we support options199 if (route) {200 // we don't run any routes with error first201 if (layerError) {202 return next(layerError);203 }217 if (!route) {218 // process non-route handlers normally219 continue;220 }221205 var method = req.method;206 var has_method = route._handles_method(method);222 if (layerError) {223 // routes do not match with a pending error224 match = false;225 continue;226 }227208 // build up automatic options response209 if (!has_method && method === 'OPTIONS') {210 appendMethods(options, route._options());211 }228 var method = req.method;229 var has_method = route._handles_method(method);230213 // don't even bother214 if (!has_method && method !== 'HEAD') {215 return next();216 }231 // build up automatic options response232 if (!has_method && method === 'OPTIONS') {233 appendMethods(options, route._options());234 }235218 // we can now dispatch to the route219 req.route = route;236 // don't even bother matching route237 if (!has_method && method !== 'HEAD') {238 match = false;239 continue;240 }241 }242222 // Capture one-time layer values223 req.params = self.mergeParams224 ? mergeParams(layer.params, parentParams)225 : layer.params;226 var layerPath = layer.path;243 // no match244 if (match !== true) {245 return done(layerError);246 }247228 // this should be done for the layer229 self.process_params(layer, paramcalled, req, res, function (err) {230 if (err) {231 return next(layerError || err);232 }248 // store route for dispatch on change249 if (route) {250 req.route = route;251 }252234 if (route) {235 return layer.handle_request(req, res, next);236 }253 // Capture one-time layer values254 req.params = self.mergeParams255 ? mergeParams(layer.params, parentParams)256 : layer.params;257 var layerPath = layer.path;258238 trim_prefix(layer, layerError, layerPath, path);239 });259 // this should be done for the layer260 self.process_params(layer, paramcalled, req, res, function (err) {261 if (err) {262 return next(layerError || err);263 }264265 if (route) {266 return layer.handle_request(req, res, next);267 }268269 trim_prefix(layer, layerError, layerPath, path);270 });271 }272273 function trim_prefix(layer, layerError, layerPath, path) {
29 unmodified lines
303 }304};305306/**277 * Match request to a layer.278 *279 * @api private280 */281282proto.match_layer = function match_layer(layer, req, res, done) {283 var error = null;284 var path;285286 try {287 path = parseUrl(req).pathname;288289 if (!layer.match(path)) {290 path = undefined;291 }292 } catch (err) {293 error = err;294 }295296 done(error, path);297};298299/**307 * Process any parameters for the layer.308 *309 * @api private310 */
197 unmodified lines
508 }509 }510}511512// get pathname of request513function getPathname(req) {514 try {515 return parseUrl(req).pathname;516 } catch (err) {517 return undefined;518 }519}520521// get type for error message522function gettype(obj) {523 var type = typeof obj;524
5 unmodified lines
530 return toString.call(obj)531 .replace(objectRegExp, '$1');532}533534/**535 * Match path to a layer.536 *537 * @param {Layer} layer538 * @param {string} path539 * @private540 */541542function matchLayer(layer, path) {543 try {544 return layer.match(path);545 } catch (err) {546 return err;547 }548}549550// merge params with parent params551function mergeParams(params, parent) {552 if (typeof parent !== 'object' || !parent) {553 return params;
76 unmodified lines
modified:test/Router.js+15 added
51 unmodified lines
5253 router.handle({ url: '', method: 'GET' }, {}, done);54 });5556 it('should not stack overflow with many registered routes', function(done){57 var handler = function(req, res){ res.end(new Error('wrong handler')) };58 var router = new Router();5960 for (var i = 0; i < 6000; i++) {61 router.get('/thing' + i, handler)62 }6364 router.get('/', function (req, res) {65 res.end();66 });6768 router.handle({ url: '/', method: 'GET' }, { end: done });69 });7071 describe('.handle', function(){72 it('should dispatch', function(done){73 var router = new Router();74
431 unmodified lines
modified:History.md+1 added
2 unmodified lines
34 * Deprecate leading `:` in `name` for `app.param(name, fn)`5 * Fix `OPTIONS` responses to include the `HEAD` method properly6 * Fix `res.sendFile` not always detecting aborted connection7 * Match routes iteratively to prevent stack overflows894.10.8 / 2015-01-1310===================11
2546 unmodified lines