webmcp

view framework/env/request/handler.lua @ 264:8aa38ddcc1b2

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

Impressum / About Us