Changeset

309:5ec9125575fc

mod_admin_web: Handle paths without trailing slash
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 06 Jan 2011 23:41:45 +0100
parents 308:f406e300d709
children 310:b3bcd1913c85
files mod_admin_web/admin_web/mod_admin_web.lua
diffstat 1 files changed, 9 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mod_admin_web/admin_web/mod_admin_web.lua	Thu Jan 06 21:29:15 2011 +0100
+++ b/mod_admin_web/admin_web/mod_admin_web.lua	Thu Jan 06 23:41:45 2011 +0100
@@ -30,6 +30,7 @@
 local xmlns_c2s_session = "http://prosody.im/streams/c2s";
 local xmlns_s2s_session = "http://prosody.im/streams/s2s";
 
+local response_301 = { status = "301 Moved Permanently" };
 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" };
 local response_403 = { status = "403 Forbidden", body = "<h1>Forbidden</h1>You don't have permission to view the contents of this directory :(" };
 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" };
@@ -116,9 +117,14 @@
 	return path;
 end
 
-function serve_file(path)
+function serve_file(path, base)
 	local full_path = http_base..path;
 	if stat(full_path, "mode") == "directory" then
+		if not path:find("/$") then
+			local response = response_301;
+			response.headers = { ["Location"] = base .. "/" };
+			return response;
+		end
 		if stat(full_path.."/index.html", "mode") == "file" then
 			return serve_file(path.."/index.html");
 		end
@@ -143,8 +149,8 @@
 local function handle_file_request(method, body, request)
 	local path = preprocess_path(request.url.path);
 	if not path then return response_400; end
-	path = path:gsub("^/[^/]+", ""); -- Strip /admin/
-	return serve_file(path);
+	path_stripped = path:gsub("^/[^/]+", ""); -- Strip /admin/
+	return serve_file(path_stripped, path);
 end
 
 function module.load()