webmcp

changeset 358:545ec2e3eafa

Code cleanup in request.handler(...) and request.default_router(...)
author jbe
date Thu Mar 26 20:19:54 2015 +0100 (2015-03-26)
parents ffdd32b48296
children a74e62c96501
files framework/env/request/default_router.lua framework/env/request/handler.lua framework/env/request/initialize.lua
line diff
     1.1 --- a/framework/env/request/default_router.lua	Thu Mar 26 20:09:13 2015 +0100
     1.2 +++ b/framework/env/request/default_router.lua	Thu Mar 26 20:19:54 2015 +0100
     1.3 @@ -17,15 +17,7 @@
     1.4    end
     1.5    local static = string.match(path, "^static/([-./0-9A-Z_a-z]*)$")
     1.6    if static then
     1.7 -    -- TODO: move sanitizer to request.handler(...)
     1.8 -    if string.match(static, "^/") or string.match(static, "//") then
     1.9 -      return nil
    1.10 -    end
    1.11 -    for element in string.gmatch(static, "[^/]+") do
    1.12 -      if element == "." or element == ".." then
    1.13 -        return nil
    1.14 -      end
    1.15 -    end
    1.16 +    -- Note: sanitizer is in request.handler(...)
    1.17      return {static = static}
    1.18    end
    1.19    local module, action, view, id, suffix
     2.1 --- a/framework/env/request/handler.lua	Thu Mar 26 20:09:13 2015 +0100
     2.2 +++ b/framework/env/request/handler.lua	Thu Mar 26 20:19:54 2015 +0100
     2.3 @@ -4,7 +4,7 @@
     2.4    http_request    -- HTTP request object
     2.5  )
     2.6  
     2.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.
     2.8 +Called by mcp.lua to process an HTTP request. Calls request.router() and handles the request. Note: request initializers (see request.initialize()) are to be executed by mcp.lua before this function is invoked by mcp.lua.
     2.9  
    2.10  --]]--
    2.11  
    2.12 @@ -46,32 +46,39 @@
    2.13        end
    2.14  
    2.15        if request._route.static then
    2.16 -        local filename = WEBMCP_BASE_PATH .. "static/" .. request._route.static
    2.17 -        -- TODO: move sanitizer from request.default_router(...) to request.handler(...)
    2.18 +        local subpath = request._route.static
    2.19 +        for element in string.gmatch(subpath, "[^/]+") do
    2.20 +          if element == "." or element == ".." then
    2.21 +            subpath = nil
    2.22 +            break
    2.23 +          end
    2.24 +        end
    2.25          local fstat, f, errmsg
    2.26 -        fstat, errmsg = extos.stat(filename)
    2.27 -        if fstat then
    2.28 -          if fstat.isdir then
    2.29 -            errmsg = "Is a directory"
    2.30 -          elseif not fstat.isreg then
    2.31 -            errmsg = "Not a regular file"
    2.32 -          else
    2.33 -            f, errmsg = io.open(filename, "r")
    2.34 +        if subpath then
    2.35 +          local filename = WEBMCP_BASE_PATH .. "static/" .. subpath
    2.36 +          fstat, errmsg = extos.stat(filename)
    2.37 +          if fstat then
    2.38 +            if fstat.isdir then
    2.39 +              errmsg = "Is a directory"
    2.40 +            elseif not fstat.isreg then
    2.41 +              errmsg = "Not a regular file"
    2.42 +            else
    2.43 +              f, errmsg = io.open(filename, "r")
    2.44 +            end
    2.45            end
    2.46          end
    2.47          if not f then
    2.48 -          request.set_status("404 Not Found")
    2.49            if request.get_404_route() then
    2.50              request.set_status("404 Not Found")
    2.51              request.forward(request.get_404_route())
    2.52            else
    2.53 -            error('Could not open static file "' .. request._route.static .. '": ' .. errmsg)
    2.54 +            error('Could not open static file "' .. subpath .. '": ' .. errmsg)
    2.55            end
    2.56          else
    2.57            local d = assert(f:read("*a"))
    2.58            f:close()
    2.59            slot.put_into("data", d)
    2.60 -          local filename_extension = string.match(request._route.static, "%.([^.]+)$")
    2.61 +          local filename_extension = string.match(subpath, "%.([^.]+)$")
    2.62            slot.set_layout(nil, request._mime_types[filename_extension] or "application/octet-stream")
    2.63            request.allow_caching()
    2.64            return
    2.65 @@ -94,12 +101,12 @@
    2.66            action = request.get_action()
    2.67          }
    2.68          if
    2.69 -          request.get_404_route() and
    2.70            not execute.action{
    2.71              module = request.get_module(),
    2.72              action = request.get_action(),
    2.73              test_existence = true
    2.74 -          }
    2.75 +          } and
    2.76 +          request.get_404_route()
    2.77          then
    2.78            request.set_status("404 Not Found")
    2.79            request.forward(request.get_404_route())
    2.80 @@ -159,12 +166,11 @@
    2.81            view   = request.get_view()
    2.82          }
    2.83          if
    2.84 -          request.get_404_route() and
    2.85            not execute.view{
    2.86              module = request.get_module(),
    2.87              view   = request.get_view(),
    2.88              test_existence = true
    2.89 -          }
    2.90 +          } and request.get_404_route()
    2.91          then
    2.92            request.set_status("404 Not Found")
    2.93            request.forward(request.get_404_route())
    2.94 @@ -198,9 +204,6 @@
    2.95  
    2.96    if not success then trace.error{} end
    2.97  
    2.98 -  -- TODO: extend trace system to generally monitor execution time
    2.99 -  -- trace.exectime{ real = extos.monotonic_hires_time(), cpu = os.clock() }
   2.100 -
   2.101    slot.select('trace', trace.render)  -- render trace information
   2.102  
   2.103    local redirect_data = request.get_redirect_data()
     3.1 --- a/framework/env/request/initialize.lua	Thu Mar 26 20:09:13 2015 +0100
     3.2 +++ b/framework/env/request/initialize.lua	Thu Mar 26 20:19:54 2015 +0100
     3.3 @@ -5,7 +5,7 @@
     3.4  
     3.5  --]]--
     3.6  function request.initialize()
     3.7 -  _G.app = {}  -- may be overwritten or modified by request initializers
     3.8 +  _G.app = {}  -- may be filled and modified by request initializers
     3.9    do
    3.10      request._in_progress = true  -- NOTE: must be set to true before initializer functions are called
    3.11      for i, func in ipairs(request._initializers) do

Impressum / About Us