Skip to content
Myr

Fix handling very large stacks of sync middleware

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

closes #4891

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 92c5ce5 · Browse files · 5 files+56 added−0 removed
This commit

State

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

Diff stats

5 files+56 added−0 removed

  • tests:+38 added−0 removed
  • other:+18 added−0 removed

5 files+56 added−0 removed

modified:lib/router/index.js+8 added
140 unmodified lines
141 var idx = 0;142 var protohost = getProtohost(req.url) || ''143 var removed = '';144 var slashAdded = false;145 var sync = 0146 var paramcalled = {};147148 // store options for OPTIONS request149 // only used if OPTIONS request
53 unmodified lines
203 setImmediate(done, layerError);204 return;205 }206207 // max sync stack208 if (++sync > 100) {209 return setImmediate(next, err)210 }211212 // get pathname of request213 var path = getPathname(req);214215 if (path == null) {
110 unmodified lines
326 layer.handle_error(layerError, req, res, next);327 } else {328 layer.handle_request(req, res, next);329 }330331 sync = 0332 }333};334335/**
340 unmodified lines
modified:lib/router/route.js+9 added
96 unmodified lines
9798Route.prototype.dispatch = function dispatch(req, res, done) {99 var idx = 0;100 var stack = this.stack;101 var sync = 0102103 if (stack.length === 0) {104 return done();105 }106
21 unmodified lines
128 if (!layer) {129 return done(err);130 }131132 // max sync stack133 if (++sync > 100) {134 return setImmediate(next, err)135 }136137 if (layer.method && layer.method !== method) {138 return next(err);139 }140
1 unmodified line
142 layer.handle_error(err, req, res, next);143 } else {144 layer.handle_request(req, res, next);145 }146147 sync = 0148 }149};150151/**
74 unmodified lines
modified:test/Route.js+22 added
11 unmodified lines
12 var route = new Route('/foo')13 route.dispatch(req, {}, done)14 })1516 it('should not stack overflow with a large sync stack', function (done) {17 this.timeout(5000) // long-running test1819 var req = { method: 'GET', url: '/' }20 var route = new Route('/foo')2122 for (var i = 0; i < 6000; i++) {23 route.all(function (req, res, next) { next() })24 }2526 route.get(function (req, res, next) {27 req.called = true28 next()29 })3031 route.dispatch(req, {}, function (err) {32 if (err) return done(err)33 assert.ok(req.called)34 done()35 })36 })3738 describe('.all', function(){39 it('should add handler', function(done){40 var req = { method: 'GET', url: '/' };41 var route = new Route('/foo');
224 unmodified lines
modified:test/Router.js+16 added
74 unmodified lines
7576 router.handle({ url: '/', method: 'GET' }, { end: done });77 });7879 it('should not stack overflow with a large sync stack', function (done) {80 this.timeout(5000) // long-running test8182 var router = new Router()8384 for (var i = 0; i < 6000; i++) {85 router.use(function (req, res, next) { next() })86 }8788 router.use(function (req, res) {89 res.end()90 })9192 router.handle({ url: '/', method: 'GET' }, { end: done })93 })9495 describe('.handle', function(){96 it('should dispatch', function(done){97 var router = new Router();98
479 unmodified lines
modified:History.md+1 added
3 unmodified lines
4 * Add "root" option to `res.download`5 * Allow `options` without `filename` in `res.download`6 * Deprecate string and non-integer arguments to `res.status`7 * Fix behavior of `null`/`undefined` as `maxAge` in `res.cookie`8 * Fix handling very large stacks of sync middleware9 * Ignore `Object.prototype` values in settings through `app.set`/`app.get`10 * Invoke `default` with same arguments as types in `res.format`11 * Support proper 205 responses using `res.send`12 * Use `http-errors` for `res.format` error
3562 unmodified lines