webmcp

view framework/cgi-bin/webmcp.lua @ 148:5354d836e6fb

Minor bugfix in json_newindex function of JSON library (should return nothing)
author jbe
date Wed Jul 30 22:32:33 2014 +0200 (2014-07-30)
parents 30391b40722f
children 8d7665e0d490
line source
1 #!/usr/bin/env lua
3 _WEBMCP_VERSION = "1.2.6"
5 -- Lua 5.1 compatibility
6 if not table.unpack then
7 table.unpack = unpack
8 end
9 do
10 local old_load = load
11 function load(ld, ...)
12 if type(ld) == "string" then
13 local done = false
14 local func = function()
15 if not done then
16 done = true
17 return ld
18 end
19 end
20 return old_load(func, ...)
21 else
22 return old_load(ld, ...)
23 end
24 end
25 end
27 -- include "../lib/" in search path for libraries
28 if not WEBMCP_PATH then
29 WEBMCP_PATH = "../"
30 end
31 do
32 package.path = WEBMCP_PATH .. 'lib/?.lua;' .. package.path
33 -- find out which file name extension shared libraries have
34 local slib_exts = {}
35 for ext in string.gmatch(package.cpath, "%?%.([A-Za-z0-9_-]+)") do
36 slib_exts[ext] = true
37 end
38 local paths = {}
39 for ext in pairs(slib_exts) do
40 paths[#paths+1] = WEBMCP_PATH .. "accelerator/?." .. ext
41 end
42 for ext in pairs(slib_exts) do
43 paths[#paths+1] = WEBMCP_PATH .. "lib/?." .. ext
44 end
45 paths[#paths+1] = package.cpath
46 package.cpath = table.concat(paths, ";")
47 end
49 -- load os extensions for lua
50 -- (should happen as soon as possible due to run time measurement)
51 extos = require 'extos'
53 -- load nihil library
54 nihil = require 'nihil'
56 -- load random generator library
57 multirand = require 'multirand'
59 -- load rocketcgi library and map it to cgi, unless interactive
60 do
61 local option = os.getenv("WEBMCP_INTERACTIVE")
62 if option and option ~= "" and option ~= "0" then
63 cgi = nil
64 else
65 rocketcgi = require 'rocketcgi' -- TODO: no "rocketcgi" alias
66 cgi = rocketcgi
67 end
68 end
70 -- load database access library with object relational mapper
71 mondelefant = require 'mondelefant'
72 mondelefant.connection_prototype.error_objects = true
74 -- load type system "atom"
75 atom = require 'atom'
77 -- load mondelefant atom connector
78 require 'mondelefant_atom_connector'
80 --[[--
81 cloned_table = -- newly generated table
82 table.new(
83 table_or_nil -- keys of a given table will be copied to the new table
84 )
86 If a table is given, then a cloned table is returned.
87 If nil is given, then a new empty table is returned.
89 --]]--
90 function table.new(tbl)
91 new_tbl = {}
92 if tbl then
93 for key, value in pairs(tbl) do
94 new_tbl[key] = value
95 end
96 end
97 return new_tbl
98 end
99 --//--
101 --[[--
102 at_exit(
103 func -- function to be called before the process is ending
104 )
106 Registers a function to be called before the CGI process is exiting.
107 --]]--
108 do
109 local exit_handlers = {}
110 function at_exit(func)
111 table.insert(exit_handlers, func)
112 end
113 function exit(code)
114 for i = #exit_handlers, 1, -1 do
115 exit_handlers[i]()
116 end
117 os.exit(code)
118 end
119 end
120 --//--
122 --[[--
123 app -- table to store an application state
125 'app' is a global table for storing any application state data
126 --]]--
127 app = {}
128 --//--
130 --[[--
131 config -- table to store application configuration
133 'config' is a global table, which can be modified by a config file of an application to modify the behaviour of that application.
134 --]]--
135 config = {}
136 --//--
138 -- autoloader system for WebMCP environment "../env/",
139 -- application environment extensions "$WEBMCP_APP_BASE/env/"
140 -- and models "$WEBMCP_APP_BASE/model/"
141 do
142 local app_base = os.getenv("WEBMCP_APP_BASEPATH")
143 if not app_base then
144 error(
145 "Failed to initialize autoloader " ..
146 "due to unset WEBMCP_APP_BASEPATH environment variable."
147 )
148 end
149 local weakkey_mt = { __mode = "k" }
150 local autoloader_category = setmetatable({}, weakkey_mt)
151 local autoloader_path = setmetatable({}, weakkey_mt)
152 local autoloader_mt = {}
153 local function install_autoloader(self, category, path)
154 autoloader_category[self] = category
155 autoloader_path[self] = path
156 setmetatable(self, autoloader_mt)
157 end
158 local function try_exec(filename)
159 local file = io.open(filename, "r")
160 if file then
161 local filedata = file:read("*a")
162 io.close(file)
163 local func, errmsg = load(filedata, "=" .. filename)
164 if func then
165 func()
166 return true
167 else
168 error(errmsg, 0)
169 end
170 else
171 return false
172 end
173 end
174 local function compose_path_string(base, path, key)
175 return string.gsub(
176 base .. table.concat(path, "/") .. "/" .. key, "/+", "/"
177 )
178 end
179 function autoloader_mt.__index(self, key)
180 local category, base_path, merge_base_path, file_key
181 local merge = false
182 if
183 string.find(key, "^[a-z_][A-Za-z0-9_]*$") and
184 not string.find(key, "^__")
185 then
186 category = "env"
187 base_path = WEBMCP_PATH .. "/env/"
188 merge = true
189 merge_base_path = app_base .. "/env/"
190 file_key = key
191 elseif string.find(key, "^[A-Z][A-Za-z0-9]*$") then
192 category = "model"
193 base_path = app_base .. "/model/"
194 local first = true
195 file_key = string.gsub(key, "[A-Z]",
196 function(c)
197 if first then
198 first = false
199 return string.lower(c)
200 else
201 return "_" .. string.lower(c)
202 end
203 end
204 )
205 else
206 return
207 end
208 local required_category = autoloader_category[self]
209 if required_category and required_category ~= category then return end
210 local path = autoloader_path[self]
211 local path_string = compose_path_string(base_path, path, file_key)
212 local merge_path_string
213 if merge then
214 merge_path_string = compose_path_string(
215 merge_base_path, path, file_key
216 )
217 end
218 local function try_dir(dirname)
219 local dir = io.open(dirname)
220 if dir then
221 io.close(dir)
222 local obj = {}
223 local sub_path = {}
224 for i, v in ipairs(path) do sub_path[i] = v end
225 table.insert(sub_path, file_key)
226 install_autoloader(obj, category, sub_path)
227 rawset(self, key, obj)
228 try_exec(path_string .. "/__init.lua")
229 if merge then try_exec(merge_path_string .. "/__init.lua") end
230 return true
231 else
232 return false
233 end
234 end
235 if merge and try_exec(merge_path_string .. ".lua") then
236 elseif merge and try_dir(merge_path_string .. "/") then
237 elseif try_exec(path_string .. ".lua") then
238 elseif try_dir(path_string .. "/") then
239 else end
240 return rawget(self, key)
241 end
242 install_autoloader(_G, nil, {})
243 try_exec(WEBMCP_PATH .. "env/__init.lua")
244 end
246 -- interactive console mode
247 if not cgi then
248 trace.disable() -- avoids memory leakage
249 local config_name = request.get_config_name()
250 if config_name then
251 execute.config(config_name)
252 end
253 return
254 end
256 local success, error_info = xpcall(
257 function()
259 -- execute configuration file
260 do
261 local config_name = request.get_config_name()
262 if config_name then
263 execute.config(config_name)
264 end
265 end
267 -- restore slots if coming from http redirect
268 if cgi.params.tempstore then
269 trace.restore_slots{}
270 local blob = tempstore.pop(cgi.params.tempstore)
271 if blob then slot.restore_all(blob) end
272 end
274 local function file_exists(filename)
275 local file = io.open(filename, "r")
276 if file then
277 io.close(file)
278 return true
279 else
280 return false
281 end
282 end
284 if request.is_404() then
285 request.set_status("404 Not Found")
286 if request.get_404_route() then
287 request.forward(request.get_404_route())
288 else
289 error("No 404 page set.")
290 end
291 elseif request.get_action() then
292 trace.request{
293 module = request.get_module(),
294 action = request.get_action()
295 }
296 if
297 request.get_404_route() and
298 not file_exists(
299 encode.action_file_path{
300 module = request.get_module(),
301 action = request.get_action()
302 }
303 )
304 then
305 request.set_status("404 Not Found")
306 request.forward(request.get_404_route())
307 else
308 if cgi.method ~= "POST" then
309 request.set_status("405 Method Not Allowed")
310 cgi.add_header("Allow: POST")
311 error("Tried to invoke an action with a GET request.")
312 end
313 local action_status = execute.filtered_action{
314 module = request.get_module(),
315 action = request.get_action(),
316 }
317 if not request.is_rerouted() then
318 local routing_mode, routing_module, routing_view
319 routing_mode = cgi.params["_webmcp_routing." .. action_status .. ".mode"]
320 routing_module = cgi.params["_webmcp_routing." .. action_status .. ".module"]
321 routing_view = cgi.params["_webmcp_routing." .. action_status .. ".view"]
322 routing_anchor = cgi.params["_webmcp_routing." .. action_status .. ".anchor"]
323 if not (routing_mode or routing_module or routing_view) then
324 action_status = "default"
325 routing_mode = cgi.params["_webmcp_routing.default.mode"]
326 routing_module = cgi.params["_webmcp_routing.default.module"]
327 routing_view = cgi.params["_webmcp_routing.default.view"]
328 routing_anchor = cgi.params["_webmcp_routing.default.anchor"]
329 end
330 assert(routing_module, "Routing information has no module.")
331 assert(routing_view, "Routing information has no view.")
332 if routing_mode == "redirect" then
333 local routing_params = {}
334 for key, value in pairs(cgi.params) do
335 local status, stripped_key = string.match(
336 key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$"
337 )
338 if status == action_status then
339 routing_params[stripped_key] = value
340 end
341 end
342 request.redirect{
343 module = routing_module,
344 view = routing_view,
345 id = cgi.params["_webmcp_routing." .. action_status .. ".id"],
346 params = routing_params,
347 anchor = routing_anchor
348 }
349 elseif routing_mode == "forward" then
350 request.forward{ module = routing_module, view = routing_view }
351 else
352 error("Missing or unknown routing mode in request parameters.")
353 end
354 end
355 end
356 else
357 -- no action
358 trace.request{
359 module = request.get_module(),
360 view = request.get_view()
361 }
362 if
363 request.get_404_route() and
364 not file_exists(
365 encode.view_file_path{
366 module = request.get_module(),
367 view = request.get_view()
368 }
369 )
370 then
371 request.set_status("404 Not Found")
372 request.forward(request.get_404_route())
373 end
374 end
376 if not request.get_redirect_data() then
377 request.process_forward()
378 local view = request.get_view()
379 if string.find(view, "^_") then
380 error("Tried to call a private view (prefixed with underscore).")
381 end
382 execute.filtered_view{
383 module = request.get_module(),
384 view = view,
385 }
386 end
388 -- force error due to missing absolute base URL until its too late to display error message
389 --if request.get_redirect_data() then
390 -- request.get_absolute_baseurl()
391 --end
393 end,
395 function(errobj)
396 return {
397 errobj = errobj,
398 stacktrace = string.gsub(
399 debug.traceback('', 2),
400 "^\r?\n?stack traceback:\r?\n?", ""
401 )
402 }
403 end
404 )
406 if not success then trace.error{} end
408 -- laufzeitermittlung
409 trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() }
411 slot.select('trace', trace.render) -- render trace information
413 local redirect_data = request.get_redirect_data()
415 -- log error and switch to error layout, unless success
416 if not success then
417 local errobj = error_info.errobj
418 local stacktrace = error_info.stacktrace
419 if not request.get_status() and not request.get_json_request_slots() then
420 request.set_status("500 Internal Server Error")
421 end
422 slot.set_layout('system_error')
423 slot.select('system_error', function()
424 if getmetatable(errobj) == mondelefant.errorobject_metatable then
425 slot.put(
426 "<p>Database error of class <b>",
427 encode.html(errobj.code),
428 "</b> occured:<br/><b>",
429 encode.html(errobj.message),
430 "</b></p>"
431 )
432 else
433 slot.put("<p><b>", encode.html(tostring(errobj)), "</b></p>")
434 end
435 slot.put("<p>Stack trace follows:<br/>")
436 slot.put(encode.html_newlines(encode.html(stacktrace)))
437 slot.put("</p>")
438 end)
439 elseif redirect_data then
440 local redirect_params = {}
441 for key, value in pairs(redirect_data.params) do
442 redirect_params[key] = value
443 end
444 local slot_dump = slot.dump_all()
445 if slot_dump ~= "" then
446 redirect_params.tempstore = tempstore.save(slot_dump)
447 end
448 local json_request_slots = request.get_json_request_slots()
449 if json_request_slots then
450 redirect_params["_webmcp_json_slots[]"] = json_request_slots
451 end
452 cgi.redirect(
453 encode.url{
454 base = request.get_absolute_baseurl(),
455 module = redirect_data.module,
456 view = redirect_data.view,
457 id = redirect_data.id,
458 params = redirect_params,
459 anchor = redirect_data.anchor
460 }
461 )
462 cgi.send_data()
463 end
465 if not success or not redirect_data then
467 local http_status = request.get_status()
468 if http_status then
469 cgi.set_status(http_status)
470 end
472 local json_request_slots = request.get_json_request_slots()
473 if json_request_slots then
474 cgi.set_content_type('application/json')
475 local data = {}
476 for idx, slot_ident in ipairs(json_request_slots) do
477 data[slot_ident] = slot.get_content(slot_ident)
478 end
479 cgi.send_data(encode.json(data))
480 else
481 cgi.set_content_type(slot.get_content_type())
482 cgi.send_data(slot.render_layout())
483 end
484 end
486 exit()

Impressum / About Us