webmcp

view framework/env/request/handler.lua @ 328:04b0687130d8

Bugfix regarding request_per_connection counting; Use new :close_after_finish() method of Moonbridge
author jbe
date Tue Mar 24 01:58:17 2015 +0100 (2015-03-24)
parents d8480a60a07d
children 3db9b672ee73
line source
1 --[[--
2 success = -- false if an error occurred, true otherwise
3 request.handler(
4 http_request, -- HTTP request object
5 )
7 Called by mcp.lua to process an HTTP request. Calls request.router(), and handles the request. Note: request initializers will have to be (automatically) executed before this function is invoked by mcp.lua.
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 request._http_request = http_request
23 local path = http_request.path
24 if path then
25 local relative_baseurl_elements = {}
26 for match in string.gmatch(path, "/") do
27 relative_baseurl_elements[#relative_baseurl_elements+1] = "../"
28 end
29 if #relative_baseurl_elements > 0 then
30 request._relative_baseurl = table.concat(relative_baseurl_elements)
31 else
32 request._relative_baseurl = "./"
33 end
34 else
35 request._relative_baseurl = nil
36 end
37 request._route = request.router() or {}
38 do
39 local post_id = http_request.post_params["_webmcp_id"]
40 if post_id then
41 request._route.id = post_id
42 end
43 end
45 local success, error_info = xpcall(
46 function()
48 if request._route.static then
49 local f, errmsg = io.open(WEBMCP_BASE_PATH .. "static/" .. request._route.static, "r")
50 if not f then
51 request.set_status("404 Not Found")
52 if request.get_404_route() then
53 request.forward(request.get_404_route())
54 else
55 error('Could not open static file "' .. request._route.static .. '": ' .. errmsg)
56 end
57 else
58 local d = assert(f:read("*a"))
59 f:close()
60 slot.put_into("data", d)
61 local filename_extension = string.match(request._route.static, "%.([^.]+)$")
62 slot.set_layout(nil, request._mime_types[filename_extension] or "application/octet-stream")
63 request.allow_caching()
64 return
65 end
66 end
68 -- restore slots if coming from http redirect
69 do
70 local tempstore_value = http_request.get_params["_tempstore"]
71 if tempstore_value then
72 trace.restore_slots{}
73 local blob = tempstore.pop(tempstore_value)
74 if blob then slot.restore_all(blob) end
75 end
76 end
78 if request.get_action() then
79 trace.request{
80 module = request.get_module(),
81 action = request.get_action()
82 }
83 if
84 request.get_404_route() and
85 not file_exists(
86 encode.action_file_path{
87 module = request.get_module(),
88 action = request.get_action()
89 }
90 )
91 then
92 request.set_status("404 Not Found")
93 request.forward(request.get_404_route())
94 else
95 if http_request.method ~= "POST" then
96 request.set_status("405 Method Not Allowed")
97 request.add_header("Allow", "POST")
98 error("Tried to invoke an action with a GET request.")
99 end
100 local action_status = execute.filtered_action{
101 module = request.get_module(),
102 action = request.get_action(),
103 }
104 if not request.is_rerouted() then
105 local routing_mode, routing_module, routing_view, routing_anchor
106 routing_mode = http_request.post_params["_webmcp_routing." .. action_status .. ".mode"]
107 routing_module = http_request.post_params["_webmcp_routing." .. action_status .. ".module"]
108 routing_view = http_request.post_params["_webmcp_routing." .. action_status .. ".view"]
109 routing_anchor = http_request.post_params["_webmcp_routing." .. action_status .. ".anchor"]
110 if not (routing_mode or routing_module or routing_view) then
111 action_status = "default"
112 routing_mode = http_request.post_params["_webmcp_routing.default.mode"]
113 routing_module = http_request.post_params["_webmcp_routing.default.module"]
114 routing_view = http_request.post_params["_webmcp_routing.default.view"]
115 routing_anchor = http_request.post_params["_webmcp_routing.default.anchor"]
116 end
117 assert(routing_module, "Routing information has no module.")
118 assert(routing_view, "Routing information has no view.")
119 if routing_mode == "redirect" then
120 local routing_params = {}
121 for key, value in pairs(request.get_param_strings{ method="POST", include_internal=true }) do
122 local status, stripped_key = string.match(
123 key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$"
124 )
125 if status == action_status then
126 routing_params[stripped_key] = value
127 end
128 end
129 request.redirect{
130 module = routing_module,
131 view = routing_view,
132 id = http_request.post_params["_webmcp_routing." .. action_status .. ".id"],
133 params = routing_params,
134 anchor = routing_anchor
135 }
136 elseif routing_mode == "forward" then
137 request.forward{ module = routing_module, view = routing_view }
138 else
139 error("Missing or unknown routing mode in request parameters.")
140 end
141 end
142 end
143 else
144 -- no action
145 trace.request{
146 module = request.get_module(),
147 view = request.get_view()
148 }
149 if
150 request.get_404_route() and
151 not file_exists(
152 encode.view_file_path{
153 module = request.get_module(),
154 view = request.get_view()
155 }
156 )
157 then
158 request.set_status("404 Not Found")
159 request.forward(request.get_404_route())
160 end
161 end
163 if not request.get_redirect_data() then
164 request.process_forward()
165 local view = request.get_view()
166 if string.find(view, "^_") then
167 error("Tried to call a private view (prefixed with underscore).")
168 end
169 execute.filtered_view{
170 module = request.get_module(),
171 view = view,
172 }
173 end
175 end,
177 function(errobj)
178 return {
179 errobj = errobj,
180 stacktrace = string.gsub(
181 debug.traceback('', 2),
182 "^\r?\n?stack traceback:\r?\n?", ""
183 )
184 }
185 end
186 )
188 if not success then trace.error{} end
190 -- TODO: extend trace system to generally monitor execution time
191 -- trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() }
193 slot.select('trace', trace.render) -- render trace information
195 local redirect_data = request.get_redirect_data()
197 -- log error and switch to error layout, unless success
198 if not success then
199 local errobj = error_info.errobj
200 local stacktrace = error_info.stacktrace
201 if not request._status then
202 request._status = "500 Internal Server Error"
203 end
204 http_request:close_after_finish()
205 slot.set_layout('system_error')
206 slot.select('system_error', function()
207 if getmetatable(errobj) == mondelefant.errorobject_metatable then
208 slot.put(
209 "<p>Database error of class <b>",
210 encode.html(errobj.code),
211 "</b> occured:<br/><b>",
212 encode.html(errobj.message),
213 "</b></p>"
214 )
215 else
216 slot.put("<p><b>", encode.html(tostring(errobj)), "</b></p>")
217 end
218 slot.put("<p>Stack trace follows:<br/>")
219 slot.put(encode.html_newlines(encode.html(stacktrace)))
220 slot.put("</p>")
221 end)
222 elseif redirect_data then
223 redirect_data = table.new(redirect_data)
224 redirect_data.params = table.new(redirect_data.params)
225 local slot_dump = slot.dump_all()
226 if slot_dump ~= "" then
227 redirect_data.params._tempstore = tempstore.save(slot_dump)
228 end
229 http_request:send_status("303 See Other")
230 for i, header in ipairs(request._response_headers) do
231 http_request:send_header(header[1], header[2])
232 end
233 http_request:send_header("Location", encode.url(redirect_data))
234 http_request:finish()
235 end
237 if not success or not redirect_data then
239 http_request:send_status(request._status or "200 OK")
240 for i, header in ipairs(request._response_headers) do
241 http_request:send_header(header[1], header[2])
242 end
243 if not request._cache_manual then
244 local cache_time = request._cache_time
245 if request._cache and cache_time and cache_time > 0 then
246 http_request:send_header("Cache-Control", "max-age=" .. cache_time)
247 else
248 http_request:send_header("Cache-Control", "no-cache")
249 end
250 end
251 http_request:send_header("Content-Type", slot.get_content_type())
252 http_request:send_data(slot.render_layout())
253 http_request:finish()
254 end
256 return success
258 end

Impressum / About Us