# HG changeset patch # User jbe # Date 1420846083 -3600 # Node ID 474cf0cfb85b710da7a0cbe364c6c88156d687b0 # Parent 2cb27106aa73414f3759f98ead597361348f3d6e Moved request handling code from mcp.lua to request.handler(...) diff -r 2cb27106aa73 -r 474cf0cfb85b framework/bin/mcp.lua --- a/framework/bin/mcp.lua Sat Jan 10 00:19:38 2015 +0100 +++ b/framework/bin/mcp.lua Sat Jan 10 00:28:03 2015 +0100 @@ -204,9 +204,9 @@ listener.prepare = execute.prefork_initializers listener.connect = http.generate_handler( request.get_http_options(), - function(req) + function(http_request) execute.postfork_initializers() - request.handler(req) + request.handler(http_request) end ) listener.finish = execute.finalizers @@ -215,229 +215,3 @@ end end ---[[ TODO: following lines to be moved to execute.server(...) - -local success, error_info = xpcall( - function() - - -- restore slots if coming from http redirect - if cgi.params.tempstore then - trace.restore_slots{} - local blob = tempstore.pop(cgi.params.tempstore) - if blob then slot.restore_all(blob) end - end - - local function file_exists(filename) - local file = io.open(filename, "r") - if file then - io.close(file) - return true - else - return false - end - end - - if request.is_404() then - request.set_status("404 Not Found") - if request.get_404_route() then - request.forward(request.get_404_route()) - else - error("No 404 page set.") - end - elseif request.get_action() then - trace.request{ - module = request.get_module(), - action = request.get_action() - } - if - request.get_404_route() and - not file_exists( - encode.action_file_path{ - module = request.get_module(), - action = request.get_action() - } - ) - then - request.set_status("404 Not Found") - request.forward(request.get_404_route()) - else - if cgi.method ~= "POST" then - request.set_status("405 Method Not Allowed") - cgi.add_header("Allow: POST") - error("Tried to invoke an action with a GET request.") - end - local action_status = execute.filtered_action{ - module = request.get_module(), - action = request.get_action(), - } - if not request.is_rerouted() then - local routing_mode, routing_module, routing_view - routing_mode = cgi.params["_webmcp_routing." .. action_status .. ".mode"] - routing_module = cgi.params["_webmcp_routing." .. action_status .. ".module"] - routing_view = cgi.params["_webmcp_routing." .. action_status .. ".view"] - routing_anchor = cgi.params["_webmcp_routing." .. action_status .. ".anchor"] - if not (routing_mode or routing_module or routing_view) then - action_status = "default" - routing_mode = cgi.params["_webmcp_routing.default.mode"] - routing_module = cgi.params["_webmcp_routing.default.module"] - routing_view = cgi.params["_webmcp_routing.default.view"] - routing_anchor = cgi.params["_webmcp_routing.default.anchor"] - end - assert(routing_module, "Routing information has no module.") - assert(routing_view, "Routing information has no view.") - if routing_mode == "redirect" then - local routing_params = {} - for key, value in pairs(cgi.params) do - local status, stripped_key = string.match( - key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$" - ) - if status == action_status then - routing_params[stripped_key] = value - end - end - request.redirect{ - module = routing_module, - view = routing_view, - id = cgi.params["_webmcp_routing." .. action_status .. ".id"], - params = routing_params, - anchor = routing_anchor - } - elseif routing_mode == "forward" then - request.forward{ module = routing_module, view = routing_view } - else - error("Missing or unknown routing mode in request parameters.") - end - end - end - else - -- no action - trace.request{ - module = request.get_module(), - view = request.get_view() - } - if - request.get_404_route() and - not file_exists( - encode.view_file_path{ - module = request.get_module(), - view = request.get_view() - } - ) - then - request.set_status("404 Not Found") - request.forward(request.get_404_route()) - end - end - - if not request.get_redirect_data() then - request.process_forward() - local view = request.get_view() - if string.find(view, "^_") then - error("Tried to call a private view (prefixed with underscore).") - end - execute.filtered_view{ - module = request.get_module(), - view = view, - } - end - - -- force error due to missing absolute base URL until its too late to display error message - --if request.get_redirect_data() then - -- request.get_absolute_baseurl() - --end - - end, - - function(errobj) - return { - errobj = errobj, - stacktrace = string.gsub( - debug.traceback('', 2), - "^\r?\n?stack traceback:\r?\n?", "" - ) - } - end -) - -if not success then trace.error{} end - --- laufzeitermittlung -trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() } - -slot.select('trace', trace.render) -- render trace information - -local redirect_data = request.get_redirect_data() - --- log error and switch to error layout, unless success -if not success then - local errobj = error_info.errobj - local stacktrace = error_info.stacktrace - if not request.get_status() and not request.get_json_request_slots() then - request.set_status("500 Internal Server Error") - end - slot.set_layout('system_error') - slot.select('system_error', function() - if getmetatable(errobj) == mondelefant.errorobject_metatable then - slot.put( - "

