Skip to content

Commit

Permalink
[minor] Add shouldHandle() method to WebSocketServer
Browse files Browse the repository at this point in the history
Moved the check to see if a `WebSocketServer` instance should handle a
given request into a public method. This will allow people to override
this logic with a patch if they desire.
  • Loading branch information
yaworsw authored and lpinca committed Nov 8, 2016
1 parent ebf86b5 commit 6472425
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions lib/WebSocketServer.js
Expand Up @@ -156,21 +156,35 @@ WebSocketServer.prototype.close = function (callback) {
};

/**
* Handle a HTTP Upgrade request.
* See if a given request should be handled by this server instance.
*
* @api public
* @param {http.IncomingMessage} req Request object to inspect
* @return {Boolean} `true` if the request is valid, else `false`
* @public
*/

WebSocketServer.prototype.handleUpgrade = function (req, socket, upgradeHead, cb) {
// check for wrong path
if (this.options.path) {
var u = url.parse(req.url);
if (u && u.pathname !== this.options.path) {
return abortConnection(socket, 400);
}
WebSocketServer.prototype.shouldHandle = function (req) {
if (this.options.path && url.parse(req.url).pathname !== this.options.path) {
return false;
}

if (!req.headers.upgrade || req.headers.upgrade.toLowerCase() !== 'websocket') {
return true;
};

/**
* Handle a HTTP Upgrade request.
*
* @param {http.IncomingMessage} req The request object
* @param {net.Socket} socket The network socket between the server and client
* @param {Buffer} head The first packet of the upgraded stream
* @param {Function} cb Callback
* @public
*/
WebSocketServer.prototype.handleUpgrade = function (req, socket, head, cb) {
if (
!this.shouldHandle(req) ||
!req.headers.upgrade ||
req.headers.upgrade.toLowerCase() !== 'websocket'
) {
return abortConnection(socket, 400);
}

Expand Down

0 comments on commit 6472425

Please sign in to comment.