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
Provenance
- Commit
ec8daf0- Tree
f402b8f- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
5312a99
3 files changed+112 added−64 removed
modified:lib/router/index.js+96 added-64 removed
167 unmodified lines168 var layerError = err === 'route'169 ? null170 : err;171 172- var layer = stack[idx++];173-172+ // remove added slash173 if (slashAdded) {174 req.url = req.url.substr(1);175 slashAdded = false;176 }177 178+ // 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 }184 185- if (!layer) {185+ // no more matching layers186+ if (idx >= stack.length) {187 setImmediate(done, layerError);188 return;189 }190 190- 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);193+194+ if (path == null) {195+ return done(layerError);196+ }197+198+ // find next matching layer199+ var layer;200+ var match;201+ var route;202+203+ while (match !== true && idx < stack.length) {204+ layer = stack[idx++];205+ match = matchLayer(layer, path);206+ route = layer.route;207+208+ if (typeof match !== 'boolean') {209+ // hold on to layerError210+ layerError = layerError || match;211 }212 195- // route object and not middleware196- var route = layer.route;213+ if (match !== true) {214+ continue;215+ }216 198- // 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+ }221 205- 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+ }227 208- // 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);230 213- // 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+ }235 218- // 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+ }242 222- // 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+ }247 228- // 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+ }252 234- 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;258 238- 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+ }264+265+ if (route) {266+ return layer.handle_request(req, res, next);267+ }268+269+ trim_prefix(layer, layerError, layerPath, path);270 });271 }272 273 function trim_prefix(layer, layerError, layerPath, path) {29 unmodified lines303 }304 };305 306 /**277- * Match request to a layer.278- *279- * @api private280- */281-282-proto.match_layer = function match_layer(layer, req, res, done) {283- var error = null;284- var path;285-286- try {287- path = parseUrl(req).pathname;288-289- if (!layer.match(path)) {290- path = undefined;291- }292- } catch (err) {293- error = err;294- }295-296- done(error, path);297-};298-299-/**307 * Process any parameters for the layer.308 *309 * @api private310 */197 unmodified lines508 }509 }510 }511 512+// get pathname of request513+function getPathname(req) {514+ try {515+ return parseUrl(req).pathname;516+ } catch (err) {517+ return undefined;518+ }519+}520+521 // get type for error message522 function gettype(obj) {523 var type = typeof obj;524 5 unmodified lines530 return toString.call(obj)531 .replace(objectRegExp, '$1');532 }533 534+/**535+ * Match path to a layer.536+ *537+ * @param {Layer} layer538+ * @param {string} path539+ * @private540+ */541+542+function matchLayer(layer, path) {543+ try {544+ return layer.match(path);545+ } catch (err) {546+ return err;547+ }548+}549+550 // merge params with parent params551 function mergeParams(params, parent) {552 if (typeof parent !== 'object' || !parent) {553 return params;76 unmodified linesmodified:test/Router.js+15 added
51 unmodified lines52 53 router.handle({ url: '', method: 'GET' }, {}, done);54 });55 56+ 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();59+60+ for (var i = 0; i < 6000; i++) {61+ router.get('/thing' + i, handler)62+ }63+64+ router.get('/', function (req, res) {65+ res.end();66+ });67+68+ router.handle({ url: '/', method: 'GET' }, { end: done });69+ });70+71 describe('.handle', function(){72 it('should dispatch', function(done){73 var router = new Router();74 431 unmodified linesmodified:History.md+1 added
2 unmodified lines3 4 * 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 overflows8 9 4.10.8 / 2015-01-1310 ===================11 2546 unmodified lines