LearnBoost/socket.io — pull request #983 by GICodeWarrior (2012-08-06 20:13:57+00:00)
Title: Fix infinite recursion in Websocket parsers.

If a client is feeding messages faster than server can handle them, infinite
recursion occurs. Basically, the "overflow" data gets added to the parser and
it immediately parses a new message.

The fix pushes the processing of the next message (in this edge case) onto the
event queue. This prevents the stack from recursing indefinitely. This also
prevents a fast client from starving other clients.

Example Client:
```javascript
'use strict';

var socket = require('socket.io-client').connect('http://localhost:8765');

socket.on('connect', function() {
for (var i = 0; i < 5000; ++i) {
socket.send('Howdy there!');
}
});
```
Example Server:
```javascript

'use strict';

var io = require('socket.io');
var sio = io.listen(8765);

var connectionCount = 0;
sio.sockets.on('connection', function(socket) {
var messageCount = 0;
console.log('Connection count: ', ++connectionCount);
socket.on('disconnect', function() {
console.log('Connection count: ', --connectionCount);
});

socket.on('message', function() {
if (++messageCount % 100 === 0) {
console.log('Message count (' + socket.id + '): ', ++messageCount);
}
});
});
```
Server output before patch:
```
$ node test5.js
info - socket.io started
debug - client authorized
info - handshake authorized fM1mVGYS6_AL49NykZbJ
debug - setting request GET /socket.io/1/websocket/fM1mVGYS6_AL49NykZbJ
debug - set heartbeat interval for client fM1mVGYS6_AL49NykZbJ
debug - client authorized for
debug - websocket writing 1::
Connection count: 1
Message count (fM1mVGYS6_AL49NykZbJ): 1000
Message count (fM1mVGYS6_AL49NykZbJ): 2000
Message count (fM1mVGYS6_AL49NykZbJ): 3000

buffer.js:281
pool = new SlowBuffer(Buffer.poolSize);
^
RangeError: Maximum call stack size exceeded
```
Server output after patch:
```
$ node test5.js
info - socket.io started
debug - client authorized
info - handshake authorized 7sUkDfi6mPK8ZIzHkLVX
debug - setting request GET /socket.io/1/websocket/7sUkDfi6mPK8ZIzHkLVX
debug - set heartbeat interval for client 7sUkDfi6mPK8ZIzHkLVX
debug - client authorized for
debug - websocket writing 1::
Connection count: 1
Message count (7sUkDfi6mPK8ZIzHkLVX): 1000
Message count (7sUkDfi6mPK8ZIzHkLVX): 2000
Message count (7sUkDfi6mPK8ZIzHkLVX): 3000
Message count (7sUkDfi6mPK8ZIzHkLVX): 4000
Message count (7sUkDfi6mPK8ZIzHkLVX): 5000
```