Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e4bbf9

Browse files
committed
fix protocol violations and improve error handling (fixes socketio#1880)
1 parent b49f5c8 commit 4e4bbf9

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

lib/client.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ function Client(server, conn){
4242
Client.prototype.setup = function(){
4343
this.onclose = this.onclose.bind(this);
4444
this.ondata = this.ondata.bind(this);
45+
this.onerror = this.onerror.bind(this);
4546
this.ondecoded = this.ondecoded.bind(this);
47+
4648
this.decoder.on('decoded', this.ondecoded);
4749
this.conn.on('data', this.ondata);
50+
this.conn.on('error', this.onerror);
4851
this.conn.on('close', this.onclose);
4952
};
5053

@@ -167,7 +170,12 @@ Client.prototype.packet = function(packet, preEncoded, volatile){
167170
*/
168171

169172
Client.prototype.ondata = function(data){
170-
this.decoder.add(data);
173+
// try/catch is needed for protocol violations (GH-1880)
174+
try {
175+
this.decoder.add(data);
176+
} catch(e) {
177+
this.onerror(e);
178+
}
171179
};
172180

173181
/**
@@ -189,6 +197,20 @@ Client.prototype.ondecoded = function(packet) {
189197
}
190198
};
191199

200+
/**
201+
* Handles an error.
202+
*
203+
* @param {Objcet} error object
204+
* @api private
205+
*/
206+
207+
Client.prototype.onerror = function(err){
208+
this.sockets.forEach(function(socket){
209+
socket.onerror(err);
210+
});
211+
this.onclose('client error');
212+
};
213+
192214
/**
193215
* Called upon transport close.
194216
*
@@ -219,6 +241,7 @@ Client.prototype.onclose = function(reason){
219241

220242
Client.prototype.destroy = function(){
221243
this.conn.removeListener('data', this.ondata);
244+
this.conn.removeListener('error', this.onerror);
222245
this.conn.removeListener('close', this.onclose);
223246
this.decoder.removeListener('decoded', this.ondecoded);
224247
};

lib/socket.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,21 @@ Socket.prototype.ondisconnect = function(){
380380
this.onclose('client namespace disconnect');
381381
};
382382

383+
/**
384+
* Handles a client error.
385+
*
386+
* @api private
387+
*/
388+
389+
Socket.prototype.onerror = function(err){
390+
this.emit('error', err);
391+
};
392+
383393
/**
384394
* Called upon closing. Called by `Client`.
385395
*
386396
* @param {String} reason
397+
* @param {Error} optional error object
387398
* @api private
388399
*/
389400

test/socket.io.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,39 @@ describe('socket.io', function(){
639639
});
640640
});
641641

642+
it('should error with null messages', function(done){
643+
var srv = http();
644+
var sio = io(srv);
645+
srv.listen(function(){
646+
var socket = client(srv);
647+
sio.on('connection', function(s){
648+
s.on('message', function(a){
649+
expect(a).to.be(null);
650+
done();
651+
});
652+
socket.send(null);
653+
});
654+
});
655+
});
656+
657+
it('should handle transport null messages', function(done){
658+
var srv = http();
659+
var sio = io(srv);
660+
srv.listen(function(){
661+
var socket = client(srv);
662+
sio.on('connection', function(s){
663+
s.on('error', function(err){
664+
expect(err).to.be.an(Error);
665+
s.on('disconnect', function(reason){
666+
expect(reason).to.be('client error');
667+
done();
668+
});
669+
});
670+
s.client.ondata(null);
671+
});
672+
});
673+
});
674+
642675
it('should emit events', function(done){
643676
var srv = http();
644677
var sio = io(srv);

0 commit comments

Comments
 (0)