Skip to content

Commit

Permalink
Merge pull request #681 from LordMajestros/master
Browse files Browse the repository at this point in the history
Fix for #680 (#680)
  • Loading branch information
3rd-Eden committed Mar 29, 2016
2 parents 8490213 + ecc5b17 commit 1242a8c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/Receiver.hixie.js
Expand Up @@ -47,6 +47,7 @@ module.exports = Receiver;
*/

Receiver.prototype.add = function(data) {
if (this.dead) return;
var self = this;
function doAdd() {
if (self.state === EMPTY) {
Expand Down Expand Up @@ -153,8 +154,17 @@ Receiver.prototype.parse = function() {
*/

Receiver.prototype.error = function (reason, terminate) {
if (this.dead) return;
this.reset();
this.onerror(reason, terminate);
if(typeof reason == 'string'){
this.onerror(new Error(reason), terminate);
}
else if(reason.constructor == Error){
this.onerror(reason, terminate);
}
else{
this.onerror(new Error("An error occured"),terminate);
}
return this;
};

Expand Down
12 changes: 11 additions & 1 deletion lib/Receiver.js
Expand Up @@ -83,6 +83,7 @@ module.exports = Receiver;
*/

Receiver.prototype.add = function(data) {
if (this.dead) return;
var dataLength = data.length;
if (dataLength == 0) return;
if (this.expectBuffer == null) {
Expand Down Expand Up @@ -327,8 +328,17 @@ Receiver.prototype.concatBuffers = function(buffers) {
*/

Receiver.prototype.error = function (reason, protocolErrorCode) {
if (this.dead) return;
this.reset();
this.onerror(reason, protocolErrorCode);
if(typeof reason == 'string'){
this.onerror(new Error(reason), protocolErrorCode);
}
else if(reason.constructor == Error){
this.onerror(reason, protocolErrorCode);
}
else{
this.onerror(new Error("An error occured"),protocolErrorCode);
}
return this;
};

Expand Down
26 changes: 26 additions & 0 deletions test/Receiver.test.js
Expand Up @@ -382,6 +382,32 @@ describe('Receiver', function() {
});
});
});
it('will not crash if another message is received after receiving a message that exceeds maxpayload', function(done) {
var perMessageDeflate = new PerMessageDeflate({},false,2);
perMessageDeflate.accept([{}]);

var p = new Receiver({ 'permessage-deflate': perMessageDeflate },2);
var buf1 = new Buffer('fooooooooooooooooooooooooooooooooooooooooooooooooooooooo');
var buf2 = new Buffer('baaaarrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr');

p.onerror = function(reason,code) {
assert.equal(code, 1009);
};

perMessageDeflate.compress(buf1, false, function(err, compressed1) {
if (err) return done(err);
p.add(new Buffer([0x41, compressed1.length]));
p.add(compressed1);

assert.equal(p.onerror,null);

perMessageDeflate.compress(buf2, true, function(err, compressed2) {
p.add(new Buffer([0x80, compressed2.length]));
p.add(compressed2);
done();
});
});
});
it('can cleanup during consuming data', function(done) {
var perMessageDeflate = new PerMessageDeflate();
perMessageDeflate.accept([{}]);
Expand Down
15 changes: 15 additions & 0 deletions test/WebSocketServer.test.js
Expand Up @@ -185,6 +185,21 @@ describe('WebSocketServer', function() {
}
});
});
it('will not crash when it receives an unhandled opcode', function(done) {
var wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.onerror = function(error) {
done();
};
});

var socket = new WebSocket('ws://127.0.0.1:8080/');

socket.onopen = function() {
socket._socket.write(new Buffer([5]));
socket.send('');
};
});
});

describe('#close', function() {
Expand Down

0 comments on commit 1242a8c

Please sign in to comment.