From e64c982be472e25835cf9bf9f14efb131b73aece Mon Sep 17 00:00:00 2001
From: Matthew Wild <mwild1@gmail.com>
Date: Thu, 26 Apr 2012 16:48:16 +0100
Subject: mod_http_files, net.http.parser: Move path normalization to
 net.http.parser so that all modules can benefit

---
 net/http/parser.lua | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

(limited to 'net/http')

diff --git a/net/http/parser.lua b/net/http/parser.lua
index fdcb8ebb..3d9d1a87 100644
--- a/net/http/parser.lua
+++ b/net/http/parser.lua
@@ -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 @@ function httpstream.new(success_cb, error_cb, parser_type, options_cb)
 						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 = {
-- 
cgit v1.2.3