Skip to content

Commit

Permalink
Merge branch 'master' of github.com:LearnBoost/socket.io into ACAO
Browse files Browse the repository at this point in the history
Conflicts:
	lib/manager.js
  • Loading branch information
3rd-Eden committed Sep 13, 2011
2 parents e269fca + b2f9f19 commit 6df152c
Show file tree
Hide file tree
Showing 22 changed files with 2,178 additions and 644 deletions.
51 changes: 51 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@

0.8.3 / 2011-09-03
==================

* Fixed `\n` parsing for non-JSON packets (fixes #479).
* Fixed parsing of certain unicode characters (fixes #451).
* Fixed transport message packet logging.
* Fixed emission of `error` event resulting in an uncaught exception if unhandled (fixes #476).
* Fixed; allow for falsy values as the configuration value of `log level` (fixes #491).
* Fixed repository URI in `package.json`. Fixes #504.
* Added text/plain content-type to handshake responses [einaros]
* Improved single byte writes [einaros]
* Updated socket.io-flashsocket default port from 843 to 10843 [3rd-Eden]
* Updated client.

0.8.2 / 2011-08-29
==================

* Updated client.

0.8.1 / 2011-08-29
==================

* Fixed utf8 bug in send framing in websocket [einaros]
* Fixed typo in docs [Znarkus]
* Fixed bug in send framing for over 64kB of data in websocket [einaros]
* Corrected ping handling in websocket transport [einaros]

0.8.0 / 2011-08-28
==================

* Updated to work with two-level websocket versioning. [einaros]
* Added hybi07 support. [einaros]
* Added hybi10 support. [einaros]
* Added http referrer verification to manager.js verifyOrigin. [einaors]

0.7.11 / 2011-08-27
===================

* Updated socket.io-client.

0.7.10 / 2011-08-27
===================

* Updated socket.io-client.

0.7.9 / 2011-08-12
==================

* Updated socket.io-client.
* Make sure we only do garbage collection when the server we receive is actually run.

0.7.8 / 2011-08-08
==================

Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Besides `connect`, `message` and `disconnect`, you can emit custom events:
var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
io.sockets.emit('this', { will: 'be received by everyone');
io.sockets.emit('this', { will: 'be received by everyone' });

socket.on('private message', function (from, msg) {
console.log('I received a private message by ', from, ' saying ', msg);
Expand Down
141 changes: 32 additions & 109 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var fs = require('fs')
, Socket = require('./socket')
, MemoryStore = require('./stores/memory')
, SocketNamespace = require('./namespace')
, Static = require('./static')
, EventEmitter = process.EventEmitter;

/**
Expand Down Expand Up @@ -61,6 +62,7 @@ function Manager (server, options) {
, log: true
, store: new MemoryStore
, logger: new Logger
, static: new Static(this)
, heartbeats: true
, resource: '/socket.io'
, transports: defaultTransports
Expand All @@ -74,8 +76,10 @@ function Manager (server, options) {
, 'flash policy port': 10843
, 'destroy upgrade': true
, 'browser client': true
, 'browser client cache': true
, 'browser client minification': false
, 'browser client etag': false
, 'browser client gzip': false
, 'browser client handler': false
, 'client store expiration': 15
};
Expand Down Expand Up @@ -141,11 +145,21 @@ Manager.prototype.__defineGetter__('log', function () {
if (this.disabled('log')) return;

var logger = this.get('logger');
logger.level = this.get('log level');
logger.level = this.get('log level') || -1;

return logger;
});

/**
* Static accessor.
*
* @api public
*/

Manager.prototype.__defineGetter__('static', function () {
return this.get('static');
});

/**
* Get settings.
*
Expand Down Expand Up @@ -516,7 +530,7 @@ Manager.prototype.handleRequest = function (req, res) {

if (data.static || !data.transport && !data.protocol) {
if (data.static && this.enabled('browser client')) {
this.handleClientRequest(req, res, data);
this.static.write(data.path, req, res);
} else {
res.writeHead(200);
res.end('Welcome to socket.io.');
Expand Down Expand Up @@ -652,104 +666,6 @@ Manager.prototype.handleClient = function (data, req) {
}
};

/**
* Dictionary for static file serving
*
* @api public
*/

Manager.static = {
cache: {}
, paths: {
'/static/flashsocket/WebSocketMain.swf': client.dist + '/WebSocketMain.swf'
, '/static/flashsocket/WebSocketMainInsecure.swf':
client.dist + '/WebSocketMainInsecure.swf'
, '/socket.io.js': client.dist + '/socket.io.js'
, '/socket.io.js.min': client.dist + '/socket.io.min.js'
}
, mime: {
'js': {
contentType: 'application/javascript'
, encoding: 'utf8'
}
, 'swf': {
contentType: 'application/x-shockwave-flash'
, encoding: 'binary'
}
}
};

/**
* Serves the client.
*
* @api private
*/

Manager.prototype.handleClientRequest = function (req, res, data) {
var static = Manager.static
, extension = data.path.split('.').pop()
, file = data.path + (this.enabled('browser client minification')
&& extension == 'js' ? '.min' : '')
, location = static.paths[file]
, cache = static.cache[file];

var self = this;

/**
* Writes a response, safely
*
* @api private
*/

function write (status, headers, content, encoding) {
try {
res.writeHead(status, headers || null);
res.end(content || '', encoding || null);
} catch (e) {}
}

function serve () {
if (req.headers['if-none-match'] === cache.Etag) {
return write(304);
}

var mime = static.mime[extension]
, headers = {
'Content-Type': mime.contentType
, 'Content-Length': cache.length
};

if (self.enabled('browser client etag') && cache.Etag) {
headers.Etag = cache.Etag;
}

write(200, headers, cache.content, mime.encoding);
self.log.debug('served static ' + data.path);
}

if (this.get('browser client handler')) {
this.get('browser client handler').call(this, req, res);
} else if (!cache) {
fs.readFile(location, function (err, data) {
if (err) {
write(500, null, 'Error serving static ' + data.path);
self.log.warn('Can\'t cache '+ data.path +', ' + err.message);
return;
}

cache = Manager.static.cache[file] = {
content: data
, length: data.length
, Etag: client.version
};

serve();
});
} else {
serve();
}
};

/**
* Generates a session id.
*
Expand All @@ -770,7 +686,9 @@ Manager.prototype.generateId = function () {
Manager.prototype.handleHandshake = function (data, req, res) {
var self = this
, origin = req.headers.origin
, headers = {};
, headers = {
'Content-Type': 'text/plain'
};

function writeErr (status, message) {
if (data.query.jsonp) {
Expand Down Expand Up @@ -877,7 +795,7 @@ Manager.prototype.handshakeData = function (data) {
*/

Manager.prototype.verifyOrigin = function (request) {
var origin = request.headers.origin
var origin = request.headers.origin || request.headers.referer
, origins = this.get('origins');

if (origin === 'null') origin = '*';
Expand All @@ -889,14 +807,19 @@ Manager.prototype.verifyOrigin = function (request) {
if (origin) {
try {
var parts = url.parse(origin);

return
~origins.indexOf(parts.host + ':' + parts.port) ||
~origins.indexOf(parts.host + ':*') ||
var ok =
~origins.indexOf(parts.hostname + ':' + parts.port) ||
~origins.indexOf(parts.hostname + ':*') ||
~origins.indexOf('*:' + parts.port);
} catch (ex) {}
if (!ok) this.log.warn('illegal origin: ' + origin);
return ok;
} catch (ex) {
this.log.warn('error parsing origin');
}
}
else {
this.log.warn('origin missing from handshake, yet required by config');
}

return false;
};

Expand Down Expand Up @@ -985,7 +908,7 @@ Manager.prototype.checkRequest = function (req) {
data.protocol = Number(pieces[1]);
data.transport = pieces[2];
data.id = pieces[3];
data.static = !!Manager.static.paths[path];
data.static = !!this.static.has(path);
};

return data;
Expand Down
3 changes: 2 additions & 1 deletion lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ SocketNamespace.prototype.handlePacket = function (sessid, packet) {
case 'event':
var params = [packet.name].concat(packet.args);

if (dataAck)
if (dataAck) {
params.push(ack);
}

socket.$emit.apply(socket, params);
break;
Expand Down
6 changes: 5 additions & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ exports.encodePayload = function (packets) {
* @api private
*/

var regexp = /^([^:]+):([0-9]+)?(\+)?:([^:]+)?:?(.*)?$/;
var regexp = /([^:]+):([0-9]+)?(\+)?:([^:]+)?:?([\s\S]*)?/;

exports.decodePacket = function (data) {
var pieces = data.match(regexp);
Expand Down Expand Up @@ -223,6 +223,10 @@ exports.decodePacket = function (data) {
*/

exports.decodePayload = function (data) {
if (undefined == data || null == data) {
return [];
}

if (data[0] == '\ufffd') {
var ret = [];

Expand Down
10 changes: 9 additions & 1 deletion lib/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var client = require('socket.io-client');
* Version.
*/

exports.version = '0.7.8';
exports.version = '0.8.4';

/**
* Supported protocol version.
Expand Down Expand Up @@ -95,6 +95,14 @@ exports.Transport = require('./transport');

exports.Socket = require('./socket');

/**
* Static constructor.
*
* @api public
*/

exports.Static = require('./static');

/**
* Store constructor.
*
Expand Down
11 changes: 9 additions & 2 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@

var parser = require('./parser')
, util = require('./util')
, EventEmitter = process.EventEmitter;
, EventEmitter = process.EventEmitter

/**
* Export the constructor.
*/

exports = module.exports = Socket;

/**
* Default error event listener to prevent uncaught exceptions.
*/

var defaultError = function () {};

/**
* Socket constructor.
*
Expand All @@ -39,6 +45,7 @@ function Socket (manager, id, nsp, readable) {
this.setFlags();
this.readable = readable;
this.store = this.manager.store.client(this.id);
this.on('error', defaultError);
};

/**
Expand Down Expand Up @@ -160,7 +167,7 @@ Socket.prototype.join = function (name, fn) {
};

/**
* Joins a user to a room.
* Un-joins a user from a room.
*
* @api public
*/
Expand Down
Loading

0 comments on commit 6df152c

Please sign in to comment.