url.lua:absolute_path(): fix issue #254, simplify, add more test cases

This commit is contained in:
E. Westbrook 2018-08-19 11:32:42 -06:00
parent 5813cd0505
commit 17a95c126a
2 changed files with 36 additions and 15 deletions

View File

@ -88,20 +88,18 @@ local function absolute_path(base_path, relative_path)
if string.sub(relative_path, 1, 1) == "/" then return relative_path end if string.sub(relative_path, 1, 1) == "/" then return relative_path end
local path = string.gsub(base_path, "[^/]*$", "") local path = string.gsub(base_path, "[^/]*$", "")
path = path .. relative_path path = path .. relative_path
path = string.gsub(path, "([^/]*%./)", function (s) repeat
if s ~= "./" then return s else return "" end local was = path
end) path = path:gsub('/%./', '/')
path = string.gsub(path, "/%.$", "/") until path == was
local reduced repeat
while reduced ~= path do local was = path
reduced = path path = path:gsub('[^/]+/%.%./([^/]+)', '%1')
path = string.gsub(reduced, "([^/]*/%.%./)", function (s) until path == was
if s ~= "../../" then return "" else return s end path = path:gsub('[^/]+/%.%./*$', '')
end) path = path:gsub('/%.%.$', '/')
end path = path:gsub('/%.$', '/')
path = string.gsub(reduced, "([^/]*/%.%.)$", function (s) path = path:gsub('^/%.%.', '')
if s ~= "../.." then return "" else return s end
end)
return path return path
end end

View File

@ -61,7 +61,7 @@ end
local check_absolute_url = function(base, relative, absolute) local check_absolute_url = function(base, relative, absolute)
local res = socket.url.absolute(base, relative) local res = socket.url.absolute(base, relative)
if res ~= absolute then if res ~= absolute then
io.write("absolute: In test for '", relative, "' expected '", io.write("absolute: In test for base='", base, "', rel='", relative, "' expected '",
absolute, "' but got '", res, "'\n") absolute, "' but got '", res, "'\n")
os.exit() os.exit()
end end
@ -637,6 +637,17 @@ check_absolute_url("http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x")
check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s") check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s")
check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/") check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/")
check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/") check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/")
check_absolute_url("http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g")
check_absolute_url("http://a/b/c/d;p?q#f", "./g/", "http://a/b/c/g/")
check_absolute_url("http://a/b/c/d;p?q#f", "././g", "http://a/b/c/g")
check_absolute_url("http://a/b/c/d;p?q#f", "././g/", "http://a/b/c/g/")
check_absolute_url("http://a/b/c/d;p?q#f", "g/.", "http://a/b/c/g/")
check_absolute_url("http://a/b/c/d;p?q#f", "g/./", "http://a/b/c/g/")
check_absolute_url("http://a/b/c/d;p?q#f", "g/./.", "http://a/b/c/g/")
check_absolute_url("http://a/b/c/d;p?q#f", "g/././", "http://a/b/c/g/")
check_absolute_url("http://a/b/c/d;p?q#f", "./.", "http://a/b/c/")
check_absolute_url("http://a/b/c/d;p?q#f", "././.", "http://a/b/c/")
check_absolute_url("http://a/b/c/d;p?q#f", "././g/./.", "http://a/b/c/g/")
check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/") check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/")
check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/") check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/")
check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g") check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g")
@ -655,6 +666,17 @@ check_absolute_url("http://a/b/c/d;p?q#f", "./g/.", "http://a/b/c/g/")
check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h") check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h")
check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h") check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h")
check_absolute_url("http://a/b/c/d:p?q#f/", "../g/", "http://a/b/g/")
check_absolute_url("http://a/b/c/d:p?q#f/", "../g", "http://a/b/g")
check_absolute_url("http://a/b/c/d:p?q#f/", "../.g/", "http://a/b/.g/")
check_absolute_url("http://a/b/c/d:p?q#f/", "../.g", "http://a/b/.g")
check_absolute_url("http://a/b/c/d:p?q#f/", "../.g.h/", "http://a/b/.g.h/")
check_absolute_url("http://a/b/c/d:p?q#f/", "../.g.h", "http://a/b/.g.h")
check_absolute_url("http://a/b/c/d:p?q#f/", "g.h/", "http://a/b/c/g.h/")
check_absolute_url("http://a/b/c/d:p?q#f/", "../g.h/", "http://a/b/g.h/")
check_absolute_url("http://a/", "../g.h/", "http://a/g.h/")
-- extra tests -- extra tests
check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f") check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f")
check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f") check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f")
@ -662,6 +684,7 @@ check_absolute_url("a/b/c/d", "d/e/f", "a/b/c/d/e/f")
check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f") check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f")
check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html", check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html",
"http://velox.telemar.com.br/dashboard/index.html") "http://velox.telemar.com.br/dashboard/index.html")
check_absolute_url("http://example.com/", "../.badhost.com/", "http://example.com/.badhost.com/")
print("testing path parsing and composition") print("testing path parsing and composition")
check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 })