diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2011-05-25 20:57:22 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2011-05-25 20:57:22 +0000 |
commit | 3a8ba90dfb0c2eb224f317dd692ede426691e72a (patch) | |
tree | fe1cc5379a2e0e031663fe9c15d908653844bc73 /src/url.lua | |
parent | bce60be30fe8e9c1b0eb33128c23c93d7bca5303 (diff) | |
download | luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.tar.gz luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.tar.bz2 luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.zip |
Saving before big changes to support IPv6.
Diffstat (limited to 'src/url.lua')
-rw-r--r-- | src/url.lua | 122 |
1 files changed, 64 insertions, 58 deletions
diff --git a/src/url.lua b/src/url.lua index 18e5ab2..7623557 100644 --- a/src/url.lua +++ b/src/url.lua | |||
@@ -40,25 +40,25 @@ end | |||
40 | -- escaped representation of string binary | 40 | -- escaped representation of string binary |
41 | ----------------------------------------------------------------------------- | 41 | ----------------------------------------------------------------------------- |
42 | local function make_set(t) | 42 | local function make_set(t) |
43 | local s = {} | 43 | local s = {} |
44 | for i,v in base.ipairs(t) do | 44 | for i,v in base.ipairs(t) do |
45 | s[t[i]] = 1 | 45 | s[t[i]] = 1 |
46 | end | 46 | end |
47 | return s | 47 | return s |
48 | end | 48 | end |
49 | 49 | ||
50 | -- these are allowed withing a path segment, along with alphanum | 50 | -- these are allowed withing a path segment, along with alphanum |
51 | -- other characters must be escaped | 51 | -- other characters must be escaped |
52 | local segment_set = make_set { | 52 | local segment_set = make_set { |
53 | "-", "_", ".", "!", "~", "*", "'", "(", | 53 | "-", "_", ".", "!", "~", "*", "'", "(", |
54 | ")", ":", "@", "&", "=", "+", "$", ",", | 54 | ")", ":", "@", "&", "=", "+", "$", ",", |
55 | } | 55 | } |
56 | 56 | ||
57 | local function protect_segment(s) | 57 | local function protect_segment(s) |
58 | return string.gsub(s, "([^A-Za-z0-9_])", function (c) | 58 | return string.gsub(s, "([^A-Za-z0-9_])", function (c) |
59 | if segment_set[c] then return c | 59 | if segment_set[c] then return c |
60 | else return string.format("%%%02x", string.byte(c)) end | 60 | else return string.format("%%%02x", string.byte(c)) end |
61 | end) | 61 | end) |
62 | end | 62 | end |
63 | 63 | ||
64 | ----------------------------------------------------------------------------- | 64 | ----------------------------------------------------------------------------- |
@@ -182,20 +182,26 @@ function build(parsed) | |||
182 | local url = build_path(ppath) | 182 | local url = build_path(ppath) |
183 | if parsed.params then url = url .. ";" .. parsed.params end | 183 | if parsed.params then url = url .. ";" .. parsed.params end |
184 | if parsed.query then url = url .. "?" .. parsed.query end | 184 | if parsed.query then url = url .. "?" .. parsed.query end |
185 | local authority = parsed.authority | 185 | local authority = parsed.authority |
186 | if parsed.host then | 186 | if parsed.host then |
187 | authority = parsed.host | 187 | authority = parsed.host |
188 | if parsed.port then authority = authority .. ":" .. parsed.port end | 188 | if parsed.port then authority = authority .. ":" .. parsed.port end |
189 | local userinfo = parsed.userinfo | 189 | local userinfo = parsed.userinfo |
190 | if parsed.user then | 190 | if parsed.user then |
191 | userinfo = parsed.user | 191 | userinfo = parsed.user |
192 | if parsed.password then | 192 | if parsed.password then |
193 | userinfo = userinfo .. ":" .. parsed.password | 193 | userinfo = userinfo .. ":" .. parsed.password |
194 | end | 194 | end |
195 | end | 195 | end |
196 | if userinfo then authority = userinfo .. "@" .. authority end | 196 | if userinfo then authority = userinfo .. "@" .. authority end |
197 | end | 197 | end |
198 | if authority then url = "//" .. authority .. url end | 198 | if authority then |
199 | if string.sub(url, 1, 1) == "/" then | ||
200 | url = "//" .. authority .. url | ||
201 | else | ||
202 | url = "//" .. authority .. "/" .. url | ||
203 | end | ||
204 | end | ||
199 | if parsed.scheme then url = parsed.scheme .. ":" .. url end | 205 | if parsed.scheme then url = parsed.scheme .. ":" .. url end |
200 | if parsed.fragment then url = url .. "#" .. parsed.fragment end | 206 | if parsed.fragment then url = url .. "#" .. parsed.fragment end |
201 | -- url = string.gsub(url, "%s", "") | 207 | -- url = string.gsub(url, "%s", "") |
@@ -211,8 +217,8 @@ end | |||
211 | -- corresponding absolute url | 217 | -- corresponding absolute url |
212 | ----------------------------------------------------------------------------- | 218 | ----------------------------------------------------------------------------- |
213 | function absolute(base_url, relative_url) | 219 | function absolute(base_url, relative_url) |
220 | local base_parsed = base_url | ||
214 | if base.type(base_url) == "table" then | 221 | if base.type(base_url) == "table" then |
215 | base_parsed = base_url | ||
216 | base_url = build(base_parsed) | 222 | base_url = build(base_parsed) |
217 | else | 223 | else |
218 | base_parsed = parse(base_url) | 224 | base_parsed = parse(base_url) |
@@ -250,16 +256,16 @@ end | |||
250 | -- segment: a table with one entry per segment | 256 | -- segment: a table with one entry per segment |
251 | ----------------------------------------------------------------------------- | 257 | ----------------------------------------------------------------------------- |
252 | function parse_path(path) | 258 | function parse_path(path) |
253 | local parsed = {} | 259 | local parsed = {} |
254 | path = path or "" | 260 | path = path or "" |
255 | --path = string.gsub(path, "%s", "") | 261 | --path = string.gsub(path, "%s", "") |
256 | string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) | 262 | string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) |
257 | for i = 1, table.getn(parsed) do | 263 | for i = 1, table.getn(parsed) do |
258 | parsed[i] = unescape(parsed[i]) | 264 | parsed[i] = unescape(parsed[i]) |
259 | end | 265 | end |
260 | if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end | 266 | if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end |
261 | if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end | 267 | if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end |
262 | return parsed | 268 | return parsed |
263 | end | 269 | end |
264 | 270 | ||
265 | ----------------------------------------------------------------------------- | 271 | ----------------------------------------------------------------------------- |
@@ -271,27 +277,27 @@ end | |||
271 | -- path: corresponding path stringing | 277 | -- path: corresponding path stringing |
272 | ----------------------------------------------------------------------------- | 278 | ----------------------------------------------------------------------------- |
273 | function build_path(parsed, unsafe) | 279 | function build_path(parsed, unsafe) |
274 | local path = "" | 280 | local path = "" |
275 | local n = table.getn(parsed) | 281 | local n = table.getn(parsed) |
276 | if unsafe then | 282 | if unsafe then |
277 | for i = 1, n-1 do | 283 | for i = 1, n-1 do |
278 | path = path .. parsed[i] | 284 | path = path .. parsed[i] |
279 | path = path .. "/" | 285 | path = path .. "/" |
280 | end | 286 | end |
281 | if n > 0 then | 287 | if n > 0 then |
282 | path = path .. parsed[n] | 288 | path = path .. parsed[n] |
283 | if parsed.is_directory then path = path .. "/" end | 289 | if parsed.is_directory then path = path .. "/" end |
284 | end | 290 | end |
285 | else | 291 | else |
286 | for i = 1, n-1 do | 292 | for i = 1, n-1 do |
287 | path = path .. protect_segment(parsed[i]) | 293 | path = path .. protect_segment(parsed[i]) |
288 | path = path .. "/" | 294 | path = path .. "/" |
289 | end | 295 | end |
290 | if n > 0 then | 296 | if n > 0 then |
291 | path = path .. protect_segment(parsed[n]) | 297 | path = path .. protect_segment(parsed[n]) |
292 | if parsed.is_directory then path = path .. "/" end | 298 | if parsed.is_directory then path = path .. "/" end |
293 | end | 299 | end |
294 | end | 300 | end |
295 | if parsed.is_absolute then path = "/" .. path end | 301 | if parsed.is_absolute then path = "/" .. path end |
296 | return path | 302 | return path |
297 | end | 303 | end |