webmcp

view framework/cgi-bin/webmcp.lua @ 85:79bf3deefe21

Disable trace system for interactive (non-cgi) sessions
author jbe
date Tue Jul 03 23:33:17 2012 +0200 (2012-07-03)
parents 6ab448e71d66
children ed22ba3a2888
line source
1 #!/usr/bin/env lua
3 _WEBMCP_VERSION = "1.2.3"
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 -- and disable trace system, if interactive (avoids memory leakage)
61 do
62 local option = os.getenv("WEBMCP_INTERACTIVE")
63 if option and option ~= "" and option ~= "0" then
64 cgi = nil
65 trace.disable()
66 else
67 rocketcgi = require 'rocketcgi' -- TODO: no "rocketcgi" alias
68 cgi = rocketcgi
69 end
70 end
72 -- load database access library with object relational mapper
73 mondelefant = require 'mondelefant'
74 mondelefant.connection_prototype.error_objects = true
76 -- load type system "atom"
77 atom = require 'atom'
79 -- load mondelefant atom connector
80 require 'mondelefant_atom_connector'
82 --[[--
83 cloned_table = -- newly generated table
84 table.new(
85 table_or_nil -- keys of a given table will be copied to the new table
86 )
88 If a table is given, then a cloned table is returned.
89 If nil is given, then a new empty table is returned.
91 --]]--
92 function table.new(tbl)
93 new_tbl = {}
94 if tbl then
95 for key, value in pairs(tbl) do
96 new_tbl[key] = value
97 end
98 end
99 return new_tbl
100 end
101 --//--
103 --[[--
104 at_exit(
105 func -- function to be called before the process is ending
106 )
108 Registers a function to be called before the CGI process is exiting.
109 --]]--
110 do
111 local exit_handlers = {}
112 function at_exit(func)
113 table.insert(exit_handlers, func)
114 end
115 function exit(code)
116 for i = #exit_handlers, 1, -1 do
117 exit_handlers[i]()
118 end
119 os.exit(code)
120 end
121 end
122 --//--
124 --[[--
125 app -- table to store an application state
127 'app' is a global table for storing any application state data
128 --]]--
129 app = {}
130 --//--
132 --[[--
133 config -- table to store application configuration
135 'config' is a global table, which can be modified by a config file of an application to modify the behaviour of that application.
136 --]]--
137 config = {}
138 --//--
140 -- autoloader system for WebMCP environment "../env/",
141 -- application environment extensions "$WEBMCP_APP_BASE/env/"
142 -- and models "$WEBMCP_APP_BASE/model/"
143 do
144 local app_base = os.getenv("WEBMCP_APP_BASEPATH")
145 if not app_base then
146 error(
147 "Failed to initialize autoloader " ..
148 "due to unset WEBMCP_APP_BASEPATH environment variable."
149 )
150 end
151 local weakkey_mt = { __mode = "k" }
152 local autoloader_category = setmetatable({}, weakkey_mt)
153 local autoloader_path = setmetatable({}, weakkey_mt)
154 local autoloader_mt = {}
155 local function install_autoloader(self, category, path)
156 autoloader_category[self] = category
157 autoloader_path[self] = path
158 setmetatable(self, autoloader_mt)
159 end
160 local function try_exec(filename)
161 local file = io.open(filename, "r")
162 if file then
163 local filedata = file:read("*a")
164 io.close(file)
165 local func, errmsg = load(filedata, "=" .. filename)
166 if func then
167 func()
168 return true
169 else
170 error(errmsg, 0)
171 end
172 else
173 return false
174 end
175 end
176 local function compose_path_string(base, path, key)
177 return string.gsub(
178 base .. table.concat(path, "/") .. "/" .. key, "/+", "/"
179 )
180 end
181 function autoloader_mt.__index(self, key)
182 local category, base_path, merge_base_path, file_key
183 local merge = false
184 if
185 string.find(key, "^[a-z_][A-Za-z0-9_]*$") and
186 not string.find(key, "^__")
187 then
188 category = "env"
189 base_path = WEBMCP_PATH .. "/env/"
190 merge = true
191 merge_base_path = app_base .. "/env/"
192 file_key = key
193 elseif string.find(key, "^[A-Z][A-Za-z0-9]*$") then
194 category = "model"
195 base_path = app_base .. "/model/"
196 local first = true
197 file_key = string.gsub(key, "[A-Z]",
198 function(c)
199 if first then
200 first = false
201 return string.lower(c)
202 else
203 return "_" .. string.lower(c)
204 end
205 end
206 )
207 else
208 return
209 end
210 local required_category = autoloader_category[self]
211 if required_category and required_category ~= category then return end
212 local path = autoloader_path[self]
213 local path_string = compose_path_string(base_path, path, file_key)
214 local merge_path_string
215 if merge then
216 merge_path_string = compose_path_string(
217 merge_base_path, path, file_key
218 )
219 end
220 local function try_dir(dirname)
221 local dir = io.open(dirname)
222 if dir then
223 io.close(dir)
224 local obj = {}
225 local sub_path = {}
226 for i, v in ipairs(path) do sub_path[i] = v end
227 table.insert(sub_path, file_key)
228 install_autoloader(obj, category, sub_path)
229 rawset(self, key, obj)
230 try_exec(path_string .. "/__init.lua")
231 if merge then try_exec(merge_path_string .. "/__init.lua") end
232 return true
233 else
234 return false
235 end
236 end
237 if merge and try_exec(merge_path_string .. ".lua") then
238 elseif merge and try_dir(merge_path_string .. "/") then
239 elseif try_exec(path_string .. ".lua") then
240 elseif try_dir(path_string .. "/") then
241 else end
242 return rawget(self, key)
243 end
244 install_autoloader(_G, nil, {})
245 try_exec(WEBMCP_PATH .. "env/__init.lua")
246 end
248 -- interactive console mode
249 if not cgi then
250 local config_name = request.get_config_name()
251 if config_name then
252 execute.config(config_name)
253 end
254 return
255 end
257 local success, error_info = xpcall(
258 function()
260 -- execute configuration file
261 do
262 local config_name = request.get_config_name()
263 if config_name then
264 execute.config(config_name)
265 end
266 end
268 -- restore slots if coming from http redirect
269 if cgi.params.tempstore then
270 trace.restore_slots{}
271 local blob = tempstore.pop(cgi.params.tempstore)
272 if blob then slot.restore_all(blob) end
273 end
275 local function file_exists(filename)
276 local file = io.open(filename, "r")
277 if file then
278 io.close(file)
279 return true
280 else
281 return false
282 end
283 end
285 if cgi.params["_webmcp_404"] then
286 request.force_absolute_baseurl()
287 request.set_status("404 Not Found")
288 if request.get_404_route() then
289 request.forward(request.get_404_route())
290 else
291 error("No 404 page set.")
292 end
293 elseif request.get_action() then
294 trace.request{
295 module = request.get_module(),
296 action = request.get_action()
297 }
298 if
299 request.get_404_route() and
300 not file_exists(
301 encode.action_file_path{
302 module = request.get_module(),
303 action = request.get_action()
304 }
305 )
306 then
307 request.set_status("404 Not Found")
308 request.forward(request.get_404_route())
309 else
310 if cgi.method ~= "POST" then
311 request.set_status("405 Method Not Allowed")
312 cgi.add_header("Allow: POST")
313 error("Tried to invoke an action with a GET request.")
314 end
315 local action_status = execute.filtered_action{
316 module = request.get_module(),
317 action = request.get_action(),
318 }
319 if not request.is_rerouted() then
320 local routing_mode, routing_module, routing_view
321 routing_mode = cgi.params["_webmcp_routing." .. action_status .. ".mode"]
322 routing_module = cgi.params["_webmcp_routing." .. action_status .. ".module"]
323 routing_view = cgi.params["_webmcp_routing." .. action_status .. ".view"]
324 if not (routing_mode or routing_module or routing_view) then
325 action_status = "default"
326 routing_mode = cgi.params["_webmcp_routing.default.mode"]
327 routing_module = cgi.params["_webmcp_routing.default.module"]
328 routing_view = cgi.params["_webmcp_routing.default.view"]
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 }
348 elseif routing_mode == "forward" then
349 request.forward{ module = routing_module, view = routing_view }
350 else
351 error("Missing or unknown routing mode in request parameters.")
352 end
353 end
354 end
355 else
356 -- no action
357 trace.request{
358 module = request.get_module(),
359 view = request.get_view()
360 }
361 if
362 request.get_404_route() and
363 not file_exists(
364 encode.view_file_path{
365 module = request.get_module(),
366 view = request.get_view()
367 }
368 )
369 then
370 request.set_status("404 Not Found")
371 request.forward(request.get_404_route())
372 end
373 end
375 if not request.get_redirect_data() then
376 request.process_forward()
377 local view = request.get_view()
378 if string.find(view, "^_") then
379 error("Tried to call a private view (prefixed with underscore).")
380 end
381 execute.filtered_view{
382 module = request.get_module(),
383 view = view,
384 }
385 end
387 -- force error due to missing absolute base URL until its too late to display error message
388 --if request.get_redirect_data() then
389 -- request.get_absolute_baseurl()
390 --end
392 end,
394 function(errobj)
395 return {
396 errobj = errobj,
397 stacktrace = string.gsub(
398 debug.traceback('', 2),
399 "^\r?\n?stack traceback:\r?\n?", ""
400 )
401 }
402 end
403 )
405 if not success then trace.error{} end
407 -- laufzeitermittlung
408 trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() }
410 slot.select('trace', trace.render) -- render trace information
412 local redirect_data = request.get_redirect_data()
414 -- log error and switch to error layout, unless success
415 if not success then
416 local errobj = error_info.errobj
417 local stacktrace = error_info.stacktrace
418 if not request.get_status() and not request.get_json_request_slots() then
419 request.set_status("500 Internal Server Error")
420 end
421 slot.set_layout('system_error')
422 slot.select('system_error', function()
423 if getmetatable(errobj) == mondelefant.errorobject_metatable then
424 slot.put(
425 "<p>Database error of class <b>",
426 encode.html(errobj.code),
427 "</b> occured:<br/><b>",
428 encode.html(errobj.message),
429 "</b></p>"
430 )
431 else
432 slot.put("<p><b>", encode.html(tostring(errobj)), "</b></p>")
433 end
434 slot.put("<p>Stack trace follows:<br/>")
435 slot.put(encode.html_newlines(encode.html(stacktrace)))
436 slot.put("</p>")
437 end)
438 elseif redirect_data then
439 local redirect_params = {}
440 for key, value in pairs(redirect_data.params) do
441 redirect_params[key] = value
442 end
443 local slot_dump = slot.dump_all()
444 if slot_dump ~= "" then
445 redirect_params.tempstore = tempstore.save(slot_dump)
446 end
447 local json_request_slots = request.get_json_request_slots()
448 if json_request_slots then
449 redirect_params["_webmcp_json_slots[]"] = json_request_slots
450 end
451 cgi.redirect(
452 encode.url{
453 base = request.get_absolute_baseurl(),
454 module = redirect_data.module,
455 view = redirect_data.view,
456 id = redirect_data.id,
457 params = redirect_params
458 }
459 )
460 cgi.send_data()
461 end
463 if not success or not redirect_data then
465 local http_status = request.get_status()
466 if http_status then
467 cgi.set_status(http_status)
468 end
470 local json_request_slots = request.get_json_request_slots()
471 if json_request_slots then
472 cgi.set_content_type('application/json')
473 local data = {}
474 for idx, slot_ident in ipairs(json_request_slots) do
475 data[slot_ident] = slot.get_content(slot_ident)
476 end
477 cgi.send_data(encode.json(data))
478 else
479 cgi.set_content_type(slot.get_content_type())
480 cgi.send_data(slot.render_layout())
481 end
482 end
484 exit()

Impressum / About Us