diff options
-rw-r--r-- | src/url.lua | 26 | ||||
-rw-r--r-- | test/urltest.lua | 25 |
2 files changed, 36 insertions, 15 deletions
diff --git a/src/url.lua b/src/url.lua index 110ea94..29b6734 100644 --- a/src/url.lua +++ b/src/url.lua | |||
@@ -88,20 +88,18 @@ local function absolute_path(base_path, relative_path) | |||
88 | if string.sub(relative_path, 1, 1) == "/" then return relative_path end | 88 | if string.sub(relative_path, 1, 1) == "/" then return relative_path end |
89 | local path = string.gsub(base_path, "[^/]*$", "") | 89 | local path = string.gsub(base_path, "[^/]*$", "") |
90 | path = path .. relative_path | 90 | path = path .. relative_path |
91 | path = string.gsub(path, "([^/]*%./)", function (s) | 91 | repeat |
92 | if s ~= "./" then return s else return "" end | 92 | local was = path |
93 | end) | 93 | path = path:gsub('/%./', '/') |
94 | path = string.gsub(path, "/%.$", "/") | 94 | until path == was |
95 | local reduced | 95 | repeat |
96 | while reduced ~= path do | 96 | local was = path |
97 | reduced = path | 97 | path = path:gsub('[^/]+/%.%./([^/]+)', '%1') |
98 | path = string.gsub(reduced, "([^/]*/%.%./)", function (s) | 98 | until path == was |
99 | if s ~= "../../" then return "" else return s end | 99 | path = path:gsub('[^/]+/%.%./*$', '') |
100 | end) | 100 | path = path:gsub('/%.%.$', '/') |
101 | end | 101 | path = path:gsub('/%.$', '/') |
102 | path = string.gsub(reduced, "([^/]*/%.%.)$", function (s) | 102 | path = path:gsub('^/%.%.', '') |
103 | if s ~= "../.." then return "" else return s end | ||
104 | end) | ||
105 | return path | 103 | return path |
106 | end | 104 | end |
107 | 105 | ||
diff --git a/test/urltest.lua b/test/urltest.lua index 1090a7e..63a33ea 100644 --- a/test/urltest.lua +++ b/test/urltest.lua | |||
@@ -61,7 +61,7 @@ end | |||
61 | local check_absolute_url = function(base, relative, absolute) | 61 | local 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 '", relative, "' expected '", | 64 | io.write("absolute: In test for base='", base, "', rel='", relative, "' expected '", |
65 | absolute, "' but got '", res, "'\n") | 65 | absolute, "' but got '", res, "'\n") |
66 | os.exit() | 66 | os.exit() |
67 | end | 67 | end |
@@ -637,6 +637,17 @@ check_absolute_url("http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x") | |||
637 | check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s") | 637 | check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s") |
638 | check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/") | 638 | check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/") |
639 | check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/") | 639 | check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/") |
640 | check_absolute_url("http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g") | ||
641 | check_absolute_url("http://a/b/c/d;p?q#f", "./g/", "http://a/b/c/g/") | ||
642 | check_absolute_url("http://a/b/c/d;p?q#f", "././g", "http://a/b/c/g") | ||
643 | check_absolute_url("http://a/b/c/d;p?q#f", "././g/", "http://a/b/c/g/") | ||
644 | check_absolute_url("http://a/b/c/d;p?q#f", "g/.", "http://a/b/c/g/") | ||
645 | check_absolute_url("http://a/b/c/d;p?q#f", "g/./", "http://a/b/c/g/") | ||
646 | check_absolute_url("http://a/b/c/d;p?q#f", "g/./.", "http://a/b/c/g/") | ||
647 | check_absolute_url("http://a/b/c/d;p?q#f", "g/././", "http://a/b/c/g/") | ||
648 | check_absolute_url("http://a/b/c/d;p?q#f", "./.", "http://a/b/c/") | ||
649 | check_absolute_url("http://a/b/c/d;p?q#f", "././.", "http://a/b/c/") | ||
650 | check_absolute_url("http://a/b/c/d;p?q#f", "././g/./.", "http://a/b/c/g/") | ||
640 | check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/") | 651 | check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/") |
641 | check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/") | 652 | check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/") |
642 | check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g") | 653 | 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/") | |||
655 | check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h") | 666 | check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h") |
656 | check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h") | 667 | check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h") |
657 | 668 | ||
669 | check_absolute_url("http://a/b/c/d:p?q#f/", "../g/", "http://a/b/g/") | ||
670 | check_absolute_url("http://a/b/c/d:p?q#f/", "../g", "http://a/b/g") | ||
671 | check_absolute_url("http://a/b/c/d:p?q#f/", "../.g/", "http://a/b/.g/") | ||
672 | check_absolute_url("http://a/b/c/d:p?q#f/", "../.g", "http://a/b/.g") | ||
673 | check_absolute_url("http://a/b/c/d:p?q#f/", "../.g.h/", "http://a/b/.g.h/") | ||
674 | check_absolute_url("http://a/b/c/d:p?q#f/", "../.g.h", "http://a/b/.g.h") | ||
675 | |||
676 | check_absolute_url("http://a/b/c/d:p?q#f/", "g.h/", "http://a/b/c/g.h/") | ||
677 | check_absolute_url("http://a/b/c/d:p?q#f/", "../g.h/", "http://a/b/g.h/") | ||
678 | check_absolute_url("http://a/", "../g.h/", "http://a/g.h/") | ||
679 | |||
658 | -- extra tests | 680 | -- extra tests |
659 | check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f") | 681 | check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f") |
660 | check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f") | 682 | 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") | |||
662 | check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f") | 684 | check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f") |
663 | check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html", | 685 | check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html", |
664 | "http://velox.telemar.com.br/dashboard/index.html") | 686 | "http://velox.telemar.com.br/dashboard/index.html") |
687 | check_absolute_url("http://example.com/", "../.badhost.com/", "http://example.com/.badhost.com/") | ||
665 | 688 | ||
666 | print("testing path parsing and composition") | 689 | print("testing path parsing and composition") |
667 | check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) | 690 | check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) |