webmcp

changeset 210:474cf0cfb85b

Moved request handling code from mcp.lua to request.handler(...)
author jbe
date Sat Jan 10 00:28:03 2015 +0100 (2015-01-10)
parents 2cb27106aa73
children 9d3d11cf1bf6
files framework/bin/mcp.lua framework/env/request/handler.lua
line diff
     1.1 --- a/framework/bin/mcp.lua	Sat Jan 10 00:19:38 2015 +0100
     1.2 +++ b/framework/bin/mcp.lua	Sat Jan 10 00:28:03 2015 +0100
     1.3 @@ -204,9 +204,9 @@
     1.4        listener.prepare = execute.prefork_initializers
     1.5        listener.connect = http.generate_handler(
     1.6          request.get_http_options(),
     1.7 -        function(req)
     1.8 +        function(http_request)
     1.9            execute.postfork_initializers()
    1.10 -          request.handler(req)
    1.11 +          request.handler(http_request)
    1.12          end
    1.13        )
    1.14        listener.finish = execute.finalizers
    1.15 @@ -215,229 +215,3 @@
    1.16    end
    1.17  end
    1.18  
    1.19 ---[[ TODO: following lines to be moved to execute.server(...)
    1.20 -
    1.21 -local success, error_info = xpcall(
    1.22 -  function()
    1.23 -
    1.24 -    -- restore slots if coming from http redirect
    1.25 -    if cgi.params.tempstore then
    1.26 -      trace.restore_slots{}
    1.27 -      local blob = tempstore.pop(cgi.params.tempstore)
    1.28 -      if blob then slot.restore_all(blob) end
    1.29 -    end
    1.30 -
    1.31 -    local function file_exists(filename)
    1.32 -      local file = io.open(filename, "r")
    1.33 -      if file then
    1.34 -        io.close(file)
    1.35 -        return true
    1.36 -      else
    1.37 -        return false
    1.38 -      end
    1.39 -    end
    1.40 -
    1.41 -    if request.is_404() then
    1.42 -      request.set_status("404 Not Found")
    1.43 -      if request.get_404_route() then
    1.44 -        request.forward(request.get_404_route())
    1.45 -      else
    1.46 -        error("No 404 page set.")
    1.47 -      end
    1.48 -    elseif request.get_action() then
    1.49 -      trace.request{
    1.50 -        module = request.get_module(),
    1.51 -        action = request.get_action()
    1.52 -      }
    1.53 -      if
    1.54 -        request.get_404_route() and
    1.55 -        not file_exists(
    1.56 -          encode.action_file_path{
    1.57 -            module = request.get_module(),
    1.58 -            action = request.get_action()
    1.59 -          }
    1.60 -        )
    1.61 -      then
    1.62 -        request.set_status("404 Not Found")
    1.63 -        request.forward(request.get_404_route())
    1.64 -      else
    1.65 -        if cgi.method ~= "POST" then
    1.66 -          request.set_status("405 Method Not Allowed")
    1.67 -          cgi.add_header("Allow: POST")
    1.68 -          error("Tried to invoke an action with a GET request.")
    1.69 -        end
    1.70 -        local action_status = execute.filtered_action{
    1.71 -          module = request.get_module(),
    1.72 -          action = request.get_action(),
    1.73 -        }
    1.74 -        if not request.is_rerouted() then
    1.75 -          local routing_mode, routing_module, routing_view
    1.76 -          routing_mode   = cgi.params["_webmcp_routing." .. action_status .. ".mode"]
    1.77 -          routing_module = cgi.params["_webmcp_routing." .. action_status .. ".module"]
    1.78 -          routing_view   = cgi.params["_webmcp_routing." .. action_status .. ".view"]
    1.79 -          routing_anchor = cgi.params["_webmcp_routing." .. action_status .. ".anchor"]
    1.80 -          if not (routing_mode or routing_module or routing_view) then
    1.81 -            action_status = "default"
    1.82 -            routing_mode   = cgi.params["_webmcp_routing.default.mode"]
    1.83 -            routing_module = cgi.params["_webmcp_routing.default.module"]
    1.84 -            routing_view   = cgi.params["_webmcp_routing.default.view"]
    1.85 -            routing_anchor = cgi.params["_webmcp_routing.default.anchor"]
    1.86 -          end
    1.87 -          assert(routing_module, "Routing information has no module.")
    1.88 -          assert(routing_view,   "Routing information has no view.")
    1.89 -          if routing_mode == "redirect" then
    1.90 -            local routing_params = {}
    1.91 -            for key, value in pairs(cgi.params) do
    1.92 -              local status, stripped_key = string.match(
    1.93 -                key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$"
    1.94 -              )
    1.95 -              if status == action_status then
    1.96 -                routing_params[stripped_key] = value
    1.97 -              end
    1.98 -            end
    1.99 -            request.redirect{
   1.100 -              module = routing_module,
   1.101 -              view   = routing_view,
   1.102 -              id     = cgi.params["_webmcp_routing." .. action_status .. ".id"],
   1.103 -              params = routing_params,
   1.104 -              anchor = routing_anchor
   1.105 -            }
   1.106 -          elseif routing_mode == "forward" then
   1.107 -            request.forward{ module = routing_module, view = routing_view }
   1.108 -          else
   1.109 -            error("Missing or unknown routing mode in request parameters.")
   1.110 -          end
   1.111 -        end
   1.112 -      end
   1.113 -    else
   1.114 -      -- no action
   1.115 -      trace.request{
   1.116 -        module = request.get_module(),
   1.117 -        view   = request.get_view()
   1.118 -      }
   1.119 -      if
   1.120 -        request.get_404_route() and
   1.121 -        not file_exists(
   1.122 -          encode.view_file_path{
   1.123 -            module = request.get_module(),
   1.124 -            view   = request.get_view()
   1.125 -          }
   1.126 -        )
   1.127 -      then
   1.128 -        request.set_status("404 Not Found")
   1.129 -        request.forward(request.get_404_route())
   1.130 -      end
   1.131 -    end
   1.132 -
   1.133 -    if not request.get_redirect_data() then
   1.134 -      request.process_forward()
   1.135 -      local view = request.get_view()
   1.136 -      if string.find(view, "^_") then
   1.137 -        error("Tried to call a private view (prefixed with underscore).")
   1.138 -      end
   1.139 -      execute.filtered_view{
   1.140 -        module = request.get_module(),
   1.141 -        view   = view,
   1.142 -      }
   1.143 -    end
   1.144 -
   1.145 -    -- force error due to missing absolute base URL until its too late to display error message
   1.146 -    --if request.get_redirect_data() then
   1.147 -    --  request.get_absolute_baseurl()
   1.148 -    --end
   1.149 -
   1.150 -  end,
   1.151 -
   1.152 -  function(errobj)
   1.153 -    return {
   1.154 -      errobj = errobj,
   1.155 -      stacktrace = string.gsub(
   1.156 -        debug.traceback('', 2),
   1.157 -        "^\r?\n?stack traceback:\r?\n?", ""
   1.158 -      )
   1.159 -    }
   1.160 -  end
   1.161 -)
   1.162 -
   1.163 -if not success then trace.error{} end
   1.164 -
   1.165 --- laufzeitermittlung
   1.166 -trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() }
   1.167 -
   1.168 -slot.select('trace', trace.render)  -- render trace information
   1.169 -
   1.170 -local redirect_data = request.get_redirect_data()
   1.171 -
   1.172 --- log error and switch to error layout, unless success
   1.173 -if not success then
   1.174 -  local errobj     = error_info.errobj
   1.175 -  local stacktrace = error_info.stacktrace
   1.176 -  if not request.get_status() and not request.get_json_request_slots() then
   1.177 -    request.set_status("500 Internal Server Error")
   1.178 -  end
   1.179 -  slot.set_layout('system_error')
   1.180 -  slot.select('system_error', function()
   1.181 -    if getmetatable(errobj) == mondelefant.errorobject_metatable then
   1.182 -      slot.put(
   1.183 -        "<p>Database error of class <b>",
   1.184 -        encode.html(errobj.code),
   1.185 -        "</b> occured:<br/><b>",
   1.186 -        encode.html(errobj.message),
   1.187 -        "</b></p>"
   1.188 -      )
   1.189 -    else
   1.190 -      slot.put("<p><b>", encode.html(tostring(errobj)), "</b></p>")
   1.191 -    end
   1.192 -    slot.put("<p>Stack trace follows:<br/>")
   1.193 -    slot.put(encode.html_newlines(encode.html(stacktrace)))
   1.194 -    slot.put("</p>")
   1.195 -  end)
   1.196 -elseif redirect_data then
   1.197 -  local redirect_params = {}
   1.198 -  for key, value in pairs(redirect_data.params) do
   1.199 -    redirect_params[key] = value
   1.200 -  end
   1.201 -  local slot_dump = slot.dump_all()
   1.202 -  if slot_dump ~= "" then
   1.203 -    redirect_params.tempstore = tempstore.save(slot_dump)
   1.204 -  end
   1.205 -  local json_request_slots = request.get_json_request_slots()
   1.206 -  if json_request_slots then
   1.207 -    redirect_params["_webmcp_json_slots[]"] = json_request_slots  
   1.208 -  end
   1.209 -  cgi.redirect(
   1.210 -    encode.url{
   1.211 -      base   = request.get_absolute_baseurl(),
   1.212 -      module = redirect_data.module,
   1.213 -      view   = redirect_data.view,
   1.214 -      id     = redirect_data.id,
   1.215 -      params = redirect_params,
   1.216 -      anchor = redirect_data.anchor
   1.217 -    }
   1.218 -  )
   1.219 -  cgi.send_data()
   1.220 -end
   1.221 -
   1.222 -if not success or not redirect_data then
   1.223 -
   1.224 -  local http_status = request.get_status()
   1.225 -  if http_status then
   1.226 -    cgi.set_status(http_status)
   1.227 -  end
   1.228 -
   1.229 -  local json_request_slots = request.get_json_request_slots()
   1.230 -  if json_request_slots then
   1.231 -    cgi.set_content_type('application/json')
   1.232 -    local data = {}
   1.233 -    for idx, slot_ident in ipairs(json_request_slots) do
   1.234 -      data[slot_ident] = slot.get_content(slot_ident)
   1.235 -    end
   1.236 -    cgi.send_data(encode.json(data))
   1.237 -  else
   1.238 -    cgi.set_content_type(slot.get_content_type())
   1.239 -    cgi.send_data(slot.render_layout())
   1.240 -  end
   1.241 -end
   1.242 -
   1.243 ---]]
   1.244 -
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/framework/env/request/handler.lua	Sat Jan 10 00:28:03 2015 +0100
     2.3 @@ -0,0 +1,227 @@
     2.4 +-- TODO: function incomplete yet
     2.5 +
     2.6 +function request.handler(http_request)
     2.7 +
     2.8 +  local success, error_info = xpcall(
     2.9 +    function()
    2.10 +
    2.11 +      -- restore slots if coming from http redirect
    2.12 +      if cgi.params.tempstore then
    2.13 +        trace.restore_slots{}
    2.14 +        local blob = tempstore.pop(cgi.params.tempstore)
    2.15 +        if blob then slot.restore_all(blob) end
    2.16 +      end
    2.17 +
    2.18 +      local function file_exists(filename)
    2.19 +        local file = io.open(filename, "r")
    2.20 +        if file then
    2.21 +          io.close(file)
    2.22 +          return true
    2.23 +        else
    2.24 +          return false
    2.25 +        end
    2.26 +      end
    2.27 +
    2.28 +      if request.is_404() then
    2.29 +        request.set_status("404 Not Found")
    2.30 +        if request.get_404_route() then
    2.31 +          request.forward(request.get_404_route())
    2.32 +        else
    2.33 +          error("No 404 page set.")
    2.34 +        end
    2.35 +      elseif request.get_action() then
    2.36 +        trace.request{
    2.37 +          module = request.get_module(),
    2.38 +          action = request.get_action()
    2.39 +        }
    2.40 +        if
    2.41 +          request.get_404_route() and
    2.42 +          not file_exists(
    2.43 +            encode.action_file_path{
    2.44 +              module = request.get_module(),
    2.45 +              action = request.get_action()
    2.46 +            }
    2.47 +          )
    2.48 +        then
    2.49 +          request.set_status("404 Not Found")
    2.50 +          request.forward(request.get_404_route())
    2.51 +        else
    2.52 +          if cgi.method ~= "POST" then
    2.53 +            request.set_status("405 Method Not Allowed")
    2.54 +            cgi.add_header("Allow: POST")
    2.55 +            error("Tried to invoke an action with a GET request.")
    2.56 +          end
    2.57 +          local action_status = execute.filtered_action{
    2.58 +            module = request.get_module(),
    2.59 +            action = request.get_action(),
    2.60 +          }
    2.61 +          if not request.is_rerouted() then
    2.62 +            local routing_mode, routing_module, routing_view
    2.63 +            routing_mode   = cgi.params["_webmcp_routing." .. action_status .. ".mode"]
    2.64 +            routing_module = cgi.params["_webmcp_routing." .. action_status .. ".module"]
    2.65 +            routing_view   = cgi.params["_webmcp_routing." .. action_status .. ".view"]
    2.66 +            routing_anchor = cgi.params["_webmcp_routing." .. action_status .. ".anchor"]
    2.67 +            if not (routing_mode or routing_module or routing_view) then
    2.68 +              action_status = "default"
    2.69 +              routing_mode   = cgi.params["_webmcp_routing.default.mode"]
    2.70 +              routing_module = cgi.params["_webmcp_routing.default.module"]
    2.71 +              routing_view   = cgi.params["_webmcp_routing.default.view"]
    2.72 +              routing_anchor = cgi.params["_webmcp_routing.default.anchor"]
    2.73 +            end
    2.74 +            assert(routing_module, "Routing information has no module.")
    2.75 +            assert(routing_view,   "Routing information has no view.")
    2.76 +            if routing_mode == "redirect" then
    2.77 +              local routing_params = {}
    2.78 +              for key, value in pairs(cgi.params) do
    2.79 +                local status, stripped_key = string.match(
    2.80 +                  key, "^_webmcp_routing%.([^%.]*)%.params%.(.*)$"
    2.81 +                )
    2.82 +                if status == action_status then
    2.83 +                  routing_params[stripped_key] = value
    2.84 +                end
    2.85 +              end
    2.86 +              request.redirect{
    2.87 +                module = routing_module,
    2.88 +                view   = routing_view,
    2.89 +                id     = cgi.params["_webmcp_routing." .. action_status .. ".id"],
    2.90 +                params = routing_params,
    2.91 +                anchor = routing_anchor
    2.92 +              }
    2.93 +            elseif routing_mode == "forward" then
    2.94 +              request.forward{ module = routing_module, view = routing_view }
    2.95 +            else
    2.96 +              error("Missing or unknown routing mode in request parameters.")
    2.97 +            end
    2.98 +          end
    2.99 +        end
   2.100 +      else
   2.101 +        -- no action
   2.102 +        trace.request{
   2.103 +          module = request.get_module(),
   2.104 +          view   = request.get_view()
   2.105 +        }
   2.106 +        if
   2.107 +          request.get_404_route() and
   2.108 +          not file_exists(
   2.109 +            encode.view_file_path{
   2.110 +              module = request.get_module(),
   2.111 +              view   = request.get_view()
   2.112 +            }
   2.113 +          )
   2.114 +        then
   2.115 +          request.set_status("404 Not Found")
   2.116 +          request.forward(request.get_404_route())
   2.117 +        end
   2.118 +      end
   2.119 +
   2.120 +      if not request.get_redirect_data() then
   2.121 +        request.process_forward()
   2.122 +        local view = request.get_view()
   2.123 +        if string.find(view, "^_") then
   2.124 +          error("Tried to call a private view (prefixed with underscore).")
   2.125 +        end
   2.126 +        execute.filtered_view{
   2.127 +          module = request.get_module(),
   2.128 +          view   = view,
   2.129 +        }
   2.130 +      end
   2.131 +
   2.132 +      -- force error due to missing absolute base URL until its too late to display error message
   2.133 +      --if request.get_redirect_data() then
   2.134 +      --  request.get_absolute_baseurl()
   2.135 +      --end
   2.136 +
   2.137 +    end,
   2.138 +
   2.139 +    function(errobj)
   2.140 +      return {
   2.141 +        errobj = errobj,
   2.142 +        stacktrace = string.gsub(
   2.143 +          debug.traceback('', 2),
   2.144 +          "^\r?\n?stack traceback:\r?\n?", ""
   2.145 +        )
   2.146 +      }
   2.147 +    end
   2.148 +  )
   2.149 +
   2.150 +  if not success then trace.error{} end
   2.151 +
   2.152 +  -- laufzeitermittlung
   2.153 +  trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() }
   2.154 +
   2.155 +  slot.select('trace', trace.render)  -- render trace information
   2.156 +
   2.157 +  local redirect_data = request.get_redirect_data()
   2.158 +
   2.159 +  -- log error and switch to error layout, unless success
   2.160 +  if not success then
   2.161 +    local errobj     = error_info.errobj
   2.162 +    local stacktrace = error_info.stacktrace
   2.163 +    if not request.get_status() and not request.get_json_request_slots() then
   2.164 +      request.set_status("500 Internal Server Error")
   2.165 +    end
   2.166 +    slot.set_layout('system_error')
   2.167 +    slot.select('system_error', function()
   2.168 +      if getmetatable(errobj) == mondelefant.errorobject_metatable then
   2.169 +        slot.put(
   2.170 +          "<p>Database error of class <b>",
   2.171 +          encode.html(errobj.code),
   2.172 +          "</b> occured:<br/><b>",
   2.173 +          encode.html(errobj.message),
   2.174 +          "</b></p>"
   2.175 +        )
   2.176 +      else
   2.177 +        slot.put("<p><b>", encode.html(tostring(errobj)), "</b></p>")
   2.178 +      end
   2.179 +      slot.put("<p>Stack trace follows:<br/>")
   2.180 +      slot.put(encode.html_newlines(encode.html(stacktrace)))
   2.181 +      slot.put("</p>")
   2.182 +    end)
   2.183 +  elseif redirect_data then
   2.184 +    local redirect_params = {}
   2.185 +    for key, value in pairs(redirect_data.params) do
   2.186 +      redirect_params[key] = value
   2.187 +    end
   2.188 +    local slot_dump = slot.dump_all()
   2.189 +    if slot_dump ~= "" then
   2.190 +      redirect_params.tempstore = tempstore.save(slot_dump)
   2.191 +    end
   2.192 +    local json_request_slots = request.get_json_request_slots()
   2.193 +    if json_request_slots then
   2.194 +      redirect_params["_webmcp_json_slots[]"] = json_request_slots  
   2.195 +    end
   2.196 +    cgi.redirect(
   2.197 +      encode.url{
   2.198 +        base   = request.get_absolute_baseurl(),
   2.199 +        module = redirect_data.module,
   2.200 +        view   = redirect_data.view,
   2.201 +        id     = redirect_data.id,
   2.202 +        params = redirect_params,
   2.203 +        anchor = redirect_data.anchor
   2.204 +      }
   2.205 +    )
   2.206 +    cgi.send_data()
   2.207 +  end
   2.208 +
   2.209 +  if not success or not redirect_data then
   2.210 +
   2.211 +    local http_status = request.get_status()
   2.212 +    if http_status then
   2.213 +      cgi.set_status(http_status)
   2.214 +    end
   2.215 +
   2.216 +    local json_request_slots = request.get_json_request_slots()
   2.217 +    if json_request_slots then
   2.218 +      cgi.set_content_type('application/json')
   2.219 +      local data = {}
   2.220 +      for idx, slot_ident in ipairs(json_request_slots) do
   2.221 +        data[slot_ident] = slot.get_content(slot_ident)
   2.222 +      end
   2.223 +      cgi.send_data(encode.json(data))
   2.224 +    else
   2.225 +      cgi.set_content_type(slot.get_content_type())
   2.226 +      cgi.send_data(slot.render_layout())
   2.227 +    end
   2.228 +  end
   2.229 +
   2.230 +end

Impressum / About Us