Database error of class ", - encode.html(errobj.code), - " occured:
", - encode.html(errobj.message), - "

" - ) - else - slot.put("

", encode.html(tostring(errobj)), "

") - end - slot.put("

Stack trace follows:
") - slot.put(encode.html_newlines(encode.html(stacktrace))) - slot.put("

") - end) -elseif redirect_data then - local redirect_params = {} - for key, value in pairs(redirect_data.params) do - redirect_params[key] = value - end - local slot_dump = slot.dump_all() - if slot_dump ~= "" then - redirect_params.tempstore = tempstore.save(slot_dump) - end - local json_request_slots = request.get_json_request_slots() - if json_request_slots then - redirect_params["_webmcp_json_slots[]"] = json_request_slots - end - cgi.redirect( - encode.url{ - base = request.get_absolute_baseurl(), - module = redirect_data.module, - view = redirect_data.view, - id = redirect_data.id, - params = redirect_params, - anchor = redirect_data.anchor - } - ) - cgi.send_data() -end - -if not success or not redirect_data then - - local http_status = request.get_status() - if http_status then - cgi.set_status(http_status) - end - - local json_request_slots = request.get_json_request_slots() - if json_request_slots then - cgi.set_content_type('application/json') - local data = {} - for idx, slot_ident in ipairs(json_request_slots) do - data[slot_ident] = slot.get_content(slot_ident) - end - cgi.send_data(encode.json(data)) - else - cgi.set_content_type(slot.get_content_type()) - cgi.send_data(slot.render_layout()) - end -end - ---]] - diff -r 2cb27106aa73 -r 474cf0cfb85b framework/env/request/handler.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/framework/env/request/handler.lua Sat Jan 10 00:28:03 2015 +0100 @@ -0,0 +1,227 @@ +-- TODO: function incomplete yet + +function request.handler(http_request) + + local success, error_info = xpcall( + function() + + -- restore slots if coming from http redirect + if cgi.params.tempstore then + trace.restore_slots{} + local blob = tempstore.pop(cgi.params.tempstore) + if blob then slot.restore_all(blob) end + end + + local function file_exists(filename) + local file = io.open(filename, "r") + if file then + io.close(file) + return true + else + return false + end + end + + if request.is_404() then + request.set_status("404 Not Found") + if request.get_404_route() then + request.forward(request.get_404_route()) + else + error("No 404 page set.") + end + elseif request.get_action() then + trace.request{ + module = request.get_module(), + action = request.get_action() + } + if + request.get_404_route() and + not file_exists( + encode.action_file_path{ + module = request.get_module(), + action = request.get_action() + } + ) + then + request.set_status("404 Not Found") + request.forward(request.get_404_route()) + else + if cgi.method ~= "POST" then + request.set_status("405 Method Not Allowed") + cgi.add_header("Allow: POST") + error("Tried to invoke an action with a GET request.") + end + local action_status = execute.filtered_action{ + module = request.get_module(), + action = request.get_action(), + } + if not request.is_rerouted() then + local routing_mode, routing_module, routing_view + routing_mode = cgi.params["_webmcp_routing." .. action_status .. ".mode"] + routing_module = cgi.params["_webmcp_routing." .. action_status .. ".module"] + routing_view = cgi.params["_webmcp_routing." .. action_status .. ".view"] + routing_anchor = cgi.params["_webmcp_routing." .. action_status .. ".anchor"] + if not (routing_mode or routing_module or routing_view) then + action_status = "default" + routing_mode = cgi.params["_webmcp_routing.default.mode"] + routing_module = cgi.params["_webmcp_routing.default.module"] + routing_view = cgi.params["_webmcp_routing.default.view"] + routing_anchor = cgi.params["_webmcp_routing.default.anchor"] + end + assert(routing_module, "Routing information has no module.") + assert(routing_view, "Routing information has no view.") + if routing_mode == "redirect" then + local routing_params = {} + for key, value in pairs(cgi.params) do + local status, stripped_key = string.match( + key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$" + ) + if status == action_status then + routing_params[stripped_key] = value + end + end + request.redirect{ + module = routing_module, + view = routing_view, + id = cgi.params["_webmcp_routing." .. action_status .. ".id"], + params = routing_params, + anchor = routing_anchor + } + elseif routing_mode == "forward" then + request.forward{ module = routing_module, view = routing_view } + else + error("Missing or unknown routing mode in request parameters.") + end + end + end + else + -- no action + trace.request{ + module = request.get_module(), + view = request.get_view() + } + if + request.get_404_route() and + not file_exists( + encode.view_file_path{ + module = request.get_module(), + view = request.get_view() + } + ) + then + request.set_status("404 Not Found") + request.forward(request.get_404_route()) + end + end + + if not request.get_redirect_data() then + request.process_forward() + local view = request.get_view() + if string.find(view, "^_") then + error("Tried to call a private view (prefixed with underscore).") + end + execute.filtered_view{ + module = request.get_module(), + view = view, + } + end + + -- force error due to missing absolute base URL until its too late to display error message + --if request.get_redirect_data() then + -- request.get_absolute_baseurl() + --end + + end, + + function(errobj) + return { + errobj = errobj, + stacktrace = string.gsub( + debug.traceback('', 2), + "^\r?\n?stack traceback:\r?\n?", "" + ) + } + end + ) + + if not success then trace.error{} end + + -- laufzeitermittlung + trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() } + + slot.select('trace', trace.render) -- render trace information + + local redirect_data = request.get_redirect_data() + + -- log error and switch to error layout, unless success + if not success then + local errobj = error_info.errobj + local stacktrace = error_info.stacktrace + if not request.get_status() and not request.get_json_request_slots() then + request.set_status("500 Internal Server Error") + end + slot.set_layout('system_error') + slot.select('system_error', function() + if getmetatable(errobj) == mondelefant.errorobject_metatable then + slot.put( + "

Database error of class ", + encode.html(errobj.code), + " occured:
", + encode.html(errobj.message), + "

" + ) + else + slot.put("

", encode.html(tostring(errobj)), "

") + end + slot.put("

Stack trace follows:
") + slot.put(encode.html_newlines(encode.html(stacktrace))) + slot.put("

") + end) + elseif redirect_data then + local redirect_params = {} + for key, value in pairs(redirect_data.params) do + redirect_params[key] = value + end + local slot_dump = slot.dump_all() + if slot_dump ~= "" then + redirect_params.tempstore = tempstore.save(slot_dump) + end + local json_request_slots = request.get_json_request_slots() + if json_request_slots then + redirect_params["_webmcp_json_slots[]"] = json_request_slots + end + cgi.redirect( + encode.url{ + base = request.get_absolute_baseurl(), + module = redirect_data.module, + view = redirect_data.view, + id = redirect_data.id, + params = redirect_params, + anchor = redirect_data.anchor + } + ) + cgi.send_data() + end + + if not success or not redirect_data then + + local http_status = request.get_status() + if http_status then + cgi.set_status(http_status) + end + + local json_request_slots = request.get_json_request_slots() + if json_request_slots then + cgi.set_content_type('application/json') + local data = {} + for idx, slot_ident in ipairs(json_request_slots) do + data[slot_ident] = slot.get_content(slot_ident) + end + cgi.send_data(encode.json(data)) + else + cgi.set_content_type(slot.get_content_type()) + cgi.send_data(slot.render_layout()) + end + end + +end