Skip to content

Commit

Permalink
updated buffered nonce reading
Browse files Browse the repository at this point in the history
  • Loading branch information
einaros committed Feb 3, 2012
1 parent ce7a93d commit 0739397
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/WebSocketServer.js
Expand Up @@ -313,9 +313,23 @@ function handleHixieUpgrade(req, socket, upgradeHead, cb) {
completeHandshake.call(this, nonce, rest);
}
else {
throw new Error('not supported');
// ... wait for enough data
// call completeHandshake with a slice of the head
var nonce = new Buffer(8);
upgradeHead.copy(nonce, 0);
var received = upgradeHead.length;
var rest = null;
var self = this;
var handler = function (data) {
var toRead = Math.min(data.length, 8 - received);
if (toRead === 0) return;
data.copy(nonce, received, 0, toRead);
received += toRead;
if (received == 8) {
socket.removeListener('data', handler);
if (toRead < data.length) rest = data.slice(toRead);
completeHandshake.call(self, nonce, rest);
}
}
socket.on('data', handler);
}
}

Expand Down

0 comments on commit 0739397

Please sign in to comment.