diff options
Diffstat (limited to 'src/url.lua')
-rw-r--r-- | src/url.lua | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/src/url.lua b/src/url.lua index e17bcf5..0ecec3c 100644 --- a/src/url.lua +++ b/src/url.lua | |||
@@ -34,26 +34,30 @@ function Public.parse_url(url, default) | |||
34 | -- empty url is parsed to nil | 34 | -- empty url is parsed to nil |
35 | if not url or url == "" then return nil end | 35 | if not url or url == "" then return nil end |
36 | -- remove whitespace | 36 | -- remove whitespace |
37 | url = gsub(url, "%s", "") | 37 | url = string.gsub(url, "%s", "") |
38 | -- get fragment | 38 | -- get fragment |
39 | url = gsub(url, "#(.*)$", function(f) parsed.fragment = f end) | 39 | url = string.gsub(url, "#(.*)$", function(f) parsed.fragment = f end) |
40 | -- get scheme | 40 | -- get scheme |
41 | url = gsub(url, "^([%w][%w%+%-%.]*)%:", function(s) parsed.scheme = s end) | 41 | url = string.gsub(url, "^([%w][%w%+%-%.]*)%:", |
42 | function(s) parsed.scheme = s end) | ||
42 | -- get authority | 43 | -- get authority |
43 | url = gsub(url, "^//([^/]*)", function(n) parsed.authority = n end) | 44 | url = string.gsub(url, "^//([^/]*)", function(n) parsed.authority = n end) |
44 | -- get query string | 45 | -- get query stringing |
45 | url = gsub(url, "%?(.*)", function(q) parsed.query = q end) | 46 | url = string.gsub(url, "%?(.*)", function(q) parsed.query = q end) |
46 | -- get params | 47 | -- get params |
47 | url = gsub(url, "%;(.*)", function(p) parsed.params = p end) | 48 | url = string.gsub(url, "%;(.*)", function(p) parsed.params = p end) |
48 | if url ~= "" then parsed.path = url end | 49 | if url ~= "" then parsed.path = url end |
49 | local authority = parsed.authority | 50 | local authority = parsed.authority |
50 | if not authority then return parsed end | 51 | if not authority then return parsed end |
51 | authority = gsub(authority,"^([^@]*)@",function(u) parsed.userinfo = u end) | 52 | authority = string.gsub(authority,"^([^@]*)@", |
52 | authority = gsub(authority, ":([^:]*)$", function(p) parsed.port = p end) | 53 | function(u) parsed.userinfo = u end) |
54 | authority = string.gsub(authority, ":([^:]*)$", | ||
55 | function(p) parsed.port = p end) | ||
53 | if authority ~= "" then parsed.host = authority end | 56 | if authority ~= "" then parsed.host = authority end |
54 | local userinfo = parsed.userinfo | 57 | local userinfo = parsed.userinfo |
55 | if not userinfo then return parsed end | 58 | if not userinfo then return parsed end |
56 | userinfo = gsub(userinfo, ":([^:]*)$", function(p) parsed.password = p end) | 59 | userinfo = string.gsub(userinfo, ":([^:]*)$", |
60 | function(p) parsed.password = p end) | ||
57 | parsed.user = userinfo | 61 | parsed.user = userinfo |
58 | return parsed | 62 | return parsed |
59 | end | 63 | end |
@@ -64,7 +68,7 @@ end | |||
64 | -- Input | 68 | -- Input |
65 | -- parsed: parsed URL, as returned by Public.parse | 69 | -- parsed: parsed URL, as returned by Public.parse |
66 | -- Returns | 70 | -- Returns |
67 | -- a string with the corresponding URL | 71 | -- a stringing with the corresponding URL |
68 | ----------------------------------------------------------------------------- | 72 | ----------------------------------------------------------------------------- |
69 | function Public.build_url(parsed) | 73 | function Public.build_url(parsed) |
70 | local url = parsed.path or "" | 74 | local url = parsed.path or "" |
@@ -86,7 +90,7 @@ function Public.build_url(parsed) | |||
86 | if authority then url = "//" .. authority .. url end | 90 | if authority then url = "//" .. authority .. url end |
87 | if parsed.scheme then url = parsed.scheme .. ":" .. url end | 91 | if parsed.scheme then url = parsed.scheme .. ":" .. url end |
88 | if parsed.fragment then url = url .. "#" .. parsed.fragment end | 92 | if parsed.fragment then url = url .. "#" .. parsed.fragment end |
89 | url = gsub(url, "%s", "") | 93 | url = string.gsub(url, "%s", "") |
90 | return url | 94 | return url |
91 | end | 95 | end |
92 | 96 | ||
@@ -134,13 +138,13 @@ end | |||
134 | function Public.parse_path(path) | 138 | function Public.parse_path(path) |
135 | local parsed = {} | 139 | local parsed = {} |
136 | path = path or "" | 140 | path = path or "" |
137 | path = gsub(path, "%s", "") | 141 | path = string.gsub(path, "%s", "") |
138 | gsub(path, "([^/]+)", function (s) tinsert(parsed, s) end) | 142 | string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) |
139 | for i = 1, getn(parsed) do | 143 | for i = 1, table.getn(parsed) do |
140 | parsed[i] = Code.unescape(parsed[i]) | 144 | parsed[i] = Code.unescape(parsed[i]) |
141 | end | 145 | end |
142 | if strsub(path, 1, 1) == "/" then parsed.is_absolute = 1 end | 146 | if stringsub(path, 1, 1) == "/" then parsed.is_absolute = 1 end |
143 | if strsub(path, -1, -1) == "/" then parsed.is_directory = 1 end | 147 | if stringsub(path, -1, -1) == "/" then parsed.is_directory = 1 end |
144 | return parsed | 148 | return parsed |
145 | end | 149 | end |
146 | 150 | ||
@@ -150,11 +154,11 @@ end | |||
150 | -- parsed: path segments | 154 | -- parsed: path segments |
151 | -- unsafe: if true, segments are not protected before path is built | 155 | -- unsafe: if true, segments are not protected before path is built |
152 | -- Returns | 156 | -- Returns |
153 | -- path: correspondin path string | 157 | -- path: correspondin path stringing |
154 | ----------------------------------------------------------------------------- | 158 | ----------------------------------------------------------------------------- |
155 | function Public.build_path(parsed, unsafe) | 159 | function Public.build_path(parsed, unsafe) |
156 | local path = "" | 160 | local path = "" |
157 | local n = getn(parsed) | 161 | local n = table.getn(parsed) |
158 | if unsafe then | 162 | if unsafe then |
159 | for i = 1, n-1 do | 163 | for i = 1, n-1 do |
160 | path = path .. parsed[i] | 164 | path = path .. parsed[i] |
@@ -178,10 +182,10 @@ function Public.build_path(parsed, unsafe) | |||
178 | return path | 182 | return path |
179 | end | 183 | end |
180 | 184 | ||
181 | function Private.make_set(table) | 185 | function Private.make_set(t) |
182 | local s = {} | 186 | local s = {} |
183 | for i = 1, getn(table) do | 187 | for i = 1, table.getn(t) do |
184 | s[table[i]] = 1 | 188 | s[t[i]] = 1 |
185 | end | 189 | end |
186 | return s | 190 | return s |
187 | end | 191 | end |
@@ -195,7 +199,7 @@ Private.segment_set = Private.make_set { | |||
195 | 199 | ||
196 | function Private.protect_segment(s) | 200 | function Private.protect_segment(s) |
197 | local segment_set = Private.segment_set | 201 | local segment_set = Private.segment_set |
198 | return gsub(s, "(%W)", function (c) | 202 | return string.gsub(s, "(%W)", function (c) |
199 | if segment_set[c] then return c | 203 | if segment_set[c] then return c |
200 | else return Code.escape(c) end | 204 | else return Code.escape(c) end |
201 | end) | 205 | end) |
@@ -210,21 +214,21 @@ end | |||
210 | -- corresponding absolute path | 214 | -- corresponding absolute path |
211 | ----------------------------------------------------------------------------- | 215 | ----------------------------------------------------------------------------- |
212 | function Private.absolute_path(base_path, relative_path) | 216 | function Private.absolute_path(base_path, relative_path) |
213 | if strsub(relative_path, 1, 1) == "/" then return relative_path end | 217 | if stringsub(relative_path, 1, 1) == "/" then return relative_path end |
214 | local path = gsub(base_path, "[^/]*$", "") | 218 | local path = string.gsub(base_path, "[^/]*$", "") |
215 | path = path .. relative_path | 219 | path = path .. relative_path |
216 | path = gsub(path, "([^/]*%./)", function (s) | 220 | path = string.gsub(path, "([^/]*%./)", function (s) |
217 | if s ~= "./" then return s else return "" end | 221 | if s ~= "./" then return s else return "" end |
218 | end) | 222 | end) |
219 | path = gsub(path, "/%.$", "/") | 223 | path = string.gsub(path, "/%.$", "/") |
220 | local reduced | 224 | local reduced |
221 | while reduced ~= path do | 225 | while reduced ~= path do |
222 | reduced = path | 226 | reduced = path |
223 | path = gsub(reduced, "([^/]*/%.%./)", function (s) | 227 | path = string.gsub(reduced, "([^/]*/%.%./)", function (s) |
224 | if s ~= "../../" then return "" else return s end | 228 | if s ~= "../../" then return "" else return s end |
225 | end) | 229 | end) |
226 | end | 230 | end |
227 | path = gsub(reduced, "([^/]*/%.%.)$", function (s) | 231 | path = string.gsub(reduced, "([^/]*/%.%.)$", function (s) |
228 | if s ~= "../.." then return "" else return s end | 232 | if s ~= "../.." then return "" else return s end |
229 | end) | 233 | end) |
230 | return path | 234 | return path |