webmcp

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

Impressum / About Us