Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improved static serving of content
  • Loading branch information
3rd-Eden committed Jun 18, 2011
commit b1837f348e581ad61d5f53d7b9b4eb54fef5df99
77 changes: 52 additions & 25 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ Manager.prototype.handleRequest = function (req, res) {
return;
}

if (!data.transport && !data.protocol) {
if (data.path == '/socket.io.js' && this.enabled('browser client')) {
this.handleClientRequest(req, res);
if (data.static || !data.transport && !data.protocol) {
if (data.static && this.enabled('browser client')) {
this.handleClientRequest(req, res, data);
} else {
res.writeHead(200);
res.end('Welcome to socket.io.');
Expand Down Expand Up @@ -356,57 +356,83 @@ 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'
}
, contentType: {
'js': 'application/javascript'
, 'swf': 'application/x-shockwave-flash'
}
, encoding:{
'js': 'utf8'
, 'swf': 'binary'
}
};

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

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

var self = this;

function serve () {
if (!self.clientLength) {
self.clientLength = Buffer.byteLength(self.client);
}

var headers = {
'Content-Type': 'application/javascript'
, 'Content-Length': self.clientLength
'Content-Type': static.contentType[extension]
, 'Content-Length': cache.length
};

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

console.dir(cache);
res.writeHead(200, headers);
res.end(self.client);
res.end(cache.content, cache.encoding);

self.log.debug('served client');
};
self.log.debug('served static ' + data.path);
}

if (this.get('browser client handler')) {
this.get('browser client handler').call(this, req, res);
} else if (!this.client) {
var file = this.enabled('browser client minification')
? 'socket.io.min.js'
: 'socket.io.js';

fs.readFile(client.dist + '/' + file, function (err, data) {
} else if (!cache) {
fs.readFile(location, function (err, data) {
if (err) {
res.writeHead(500);
res.end('Error serving socket.io client.');

self.log.warn('Can\'t cache socket.io client');
self.log.warn('Can\'t cache socket.io client, ' + err.message);
return;
}

self.client = data.toString();
self.clientEtag = client.version;
self.log.debug('caching', client.version, 'client');
cache = Manager.static.cache[file] = {
content: data.toString()
, length: data.length
, Etag: client.version
};

serve();
});

} else {
serve();
}
Expand Down Expand Up @@ -588,6 +614,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];
};

return data;
Expand Down