Repository navigation
Expand file tree
/
Copy pathutility.lua
More file actions
153 lines (132 loc) · 3.28 KB
/
Copy pathutility.lua
File metadata and controls
153 lines (132 loc) · 3.28 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
local utility = {}
function utility.selectedFilter(filters)
for _, filter in ipairs(filters) do
if filter.selected then
return filter.name
end
end
return 'All'
end
function utility.hasCompleteTask(todos)
for _, todo in ipairs(todos) do
if todo.done then
return true
end
end
return false
end
function utility.countNotDone(todos)
local count = 0
for _, todo in pairs(todos) do
if not todo.done then
count = count + 1
end
end
return count
end
function utility.defChecked(todos)
local uncompletedCount = utility.countNotDone(todos)
local defaultChecked = false
if uncompletedCount == 0 and #todos > 0 then
defaultChecked = true
end
return defaultChecked
end
function utility.map(a, fcn)
local b = {}
for _, v in ipairs(a) do
table.insert(b, fcn(v))
end
return b
end
function utility.decodeURI(str)
str = string.gsub(str, "%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end)
return str
end
function utility.parseQuery(query, param)
if not query then
return query
end
local result
for key, value in query:gmatch("([^&=?]+)=([^&=?]+)") do
if key == "title" then
value = utility.decodeURI(value)
end
if param then
result = value
else
result = result or {}
result[key] = value
end
end
return result
end
function utility.getHeaders(client, headers)
while true do
local line, _ = client:receive()
if line == "" or line == nil then
break
else
local key, value = line:match("^(.-):%s*(.*)$")
if key and value then
if key:lower() == "cookie" then
headers[key:lower()] = value
break -- only read cookie
end
end
end
end
end
local charset = {}
do -- [0-9a-zA-Z]
for c = 48, 57 do table.insert(charset, string.char(c)) end
for c = 65, 90 do table.insert(charset, string.char(c)) end
for c = 97, 122 do table.insert(charset, string.char(c)) end
end
function utility.randomString(length)
if not length or length <= 0 then return '' end
math.randomseed(os.time() * 10000)
return utility.randomString(length - 1) .. charset[math.random(1, #charset)]
end
function utility.unescape(str)
return string.gsub(str, '^%s*(.-)%s*$', '%1')
end
function utility.render(client, statusCode, body, customHeaders)
local headerString = "HTTP/1.1 " .. statusCode .. "\r\n"
local headers = {
["Content-Type"] = "text/html"
}
if type(customHeaders) == "table" then
for k, v in pairs(customHeaders) do
headers[k] = v
end
end
for k, v in pairs(headers) do
headerString = headerString .. k .. ": " .. v .. "\r\n"
end
headerString = headerString .. "\r\n"
if type(body) == "table" then
body = _G.h(body)
end
client:send(headerString .. (body or ""))
end
function utility.readStaticFile(filePath)
local file, err = io.open(_G.getDir() .. filePath, "rb") -- in binary mode
if not file then
return nil, "Error opening file: " .. err
end
local content = file:read("*all")
file:close()
return content
end
utility.mimes = {
_hs = 'text/hyperscript',
json = 'application/json',
js = 'application/javascript'
}
function utility.getType(path)
return utility.mimes[path:lower():match("[^.]*$")] or "application/octet-stream"
end
return utility