Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static #391

Merged
merged 15 commits into from
Sep 7, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
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
116 changes: 16 additions & 100 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var http = require('http')
, Socket = require('./socket')
, MemoryStore = require('./stores/memory')
, SocketNamespace = require('./namespace')
, Static = require('./static')
, EventEmitter = process.EventEmitter;

/**
Expand Down Expand Up @@ -64,6 +65,7 @@ function Manager (server) {
, log: true
, store: new MemoryStore
, logger: new Logger
, static: new Static(this)
, heartbeats: true
, resource: '/socket.io'
, transports: defaultTransports
Expand All @@ -77,8 +79,10 @@ function Manager (server) {
, 'flash policy port': 843
, '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 @@ -137,6 +141,16 @@ Manager.prototype.__defineGetter__('log', function () {
return logger;
});

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

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

/**
* Get settings.
*
Expand Down Expand Up @@ -496,7 +510,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 @@ -626,104 +640,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 Down Expand Up @@ -944,7 +860,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
8 changes: 8 additions & 0 deletions lib/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ exports.Transport = require('./transport');

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

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

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

/**
* Store constructor.
*
Expand Down
Loading