-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,8 @@ function WebSocketRequest(socket, httpRequest, serverConfig) { | |
this._socketCloseHandler = this._handleSocketCloseBeforeAccept.bind(this); | ||
this.socket.on('end', this._socketCloseHandler); | ||
this.socket.on('close', this._socketCloseHandler); | ||
|
||
this._resolved = false; | ||
} | ||
|
||
util.inherits(WebSocketRequest, EventEmitter); | ||
|
@@ -245,6 +247,8 @@ WebSocketRequest.prototype.parseCookies = function(str) { | |
}; | ||
|
||
WebSocketRequest.prototype.accept = function(acceptedProtocol, allowedOrigin, cookies) { | ||
this._verifyResolution(); | ||
|
||
// TODO: Handle extensions | ||
|
||
var protocolFullCase; | ||
|
@@ -454,6 +458,8 @@ WebSocketRequest.prototype.accept = function(acceptedProtocol, allowedOrigin, co | |
}; | ||
|
||
WebSocketRequest.prototype.reject = function(status, reason, extraHeaders) { | ||
this._verifyResolution(); | ||
|
||
if (typeof(status) !== 'number') { | ||
status = 403; | ||
} | ||
|
@@ -488,6 +494,13 @@ WebSocketRequest.prototype._removeSocketCloseListeners = function() { | |
this.socket.removeListener('close', this._socketCloseHandler); | ||
}; | ||
|
||
WebSocketRequest.prototype._verifyResolution = function() { | ||
if (this._resolved) { | ||
throw new Error('WebSocketRequest may only be accepted or rejected one time.'); | ||
} | ||
this._resolved = true; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
hesalx
|
||
}; | ||
|
||
function cleanupFailedConnection(connection) { | ||
// Since we have to return a connection object even if the socket is | ||
// already dead in order not to break the API, we schedule a 'close' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
var http = require('http'); | ||
var WebSocketServer = require('../../lib/WebSocketServer'); | ||
|
||
var server; | ||
var wsServer; | ||
|
||
function prepare(callback) { | ||
if (typeof(callback) !== 'function') { callback = function(){}; } | ||
server = http.createServer(function(request, response) { | ||
response.writeHead(404); | ||
response.end(); | ||
}); | ||
|
||
wsServer = new WebSocketServer({ | ||
httpServer: server, | ||
autoAcceptConnections: false, | ||
maxReceivedFrameSize: 64*1024*1024, // 64MiB | ||
maxReceivedMessageSize: 64*1024*1024, // 64MiB | ||
fragmentOutgoingMessages: false, | ||
keepalive: false, | ||
disableNagleAlgorithm: false | ||
}); | ||
|
||
server.listen(64321, function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
callback(null, wsServer); | ||
}); | ||
} | ||
|
||
function stopServer() { | ||
try { | ||
wsServer.shutDown(); | ||
server.close(); | ||
} | ||
catch(e) { /* do nothing */ } | ||
} | ||
|
||
module.exports = { | ||
prepare: prepare, | ||
stopServer: stopServer | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
var test = require('tape'); | ||
|
||
var WebSocketClient = require('../../lib/WebSocketClient'); | ||
var server = require('../shared/test-server'); | ||
var stopServer = server.stopServer; | ||
|
||
var testCase = test('Request can only be rejected or accepted once.', function(t) { | ||
t.plan(6); | ||
|
||
server.prepare(function(err, wsServer) { | ||
if (err) { | ||
t.fail('Unable to start test server'); | ||
return t.end(); | ||
} | ||
|
||
wsServer.once('request', firstReq); | ||
connect(2); | ||
|
||
function firstReq(request) { | ||
var accept = request.accept.bind(request, request.requestedProtocols[0], request.origin); | ||
var reject = request.reject.bind(request); | ||
|
||
t.doesNotThrow(accept, 'First call to accept() should succeed.'); | ||
t.throws(accept, 'Second call to accept() should throw.'); | ||
t.throws(reject, 'Call to reject() after accept() should throw.'); | ||
|
||
wsServer.once('request', secondReq); | ||
} | ||
|
||
function secondReq(request) { | ||
var accept = request.accept.bind(request, request.requestedProtocols[0], request.origin); | ||
var reject = request.reject.bind(request); | ||
|
||
t.doesNotThrow(reject, 'First call to reject() should succeed.'); | ||
t.throws(reject, 'Second call to reject() should throw.'); | ||
t.throws(accept, 'Call to accept() after reject() should throw.'); | ||
|
||
t.end(); | ||
} | ||
|
||
function connect(numTimes) { | ||
var client; | ||
for (var i=0; i < numTimes; i++) { | ||
client = new WebSocketClient(); | ||
client.connect('ws://localhost:64321/', 'foo'); | ||
client.on('connect', function(connection) { connection.close(); }); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
testCase.on('end', function() { | ||
stopServer(); | ||
}); |
Omg, WebSocketRequest#accept() will throw error in at least 14 places after this