webmcp

view framework/env/request/handler.lua @ 320:e3e2a03f75b2

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

Impressum / About Us