-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhelper.lua
More file actions
177 lines (157 loc) · 5.16 KB
/
helper.lua
File metadata and controls
177 lines (157 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
--[[--------------------------------------------------------------------------
luahttpd: Lua-powered httpd server project
Copyright (C) 2013 AfterLifeLochie <afterlifelochie@afterlifelochie.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
--------------------------------------------------------------------------]]--
-- PHP-like split() function, probably has issues with non-sanitized
-- patterns.
function split(sString, sSeparator, nMax, bRegexp)
assert(sSeparator ~= '')
assert(nMax == nil or nMax >= 1)
if (sString == nil) then error("String expected!", 2) end
local aRecord = {}
if sString:len() > 0 then
local bPlain = not bRegexp
nMax = nMax or -1
local nField = 1 nStart = 1
local nFirst, nLast = string.find(sString, sSeparator, nStart, bPlain)
while nFirst and nMax ~= 0 do
aRecord[nField] = string.sub(sString, nStart, nFirst-1)
nField = nField + 1
nStart = nLast + 1
nFirst,nLast = string.find(sString, sSeparator, nStart, bPlain)
nMax = nMax - 1
end
aRecord[nField] = string.sub(sString, nStart)
end
return aRecord
end
-- Gets a module file
function getModule(name)
local modpath = "module/" .. name .. ".mod.lua"
local fhandle, ex = io.open(modpath, "r")
if not fhandle then error(ex) end
local fdata = fhandle:read("*a")
fhandle:close()
local fx, ex = loadstring(fdata, "module-" .. name)
if not fx then error(ex) end
setfenv(fx, _G)
local result = fx()
if (type(result) == "table") then
for k, v in pairs(result) do
if (type(v) == "function") then setfenv(v, _G) end
end
else
error("Unexpected module data; expected table, got " .. (type(result) or 'nil'))
end
return result
end
-- Parses a path
function parsePath(path)
local fragments = split(path:gsub("\\","/"), "/")
local parsed = {}
local parsedIndex = 1
for i, name in ipairs(fragments) do
if name == "" or name == "." then
elseif name == ".." then
if parsedIndex == 1 then
error("Underflow: no previous directory")
else
parsedIndex = parsedIndex - 1
parsed[parsedIndex] = nil
end
else
parsed[parsedIndex] = name
parsedIndex = parsedIndex + 1
end
end
return parsed
end
-- Copy a table
table.copy = function(t)
local result = {}
for k, v in pairs(t) do
if (type(v) == "table") then
if (k ~= "_G") and (k ~= "_M") and (k ~= "package") then result[k] = table.copy(v) end
else result[k] = v end
end
return result
end
-- Pad a string
string.pad = function(str, len, padWith, padEnd)
if (#str == len) then return str end
if (#str > len) then return str end
while (#str < len) do if (padEnd) then str = str .. padWith else str = padWith .. str end end
return str
end
-- HTTP URL utilities
url = {}
url.decode = function(str)
-- Encoded URL: `+` => ` `, `%xx` => char(xx), `\r\n` => `\n`
str = string.gsub(str, "+", " ")
str = string.gsub(str, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
str = string.gsub(str, "\r\n", "\n")
return str
end
url.encode = function(str)
-- Decoded URL: ` ` => `+`, char(xx) => `%xx`, `\n` (not `\r\n) => `\r\n`
str = string.gsub(str, "\n", "\r\n")
str = string.gsub(str, "([^0-9a-zA-Z ])",
function(c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub(str, " ", "+")
return str
end
url.insertfield = function(args, name, value)
if not args[name] then
args[name] = value
else
local t = type(args[name])
if t == "string" then
args[name] = { args[name], value, }
elseif t == "table" then
table.insert (args[name], value)
end
end
end
-- Parses an encoded blob
url.parse = function(query, args)
if type(query) == "string" then
local insertfield, decode = url.insertfield, url.decode
string.gsub(query, "([^&=]+)=([^&=]*)&?", function(key, val)
url.insertfield(args, decode(key), decode(val))
end)
end
end
-- Encodes a table
url.encodetable = function(args)
if args == nil or next(args) == nil then return "" end
local strp = ""
for key, vals in pairs(args) do
if type(vals) ~= "table" then vals = { vals } end
for i, val in ipairs(vals) do strp = strp .. "&" .. url.encode(key) .. "=" .. url.encode(val) end
end
return string.sub(strp, 2)
end
-- Pattern utilities
patterns = {}
-- Sanitizes a string so it won't be treated as a pattern
-- in things like string.gsub
patterns.sanitize = function(args)
if args == nil or type(args) ~= "string" then return "" end
local result = args
result = string.gsub(result, "%%", "%%")
for _, i in pairs({ "(", ")", ".", "+", "-", "*", "?", "[", "^", "$" }) do
result = string.gsub(result, "%" .. i, "%%" .. i)
end
return result
end