mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-11 16:08:24 -05:00
Compare commits
9 Commits
4.5.4
...
dynamic-na
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
213577204a | ||
|
|
dd72676c25 | ||
|
|
3bf7be73a0 | ||
|
|
0360553c99 | ||
|
|
de5cbdb833 | ||
|
|
0ce0ce1dbc | ||
|
|
cc7ce79251 | ||
|
|
1b77c27f7b | ||
|
|
1f8bb8a0ec |
@@ -59,26 +59,40 @@ Client.prototype.setup = function(){
|
||||
*/
|
||||
|
||||
Client.prototype.connect = function(name){
|
||||
debug('connecting to namespace %s', name);
|
||||
if (!this.server.nsps[name]) {
|
||||
this.packet({ type: parser.ERROR, nsp: name, data : 'Invalid namespace'}, false, false, true);
|
||||
return;
|
||||
}
|
||||
var nsp = this.server.of(name);
|
||||
if ('/' != name && !this.nsps['/']) {
|
||||
this.connectBuffer.push(name);
|
||||
return;
|
||||
}
|
||||
|
||||
var self = this;
|
||||
var socket = nsp.add(this, function(){
|
||||
self.sockets.push(socket);
|
||||
self.nsps[nsp.name] = socket;
|
||||
debug('connecting to namespace %s', name);
|
||||
|
||||
if ('/' == nsp.name && self.connectBuffer.length > 0) {
|
||||
self.connectBuffer.forEach(self.connect, self);
|
||||
self.connectBuffer = [];
|
||||
function connectNamespace() {
|
||||
var nsp = self.server.of(name);
|
||||
if ('/' != name && !self.nsps['/']) {
|
||||
self.connectBuffer.push(name);
|
||||
return;
|
||||
}
|
||||
|
||||
var socket = nsp.add(self, function(){
|
||||
self.sockets.push(socket);
|
||||
self.nsps[nsp.name] = socket;
|
||||
|
||||
if ('/' == nsp.name && self.connectBuffer.length > 0) {
|
||||
self.connectBuffer.forEach(self.connect, self);
|
||||
self.connectBuffer = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (self.server.nsps[name]) {
|
||||
// Namespace already created, connect
|
||||
connectNamespace();
|
||||
return;
|
||||
}
|
||||
|
||||
self.server.checkNamespace(name, function(allow) {
|
||||
if (allow) {
|
||||
connectNamespace();
|
||||
return
|
||||
}
|
||||
|
||||
self.packet({ type: parser.ERROR, nsp: name, data : 'Invalid namespace'});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
48
lib/index.js
48
lib/index.js
@@ -43,6 +43,7 @@ function Server(srv, opts){
|
||||
}
|
||||
opts = opts || {};
|
||||
this.nsps = {};
|
||||
this.nspValidators = [];
|
||||
this.path(opts.path || '/socket.io');
|
||||
this.serveClient(false !== opts.serveClient);
|
||||
this.adapter(opts.adapter || Adapter);
|
||||
@@ -137,6 +138,53 @@ Server.prototype.set = function(key, val){
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets up server middleware to validate incoming namespaces not already created on the server.
|
||||
*
|
||||
* @return {Server} self
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Server.prototype.useNamespaceValidator = function(fn){
|
||||
this.nspValidators.push(fn);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Executes the middleware for an incoming namespace not already created on the server.
|
||||
*
|
||||
* @param name of incomming namespace
|
||||
* @param {Function} last fn call in the middleware
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Server.prototype.checkNamespace = function(name, fn){
|
||||
var fns = this.nspValidators.slice(0);
|
||||
if (!fns.length) return fn(false);
|
||||
|
||||
var namespaceAllowed = false; // Deny unknown namespaces by default
|
||||
|
||||
function run(i){
|
||||
fns[i](name, function(err, allow){
|
||||
// upon error, short-circuit
|
||||
if (err) return fn(false);
|
||||
|
||||
// if one piece of middleware explicitly denies namespace, short-circuit
|
||||
if (allow === false) return fn(false);
|
||||
|
||||
namespaceAllowed = namespaceAllowed || allow === true;
|
||||
|
||||
// if no middleware left, summon callback
|
||||
if (!fns[i + 1]) return fn(namespaceAllowed);
|
||||
|
||||
// go on to next
|
||||
run(i + 1);
|
||||
});
|
||||
}
|
||||
|
||||
run(0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the client serving path.
|
||||
*
|
||||
|
||||
@@ -597,7 +597,7 @@ describe('socket.io', function(){
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should not reuse same-namespace connections', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
@@ -730,6 +730,78 @@ describe('socket.io', function(){
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('should allow connections to dynamic namespaces', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var namespace = '/dynamic';
|
||||
var dynamic = client(srv,namespace);
|
||||
sio.useNamespaceValidator(function(nsp, next) {
|
||||
expect(nsp).to.be(namespace);
|
||||
next(null, true);
|
||||
});
|
||||
dynamic.on('error', function(err) {
|
||||
expect().fail();
|
||||
});
|
||||
dynamic.on('connect', function() {
|
||||
expect(sio.nsps[namespace]).to.be.a(Namespace);
|
||||
expect(sio.nsps[namespace].sockets.length).to.be(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should not allow connections to dynamic namespaces if not supported', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var namespace = '/dynamic';
|
||||
sio.useNamespaceValidator(function(nsp, next) {
|
||||
expect(nsp).to.be(namespace);
|
||||
next(null, false);
|
||||
});
|
||||
sio.on('connect', function(socket) {
|
||||
if (socket.nsp.name === namespace) {
|
||||
expect().fail();
|
||||
}
|
||||
});
|
||||
|
||||
var dynamic = client(srv,namespace);
|
||||
dynamic.on('connect', function(){
|
||||
expect().fail();
|
||||
});
|
||||
dynamic.on('error', function(err) {
|
||||
expect(err).to.be("Invalid namespace");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should not allow connections to dynamic namespaces if there is an error', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var namespace = '/dynamic';
|
||||
sio.useNamespaceValidator(function(nsp, next) {
|
||||
expect(nsp).to.be(namespace);
|
||||
next(new Error(), true);
|
||||
});
|
||||
sio.on('connect', function(socket) {
|
||||
if (socket.nsp.name === namespace) {
|
||||
expect().fail();
|
||||
}
|
||||
});
|
||||
|
||||
var dynamic = client(srv,namespace);
|
||||
dynamic.on('connect', function(){
|
||||
expect().fail();
|
||||
});
|
||||
dynamic.on('error', function(err) {
|
||||
expect(err).to.be("Invalid namespace");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('socket', function(){
|
||||
|
||||
Reference in New Issue
Block a user