Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

allow verifyClient to run asynchronously #27

Merged
merged 1 commit into from

3 participants

@karlseguin

If the verifyClient method has 2 parameters, then it's assumed that the 2nd parameter is a callback. This allows verifyClient to asynchronous code - say, looking up an id in the database.

@einaros
Owner

Thanks, I'll merge this (and add the same for the Hixie transport).

@einaros einaros merged commit 4bc6355 into websockets:master
@3rd-Eden

This test isn't async, it's still sync.

To make it async you should wrap the cb(false) in a process.nextTick or setTImeout.

Owner

Pullreq! ;)

Owner

Hardcore forking action on it's way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 23, 2012
  1. allow verifyClient to run asynchronously

    Karl Seguin authored
This page is out of date. Refresh to see the latest.
Showing with 73 additions and 2 deletions.
  1. +15 −2 lib/WebSocketServer.js
  2. +58 −0 test/WebSocketServer.test.js
View
17 lib/WebSocketServer.js
@@ -172,19 +172,32 @@ function handleHybiUpgrade(req, socket, upgradeHead, cb) {
var origin = version < 13 ?
req.headers['sec-websocket-origin'] :
req.headers['origin'];
+
+ var args = [req, socket, upgradeHead, version, cb];
if (typeof this.options.verifyClient == 'function') {
var info = {
origin: origin,
secure: typeof req.connection.encrypted !== 'undefined',
req: req
};
- if (!this.options.verifyClient(info)) {
+ if (this.options.verifyClient.length == 2) {
+ var _this = this;
+ this.options.verifyClient(info, function(result) {
+ if (!result) abortConnection(socket, 401, 'Unauthorized')
+ else completeUpgrade.apply(_this, args);
+ });
+ return;
+ }
+ else if (!this.options.verifyClient(info)) {
abortConnection(socket, 401, 'Unauthorized');
return;
}
}
+ completeUpgrade.apply(this, args)
+}
- var protocol = req.headers['sec-websocket-protocol'];
+function completeUpgrade(req, socket, upgradeHead, version, cb) {
+ var protocol = req.headers['sec-websocket-protocol'];
// calc key
var key = req.headers['sec-websocket-key'];
View
58 test/WebSocketServer.test.js
@@ -502,6 +502,64 @@ describe('WebSocketServer', function() {
});
});
+ it('client can be denied asynchronously', function(done) {
+ var wss = new WebSocketServer({port: ++port, verifyClient: function(o, cb) {
+ cb(false);
+ }}, function() {
+ var options = {
+ port: port,
+ host: '127.0.0.1',
+ headers: {
+ 'Connection': 'Upgrade',
+ 'Upgrade': 'websocket',
+ 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',
+ 'Sec-WebSocket-Version': 8,
+ 'Sec-WebSocket-Origin': 'http://foobar.com'
+ }
+ };
+ var req = http.request(options);
+ req.end();
+ req.on('response', function(res) {
+ res.statusCode.should.eql(401);
+ process.nextTick(function() {
+ wss.close();
+ done();
+ });
+ });
+ });
+ wss.on('connection', function(ws) {
+ done(new Error('connection must not be established'));
+ });
+ wss.on('error', function() {});
+ });
+
+ it('client can be accepted asynchronously', function(done) {
+ var wss = new WebSocketServer({port: ++port, verifyClient: function(o, cb) {
+ cb(true);
+ }}, function() {
+ var options = {
+ port: port,
+ host: '127.0.0.1',
+ headers: {
+ 'Connection': 'Upgrade',
+ 'Upgrade': 'websocket',
+ 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',
+ 'Sec-WebSocket-Version': 13,
+ 'Origin': 'http://foobar.com'
+ }
+ };
+ var req = http.request(options);
+ req.end();
+ });
+ wss.on('connection', function(ws) {
+ ws.terminate();
+ wss.close();
+ done();
+ });
+ wss.on('error', function() {});
+ });
+
+
it('handles messages passed along with the upgrade request (upgrade head)', function(done) {
var wss = new WebSocketServer({port: ++port, verifyClient: function(o) {
return true;
Something went wrong with that request. Please try again.