Build with Lua 5.2 without LUA_COMPAT_MODULE flag.
LUASOCKET_USE_GLOBAL flag enable create global variables when load socket/mime modules.
This commit is contained in:
parent
bd51d8c1a5
commit
920bc97629
14 changed files with 179 additions and 115 deletions
37
src/url.lua
37
src/url.lua
|
@ -10,12 +10,15 @@
|
|||
local string = require("string")
|
||||
local base = _G
|
||||
local table = require("table")
|
||||
module("socket.url")
|
||||
local socket = require("socket")
|
||||
|
||||
socket.url = {}
|
||||
local _M = socket.url
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Module version
|
||||
-----------------------------------------------------------------------------
|
||||
_VERSION = "URL 1.0.2"
|
||||
_M._VERSION = "URL 1.0.2"
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Encodes a string into its escaped hexadecimal representation
|
||||
|
@ -24,7 +27,7 @@ _VERSION = "URL 1.0.2"
|
|||
-- Returns
|
||||
-- escaped representation of string binary
|
||||
-----------------------------------------------------------------------------
|
||||
function escape(s)
|
||||
function _M.escape(s)
|
||||
return (string.gsub(s, "([^A-Za-z0-9_])", function(c)
|
||||
return string.format("%%%02x", string.byte(c))
|
||||
end))
|
||||
|
@ -67,7 +70,7 @@ end
|
|||
-- Returns
|
||||
-- escaped representation of string binary
|
||||
-----------------------------------------------------------------------------
|
||||
function unescape(s)
|
||||
function _M.unescape(s)
|
||||
return (string.gsub(s, "%%(%x%x)", function(hex)
|
||||
return string.char(base.tonumber(hex, 16))
|
||||
end))
|
||||
|
@ -120,7 +123,7 @@ end
|
|||
-- Obs:
|
||||
-- the leading '/' in {/<path>} is considered part of <path>
|
||||
-----------------------------------------------------------------------------
|
||||
function parse(url, default)
|
||||
function _M.parse(url, default)
|
||||
-- initialize default parameters
|
||||
local parsed = {}
|
||||
for i,v in base.pairs(default or parsed) do parsed[i] = v end
|
||||
|
@ -179,9 +182,9 @@ end
|
|||
-- Returns
|
||||
-- a stringing with the corresponding URL
|
||||
-----------------------------------------------------------------------------
|
||||
function build(parsed)
|
||||
local ppath = parse_path(parsed.path or "")
|
||||
local url = build_path(ppath)
|
||||
function _M.build(parsed)
|
||||
local ppath = _M.parse_path(parsed.path or "")
|
||||
local url = _M.build_path(ppath)
|
||||
if parsed.params then url = url .. ";" .. parsed.params end
|
||||
if parsed.query then url = url .. "?" .. parsed.query end
|
||||
local authority = parsed.authority
|
||||
|
@ -215,14 +218,14 @@ end
|
|||
-- Returns
|
||||
-- corresponding absolute url
|
||||
-----------------------------------------------------------------------------
|
||||
function absolute(base_url, relative_url)
|
||||
function _M.absolute(base_url, relative_url)
|
||||
if base.type(base_url) == "table" then
|
||||
base_parsed = base_url
|
||||
base_url = build(base_parsed)
|
||||
base_url = _M.build(base_parsed)
|
||||
else
|
||||
base_parsed = parse(base_url)
|
||||
base_parsed = _M.parse(base_url)
|
||||
end
|
||||
local relative_parsed = parse(relative_url)
|
||||
local relative_parsed = _M.parse(relative_url)
|
||||
if not base_parsed then return relative_url
|
||||
elseif not relative_parsed then return base_url
|
||||
elseif relative_parsed.scheme then return relative_url
|
||||
|
@ -243,7 +246,7 @@ function absolute(base_url, relative_url)
|
|||
relative_parsed.path)
|
||||
end
|
||||
end
|
||||
return build(relative_parsed)
|
||||
return _M.build(relative_parsed)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -254,13 +257,13 @@ end
|
|||
-- Returns
|
||||
-- segment: a table with one entry per segment
|
||||
-----------------------------------------------------------------------------
|
||||
function parse_path(path)
|
||||
function _M.parse_path(path)
|
||||
local parsed = {}
|
||||
path = path or ""
|
||||
--path = string.gsub(path, "%s", "")
|
||||
string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end)
|
||||
for i = 1, #parsed do
|
||||
parsed[i] = unescape(parsed[i])
|
||||
parsed[i] = _M.unescape(parsed[i])
|
||||
end
|
||||
if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end
|
||||
if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end
|
||||
|
@ -275,7 +278,7 @@ end
|
|||
-- Returns
|
||||
-- path: corresponding path stringing
|
||||
-----------------------------------------------------------------------------
|
||||
function build_path(parsed, unsafe)
|
||||
function _M.build_path(parsed, unsafe)
|
||||
local path = ""
|
||||
local n = #parsed
|
||||
if unsafe then
|
||||
|
@ -300,3 +303,5 @@ function build_path(parsed, unsafe)
|
|||
if parsed.is_absolute then path = "/" .. path end
|
||||
return path
|
||||
end
|
||||
|
||||
return _M
|
Loading…
Add table
Add a link
Reference in a new issue