| 1 |
local string = require("string") |
| 2 |
local base = _G |
| 3 |
local table = require("table") |
| 4 |
local socket = socket or require("socket") |
| 5 |
socket.url = {} |
| 6 |
local _M = socket.url |
| 7 |
module("socket.url") |
| 8 |
_M._VERSION = "URL 1.0.3" |
| 9 |
function _M.escape(s) |
| 10 |
return (string.gsub(s, "([^A-Za-z0-9_])", function(c) |
| 11 |
return string.format("%%%02x", string.byte(c)) |
| 12 |
end)) |
| 13 |
end |
| 14 |
local function make_set(t) |
| 15 |
local s = {} |
| 16 |
for i,v in base.ipairs(t) do |
| 17 |
s[t[i]] = 1 |
| 18 |
end |
| 19 |
return s |
| 20 |
end |
| 21 |
local segment_set = make_set { |
| 22 |
"-", "_", ".", "!", "~", "*", "'", "(", |
| 23 |
")", ":", "@", "&", "=", "+", "$", ",", |
| 24 |
} |
| 25 |
local function protect_segment(s) |
| 26 |
return string.gsub(s, "([^A-Za-z0-9_])", function (c) |
| 27 |
if segment_set[c] then return c |
| 28 |
else return string.format("%%%02X", string.byte(c)) end |
| 29 |
end) |
| 30 |
end |
| 31 |
function _M.unescape(s) |
| 32 |
return (string.gsub(s, "%%(%x%x)", function(hex) |
| 33 |
return string.char(base.tonumber(hex, 16)) |
| 34 |
end)) |
| 35 |
end |
| 36 |
local function absolute_path(base_path, relative_path) |
| 37 |
if string.sub(relative_path, 1, 1) == "/" then return relative_path end |
| 38 |
local path = string.gsub(base_path, "[^/]*$", "") |
| 39 |
path = path .. relative_path |
| 40 |
path = string.gsub(path, "([^/]*%./)", function (s) |
| 41 |
if s ~= "./" then return s else return "" end |
| 42 |
end) |
| 43 |
path = string.gsub(path, "/%.$", "/") |
| 44 |
local reduced |
| 45 |
while reduced ~= path do |
| 46 |
reduced = path |
| 47 |
path = string.gsub(reduced, "([^/]*/%.%./)", function (s) |
| 48 |
if s ~= "../../" then return "" else return s end |
| 49 |
end) |
| 50 |
end |
| 51 |
path = string.gsub(reduced, "([^/]*/%.%.)$", function (s) |
| 52 |
if s ~= "../.." then return "" else return s end |
| 53 |
end) |
| 54 |
return path |
| 55 |
end |
| 56 |
function _M.parse(url, default) |
| 57 |
local parsed = {} |
| 58 |
for i,v in base.pairs(default or parsed) do parsed[i] = v end |
| 59 |
if not url or url == "" then return nil, "invalid url" end |
| 60 |
url = string.gsub(url, "#(.*)$", function(f) |
| 61 |
parsed.fragment = f |
| 62 |
return "" |
| 63 |
end) |
| 64 |
url = string.gsub(url, "^([%w][%w%+%-%.]*)%:", |
| 65 |
function(s) parsed.scheme = s; return "" end) |
| 66 |
url = string.gsub(url, "^//([^/]*)", function(n) |
| 67 |
parsed.authority = n |
| 68 |
return "" |
| 69 |
end) |
| 70 |
url = string.gsub(url, "%?(.*)", function(q) |
| 71 |
parsed.query = q |
| 72 |
return "" |
| 73 |
end) |
| 74 |
url = string.gsub(url, "%;(.*)", function(p) |
| 75 |
parsed.params = p |
| 76 |
return "" |
| 77 |
end) |
| 78 |
if url ~= "" then parsed.path = url end |
| 79 |
local authority = parsed.authority |
| 80 |
if not authority then return parsed end |
| 81 |
authority = string.gsub(authority,"^([^@]*)@", |
| 82 |
function(u) parsed.userinfo = u; return "" end) |
| 83 |
authority = string.gsub(authority, ":([^:%]]*)$", |
| 84 |
function(p) parsed.port = p; return "" end) |
| 85 |
if authority ~= "" then |
| 86 |
parsed.host = string.match(authority, "^%[(.+)%]$") or authority |
| 87 |
end |
| 88 |
local userinfo = parsed.userinfo |
| 89 |
if not userinfo then return parsed end |
| 90 |
userinfo = string.gsub(userinfo, ":([^:]*)$", |
| 91 |
function(p) parsed.password = p; return "" end) |
| 92 |
parsed.user = userinfo |
| 93 |
return parsed |
| 94 |
end |
| 95 |
function _M.build(parsed) |
| 96 |
local url = parsed.path or "" |
| 97 |
if parsed.params then url = url .. ";" .. parsed.params end |
| 98 |
if parsed.query then url = url .. "?" .. parsed.query end |
| 99 |
local authority = parsed.authority |
| 100 |
if parsed.host then |
| 101 |
authority = parsed.host |
| 102 |
if string.find(authority, ":") then |
| 103 |
authority = "[" .. authority .. "]" |
| 104 |
end |
| 105 |
if parsed.port then authority = authority .. ":" .. base.tostring(parsed.port) end |
| 106 |
local userinfo = parsed.userinfo |
| 107 |
if parsed.user then |
| 108 |
userinfo = parsed.user |
| 109 |
if parsed.password then |
| 110 |
userinfo = userinfo .. ":" .. parsed.password |
| 111 |
end |
| 112 |
end |
| 113 |
if userinfo then authority = userinfo .. "@" .. authority end |
| 114 |
end |
| 115 |
if authority then url = "//" .. authority .. url end |
| 116 |
if parsed.scheme then url = parsed.scheme .. ":" .. url end |
| 117 |
if parsed.fragment then url = url .. "#" .. parsed.fragment end |
| 118 |
return url |
| 119 |
end |
| 120 |
function _M.absolute(base_url, relative_url) |
| 121 |
local base_parsed |
| 122 |
if base.type(base_url) == "table" then |
| 123 |
base_parsed = base_url |
| 124 |
base_url = _M.build(base_parsed) |
| 125 |
else |
| 126 |
base_parsed = _M.parse(base_url) |
| 127 |
end |
| 128 |
local relative_parsed = _M.parse(relative_url) |
| 129 |
if not base_parsed then return relative_url |
| 130 |
elseif not relative_parsed then return base_url |
| 131 |
elseif relative_parsed.scheme then return relative_url |
| 132 |
else |
| 133 |
relative_parsed.scheme = base_parsed.scheme |
| 134 |
if not relative_parsed.authority then |
| 135 |
relative_parsed.authority = base_parsed.authority |
| 136 |
if not relative_parsed.path then |
| 137 |
relative_parsed.path = base_parsed.path |
| 138 |
if not relative_parsed.params then |
| 139 |
relative_parsed.params = base_parsed.params |
| 140 |
if not relative_parsed.query then |
| 141 |
relative_parsed.query = base_parsed.query |
| 142 |
end |
| 143 |
end |
| 144 |
else |
| 145 |
relative_parsed.path = absolute_path(base_parsed.path or "", |
| 146 |
relative_parsed.path) |
| 147 |
end |
| 148 |
end |
| 149 |
return _M.build(relative_parsed) |
| 150 |
end |
| 151 |
end |
| 152 |
function _M.parse_path(path) |
| 153 |
local parsed = {} |
| 154 |
path = path or "" |
| 155 |
string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) |
| 156 |
for i = 1, #parsed do |
| 157 |
parsed[i] = _M.unescape(parsed[i]) |
| 158 |
end |
| 159 |
if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end |
| 160 |
if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end |
| 161 |
return parsed |
| 162 |
end |
| 163 |
function _M.build_path(parsed, unsafe) |
| 164 |
local path = "" |
| 165 |
local n = #parsed |
| 166 |
if unsafe then |
| 167 |
for i = 1, n-1 do |
| 168 |
path = path .. parsed[i] |
| 169 |
path = path .. "/" |
| 170 |
end |
| 171 |
if n > 0 then |
| 172 |
path = path .. parsed[n] |
| 173 |
if parsed.is_directory then path = path .. "/" end |
| 174 |
end |
| 175 |
else |
| 176 |
for i = 1, n-1 do |
| 177 |
path = path .. protect_segment(parsed[i]) |
| 178 |
path = path .. "/" |
| 179 |
end |
| 180 |
if n > 0 then |
| 181 |
path = path .. protect_segment(parsed[n]) |
| 182 |
if parsed.is_directory then path = path .. "/" end |
| 183 |
end |
| 184 |
end |
| 185 |
if parsed.is_absolute then path = "/" .. path end |
| 186 |
return path |
| 187 |
end |
| 188 |
socket.url = _M |
| 189 |
return _M |