-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1
186 lines (175 loc) · 5.48 KB
/
v1
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
178
179
180
181
182
183
184
185
186
<*
-------------------------------------------------------------
--------------------- Utility functions ---------------------
-------------------------------------------------------------
local function toOrderedJSON(json)
return call(getThisResource(), "toOrderedJSON", json)
end
local function routerExecute(...)
return call(getThisResource(), "routerExecute", ...)
end
local function sendResponse(code, result)
httpSetResponseHeader("content-type", "application/json")
httpWrite( result )
end
function processRequest( headers, params )
outputServerLog( var_dump("-v", params) )
local m = headers['x-http-method-override'] or 'GET'
local p = params.p
local ok, code, result, errorMsg = routerExecute(m, p, params, {account = user})
-- outputServerLog( ok and var_dump("-v", ok) or "nil" )
-- outputServerLog( code and var_dump("-v", code) or "nil" )
-- outputServerLog( result and var_dump("-v", result) or "nil")
-- outputServerLog( errorMsg and var_dump("-v", errorMsg) or "nil" )
if not ok then -- routerExecute failed to call function
errorMsg = code
code = 500
elseif not result then -- function called but returned nil as result
result = errorMsg or "An error occured !"
else -- function called and returned a result we have to convert in json
result = toOrderedJSON(result)
end
if errorMsg then outputServerLog("[API] "..tostring(errorMsg), 1) end
sendResponse(code, result)
end
---------------- URL encode and decode ----------------
-- http://www.esp8266.com/viewtopic.php?f=21&t=2300
local function urlDecode(str)
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
local function decodeForm()
local f = {}
if type(form) ~= "table" then return end
for key, value in pairs(form) do
f[urlDecode(key)] = urlDecode(value)
end
form = f
end
function var_dump(...)
-- default options
local verbose = false
local firstLevel = true
local outputDirectly = true
local noNames = false
local indentation = "\t\t\t\t\t\t"
local depth = nil
local name = nil
local output = {}
for k,v in ipairs(arg) do
-- check for modifiers
if type(v) == "string" and k < #arg and v:sub(1,1) == "-" then
local modifiers = v:sub(2)
if modifiers:find("v") ~= nil then
verbose = true
end
if modifiers:find("s") ~= nil then
outputDirectly = false
end
if modifiers:find("n") ~= nil then
verbose = false
end
if modifiers:find("u") ~= nil then
noNames = true
end
local s,e = modifiers:find("d%d+")
if s ~= nil then
depth = tonumber(string.sub(modifiers,s+1,e))
end
-- set name if appropriate
elseif type(v) == "string" and k < #arg and name == nil and not noNames then
name = v
else
if name ~= nil then
name = ""..name..": "
else
name = ""
end
local o = ""
if type(v) == "string" then
table.insert(output,name..type(v).."("..v:len()..") \""..v.."\"")
elseif type(v) == "userdata" then
local elementType = "no valid MTA element"
if isElement(v) then
elementType = getElementType(v)
end
table.insert(output,name..type(v).."("..elementType..") \""..tostring(v).."\"")
elseif type(v) == "table" then
local count = 0
for key,value in pairs(v) do
count = count + 1
end
table.insert(output,name..type(v).."("..count..") \""..tostring(v).."\"")
if verbose and count > 0 and (depth == nil or depth > 0) then
table.insert(output,"\t{")
for key,value in pairs(v) do
-- calls itself, so be careful when you change anything
local newModifiers = "-s"
if depth == nil then
newModifiers = "-sv"
elseif depth > 1 then
local newDepth = depth - 1
newModifiers = "-svd"..newDepth
end
local keyString, keyTable = var_dump(newModifiers,key)
local valueString, valueTable = var_dump(newModifiers,value)
if #keyTable == 1 and #valueTable == 1 then
table.insert(output,indentation.."["..keyString.."]\t=>\t"..valueString)
elseif #keyTable == 1 then
table.insert(output,indentation.."["..keyString.."]\t=>")
for k,v in ipairs(valueTable) do
table.insert(output,indentation..v)
end
elseif #valueTable == 1 then
for k,v in ipairs(keyTable) do
if k == 1 then
table.insert(output,indentation.."["..v)
elseif k == #keyTable then
table.insert(output,indentation..v.."]")
else
table.insert(output,indentation..v)
end
end
table.insert(output,indentation.."\t=>\t"..valueString)
else
for k,v in ipairs(keyTable) do
if k == 1 then
table.insert(output,indentation.."["..v)
elseif k == #keyTable then
table.insert(output,indentation..v.."]")
else
table.insert(output,indentation..v)
end
end
for k,v in ipairs(valueTable) do
if k == 1 then
table.insert(output,indentation.." => "..v)
else
table.insert(output,indentation..v)
end
end
end
end
table.insert(output,"\t}")
end
else
table.insert(output,name..type(v).." \""..tostring(v).."\"")
end
name = nil
end
end
local string = ""
for k,v in ipairs(output) do
if outputDirectly then
outputConsole(v)
end
string = string..v
end
return string, output
end
-------------------------------------------------------------
decodeForm() -- Decode urlencoded form
processRequest( requestHeaders, form )
*>