Skip to content

Commit

Permalink
comments and clarifications
Browse files Browse the repository at this point in the history
  • Loading branch information
einaros committed Feb 4, 2012
1 parent 7400b54 commit e046177
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/WebSocketServer.js
Expand Up @@ -313,23 +313,26 @@ function handleHixieUpgrade(req, socket, upgradeHead, cb) {
cb(client);
}

if (upgradeHead && upgradeHead.length >= 8) {
var nonce = upgradeHead.slice(0, 8);
var rest = upgradeHead.length > 8 ? upgradeHead.slice(8) : null;
var nonceLength = 8;
if (upgradeHead && upgradeHead.length >= nonceLength) {
var nonce = upgradeHead.slice(0, nonceLength);
var rest = upgradeHead.length > nonceLength ? upgradeHead.slice(nonceLength) : null;
completeHandshake.call(this, nonce, rest);
}
else {
var nonce = new Buffer(8);
// nonce not present in upgradeHead, so we must wait for enough data
// data to arrive before continuing
var nonce = new Buffer(nonceLength);
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);
var toRead = Math.min(data.length, nonceLength - received);
if (toRead === 0) return;
data.copy(nonce, received, 0, toRead);
received += toRead;
if (received == 8) {
if (received == nonceLength) {
socket.removeListener('data', handler);
if (toRead < data.length) rest = data.slice(toRead);
completeHandshake.call(self, nonce, rest);
Expand Down

0 comments on commit e046177

Please sign in to comment.