webmcp

diff framework/env/request/handler.lua @ 215:ba3dd4a17e3d

Some code cleanup/rearrangement for request handling
author jbe
date Mon Jan 12 01:48:11 2015 +0100 (2015-01-12)
parents 47ebf4213716
children fd0360594636
line diff
     1.1 --- a/framework/env/request/handler.lua	Sat Jan 10 10:44:17 2015 +0100
     1.2 +++ b/framework/env/request/handler.lua	Mon Jan 12 01:48:11 2015 +0100
     1.3 @@ -1,228 +1,30 @@
     1.4 --- TODO: function incomplete yet
     1.5 +--[[--
     1.6 +request.handler(
     1.7 +  request         -- HTTP request object
     1.8 +)
     1.9 +
    1.10 +Called by mcp.lua to process an HTTP request. Performs some initializations, then calls request.router().
    1.11 +
    1.12 +--]]--
    1.13  
    1.14  function request.handler(http_request)
    1.15    request._http_request = http_request
    1.16 -
    1.17 -  local success, error_info = xpcall(
    1.18 -    function()
    1.19 -
    1.20 -      -- restore slots if coming from http redirect
    1.21 -      if cgi.params.tempstore then
    1.22 -        trace.restore_slots{}
    1.23 -        local blob = tempstore.pop(cgi.params.tempstore)
    1.24 -        if blob then slot.restore_all(blob) end
    1.25 -      end
    1.26 -
    1.27 -      local function file_exists(filename)
    1.28 -        local file = io.open(filename, "r")
    1.29 -        if file then
    1.30 -          io.close(file)
    1.31 -          return true
    1.32 -        else
    1.33 -          return false
    1.34 -        end
    1.35 -      end
    1.36 +  local path = http_request.path
    1.37 +  if path then
    1.38 +    local elements = {}
    1.39 +    for match in string.gmatch(path, "/") do
    1.40 +      elements[#elements+1] = "../"
    1.41 +    end
    1.42 +    elements[#elements] = nil
    1.43 +    if #elements > 0 then
    1.44 +      request._relative_baseurl = table.concat(elements)
    1.45 +    else
    1.46 +      request._relative_baseurl = "./"
    1.47 +    end
    1.48 +  else
    1.49 +    request._relative_baseurl = nil
    1.50 +  end
    1.51 +  request.router()
    1.52 +end
    1.53  
    1.54 -      if request.is_404() then
    1.55 -        request.set_status("404 Not Found")
    1.56 -        if request.get_404_route() then
    1.57 -          request.forward(request.get_404_route())
    1.58 -        else
    1.59 -          error("No 404 page set.")
    1.60 -        end
    1.61 -      elseif request.get_action() then
    1.62 -        trace.request{
    1.63 -          module = request.get_module(),
    1.64 -          action = request.get_action()
    1.65 -        }
    1.66 -        if
    1.67 -          request.get_404_route() and
    1.68 -          not file_exists(
    1.69 -            encode.action_file_path{
    1.70 -              module = request.get_module(),
    1.71 -              action = request.get_action()
    1.72 -            }
    1.73 -          )
    1.74 -        then
    1.75 -          request.set_status("404 Not Found")
    1.76 -          request.forward(request.get_404_route())
    1.77 -        else
    1.78 -          if cgi.method ~= "POST" then
    1.79 -            request.set_status("405 Method Not Allowed")
    1.80 -            cgi.add_header("Allow: POST")
    1.81 -            error("Tried to invoke an action with a GET request.")
    1.82 -          end
    1.83 -          local action_status = execute.filtered_action{
    1.84 -            module = request.get_module(),
    1.85 -            action = request.get_action(),
    1.86 -          }
    1.87 -          if not request.is_rerouted() then
    1.88 -            local routing_mode, routing_module, routing_view
    1.89 -            routing_mode   = cgi.params["_webmcp_routing." .. action_status .. ".mode"]
    1.90 -            routing_module = cgi.params["_webmcp_routing." .. action_status .. ".module"]
    1.91 -            routing_view   = cgi.params["_webmcp_routing." .. action_status .. ".view"]
    1.92 -            routing_anchor = cgi.params["_webmcp_routing." .. action_status .. ".anchor"]
    1.93 -            if not (routing_mode or routing_module or routing_view) then
    1.94 -              action_status = "default"
    1.95 -              routing_mode   = cgi.params["_webmcp_routing.default.mode"]
    1.96 -              routing_module = cgi.params["_webmcp_routing.default.module"]
    1.97 -              routing_view   = cgi.params["_webmcp_routing.default.view"]
    1.98 -              routing_anchor = cgi.params["_webmcp_routing.default.anchor"]
    1.99 -            end
   1.100 -            assert(routing_module, "Routing information has no module.")
   1.101 -            assert(routing_view,   "Routing information has no view.")
   1.102 -            if routing_mode == "redirect" then
   1.103 -              local routing_params = {}
   1.104 -              for key, value in pairs(cgi.params) do
   1.105 -                local status, stripped_key = string.match(
   1.106 -                  key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$"
   1.107 -                )
   1.108 -                if status == action_status then
   1.109 -                  routing_params[stripped_key] = value
   1.110 -                end
   1.111 -              end
   1.112 -              request.redirect{
   1.113 -                module = routing_module,
   1.114 -                view   = routing_view,
   1.115 -                id     = cgi.params["_webmcp_routing." .. action_status .. ".id"],
   1.116 -                params = routing_params,
   1.117 -                anchor = routing_anchor
   1.118 -              }
   1.119 -            elseif routing_mode == "forward" then
   1.120 -              request.forward{ module = routing_module, view = routing_view }
   1.121 -            else
   1.122 -              error("Missing or unknown routing mode in request parameters.")
   1.123 -            end
   1.124 -          end
   1.125 -        end
   1.126 -      else
   1.127 -        -- no action
   1.128 -        trace.request{
   1.129 -          module = request.get_module(),
   1.130 -          view   = request.get_view()
   1.131 -        }
   1.132 -        if
   1.133 -          request.get_404_route() and
   1.134 -          not file_exists(
   1.135 -            encode.view_file_path{
   1.136 -              module = request.get_module(),
   1.137 -              view   = request.get_view()
   1.138 -            }
   1.139 -          )
   1.140 -        then
   1.141 -          request.set_status("404 Not Found")
   1.142 -          request.forward(request.get_404_route())
   1.143 -        end
   1.144 -      end
   1.145 -
   1.146 -      if not request.get_redirect_data() then
   1.147 -        request.process_forward()
   1.148 -        local view = request.get_view()
   1.149 -        if string.find(view, "^_") then
   1.150 -          error("Tried to call a private view (prefixed with underscore).")
   1.151 -        end
   1.152 -        execute.filtered_view{
   1.153 -          module = request.get_module(),
   1.154 -          view   = view,
   1.155 -        }
   1.156 -      end
   1.157 -
   1.158 -      -- force error due to missing absolute base URL until its too late to display error message
   1.159 -      --if request.get_redirect_data() then
   1.160 -      --  request.get_absolute_baseurl()
   1.161 -      --end
   1.162 -
   1.163 -    end,
   1.164 -
   1.165 -    function(errobj)
   1.166 -      return {
   1.167 -        errobj = errobj,
   1.168 -        stacktrace = string.gsub(
   1.169 -          debug.traceback('', 2),
   1.170 -          "^\r?\n?stack traceback:\r?\n?", ""
   1.171 -        )
   1.172 -      }
   1.173 -    end
   1.174 -  )
   1.175 -
   1.176 -  if not success then trace.error{} end
   1.177 -
   1.178 -  -- laufzeitermittlung
   1.179 -  trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() }
   1.180 -
   1.181 -  slot.select('trace', trace.render)  -- render trace information
   1.182 -
   1.183 -  local redirect_data = request.get_redirect_data()
   1.184 -
   1.185 -  -- log error and switch to error layout, unless success
   1.186 -  if not success then
   1.187 -    local errobj     = error_info.errobj
   1.188 -    local stacktrace = error_info.stacktrace
   1.189 -    if not request.get_status() and not request.get_json_request_slots() then
   1.190 -      request.set_status("500 Internal Server Error")
   1.191 -    end
   1.192 -    slot.set_layout('system_error')
   1.193 -    slot.select('system_error', function()
   1.194 -      if getmetatable(errobj) == mondelefant.errorobject_metatable then
   1.195 -        slot.put(
   1.196 -          "<p>Database error of class <b>",
   1.197 -          encode.html(errobj.code),
   1.198 -          "</b> occured:<br/><b>",
   1.199 -          encode.html(errobj.message),
   1.200 -          "</b></p>"
   1.201 -        )
   1.202 -      else
   1.203 -        slot.put("<p><b>", encode.html(tostring(errobj)), "</b></p>")
   1.204 -      end
   1.205 -      slot.put("<p>Stack trace follows:<br/>")
   1.206 -      slot.put(encode.html_newlines(encode.html(stacktrace)))
   1.207 -      slot.put("</p>")
   1.208 -    end)
   1.209 -  elseif redirect_data then
   1.210 -    local redirect_params = {}
   1.211 -    for key, value in pairs(redirect_data.params) do
   1.212 -      redirect_params[key] = value
   1.213 -    end
   1.214 -    local slot_dump = slot.dump_all()
   1.215 -    if slot_dump ~= "" then
   1.216 -      redirect_params.tempstore = tempstore.save(slot_dump)
   1.217 -    end
   1.218 -    local json_request_slots = request.get_json_request_slots()
   1.219 -    if json_request_slots then
   1.220 -      redirect_params["_webmcp_json_slots[]"] = json_request_slots  
   1.221 -    end
   1.222 -    cgi.redirect(
   1.223 -      encode.url{
   1.224 -        base   = request.get_absolute_baseurl(),
   1.225 -        module = redirect_data.module,
   1.226 -        view   = redirect_data.view,
   1.227 -        id     = redirect_data.id,
   1.228 -        params = redirect_params,
   1.229 -        anchor = redirect_data.anchor
   1.230 -      }
   1.231 -    )
   1.232 -    cgi.send_data()
   1.233 -  end
   1.234 -
   1.235 -  if not success or not redirect_data then
   1.236 -
   1.237 -    local http_status = request.get_status()
   1.238 -    if http_status then
   1.239 -      cgi.set_status(http_status)
   1.240 -    end
   1.241 -
   1.242 -    local json_request_slots = request.get_json_request_slots()
   1.243 -    if json_request_slots then
   1.244 -      cgi.set_content_type('application/json')
   1.245 -      local data = {}
   1.246 -      for idx, slot_ident in ipairs(json_request_slots) do
   1.247 -        data[slot_ident] = slot.get_content(slot_ident)
   1.248 -      end
   1.249 -      cgi.send_data(encode.json(data))
   1.250 -    else
   1.251 -      cgi.set_content_type(slot.get_content_type())
   1.252 -      cgi.send_data(slot.render_layout())
   1.253 -    end
   1.254 -  end
   1.255 -
   1.256 -end
   1.257 +--//--

Impressum / About Us