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