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