webmcp
diff framework/env/request/handler.lua @ 210:474cf0cfb85b
Moved request handling code from mcp.lua to request.handler(...)
author | jbe |
---|---|
date | Sat Jan 10 00:28:03 2015 +0100 (2015-01-10) |
parents | |
children | 47ebf4213716 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/request/handler.lua Sat Jan 10 00:28:03 2015 +0100 1.3 @@ -0,0 +1,227 @@ 1.4 +-- TODO: function incomplete yet 1.5 + 1.6 +function request.handler(http_request) 1.7 + 1.8 + local success, error_info = xpcall( 1.9 + function() 1.10 + 1.11 + -- restore slots if coming from http redirect 1.12 + if cgi.params.tempstore then 1.13 + trace.restore_slots{} 1.14 + local blob = tempstore.pop(cgi.params.tempstore) 1.15 + if blob then slot.restore_all(blob) end 1.16 + end 1.17 + 1.18 + local function file_exists(filename) 1.19 + local file = io.open(filename, "r") 1.20 + if file then 1.21 + io.close(file) 1.22 + return true 1.23 + else 1.24 + return false 1.25 + end 1.26 + end 1.27 + 1.28 + if request.is_404() then 1.29 + request.set_status("404 Not Found") 1.30 + if request.get_404_route() then 1.31 + request.forward(request.get_404_route()) 1.32 + else 1.33 + error("No 404 page set.") 1.34 + end 1.35 + elseif request.get_action() then 1.36 + trace.request{ 1.37 + module = request.get_module(), 1.38 + action = request.get_action() 1.39 + } 1.40 + if 1.41 + request.get_404_route() and 1.42 + not file_exists( 1.43 + encode.action_file_path{ 1.44 + module = request.get_module(), 1.45 + action = request.get_action() 1.46 + } 1.47 + ) 1.48 + then 1.49 + request.set_status("404 Not Found") 1.50 + request.forward(request.get_404_route()) 1.51 + else 1.52 + if cgi.method ~= "POST" then 1.53 + request.set_status("405 Method Not Allowed") 1.54 + cgi.add_header("Allow: POST") 1.55 + error("Tried to invoke an action with a GET request.") 1.56 + end 1.57 + local action_status = execute.filtered_action{ 1.58 + module = request.get_module(), 1.59 + action = request.get_action(), 1.60 + } 1.61 + if not request.is_rerouted() then 1.62 + local routing_mode, routing_module, routing_view 1.63 + routing_mode = cgi.params["_webmcp_routing." .. action_status .. ".mode"] 1.64 + routing_module = cgi.params["_webmcp_routing." .. action_status .. ".module"] 1.65 + routing_view = cgi.params["_webmcp_routing." .. action_status .. ".view"] 1.66 + routing_anchor = cgi.params["_webmcp_routing." .. action_status .. ".anchor"] 1.67 + if not (routing_mode or routing_module or routing_view) then 1.68 + action_status = "default" 1.69 + routing_mode = cgi.params["_webmcp_routing.default.mode"] 1.70 + routing_module = cgi.params["_webmcp_routing.default.module"] 1.71 + routing_view = cgi.params["_webmcp_routing.default.view"] 1.72 + routing_anchor = cgi.params["_webmcp_routing.default.anchor"] 1.73 + end 1.74 + assert(routing_module, "Routing information has no module.") 1.75 + assert(routing_view, "Routing information has no view.") 1.76 + if routing_mode == "redirect" then 1.77 + local routing_params = {} 1.78 + for key, value in pairs(cgi.params) do 1.79 + local status, stripped_key = string.match( 1.80 + key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$" 1.81 + ) 1.82 + if status == action_status then 1.83 + routing_params[stripped_key] = value 1.84 + end 1.85 + end 1.86 + request.redirect{ 1.87 + module = routing_module, 1.88 + view = routing_view, 1.89 + id = cgi.params["_webmcp_routing." .. action_status .. ".id"], 1.90 + params = routing_params, 1.91 + anchor = routing_anchor 1.92 + } 1.93 + elseif routing_mode == "forward" then 1.94 + request.forward{ module = routing_module, view = routing_view } 1.95 + else 1.96 + error("Missing or unknown routing mode in request parameters.") 1.97 + end 1.98 + end 1.99 + end 1.100 + else 1.101 + -- no action 1.102 + trace.request{ 1.103 + module = request.get_module(), 1.104 + view = request.get_view() 1.105 + } 1.106 + if 1.107 + request.get_404_route() and 1.108 + not file_exists( 1.109 + encode.view_file_path{ 1.110 + module = request.get_module(), 1.111 + view = request.get_view() 1.112 + } 1.113 + ) 1.114 + then 1.115 + request.set_status("404 Not Found") 1.116 + request.forward(request.get_404_route()) 1.117 + end 1.118 + end 1.119 + 1.120 + if not request.get_redirect_data() then 1.121 + request.process_forward() 1.122 + local view = request.get_view() 1.123 + if string.find(view, "^_") then 1.124 + error("Tried to call a private view (prefixed with underscore).") 1.125 + end 1.126 + execute.filtered_view{ 1.127 + module = request.get_module(), 1.128 + view = view, 1.129 + } 1.130 + end 1.131 + 1.132 + -- force error due to missing absolute base URL until its too late to display error message 1.133 + --if request.get_redirect_data() then 1.134 + -- request.get_absolute_baseurl() 1.135 + --end 1.136 + 1.137 + end, 1.138 + 1.139 + function(errobj) 1.140 + return { 1.141 + errobj = errobj, 1.142 + stacktrace = string.gsub( 1.143 + debug.traceback('', 2), 1.144 + "^\r?\n?stack traceback:\r?\n?", "" 1.145 + ) 1.146 + } 1.147 + end 1.148 + ) 1.149 + 1.150 + if not success then trace.error{} end 1.151 + 1.152 + -- laufzeitermittlung 1.153 + trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() } 1.154 + 1.155 + slot.select('trace', trace.render) -- render trace information 1.156 + 1.157 + local redirect_data = request.get_redirect_data() 1.158 + 1.159 + -- log error and switch to error layout, unless success 1.160 + if not success then 1.161 + local errobj = error_info.errobj 1.162 + local stacktrace = error_info.stacktrace 1.163 + if not request.get_status() and not request.get_json_request_slots() then 1.164 + request.set_status("500 Internal Server Error") 1.165 + end 1.166 + slot.set_layout('system_error') 1.167 + slot.select('system_error', function() 1.168 + if getmetatable(errobj) == mondelefant.errorobject_metatable then 1.169 + slot.put( 1.170 + "<p>Database error of class <b>", 1.171 + encode.html(errobj.code), 1.172 + "</b> occured:<br/><b>", 1.173 + encode.html(errobj.message), 1.174 + "</b></p>" 1.175 + ) 1.176 + else 1.177 + slot.put("<p><b>", encode.html(tostring(errobj)), "</b></p>") 1.178 + end 1.179 + slot.put("<p>Stack trace follows:<br/>") 1.180 + slot.put(encode.html_newlines(encode.html(stacktrace))) 1.181 + slot.put("</p>") 1.182 + end) 1.183 + elseif redirect_data then 1.184 + local redirect_params = {} 1.185 + for key, value in pairs(redirect_data.params) do 1.186 + redirect_params[key] = value 1.187 + end 1.188 + local slot_dump = slot.dump_all() 1.189 + if slot_dump ~= "" then 1.190 + redirect_params.tempstore = tempstore.save(slot_dump) 1.191 + end 1.192 + local json_request_slots = request.get_json_request_slots() 1.193 + if json_request_slots then 1.194 + redirect_params["_webmcp_json_slots[]"] = json_request_slots 1.195 + end 1.196 + cgi.redirect( 1.197 + encode.url{ 1.198 + base = request.get_absolute_baseurl(), 1.199 + module = redirect_data.module, 1.200 + view = redirect_data.view, 1.201 + id = redirect_data.id, 1.202 + params = redirect_params, 1.203 + anchor = redirect_data.anchor 1.204 + } 1.205 + ) 1.206 + cgi.send_data() 1.207 + end 1.208 + 1.209 + if not success or not redirect_data then 1.210 + 1.211 + local http_status = request.get_status() 1.212 + if http_status then 1.213 + cgi.set_status(http_status) 1.214 + end 1.215 + 1.216 + local json_request_slots = request.get_json_request_slots() 1.217 + if json_request_slots then 1.218 + cgi.set_content_type('application/json') 1.219 + local data = {} 1.220 + for idx, slot_ident in ipairs(json_request_slots) do 1.221 + data[slot_ident] = slot.get_content(slot_ident) 1.222 + end 1.223 + cgi.send_data(encode.json(data)) 1.224 + else 1.225 + cgi.set_content_type(slot.get_content_type()) 1.226 + cgi.send_data(slot.render_layout()) 1.227 + end 1.228 + end 1.229 + 1.230 +end