mirror of
https://github.com/lunarmodules/luasocket.git
synced 2026-01-11 14:36:21 +01:00
Revert "url.lua:absolute_path(): fix issue #254"
This commit is contained in:
64
src/url.lua
64
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
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user