webmcp

view framework/env/request/handler.lua @ 275:ee7fcdade91d

Allow POST param "_webmcp_id" to overwrite/set the id field in routing information
author jbe
date Sat Mar 21 15:21:03 2015 +0100 (2015-03-21)
parents aedd13009ddc
children 5af7c66fbc08
line source
1 --[[--
2 request.handler(
3 http_request, -- HTTP request object
4 close -- boolean indicating whether the server should announce to close the connection
5 )
7 Called by mcp.lua to process an HTTP request. Performs some initializations, calls request.router(), and handles the request.
9 --]]--
11 local function file_exists(filename)
12 local file = io.open(filename, "r")
13 if file then
14 io.close(file)
15 return true
16 else
17 return false
18 end
19 end
21 function request.handler(http_request, close)
22 _G.app = {} -- may be overwritten or modified by request initializers
23 do
24 request._in_progress = true -- NOTE: must be set to true before initializer functions are called
25 for i, func in ipairs(request._initializers) do
26 func()
27 end
28 end
30 request._http_request = http_request
31 local path = http_request.path
32 if path then
33 local relative_baseurl_elements = {}
34 for match in string.gmatch(path, "/") do
35 relative_baseurl_elements[#relative_baseurl_elements+1] = "../"
36 end
37 request._relative_baseurl = table.concat(relative_baseurl_elements)
38 else
39 request._relative_baseurl = nil
40 end
41 request._route = request.router() or {}
42 do
43 local post_id = http_request.post_params["_webmcp_id"]
44 if post_id then
45 request._route.id = post_id
46 end
47 end
49 if close then
50 request.add_header("Connection", "close")
51 end
53 local success, error_info = xpcall(
54 function()
56 if request._route.static then
57 local f, errmsg = io.open(WEBMCP_BASE_PATH .. "static/" .. request._route.static, "r")
58 if not f then
59 if request.get_404_route() then
60 request.set_status("404 Not Found")
61 request.forward(request.get_404_route())
62 return
63 else
64 error('Could not open static file "' .. request._route.static .. '": ' .. errmsg)
65 end
66 end
67 local d = assert(f:read("*a"))
68 f:close()
69 slot.put_into("data", d)
70 local filename_extension = string.match(request._route.static, "%.([^.]+)$")
71 slot.set_layout(nil, request._mime_types[filename_extension] or "application/octet-stream")
72 return
73 end
75 -- restore slots if coming from http redirect
76 local tempstore_value = http_request.get_params["_tempstore"]
77 if tempstore_value then
78 trace.restore_slots{}
79 local blob = tempstore.pop(tempstore_value)
80 if blob then slot.restore_all(blob) end
81 end
83 if request.get_action() then
84 trace.request{
85 module = request.get_module(),
86 action = request.get_action()
87 }
88 if
89 request.get_404_route() and
90 not file_exists(
91 encode.action_file_path{
92 module = request.get_module(),
93 action = request.get_action()
94 }
95 )
96 then
97 request.set_status("404 Not Found")
98 request.forward(request.get_404_route())
99 else
100 if http_request.method ~= "POST" then
101 request.set_status("405 Method Not Allowed")
102 request.add_header("Allow", "POST")
103 error("Tried to invoke an action with a GET request.")
104 end
105 local action_status = execute.filtered_action{
106 module = request.get_module(),
107 action = request.get_action(),
108 }
109 if not request.is_rerouted() then
110 local routing_mode, routing_module, routing_view, routing_anchor
111 routing_mode = http_request.post_params["_webmcp_routing." .. action_status .. ".mode"]
112 routing_module = http_request.post_params["_webmcp_routing." .. action_status .. ".module"]
113 routing_view = http_request.post_params["_webmcp_routing." .. action_status .. ".view"]
114 routing_anchor = http_request.post_params["_webmcp_routing." .. action_status .. ".anchor"]
115 if not (routing_mode or routing_module or routing_view) then
116 action_status = "default"
117 routing_mode = http_request.post_params["_webmcp_routing.default.mode"]
118 routing_module = http_request.post_params["_webmcp_routing.default.module"]
119 routing_view = http_request.post_params["_webmcp_routing.default.view"]
120 routing_anchor = http_request.post_params["_webmcp_routing.default.anchor"]
121 end
122 assert(routing_module, "Routing information has no module.")
123 assert(routing_view, "Routing information has no view.")
124 if routing_mode == "redirect" then
125 local routing_params = {}
126 for key, value in pairs(request.get_param_strings{ method="POST", include_internal=true }) do
127 local status, stripped_key = string.match(
128 key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$"
129 )
130 if status == action_status then
131 routing_params[stripped_key] = value
132 end
133 end
134 request.redirect{
135 module = routing_module,
136 view = routing_view,
137 id = http_request.post_params["_webmcp_routing." .. action_status .. ".id"],
138 params = routing_params,
139 anchor = routing_anchor
140 }
141 elseif routing_mode == "forward" then
142 request.forward{ module = routing_module, view = routing_view }
143 else
144 error("Missing or unknown routing mode in request parameters.")
145 end
146 end
147 end
148 else
149 -- no action
150 trace.request{
151 module = request.get_module(),
152 view = request.get_view()
153 }
154 if
155 request.get_404_route() and
156 not file_exists(
157 encode.view_file_path{
158 module = request.get_module(),
159 view = request.get_view()
160 }
161 )
162 then
163 request.set_status("404 Not Found")
164 request.forward(request.get_404_route())
165 end
166 end
168 if not request.get_redirect_data() then
169 request.process_forward()
170 local view = request.get_view()
171 if string.find(view, "^_") then
172 error("Tried to call a private view (prefixed with underscore).")
173 end
174 execute.filtered_view{
175 module = request.get_module(),
176 view = view,
177 }
178 end
180 -- force error due to missing absolute base URL until its too late to display error message
181 --if request.get_redirect_data() then
182 -- request.get_absolute_baseurl()
183 --end
185 end,
187 function(errobj)
188 return {
189 errobj = errobj,
190 stacktrace = string.gsub(
191 debug.traceback('', 2),
192 "^\r?\n?stack traceback:\r?\n?", ""
193 )
194 }
195 end
196 )
198 if not success then trace.error{} end
200 -- TODO: extend trace system to generally monitor execution time
201 -- trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() }
203 slot.select('trace', trace.render) -- render trace information
205 local redirect_data = request.get_redirect_data()
207 -- log error and switch to error layout, unless success
208 if not success then
209 local errobj = error_info.errobj
210 local stacktrace = error_info.stacktrace
211 if not request._status then
212 request._status = "500 Internal Server Error"
213 end
214 slot.set_layout('system_error')
215 slot.select('system_error', function()
216 if getmetatable(errobj) == mondelefant.errorobject_metatable then
217 slot.put(
218 "<p>Database error of class <b>",
219 encode.html(errobj.code),
220 "</b> occured:<br/><b>",
221 encode.html(errobj.message),
222 "</b></p>"
223 )
224 else
225 slot.put("<p><b>", encode.html(tostring(errobj)), "</b></p>")
226 end
227 slot.put("<p>Stack trace follows:<br/>")
228 slot.put(encode.html_newlines(encode.html(stacktrace)))
229 slot.put("</p>")
230 end)
231 elseif redirect_data then
232 redirect_data = table.new(redirect_data)
233 if redirect_data.base == nil then
234 redirect_data.base = request.get_absolute_baseurl()
235 end
236 redirect_data.params = table.new(redirect_data.params)
237 local slot_dump = slot.dump_all()
238 if slot_dump ~= "" then
239 redirect_data.params.tempstore = tempstore.save(slot_dump)
240 end
241 http_request:send_status("303 See Other")
242 for i, header in ipairs(request._response_headers) do
243 http_request:send_header(header[1], header[2])
244 end
245 http_request:send_header("Location", encode.url(redirect_data))
246 http_request:finish()
247 end
249 if not success or not redirect_data then
251 http_request:send_status(request._status or "200 OK")
252 for i, header in ipairs(request._response_headers) do
253 http_request:send_header(header[1], header[2])
254 end
255 http_request:send_header("Content-Type", slot.get_content_type())
256 http_request:send_data(slot.render_layout())
257 http_request:finish()
258 end
260 end
262 --//--
264 --[[--
265 app -- table to store an application state
267 'app' is a global table for storing any application state data. It will be reset for every request.
268 --]]--
270 -- Initialized in request.handler(...).
272 --//--

Impressum / About Us