From c905b5d44f8cdfbc8110a9a7d1d62c08b5703ae3 Mon Sep 17 00:00:00 2001 From: "E. Westbrook" Date: Tue, 21 Aug 2018 08:03:51 -0600 Subject: url.lua: separate remove_dot_components() from absolute_path(); also use in _M.absolute() even when not merging --- src/url.lua | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/url.lua b/src/url.lua index 29b6734..a354ab5 100644 --- a/src/url.lua +++ b/src/url.lua @@ -77,24 +77,19 @@ function _M.unescape(s) end ----------------------------------------------------------------------------- --- Builds a path from a base path and a relative path +-- Removes '..' and '.' components appropriately from a path. -- Input --- base_path --- relative_path +-- path -- Returns --- corresponding absolute path ------------------------------------------------------------------------------ -local function absolute_path(base_path, relative_path) - if string.sub(relative_path, 1, 1) == "/" then return relative_path end - local path = string.gsub(base_path, "[^/]*$", "") - path = path .. relative_path +-- dot-normalized path +local function remove_dot_components(path) repeat - local was = path - path = path:gsub('/%./', '/') + local was = path + path = path:gsub('/%./', '/') until path == was repeat - local was = path - path = path:gsub('[^/]+/%.%./([^/]+)', '%1') + local was = path + path = path:gsub('[^/]+/%.%./([^/]+)', '%1') until path == was path = path:gsub('[^/]+/%.%./*$', '') path = path:gsub('/%.%.$', '/') @@ -103,6 +98,23 @@ local function absolute_path(base_path, relative_path) return path end +----------------------------------------------------------------------------- +-- Builds a path from a base path and a relative path +-- Input +-- base_path +-- relative_path +-- Returns +-- 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("[^/]*$", "") + local path = base_path .. relative_path + path = remove_dot_components(path) + return path +end + ----------------------------------------------------------------------------- -- Parses a url and returns a table with all its parts according to RFC 2396 -- The following grammar describes the names given to the URL parts @@ -225,10 +237,14 @@ 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 return relative_url - elseif not relative_parsed then return base_url - elseif relative_parsed.scheme then return 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 else relative_parsed.scheme = base_parsed.scheme if not relative_parsed.authority then @@ -246,8 +262,9 @@ function _M.absolute(base_url, relative_url) relative_parsed.path) end end - return _M.build(relative_parsed) + result = _M.build(relative_parsed) end + return remove_dot_components(result) end ----------------------------------------------------------------------------- -- cgit v1.2.3-55-g6feb