aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego.nehab@gmail.com>2018-08-22 14:19:48 -0300
committerGitHub <noreply@github.com>2018-08-22 14:19:48 -0300
commita24294d331d32f161e98a16f4a79fd658d798752 (patch)
tree89dfda9b116249fd1b2855c2cc5f542dacd05416
parent648d81281f177f43de3d7b66e5923bdbaaeb25d9 (diff)
downloadluasocket-revert-256-luasocket254.tar.gz
luasocket-revert-256-luasocket254.tar.bz2
luasocket-revert-256-luasocket254.zip
Revert "url.lua:absolute_path(): fix issue #254"revert-256-luasocket254
-rw-r--r--src/url.lua64
-rw-r--r--test/urltest.lua44
2 files changed, 26 insertions, 82 deletions
diff --git a/src/url.lua b/src/url.lua
index 0a3a80a..110ea94 100644
--- a/src/url.lua
+++ b/src/url.lua
@@ -77,34 +77,6 @@ function _M.unescape(s)
77end 77end
78 78
79----------------------------------------------------------------------------- 79-----------------------------------------------------------------------------
80-- Removes '..' and '.' components appropriately from a path.
81-- Input
82-- path
83-- Returns
84-- dot-normalized path
85local function remove_dot_components(path)
86 local marker = string.char(1)
87 repeat
88 local was = path
89 path = path:gsub('//', '/'..marker..'/', 1)
90 until path == was
91 repeat
92 local was = path
93 path = path:gsub('/%./', '/', 1)
94 until path == was
95 repeat
96 local was = path
97 path = path:gsub('[^/]+/%.%./([^/]+)', '%1', 1)
98 until path == was
99 path = path:gsub('[^/]+/%.%./*$', '')
100 path = path:gsub('/%.%.$', '/')
101 path = path:gsub('/%.$', '/')
102 path = path:gsub('^/%.%./', '/')
103 path = path:gsub(marker, '')
104 return path
105end
106
107-----------------------------------------------------------------------------
108-- Builds a path from a base path and a relative path 80-- Builds a path from a base path and a relative path
109-- Input 81-- Input
110-- base_path 82-- base_path
@@ -113,12 +85,23 @@ end
113-- corresponding absolute path 85-- corresponding absolute path
114----------------------------------------------------------------------------- 86-----------------------------------------------------------------------------
115local function absolute_path(base_path, relative_path) 87local function absolute_path(base_path, relative_path)
116 if string.sub(relative_path, 1, 1) == "/" then 88 if string.sub(relative_path, 1, 1) == "/" then return relative_path end
117 return remove_dot_components(relative_path) end 89 local path = string.gsub(base_path, "[^/]*$", "")
118 base_path = base_path:gsub("[^/]*$", "") 90 path = path .. relative_path
119 if not base_path:find'/$' then base_path = base_path .. '/' end 91 path = string.gsub(path, "([^/]*%./)", function (s)
120 local path = base_path .. relative_path 92 if s ~= "./" then return s else return "" end
121 path = remove_dot_components(path) 93 end)
94 path = string.gsub(path, "/%.$", "/")
95 local reduced
96 while reduced ~= path do
97 reduced = path
98 path = string.gsub(reduced, "([^/]*/%.%./)", function (s)
99 if s ~= "../../" then return "" else return s end
100 end)
101 end
102 path = string.gsub(reduced, "([^/]*/%.%.)$", function (s)
103 if s ~= "../.." then return "" else return s end
104 end)
122 return path 105 return path
123end 106end
124 107
@@ -244,14 +227,10 @@ function _M.absolute(base_url, relative_url)
244 else 227 else
245 base_parsed = _M.parse(base_url) 228 base_parsed = _M.parse(base_url)
246 end 229 end
247 local result
248 local relative_parsed = _M.parse(relative_url) 230 local relative_parsed = _M.parse(relative_url)
249 if not base_parsed then 231 if not base_parsed then return relative_url
250 result = relative_url 232 elseif not relative_parsed then return base_url
251 elseif not relative_parsed then 233 elseif relative_parsed.scheme then return relative_url
252 result = base_url
253 elseif relative_parsed.scheme then
254 result = relative_url
255 else 234 else
256 relative_parsed.scheme = base_parsed.scheme 235 relative_parsed.scheme = base_parsed.scheme
257 if not relative_parsed.authority then 236 if not relative_parsed.authority then
@@ -269,9 +248,8 @@ function _M.absolute(base_url, relative_url)
269 relative_parsed.path) 248 relative_parsed.path)
270 end 249 end
271 end 250 end
272 result = _M.build(relative_parsed) 251 return _M.build(relative_parsed)
273 end 252 end
274 return remove_dot_components(result)
275end 253end
276 254
277----------------------------------------------------------------------------- 255-----------------------------------------------------------------------------
diff --git a/test/urltest.lua b/test/urltest.lua
index ae8ba75..1090a7e 100644
--- a/test/urltest.lua
+++ b/test/urltest.lua
@@ -61,7 +61,7 @@ end
61local check_absolute_url = function(base, relative, absolute) 61local check_absolute_url = function(base, relative, absolute)
62 local res = socket.url.absolute(base, relative) 62 local res = socket.url.absolute(base, relative)
63 if res ~= absolute then 63 if res ~= absolute then
64 io.write("absolute: In test for base='", base, "', rel='", relative, "' expected '", 64 io.write("absolute: In test for '", relative, "' expected '",
65 absolute, "' but got '", res, "'\n") 65 absolute, "' but got '", res, "'\n")
66 os.exit() 66 os.exit()
67 end 67 end
@@ -627,37 +627,25 @@ check_absolute_url("http://a/b/c/d;p?q#f", "/g", "http://a/g")
627check_absolute_url("http://a/b/c/d;p?q#f", "//g", "http://g") 627check_absolute_url("http://a/b/c/d;p?q#f", "//g", "http://g")
628check_absolute_url("http://a/b/c/d;p?q#f", "?y", "http://a/b/c/d;p?y") 628check_absolute_url("http://a/b/c/d;p?q#f", "?y", "http://a/b/c/d;p?y")
629check_absolute_url("http://a/b/c/d;p?q#f", "g?y", "http://a/b/c/g?y") 629check_absolute_url("http://a/b/c/d;p?q#f", "g?y", "http://a/b/c/g?y")
630check_absolute_url("http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/x") 630check_absolute_url("http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/./x")
631check_absolute_url("http://a/b/c/d;p?q#f", "#s", "http://a/b/c/d;p?q#s") 631check_absolute_url("http://a/b/c/d;p?q#f", "#s", "http://a/b/c/d;p?q#s")
632check_absolute_url("http://a/b/c/d;p?q#f", "g#s", "http://a/b/c/g#s") 632check_absolute_url("http://a/b/c/d;p?q#f", "g#s", "http://a/b/c/g#s")
633check_absolute_url("http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/x") 633check_absolute_url("http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/./x")
634check_absolute_url("http://a/b/c/d;p?q#f", "g?y#s", "http://a/b/c/g?y#s") 634check_absolute_url("http://a/b/c/d;p?q#f", "g?y#s", "http://a/b/c/g?y#s")
635check_absolute_url("http://a/b/c/d;p?q#f", ";x", "http://a/b/c/d;x") 635check_absolute_url("http://a/b/c/d;p?q#f", ";x", "http://a/b/c/d;x")
636check_absolute_url("http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x") 636check_absolute_url("http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x")
637check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s") 637check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s")
638check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/") 638check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/")
639check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/") 639check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/")
640check_absolute_url("http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g")
641check_absolute_url("http://a/b/c/d;p?q#f", "./g/", "http://a/b/c/g/")
642check_absolute_url("http://a/b/c/d;p?q#f", "././g", "http://a/b/c/g")
643check_absolute_url("http://a/b/c/d;p?q#f", "././g/", "http://a/b/c/g/")
644check_absolute_url("http://a/b/c/d;p?q#f", "g/.", "http://a/b/c/g/")
645check_absolute_url("http://a/b/c/d;p?q#f", "g/./", "http://a/b/c/g/")
646check_absolute_url("http://a/b/c/d;p?q#f", "g/./.", "http://a/b/c/g/")
647check_absolute_url("http://a/b/c/d;p?q#f", "g/././", "http://a/b/c/g/")
648check_absolute_url("http://a/b/c/d;p?q#f", "./.", "http://a/b/c/")
649check_absolute_url("http://a/b/c/d;p?q#f", "././.", "http://a/b/c/")
650check_absolute_url("http://a/b/c/d;p?q#f", "././g/./.", "http://a/b/c/g/")
651check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/") 640check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/")
652check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/") 641check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/")
653check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g") 642check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g")
654check_absolute_url("http://a/b/c/d;p?q#f", "../..", "http://a/") 643check_absolute_url("http://a/b/c/d;p?q#f", "../..", "http://a/")
655check_absolute_url("http://a/b/c/d;p?q#f", "../../", "http://a/") 644check_absolute_url("http://a/b/c/d;p?q#f", "../../", "http://a/")
656check_absolute_url("http://a/b/c/d;p?q#f", "../../g", "http://a/g") 645check_absolute_url("http://a/b/c/d;p?q#f", "../../g", "http://a/g")
657check_absolute_url("http://a/b/c/d;p?q#f", "../../../g", "http://a/g")
658check_absolute_url("http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q#f") 646check_absolute_url("http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q#f")
659check_absolute_url("http://a/b/c/d;p?q#f", "/./g", "http://a/g") 647check_absolute_url("http://a/b/c/d;p?q#f", "/./g", "http://a/./g")
660check_absolute_url("http://a/b/c/d;p?q#f", "/../g", "http://a/g") 648check_absolute_url("http://a/b/c/d;p?q#f", "/../g", "http://a/../g")
661check_absolute_url("http://a/b/c/d;p?q#f", "g.", "http://a/b/c/g.") 649check_absolute_url("http://a/b/c/d;p?q#f", "g.", "http://a/b/c/g.")
662check_absolute_url("http://a/b/c/d;p?q#f", ".g", "http://a/b/c/.g") 650check_absolute_url("http://a/b/c/d;p?q#f", ".g", "http://a/b/c/.g")
663check_absolute_url("http://a/b/c/d;p?q#f", "g..", "http://a/b/c/g..") 651check_absolute_url("http://a/b/c/d;p?q#f", "g..", "http://a/b/c/g..")
@@ -667,17 +655,6 @@ check_absolute_url("http://a/b/c/d;p?q#f", "./g/.", "http://a/b/c/g/")
667check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h") 655check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h")
668check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h") 656check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h")
669 657
670check_absolute_url("http://a/b/c/d:p?q#f/", "../g/", "http://a/b/g/")
671check_absolute_url("http://a/b/c/d:p?q#f/", "../g", "http://a/b/g")
672check_absolute_url("http://a/b/c/d:p?q#f/", "../.g/", "http://a/b/.g/")
673check_absolute_url("http://a/b/c/d:p?q#f/", "../.g", "http://a/b/.g")
674check_absolute_url("http://a/b/c/d:p?q#f/", "../.g.h/", "http://a/b/.g.h/")
675check_absolute_url("http://a/b/c/d:p?q#f/", "../.g.h", "http://a/b/.g.h")
676
677check_absolute_url("http://a/b/c/d:p?q#f/", "g.h/", "http://a/b/c/g.h/")
678check_absolute_url("http://a/b/c/d:p?q#f/", "../g.h/", "http://a/b/g.h/")
679check_absolute_url("http://a/", "../g.h/", "http://a/g.h/")
680
681-- extra tests 658-- extra tests
682check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f") 659check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f")
683check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f") 660check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f")
@@ -685,17 +662,6 @@ check_absolute_url("a/b/c/d", "d/e/f", "a/b/c/d/e/f")
685check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f") 662check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f")
686check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html", 663check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html",
687 "http://velox.telemar.com.br/dashboard/index.html") 664 "http://velox.telemar.com.br/dashboard/index.html")
688check_absolute_url("http://example.com/", "../.badhost.com/", "http://example.com/.badhost.com/")
689check_absolute_url("http://example.com/", "...badhost.com/", "http://example.com/...badhost.com/")
690check_absolute_url("http://example.com/a/b/c/d/", "../q", "http://example.com/a/b/c/q")
691check_absolute_url("http://example.com/a/b/c/d/", "../../q", "http://example.com/a/b/q")
692check_absolute_url("http://example.com/a/b/c/d/", "../../../q", "http://example.com/a/q")
693check_absolute_url("http://example.com", ".badhost.com", "http://example.com/.badhost.com")
694check_absolute_url("http://example.com/a/b/c/d/", "..//../../../q", "http://example.com/a/q")
695check_absolute_url("http://example.com/a/b/c/d/", "..//a/../../../../q", "http://example.com/a/q")
696check_absolute_url("http://example.com/a/b/c/d/", "..//a/..//../../../q", "http://example.com/a/b/q")
697check_absolute_url("http://example.com/a/b/c/d/", "..//a/..///../../../../q", "http://example.com/a/b/q")
698check_absolute_url("http://example.com/a/b/c/d/", "../x/a/../y/z/../../../../q", "http://example.com/a/b/q")
699 665
700print("testing path parsing and composition") 666print("testing path parsing and composition")
701check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) 667check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 })