Changeset

4716:6eeb142a8073

mod_http_files, net.http.parser: Move path normalization to net.http.parser so that all modules can benefit
author Matthew Wild <mwild1@gmail.com>
date Thu, 26 Apr 2012 16:48:16 +0100
parents 4715:4d6ebe54671e
children 4717:3c0321e3fa76
files net/http/parser.lua plugins/mod_http_files.lua
diffstat 2 files changed, 20 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/net/http/parser.lua	Thu Apr 26 16:25:17 2012 +0100
+++ b/net/http/parser.lua	Thu Apr 26 16:48:16 2012 +0100
@@ -2,6 +2,24 @@
 local tonumber = tonumber;
 local assert = assert;
 
+local function preprocess_path(path)
+	if path:sub(1,1) ~= "/" then
+		path = "/"..path;
+	end
+	local level = 0;
+	for component in path:gmatch("([^/]+)/") do
+		if component == ".." then
+			level = level - 1;
+		elseif component ~= "." then
+			level = level + 1;
+		end
+		if level < 0 then
+			return nil;
+		end
+	end
+	return path;
+end
+
 local httpstream = {};
 
 function httpstream.new(success_cb, error_cb, parser_type, options_cb)
@@ -74,7 +92,7 @@
 						if path:match("^https?://") then
 							headers.host, path = path:match("^https?://([^/]*)(.*)");
 						end
-						path = path:gsub("^//+", "/"); -- TODO parse url more
+						path = preprocess_path(path);
 
 						len = len or 0;
 						packet = {
--- a/plugins/mod_http_files.lua	Thu Apr 26 16:25:17 2012 +0100
+++ b/plugins/mod_http_files.lua	Thu Apr 26 16:48:16 2012 +0100
@@ -25,31 +25,9 @@
 	css = "text/css";
 };
 
-local function preprocess_path(path)
-	if path:sub(1,1) ~= "/" then
-		path = "/"..path;
-	end
-	local level = 0;
-	for component in path:gmatch("([^/]+)/") do
-		if component == ".." then
-			level = level - 1;
-		elseif component ~= "." then
-			level = level + 1;
-		end
-		if level < 0 then
-			return nil;
-		end
-	end
-	return path;
-end
-
 function serve_file(event, path)
 	local response = event.response;
-	path = path and preprocess_path(path);
-	if not path then
-		return 400;
-	end
-	local full_path = http_base..path;
+	local full_path = http_base.."/"..path;
 	if stat(full_path, "mode") == "directory" then
 		if stat(full_path.."/index.html", "mode") == "file" then
 			return serve_file(event, path.."/index.html");