Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved the cross-site origin checking to the client class, added cross…

…-domain support to xhr-* transports, added ping functionality for the xhr-multipart transport, and fixed the listener class where the log function that can be overridden was not being referenced everywhere.
  • Loading branch information...
commit 0e483a05a50496f8cf346c3752c35caced93f66f 1 parent 1918b75
@mscdex mscdex authored visnup committed
View
10 lib/socket.io/client.js
@@ -1,4 +1,4 @@
-var Options = require('./util/options').Options;
+var Options = require('./util/options').Options, urlparse = require('url').parse;
this.Client = Class({
@@ -96,6 +96,14 @@ this.Client = Class({
if (this.sessionId) return this.listener.options.log('This client already has a session id');
this.sessionId = Math.random().toString().substr(2);
return this;
+ },
+
+ _verifyOrigin: function(origin){
+ var parts = urlparse(origin);
+ return this.listener.options.origins.indexOf('*:*') !== -1
+ || this.listener.options.origins.indexOf(parts.host + ':' + parts.port) !== -1
+ || this.listener.options.origins.indexOf(parts.host + ':*') !== -1
+ || this.listener.options.origins.indexOf('*:' + parts.port) !== -1;
}
});
View
17 lib/socket.io/listener.js
@@ -1,5 +1,4 @@
var url = require('url'),
- sys = require('sys'),
Options = require('./util/options').Options,
Client = require('./client').Client,
@@ -15,7 +14,7 @@ Listener = this.Listener = Class({
transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling'],
transportOptions: {},
log: function(message){
- sys.log(message);
+ require('sys').log(message);
}
},
@@ -74,7 +73,7 @@ Listener = this.Listener = Class({
cn._onConnect(req, res);
} else {
req.connection.end();
- sys.log('Couldnt find client with session id "' + parts[2] + '"');
+ this.options.log('Couldnt find client with session id "' + parts[2] + '"');
}
} else {
this._onConnection(parts[1], req, res, httpUpgrade, head);
@@ -90,12 +89,12 @@ Listener = this.Listener = Class({
_onClientConnect: function(client){
if (!(client instanceof Client) || !client.sessionId){
- return sys.log('Invalid client');
+ return this.options.log('Invalid client');
}
client.i = this.clients.length;
this.clients.push(client);
this.clientsIndex[client.sessionId] = client;
- sys.log('Client '+ client.sessionId +' connected');
+ this.options.log('Client '+ client.sessionId +' connected');
this.emit('clientConnect', client);
},
@@ -106,7 +105,7 @@ Listener = this.Listener = Class({
_onClientDisconnect: function(client){
this.clientsIndex[client.sessionId] = null;
this.clients[client.i] = null;
- sys.log('Client '+ client.sessionId +' disconnected');
+ this.options.log('Client '+ client.sessionId +' disconnected');
this.emit('clientDisconnect', client);
},
@@ -114,10 +113,10 @@ Listener = this.Listener = Class({
_onConnection: function(transport, req, res, httpUpgrade, head){
if (this.options.transports.indexOf(transport) === -1 || (httpUpgrade && !Transports[transport].httpUpgrade)){
httpUpgrade ? res.destroy() : req.connection.destroy();
- return sys.log('Illegal transport "'+ transport +'"');
+ return this.options.log('Illegal transport "'+ transport +'"');
}
- sys.log('Initializing client with transport "'+ transport +'"');
+ this.options.log('Initializing client with transport "'+ transport +'"');
new Transports[transport](this, req, res, this.options.transportOptions[transport], head);
}
-});
+});
View
12 lib/socket.io/transports/websocket.js
@@ -80,15 +80,7 @@ this.websocket = Client.extend({
this.connection.write(md5.digest('binary'), 'binary');
}
},
-
- _verifyOrigin: function(origin){
- var parts = url.parse(origin);
- return this.listener.options.origins.indexOf('*:*') !== -1
- || this.listener.options.origins.indexOf(parts.host + ':' + parts.port) !== -1
- || this.listener.options.origins.indexOf(parts.host + ':*') !== -1
- || this.listener.options.origins.indexOf('*:' + parts.port) !== -1;
- },
-
+
_write: function(message){
try {
this.connection.write('\u0000', 'binary');
@@ -101,4 +93,4 @@ this.websocket = Client.extend({
});
-this.websocket.httpUpgrade = true;
+this.websocket.httpUpgrade = true;
View
26 lib/socket.io/transports/xhr-multipart.js
@@ -2,23 +2,39 @@ var Client = require('../client').Client,
qs = require('querystring');
this['xhr-multipart'] = Client.extend({
+
+ options: {
+ pingInterval: 7000
+ },
+ _pingInterval: null,
+
_onConnect: function(req, res){
var self = this, body = '';
switch (req.method){
case 'GET':
var self = this;
this.__super__(req, res);
+ var headers = {'Content-Type': 'multipart/x-mixed-replace;boundary="socketio"', 'Connection': 'keep-alive'};
+ // For newer browsers that support CORS (cross-domain XHR) -- see: https://developer.mozilla.org/En/HTTP_Access_Control
+ if (this.request.headers['origin'] && this._verifyOrigin(this.request.headers['origin'])) {
+ headers['Access-Control-Allow-Origin'] = this.request.headers['origin'];
+ if (this.request.headers['cookie'])
+ headers['Access-Control-Allow-Credentials'] = 'true';
+ }
this.request.connection.addListener('end', function(){ self._onClose(); });
this.response.useChunkedEncodingByDefault = false;
this.response.shouldKeepAlive = true;
- this.response.writeHead(200, {
- 'Content-Type': 'multipart/x-mixed-replace;boundary=socketio',
- 'Connection': 'keep-alive'
- });
+ this.response.writeHead(200, headers);
this.response.write("--socketio\n");
this.response.flush();
this._payload();
+ this._pingInterval = setInterval(function() {
+ if (self.connected)
+ self._write(String.fromCharCode(6));
+ else
+ clearInterval(self._pingInterval);
+ }, this.options.pingInterval);
break;
case 'POST':
@@ -39,7 +55,7 @@ this['xhr-multipart'] = Client.extend({
},
_write: function(message){
- this.response.write("Content-Type: text/plain\n\n");
+ this.response.write("Content-Type: text/plain" + (message.length == 1 && message.charCodeAt(0) == 6 ? "; charset=us-ascii" : "") + "\n\n");
this.response.write(message + "\n");
this.response.write("--socketio\n");
this.response.flush();
View
14 lib/socket.io/transports/xhr-polling.js
@@ -1,6 +1,5 @@
var Client = require('../client').Client,
- qs = require('querystring'),
- sys = require('sys');
+ qs = require('querystring');
this['xhr-polling'] = Client.extend({
@@ -28,7 +27,7 @@ this['xhr-polling'] = Client.extend({
try {
var msg = qs.parse(body);
self._onMessage(msg.data);
- } catch(e){}
+ } catch(e){}
res.writeHead(200);
res.write('ok');
res.end();
@@ -39,7 +38,14 @@ this['xhr-polling'] = Client.extend({
_write: function(message){
if (this._closeTimeout) clearTimeout(this._closeTimeout);
- this.response.writeHead(200, {'Content-Type': 'text/plain', 'Content-Length': message.length});
+ var headers = {'Content-Type': 'text/plain', 'Content-Length': message.length};
+ // For newer browsers that support CORS (cross-domain XHR) -- see: https://developer.mozilla.org/En/HTTP_Access_Control
+ if (this.request.headers['origin'] && this._verifyOrigin(this.request.headers['origin'])) {
+ headers['Access-Control-Allow-Origin'] = this.request.headers['origin'];
+ if (this.request.headers['cookie'])
+ headers['Access-Control-Allow-Credentials'] = 'true';
+ }
+ this.response.writeHead(200, headers);
this.response.write(message);
this.response.end();
this._onClose();
Please sign in to comment.
Something went wrong with that request. Please try again.