From a24294d331d32f161e98a16f4a79fd658d798752 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Wed, 22 Aug 2018 14:19:48 -0300 Subject: Revert "url.lua:absolute_path(): fix issue #254" --- src/url.lua | 64 ++++++++++++++++++++----------------------------------------- 1 file changed, 21 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/url.lua b/src/url.lua index 0a3a80a..110ea94 100644 --- a/src/url.lua +++ b/src/url.lua @@ -76,34 +76,6 @@ function _M.unescape(s) end)) end ------------------------------------------------------------------------------ --- Removes '..' and '.' components appropriately from a path. --- Input --- path --- Returns --- dot-normalized path -local function remove_dot_components(path) - local marker = string.char(1) - repeat - local was = path - path = path:gsub('//', '/'..marker..'/', 1) - until path == was - repeat - local was = path - path = path:gsub('/%./', '/', 1) - until path == was - repeat - local was = path - path = path:gsub('[^/]+/%.%./([^/]+)', '%1', 1) - until path == was - path = path:gsub('[^/]+/%.%./*$', '') - path = path:gsub('/%.%.$', '/') - path = path:gsub('/%.$', '/') - path = path:gsub('^/%.%./', '/') - path = path:gsub(marker, '') - return path -end - ----------------------------------------------------------------------------- -- Builds a path from a base path and a relative path -- Input @@ -113,12 +85,23 @@ end -- corresponding absolute path ----------------------------------------------------------------------------- local function absolute_path(base_path, relative_path) - if string.sub(relative_path, 1, 1) == "/" then - return remove_dot_components(relative_path) end - base_path = base_path:gsub("[^/]*$", "") - if not base_path:find'/$' then base_path = base_path .. '/' end - local path = base_path .. relative_path - path = remove_dot_components(path) + if string.sub(relative_path, 1, 1) == "/" then return relative_path end + local path = string.gsub(base_path, "[^/]*$", "") + path = path .. relative_path + path = string.gsub(path, "([^/]*%./)", function (s) + if s ~= "./" then return s else return "" end + end) + path = string.gsub(path, "/%.$", "/") + local reduced + while reduced ~= path do + reduced = path + path = string.gsub(reduced, "([^/]*/%.%./)", function (s) + if s ~= "../../" then return "" else return s end + end) + end + path = string.gsub(reduced, "([^/]*/%.%.)$", function (s) + if s ~= "../.." then return "" else return s end + end) return path end @@ -244,14 +227,10 @@ function _M.absolute(base_url, relative_url) else base_parsed = _M.parse(base_url) end - local result local relative_parsed = _M.parse(relative_url) - if not base_parsed then - result = relative_url - elseif not relative_parsed then - result = base_url - elseif relative_parsed.scheme then - result = relative_url + if not base_parsed then return relative_url + elseif not relative_parsed then return base_url + elseif relative_parsed.scheme then return relative_url else relative_parsed.scheme = base_parsed.scheme if not relative_parsed.authority then @@ -269,9 +248,8 @@ function _M.absolute(base_url, relative_url) relative_parsed.path) end end - result = _M.build(relative_parsed) + return _M.build(relative_parsed) end - return remove_dot_components(result) end ----------------------------------------------------------------------------- -- cgit v1.2.3-55-g6feb