Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

redirect '/folder' requests to '/folder/'

So the browser requests images in the page relative to the folder, not
the root
  • Loading branch information...
commit 0a29a7945f237a55ad84ce52078bda3575719ba7 1 parent e40b113
@mhansen authored
View
58 lib/antinode.js
@@ -127,22 +127,12 @@ exports.stop = function(callback) {
};
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,
- "Date": (new Date()).toUTCString()
- };
- if (length) {
- headers["Content-Length"] = length;
- }
- if (content_type) {
- headers["Content-Type"] = content_type || "application/octet-stream";
- }
- if (modified_time) {
- headers["Last-Modified"] = modified_time.toUTCString();
- }
+ function send_headers(httpstatus, headers) {
+ headers = headers || {};
+ headers["Server"] = "Antinode/"+package.version+" Node.js/"+process.version;
+ headers["Date"] = (new Date()).toUTCString();
+
resp.writeHead(httpstatus, headers);
- log.info(req.connection.remoteAddress,req.method,path,httpstatus,length);
}
fs.stat(path, function (err, stats) {
@@ -155,7 +145,13 @@ function serve_static_file(path, req, resp) {
return file_not_found();
}
if (stats.isDirectory()) {
- return serve_static_file(pathlib.join(path, "index.html"), req, resp);
+ if (req.url.match(/\/$/)) {
+ return serve_static_file(pathlib.join(path, "index.html"), req, resp);
+ }
+ else {
+ var redirect_host = req.headers.host ? ('http://' + req.headers['host']) : '';
+ return redirect_302_found(redirect_host + req.url + '/');
+ }
}
if (!stats.isFile()) {
return file_not_found();
@@ -168,7 +164,11 @@ function serve_static_file(path, req, resp) {
}
else stream_file(path, stats);
} else if (req.method == 'HEAD') {
- send_headers(200, stats.size, mime.mime_type(path), stats.mtime);
+ send_headers(200, {
+ 'Content-Length':stats.size,
+ 'Content-Type':mime.mime_type(path),
+ 'Last-Modified': stats.mtime
+ });
resp.end('');
} else {
return stream_file(path, stats);
@@ -185,7 +185,11 @@ function serve_static_file(path, req, resp) {
return file_not_found();
}
- send_headers(200, stats.size, mime.mime_type(file), stats.mtime);
+ send_headers(200, {
+ 'Content-Length':stats.size,
+ 'Content-Type':mime.mime_type(file),
+ 'Last-Modified':stats.mtime
+ });
sys.pump(readStream, resp, function() {
log.debug('pumped',file);
});
@@ -222,7 +226,10 @@ function serve_static_file(path, req, resp) {
function file_not_found() {
log.debug("404 opening path: '"+path+"'");
var body = "404: " + req.url + " not found.\n";
- send_headers(404,body.length,"text/plain");
+ send_headers(404,{
+ 'Content-Length':body.length,
+ 'Content-Type':"text/plain"
+ });
if (req.method != 'HEAD') {
resp.end(body, 'utf-8');
} else {
@@ -232,11 +239,22 @@ function serve_static_file(path, req, resp) {
function server_error(message) {
log.error(message);
- send_headers(500, message.length, "text/plain");
+ send_headers(500, {
+ 'Content-Length':message.length,
+ 'Content-Type':"text/plain"
+ });
if (req.method !== 'HEAD') {
resp.end(message,'utf-8');
}
}
+
+ function redirect_302_found(location) {
+ log.debug('redirecting with "302 Found" to ' + location);
+ send_headers(302, {
+ 'Location' : location
+ });
+ resp.end('');
+ }
}
function execute_sjs(path, req, resp) {
View
3  tests/common.js
@@ -24,7 +24,8 @@ exports.settings = {
"default_host" : {
"root": path.join(fixturesDir,"default-host")
},
- "log_level": antinode.log_levels.ERROR
+ /* turn this up to 'log_levels.DEBUG' when debugging failing tests */
+ "log_level": antinode.log_levels.ERROR
};
settings.request_preprocessor = require('./fixtures/redirector').preprocessor;
View
0  tests/fixtures/default-host/folder/index.html → ...fixtures/examplevirtualhost.com/folder/index.html
File renamed without changes
View
67 tests/test-index.js
@@ -1,27 +1,50 @@
// test rewriting of urls like '/' to '/index.html'
require('./common');
-var filename = path.join(settings.default_host.root, "folder/index.html");
+var filename = path.join(settings.hosts['examplevirtualhost.com'].root, "folder/index.html");
var indexText = fs.readFileSync(filename, 'binary');
-var pathnames = [ '/folder', '/folder/' ];
-
-pathnames.forEach(function(pathname) {
- exports["GET "+pathname+" -> /folder/index.html"] = function(test) {
- antinode.start(settings, function() {
- test_http(test,
- {
- 'method':'GET',
- 'pathname':pathname
- },
- {
- 'statusCode':200,
- 'body':indexText
- },
- function() {
- antinode.stop();
- test.done();
+exports["'GET /folder' returns '302 Found: http://examplevirtualhost.com/folder/'"] = function(test) {
+ antinode.start(settings, function() {
+ test_http(test,
+ {
+ 'method':'GET',
+ 'pathname':'/folder',
+ 'headers': {
+ 'host': 'examplevirtualhost.com'
+ }
+ },
+ {
+ 'statusCode':302,
+ 'body':'',
+ 'headers': {
+ 'location': 'http://examplevirtualhost.com/folder/'
+ }
+ },
+ function() {
+ antinode.stop();
+ test.done();
+ }
+ );
+ });
+};
+exports["'GET /folder/' returns 200, text of /folder/index.html"] = function(test) {
+ antinode.start(settings, function() {
+ test_http(test,
+ {
+ 'method':'GET',
+ 'pathname':'/folder/',
+ 'headers': {
+ 'host': 'examplevirtualhost.com'
}
- );
- });
- };
-});
+ },
+ {
+ 'statusCode':200,
+ 'body':indexText
+ },
+ function() {
+ antinode.stop();
+ test.done();
+ }
+ );
+ });
+};
Please sign in to comment.
Something went wrong with that request. Please try again.