Skip to content

Commit

Permalink
Merge pull request socketio#160 from mjgil/bug-159-fix
Browse files Browse the repository at this point in the history
adding fix for when websocket is undefined
  • Loading branch information
rauchg committed Apr 6, 2013
2 parents 3159354 + 365aaaa commit af0a457
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
36 changes: 28 additions & 8 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ function Server(opts){
Server.errors = {
UNKNOWN_TRANSPORT: 0,
UNKNOWN_SID: 1,
BAD_HANDSHAKE_METHOD: 2
BAD_HANDSHAKE_METHOD: 2,
BAD_REQUEST: 3
};

Server.errorMessages = {
0: 'Transport unknown',
1: 'Session ID unknown',
2: 'Bad handshake method'
2: 'Bad handshake method',
3: 'Bad request'
};

/**
Expand Down Expand Up @@ -158,11 +160,7 @@ Server.prototype.handleRequest = function(req, res){

var code = this.verify(req);
if (code !== true) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
code: code,
message: Server.errorMessages[code]
}));
sendErrorMessage(res, code);
return this;
}

Expand All @@ -176,6 +174,22 @@ Server.prototype.handleRequest = function(req, res){
return this;
};

/**
* Sends an Engine.IO Error Message
*
* @param {http.ServerResponse} response
* @param {code} error code
* @api private
*/

function sendErrorMessage(res, code) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
code: code,
message: Server.errorMessages[code]
}));
}

/**
* Handshakes a new client.
*
Expand All @@ -189,7 +203,13 @@ Server.prototype.handshake = function(transport, req){

debug('handshaking client "%s"', id);

var transport = new transports[transport](req);
try {
var transport = new transports[transport](req);
}
catch (e) {
sendErrorMessage(req.res, Server.errors.BAD_REQUEST);
return;
}
var socket = new Socket(id, this, transport);
var self = this;

Expand Down
15 changes: 15 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ describe('server', function () {
});
});
});



it('should disallow bad requests', function (done) {
var engine = listen(function (port) {
request.get('http://localhost:%d/engine.io/default/'.s(port))
.query({ transport: 'websocket' })
.end(function (res) {
expect(res.status).to.be(400);
expect(res.body.code).to.be(3);
expect(res.body.message).to.be('Bad request');
done();
});
});
});
});

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

0 comments on commit af0a457

Please sign in to comment.