Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implement host-specific script dispatch

  • Loading branch information...
commit 1c77adee39fff1eac2fcdef718abe3fb280a4d87 1 parent bc45304
@mhansen authored
Showing with 58 additions and 40 deletions.
  1. +58 −40 lib/antinode.js
View
98 lib/antinode.js
@@ -23,39 +23,58 @@ var settings;
var server;
+/* require() all the scripts at the start, so there's no
+ * waiting for in the middle of a request */
+function load_hostspecific_scripts() {
+ for (var host in settings.hosts) {
+ var script = settings.hosts[host]['script'];
+ if (script !== undefined) {
+ /* remove filename extension */
+ var require_name = script.match(/(.*)\..*/)[1];
+ settings.hosts[host].handle = require(require_name).handle;
+ }
+ }
+}
+
exports.start = function(custom_settings, callback) {
settings = custom_settings || {};
settings.__proto__ = exports.default_settings;
+
+ load_hostspecific_scripts();
log.level = settings.log_level;
log.info( "Starting server on port", settings.port);
+
server = http.createServer(function(req,resp) {
log.debug("Request from", req.connection.remoteAddress, "for", req.url);
log.debug(JSON.stringify(req.headers));
- function get_local_file_path() {
- var url = uri.parse(req.url);
- //if the parsed url doesn't have a pathname, default to '/'
- var pathname = (url.pathname || '/');
- var clean_pathname = pathname.
- replace(/\.\.\//g,''). //disallow parent directory access
+ var url = uri.parse(req.url);
+ //if the parsed url doesn't have a pathname, default to '/'
+ var pathname = (url.pathname || '/');
+ var clean_pathname = pathname.
+ replace(/\.\.\//g,''). //disallow parent directory access
replace(/\%20/g,' '); //convert spaces
- function select_vhost() {
- if (req.headers.host) {
- var hostname = req.headers.host.split(':')[0]; //remove port
- return settings.hosts[hostname] || settings.default_host;
- } else {
- return settings.default_host;
- }
+ function select_vhost() {
+ if (req.headers.host) {
+ var hostname = req.headers.host.split(':')[0]; //remove port
+ return settings.hosts[hostname] || settings.default_host;
+ } else {
+ return settings.default_host;
}
- var vhost = select_vhost(req.headers.host);
-
+ }
+ var vhost = select_vhost(req.headers.host);
+ if (vhost['handle'] !== undefined) {
+ vhost.handle(req,resp);
+ } else {
var path = pathlib.join(vhost.root, clean_pathname);
- return path;
+ if (path.match(/\.sjs$/)) {
+ execute_sjs(path, req, resp);
+ } else {
+ serve_static_file(path, req, resp);
+ }
}
- var path = get_local_file_path();
- handle_request(path, req, resp);
});
server.listen(settings.port);
server.addListener('listening', function() {
@@ -77,7 +96,7 @@ exports.stop = function(callback) {
}
};
-function handle_request(path, req, resp) {
+function serve_static_file(path, req, resp) {
function send_headers(httpstatus, length, content_type, modified_time) {
var headers = {
"Server": "Antinode/"+package.version+" Node.js/"+process.version,
@@ -106,27 +125,23 @@ function handle_request(path, req, resp) {
return file_not_found();
}
if (stats.isDirectory()) {
- return handle_request(pathlib.join(path, "index.html"), req, resp);
+ return serve_static_file(pathlib.join(path, "index.html"), req, resp);
}
if (!stats.isFile()) {
return file_not_found();
} else {
- if (path.match(/\.sjs$/)) {
- execute_sjs(path, req, resp);
- } else {
- var if_modified_since = req.headers['if-modified-since'];
- if (if_modified_since) {
- var req_date = new Date(if_modified_since);
- if (stats.mtime <= req_date && req_date <= Date.now()) {
- return not_modified();
- }
- else stream_file(path, stats);
- } else if (req.method == 'HEAD') {
- send_headers(200, stats.size, mime.mime_type(path), stats.mtime);
- finish(resp);
- } else {
- return stream_file(path, stats);
+ var if_modified_since = req.headers['if-modified-since'];
+ if (if_modified_since) {
+ var req_date = new Date(if_modified_since);
+ if (stats.mtime <= req_date && req_date <= Date.now()) {
+ return not_modified();
}
+ else stream_file(path, stats);
+ } else if (req.method == 'HEAD') {
+ send_headers(200, stats.size, mime.mime_type(path), stats.mtime);
+ finish(resp);
+ } else {
+ return stream_file(path, stats);
}
}
});
@@ -198,15 +213,18 @@ function execute_sjs(path, req, resp) {
fs.readFile(path, 'utf8', function(err, script) {
try {
if (err) throw err;
- var handler = {
+ var sandbox = {
log: log,
- require: require
+ require: require,
+ __filename: path,
+ __dirname: pathlib.dirname(path)
};
- Script.runInNewContext(script, handler, path);
- handler.handle(req, resp);
+ Script.runInNewContext(script, sandbox, path);
+ sandbox.handle(req, resp);
}
catch (e) {
- server_error("Error executing server script "+path+": "+e);
+ resp.writeHead(500,{'Content-Type':'text/plain'});
+ resp.end("Error executing server script "+path+": "+e);
}
});
}
Please sign in to comment.
Something went wrong with that request. Please try again.