diff options
author | moteus <mimir@newmail.ru> | 2013-05-27 12:45:09 +0400 |
---|---|---|
committer | moteus <mimir@newmail.ru> | 2013-05-27 12:45:09 +0400 |
commit | 920bc9762954454468d056a61391635cbd849406 (patch) | |
tree | 048c112f95fa578d239ae3e35b2aec0a482f0c23 /src/url.lua | |
parent | bd51d8c1a5bb30e6a358dee6e85963f777edfff4 (diff) | |
download | luasocket-920bc9762954454468d056a61391635cbd849406.tar.gz luasocket-920bc9762954454468d056a61391635cbd849406.tar.bz2 luasocket-920bc9762954454468d056a61391635cbd849406.zip |
Build with Lua 5.2 without LUA_COMPAT_MODULE flag.
LUASOCKET_USE_GLOBAL flag enable create global variables when load socket/mime modules.
Diffstat (limited to 'src/url.lua')
-rw-r--r-- | src/url.lua | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/src/url.lua b/src/url.lua index 6ca6d68..3c4f85d 100644 --- a/src/url.lua +++ b/src/url.lua | |||
@@ -10,12 +10,15 @@ | |||
10 | local string = require("string") | 10 | local string = require("string") |
11 | local base = _G | 11 | local base = _G |
12 | local table = require("table") | 12 | local table = require("table") |
13 | module("socket.url") | 13 | local socket = require("socket") |
14 | |||
15 | socket.url = {} | ||
16 | local _M = socket.url | ||
14 | 17 | ||
15 | ----------------------------------------------------------------------------- | 18 | ----------------------------------------------------------------------------- |
16 | -- Module version | 19 | -- Module version |
17 | ----------------------------------------------------------------------------- | 20 | ----------------------------------------------------------------------------- |
18 | _VERSION = "URL 1.0.2" | 21 | _M._VERSION = "URL 1.0.2" |
19 | 22 | ||
20 | ----------------------------------------------------------------------------- | 23 | ----------------------------------------------------------------------------- |
21 | -- Encodes a string into its escaped hexadecimal representation | 24 | -- Encodes a string into its escaped hexadecimal representation |
@@ -24,7 +27,7 @@ _VERSION = "URL 1.0.2" | |||
24 | -- Returns | 27 | -- Returns |
25 | -- escaped representation of string binary | 28 | -- escaped representation of string binary |
26 | ----------------------------------------------------------------------------- | 29 | ----------------------------------------------------------------------------- |
27 | function escape(s) | 30 | function _M.escape(s) |
28 | return (string.gsub(s, "([^A-Za-z0-9_])", function(c) | 31 | return (string.gsub(s, "([^A-Za-z0-9_])", function(c) |
29 | return string.format("%%%02x", string.byte(c)) | 32 | return string.format("%%%02x", string.byte(c)) |
30 | end)) | 33 | end)) |
@@ -67,7 +70,7 @@ end | |||
67 | -- Returns | 70 | -- Returns |
68 | -- escaped representation of string binary | 71 | -- escaped representation of string binary |
69 | ----------------------------------------------------------------------------- | 72 | ----------------------------------------------------------------------------- |
70 | function unescape(s) | 73 | function _M.unescape(s) |
71 | return (string.gsub(s, "%%(%x%x)", function(hex) | 74 | return (string.gsub(s, "%%(%x%x)", function(hex) |
72 | return string.char(base.tonumber(hex, 16)) | 75 | return string.char(base.tonumber(hex, 16)) |
73 | end)) | 76 | end)) |
@@ -120,7 +123,7 @@ end | |||
120 | -- Obs: | 123 | -- Obs: |
121 | -- the leading '/' in {/<path>} is considered part of <path> | 124 | -- the leading '/' in {/<path>} is considered part of <path> |
122 | ----------------------------------------------------------------------------- | 125 | ----------------------------------------------------------------------------- |
123 | function parse(url, default) | 126 | function _M.parse(url, default) |
124 | -- initialize default parameters | 127 | -- initialize default parameters |
125 | local parsed = {} | 128 | local parsed = {} |
126 | for i,v in base.pairs(default or parsed) do parsed[i] = v end | 129 | for i,v in base.pairs(default or parsed) do parsed[i] = v end |
@@ -179,9 +182,9 @@ end | |||
179 | -- Returns | 182 | -- Returns |
180 | -- a stringing with the corresponding URL | 183 | -- a stringing with the corresponding URL |
181 | ----------------------------------------------------------------------------- | 184 | ----------------------------------------------------------------------------- |
182 | function build(parsed) | 185 | function _M.build(parsed) |
183 | local ppath = parse_path(parsed.path or "") | 186 | local ppath = _M.parse_path(parsed.path or "") |
184 | local url = build_path(ppath) | 187 | local url = _M.build_path(ppath) |
185 | if parsed.params then url = url .. ";" .. parsed.params end | 188 | if parsed.params then url = url .. ";" .. parsed.params end |
186 | if parsed.query then url = url .. "?" .. parsed.query end | 189 | if parsed.query then url = url .. "?" .. parsed.query end |
187 | local authority = parsed.authority | 190 | local authority = parsed.authority |
@@ -215,14 +218,14 @@ end | |||
215 | -- Returns | 218 | -- Returns |
216 | -- corresponding absolute url | 219 | -- corresponding absolute url |
217 | ----------------------------------------------------------------------------- | 220 | ----------------------------------------------------------------------------- |
218 | function absolute(base_url, relative_url) | 221 | function _M.absolute(base_url, relative_url) |
219 | if base.type(base_url) == "table" then | 222 | if base.type(base_url) == "table" then |
220 | base_parsed = base_url | 223 | base_parsed = base_url |
221 | base_url = build(base_parsed) | 224 | base_url = _M.build(base_parsed) |
222 | else | 225 | else |
223 | base_parsed = parse(base_url) | 226 | base_parsed = _M.parse(base_url) |
224 | end | 227 | end |
225 | local relative_parsed = parse(relative_url) | 228 | local relative_parsed = _M.parse(relative_url) |
226 | if not base_parsed then return relative_url | 229 | if not base_parsed then return relative_url |
227 | elseif not relative_parsed then return base_url | 230 | elseif not relative_parsed then return base_url |
228 | elseif relative_parsed.scheme then return relative_url | 231 | elseif relative_parsed.scheme then return relative_url |
@@ -243,7 +246,7 @@ function absolute(base_url, relative_url) | |||
243 | relative_parsed.path) | 246 | relative_parsed.path) |
244 | end | 247 | end |
245 | end | 248 | end |
246 | return build(relative_parsed) | 249 | return _M.build(relative_parsed) |
247 | end | 250 | end |
248 | end | 251 | end |
249 | 252 | ||
@@ -254,13 +257,13 @@ end | |||
254 | -- Returns | 257 | -- Returns |
255 | -- segment: a table with one entry per segment | 258 | -- segment: a table with one entry per segment |
256 | ----------------------------------------------------------------------------- | 259 | ----------------------------------------------------------------------------- |
257 | function parse_path(path) | 260 | function _M.parse_path(path) |
258 | local parsed = {} | 261 | local parsed = {} |
259 | path = path or "" | 262 | path = path or "" |
260 | --path = string.gsub(path, "%s", "") | 263 | --path = string.gsub(path, "%s", "") |
261 | string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) | 264 | string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) |
262 | for i = 1, #parsed do | 265 | for i = 1, #parsed do |
263 | parsed[i] = unescape(parsed[i]) | 266 | parsed[i] = _M.unescape(parsed[i]) |
264 | end | 267 | end |
265 | if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end | 268 | if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end |
266 | if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end | 269 | if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end |
@@ -275,7 +278,7 @@ end | |||
275 | -- Returns | 278 | -- Returns |
276 | -- path: corresponding path stringing | 279 | -- path: corresponding path stringing |
277 | ----------------------------------------------------------------------------- | 280 | ----------------------------------------------------------------------------- |
278 | function build_path(parsed, unsafe) | 281 | function _M.build_path(parsed, unsafe) |
279 | local path = "" | 282 | local path = "" |
280 | local n = #parsed | 283 | local n = #parsed |
281 | if unsafe then | 284 | if unsafe then |
@@ -300,3 +303,5 @@ function build_path(parsed, unsafe) | |||
300 | if parsed.is_absolute then path = "/" .. path end | 303 | if parsed.is_absolute then path = "/" .. path end |
301 | return path | 304 | return path |
302 | end | 305 | end |
306 | |||
307 | return _M \ No newline at end of file | ||