webmcp

view framework/env/request/process.lua @ 221:25a20bd1f416

More work on Moonbridge integration: several changes in env/request
author jbe
date Wed Feb 25 01:33:27 2015 +0100 (2015-02-25)
parents fd0360594636
children 38e5399718ca
line source
1 -- TODO: function incomplete yet
3 local function file_exists(filename)
4 local file = io.open(filename, "r")
5 if file then
6 io.close(file)
7 return true
8 else
9 return false
10 end
11 end
13 function request.process()
15 local success, error_info = xpcall(
16 function()
18 -- restore slots if coming from http redirect
19 local tempstore_value = request.get_param{method = "GET", name = "_tempstore"}
20 if tempstore_value then
21 trace.restore_slots{}
22 local blob = tempstore.pop(tempstore_value)
23 if blob then slot.restore_all(blob) end
24 end
26 if request.get_action() then
27 trace.request{
28 module = request.get_module(),
29 action = request.get_action()
30 }
31 if
32 request.get_404_route() and
33 not file_exists(
34 encode.action_file_path{
35 module = request.get_module(),
36 action = request.get_action()
37 }
38 )
39 then
40 request.set_status("404 Not Found")
41 request.forward(request.get_404_route())
42 else
43 if request._http_request.method ~= "POST" then
44 request.set_status("405 Method Not Allowed")
45 request.add_header("Allow", "POST")
46 error("Tried to invoke an action with a GET request.")
47 end
48 local action_status = execute.filtered_action{
49 module = request.get_module(),
50 action = request.get_action(),
51 }
52 if not request.is_rerouted() then
53 local routing_mode, routing_module, routing_view
54 routing_mode = cgi.params["_webmcp_routing." .. action_status .. ".mode"]
55 routing_module = cgi.params["_webmcp_routing." .. action_status .. ".module"]
56 routing_view = cgi.params["_webmcp_routing." .. action_status .. ".view"]
57 routing_anchor = cgi.params["_webmcp_routing." .. action_status .. ".anchor"]
58 if not (routing_mode or routing_module or routing_view) then
59 action_status = "default"
60 routing_mode = cgi.params["_webmcp_routing.default.mode"]
61 routing_module = cgi.params["_webmcp_routing.default.module"]
62 routing_view = cgi.params["_webmcp_routing.default.view"]
63 routing_anchor = cgi.params["_webmcp_routing.default.anchor"]
64 end
65 assert(routing_module, "Routing information has no module.")
66 assert(routing_view, "Routing information has no view.")
67 if routing_mode == "redirect" then
68 local routing_params = {}
69 for key, value in pairs(cgi.params) do
70 local status, stripped_key = string.match(
71 key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$"
72 )
73 if status == action_status then
74 routing_params[stripped_key] = value
75 end
76 end
77 request.redirect{
78 module = routing_module,
79 view = routing_view,
80 id = cgi.params["_webmcp_routing." .. action_status .. ".id"],
81 params = routing_params,
82 anchor = routing_anchor
83 }
84 elseif routing_mode == "forward" then
85 request.forward{ module = routing_module, view = routing_view }
86 else
87 error("Missing or unknown routing mode in request parameters.")
88 end
89 end
90 end
91 else
92 -- no action
93 trace.request{
94 module = request.get_module(),
95 view = request.get_view()
96 }
97 if
98 request.get_404_route() and
99 not file_exists(
100 encode.view_file_path{
101 module = request.get_module(),
102 view = request.get_view()
103 }
104 )
105 then
106 request.set_status("404 Not Found")
107 request.forward(request.get_404_route())
108 end
109 end
111 if not request.get_redirect_data() then
112 request.process_forward()
113 local view = request.get_view()
114 if string.find(view, "^_") then
115 error("Tried to call a private view (prefixed with underscore).")
116 end
117 execute.filtered_view{
118 module = request.get_module(),
119 view = view,
120 }
121 end
123 -- force error due to missing absolute base URL until its too late to display error message
124 --if request.get_redirect_data() then
125 -- request.get_absolute_baseurl()
126 --end
128 end,
130 function(errobj)
131 return {
132 errobj = errobj,
133 stacktrace = string.gsub(
134 debug.traceback('', 2),
135 "^\r?\n?stack traceback:\r?\n?", ""
136 )
137 }
138 end
139 )
141 if not success then trace.error{} end
143 -- laufzeitermittlung
144 trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() }
146 slot.select('trace', trace.render) -- render trace information
148 local redirect_data = request.get_redirect_data()
150 -- log error and switch to error layout, unless success
151 if not success then
152 local errobj = error_info.errobj
153 local stacktrace = error_info.stacktrace
154 if not request.get_status() and not request.get_json_request_slots() then
155 request.set_status("500 Internal Server Error")
156 end
157 slot.set_layout('system_error')
158 slot.select('system_error', function()
159 if getmetatable(errobj) == mondelefant.errorobject_metatable then
160 slot.put(
161 "<p>Database error of class <b>",
162 encode.html(errobj.code),
163 "</b> occured:<br/><b>",
164 encode.html(errobj.message),
165 "</b></p>"
166 )
167 else
168 slot.put("<p><b>", encode.html(tostring(errobj)), "</b></p>")
169 end
170 slot.put("<p>Stack trace follows:<br/>")
171 slot.put(encode.html_newlines(encode.html(stacktrace)))
172 slot.put("</p>")
173 end)
174 elseif redirect_data then
175 local redirect_params = {}
176 for key, value in pairs(redirect_data.params) do
177 redirect_params[key] = value
178 end
179 local slot_dump = slot.dump_all()
180 if slot_dump ~= "" then
181 redirect_params.tempstore = tempstore.save(slot_dump)
182 end
183 local json_request_slots = request.get_json_request_slots()
184 if json_request_slots then
185 redirect_params["_webmcp_json_slots[]"] = json_request_slots
186 end
187 cgi.redirect(
188 encode.url{
189 base = request.get_absolute_baseurl(),
190 module = redirect_data.module,
191 view = redirect_data.view,
192 id = redirect_data.id,
193 params = redirect_params,
194 anchor = redirect_data.anchor
195 }
196 )
197 cgi.send_data()
198 end
200 if not success or not redirect_data then
202 local http_status = request.get_status()
203 if http_status then
204 cgi.set_status(http_status)
205 end
207 local json_request_slots = request.get_json_request_slots()
208 if json_request_slots then
209 cgi.set_content_type('application/json')
210 local data = {}
211 for idx, slot_ident in ipairs(json_request_slots) do
212 data[slot_ident] = slot.get_content(slot_ident)
213 end
214 cgi.send_data(encode.json(data))
215 else
216 cgi.set_content_type(slot.get_content_type())
217 cgi.send_data(slot.render_layout())
218 end
219 end
221 end

Impressum / About Us