Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

tidy up source (into 80 columns)

  • Loading branch information...
commit bda109ddebcafeaf618db923e1b352ef4939a8ea 1 parent 3418b9e
@mhansen authored
Showing with 32 additions and 29 deletions.
  1. +32 −29 lib/antinode.js
View
61 lib/antinode.js
@@ -32,7 +32,28 @@ exports.start = function(custom_settings, callback) {
log.debug("Request from", req.connection.remoteAddress, "for", req.url);
log.debug(JSON.stringify(req.headers));
- var path = get_local_file_path(req);
+ 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
+ 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;
+ }
+ }
+ var vhost = select_vhost(req.headers.host);
+
+ var path = pathlib.join(vhost.root, clean_pathname);
+ return path;
+ }
+ var path = get_local_file_path();
handle_request(path, req, resp);
});
server.listen(settings.port);
@@ -42,7 +63,7 @@ exports.start = function(custom_settings, callback) {
server.addListener('connection', function(connection) {
connection.setTimeout(settings.timeout_milliseconds);
connection.addListener('timeout', function() {
- log.debug("Connection from", connection.remoteAddress, "timed out.");
+ log.debug("Connection from",connection.remoteAddress,"timed out.");
connection.destroy();
});
});
@@ -55,26 +76,6 @@ exports.stop = function(callback) {
}
};
-function get_local_file_path(req) {
- var url = uri.parse(req.url);
- //if the parsed url doesn't have a pathname, default to '/'
- var pathname = (url.pathname || '/');
- //disallow parent directory access, convert spaces
- var clean_pathname = pathname.replace(/\.\.\//g,'').replace(/\%20/g,' ');
- var vhost = select_vhost(req.headers.host);
- var path = pathlib.join(vhost.root, clean_pathname);
- return path;
-}
-
-function select_vhost(host_header) {
- if (host_header) {
- host_header = host_header.split(':')[0]; //remove the port
- return settings.hosts[host_header] || settings.default_host;
- } else {
- return settings.default_host;
- }
-}
-
function handle_request(path, req, resp) {
function send_headers(httpstatus, length, content_type, modified_time) {
var headers = {
@@ -91,7 +92,7 @@ function handle_request(path, req, resp) {
headers["Last-Modified"] = modified_time.toUTCString();
}
resp.writeHead(httpstatus, headers);
- log.info(req.connection.remoteAddress, req.method, path, httpstatus, length);
+ log.info(req.connection.remoteAddress,req.method,path,httpstatus,length);
}
fs.stat(path, function (err, stats) {
@@ -137,10 +138,11 @@ function handle_request(path, req, resp) {
req.connection.addListener('timeout', function() {
if (fd_open) {
// close file descriptors so we don't run out of them.
- // only close the file once. If our timeout handler runs after
- // we've finished sending the file, the fd will already be closed
- // and it could be reused by another request. It would interrupt
- // the other request if we closed a fd twice.
+ // only close the file once. If our timeout handler runs
+ // after we've finished sending the file, the fd will
+ // already be closed and it could be reused by another
+ // request. It would interrupt the other request if we
+ // closed a fd twice.
close(fd);
fd_open = false;
}
@@ -170,11 +172,12 @@ function handle_request(path, req, resp) {
break;
default:
- log.error("sendfile(", file, ") failed: ", err.message);
+ log.error("sendfile(",file,") failed: ",err.message);
// fall through
case process.EBADF:
- // don't write a log message, the timeout handler already did
+ // don't write a log message, the timeout handler
+ // already did
callback();
return;
}
Please sign in to comment.
Something went wrong with that request. Please try again.