webmcp

changeset 267:56d237b81c18

Support for external redirects and redirects to static files
author jbe
date Fri Mar 20 15:12:31 2015 +0100 (2015-03-20)
parents 98537ccd0800
children 599b8463b985
files framework/env/encode/url.lua framework/env/request/handler.lua framework/env/request/redirect.lua
line diff
     1.1 --- a/framework/env/encode/url.lua	Fri Mar 20 13:41:28 2015 +0100
     1.2 +++ b/framework/env/encode/url.lua	Fri Mar 20 15:12:31 2015 +0100
     1.3 @@ -1,15 +1,15 @@
     1.4  --[[--
     1.5 -url_string =              -- a string containing an URL
     1.6 +url_string =             -- a string containing an URL
     1.7  encode.url{
     1.8 -  external  = external,   -- external URL (instead of specifying base, module, etc. below)
     1.9 -  base      = base,       -- optional string containing a base URL of a WebMCP application
    1.10 -  static    = static,     -- an URL relative to the static file directory
    1.11 -  module    = module,     -- a module name of the WebMCP application
    1.12 -  view      = view,       -- a view name of the WebMCP application
    1.13 -  action    = action,     -- an action name of the WebMCP application
    1.14 -  id        = id,         -- optional id to be passed to the view or action to select a particular data record
    1.15 -  params    = params,     -- optional parameters to be passed to the view or action
    1.16 -  anchor    = anchor      -- anchor in URL
    1.17 +  external = external,   -- external URL (instead of specifying base, module, etc. below)
    1.18 +  base     = base,       -- optional string containing a base URL of a WebMCP application
    1.19 +  static   = static,     -- an URL relative to the static file directory
    1.20 +  module   = module,     -- a module name of the WebMCP application
    1.21 +  view     = view,       -- a view name of the WebMCP application
    1.22 +  action   = action,     -- an action name of the WebMCP application
    1.23 +  id       = id,         -- optional id to be passed to the view or action to select a particular data record
    1.24 +  params   = params,     -- optional parameters to be passed to the view or action
    1.25 +  anchor   = anchor      -- anchor in URL
    1.26  }
    1.27  
    1.28  This function creates URLs to external locations, to static files within the WebMCP application or to a certain view or action inside a module.
     2.1 --- a/framework/env/request/handler.lua	Fri Mar 20 13:41:28 2015 +0100
     2.2 +++ b/framework/env/request/handler.lua	Fri Mar 20 15:12:31 2015 +0100
     2.3 @@ -214,29 +214,20 @@
     2.4        slot.put("</p>")
     2.5      end)
     2.6    elseif redirect_data then
     2.7 -    local redirect_params = {}
     2.8 -    for key, value in pairs(redirect_data.params) do
     2.9 -      redirect_params[key] = value
    2.10 +    redirect_data = table.new(redirect_data)
    2.11 +    if redirect_data.base == nil then
    2.12 +      redirect_data.base = request.get_absolute_baseurl()
    2.13      end
    2.14 +    redirect_data.params = table.new(redirect_data.params)
    2.15      local slot_dump = slot.dump_all()
    2.16      if slot_dump ~= "" then
    2.17 -      redirect_params.tempstore = tempstore.save(slot_dump)
    2.18 +      redirect_data.params.tempstore = tempstore.save(slot_dump)
    2.19      end
    2.20      http_request:send_status("303 See Other")
    2.21      for i, header in ipairs(request._response_headers) do
    2.22        http_request:send_header(header[1], header[2])
    2.23      end
    2.24 -    http_request:send_header(
    2.25 -      "Location",
    2.26 -      encode.url{
    2.27 -        base   = request.get_absolute_baseurl(),
    2.28 -        module = redirect_data.module,
    2.29 -        view   = redirect_data.view,
    2.30 -        id     = redirect_data.id,
    2.31 -        params = redirect_params,
    2.32 -        anchor = redirect_data.anchor
    2.33 -      }
    2.34 -    )
    2.35 +    http_request:send_header("Location", encode.url(redirect_data))
    2.36      http_request:finish()
    2.37    end
    2.38  
     3.1 --- a/framework/env/request/redirect.lua	Fri Mar 20 13:41:28 2015 +0100
     3.2 +++ b/framework/env/request/redirect.lua	Fri Mar 20 15:12:31 2015 +0100
     3.3 @@ -1,10 +1,14 @@
     3.4  --[[--
     3.5  request.redirect{
     3.6 -  module = module,  -- module name
     3.7 -  view   = view,    -- view name
     3.8 -  id     = id,      -- optional id for view
     3.9 -  params = params,  -- optional view parameters
    3.10 -  anchor = anchor   -- anchor in URL
    3.11 +  external = external,   -- external URL (instead of specifying base, module, etc. below)
    3.12 +  base     = base,       -- optional string containing a base URL of a WebMCP application
    3.13 +  static   = static,     -- an URL relative to the static file directory
    3.14 +  module   = module,     -- a module name of the WebMCP application
    3.15 +  view     = view,       -- a view name of the WebMCP application
    3.16 +  action   = action,     -- an action name of the WebMCP application
    3.17 +  id       = id,         -- optional id to be passed to the view or action to select a particular data record
    3.18 +  params   = params,     -- optional parameters to be passed to the view or action
    3.19 +  anchor   = anchor      -- anchor in URL
    3.20  }
    3.21  
    3.22  Calling this function causes the WebMCP to do a 303 HTTP redirect after the current view or action and all filters have finished execution. If routing mode "redirect" has been chosen, then this function is called automatically after an action and all its filters have finished execution. Calling request.redirect{...} (or request.forward{...}) explicitly inside an action will cause routing information from the browser to be ignored. To preserve GET/POST parameters of an action, use request.forward{...} instead. Currently no redirects to external (absolute) URLs are possible, there will be an implementation in future though.
    3.23 @@ -12,34 +16,26 @@
    3.24  --]]--
    3.25  
    3.26  function request.redirect(args)
    3.27 -  -- TODO: support redirects to external URLs too
    3.28 -  --       (needs fixes in the trace system as well)
    3.29 -  local module = args.module
    3.30 -  local view   = args.view
    3.31 -  local id     = args.id
    3.32 -  local params = args.params or {}
    3.33 -  local anchor = args.anchor or nil
    3.34 -  if type(module) ~= "string" then
    3.35 -    error("No module string passed to request.redirect{...}.")
    3.36 -  end
    3.37 -  if type(view) ~= "string" then
    3.38 -    error("No view string passed to request.redirect{...}.")
    3.39 -  end
    3.40 -  if type(params) ~= "table" then
    3.41 -    error("Params array passed to request.redirect{...} is not a table.")
    3.42 -  end
    3.43 -  if anchor and type(anchor) ~= "string" then
    3.44 -    error("Anchor passed to request.redirect{...} must be a string or nil.")
    3.45 +  args = table.new(args)
    3.46 +  if type(args.external) ~= "string" and type(args.static) ~= "string" then
    3.47 +    if type(args.module) ~= "string" then
    3.48 +      error("No module string passed to request.redirect{...}.")
    3.49 +    end
    3.50 +    if type(args.view) ~= "string" then
    3.51 +      error("No view string passed to request.redirect{...}.")
    3.52 +    end
    3.53 +    if type(args.params) ~= "table" then
    3.54 +      error("Params array passed to request.redirect{...} is not a table.")
    3.55 +    end
    3.56 +    if args.anchor and type(args.anchor) ~= "string" then
    3.57 +      error("Anchor passed to request.redirect{...} must be a string or nil.")
    3.58 +    end
    3.59    end
    3.60    if request.is_rerouted() then
    3.61      error("Tried to redirect after another forward or redirect.")
    3.62    end
    3.63 -  request._redirect = {
    3.64 -    module = module,
    3.65 -    view   = view,
    3.66 -    id     = id,
    3.67 -    params = params,
    3.68 -    anchor = anchor
    3.69 -  }
    3.70 -  trace.redirect{ module = args.module, view = args.view }
    3.71 +  request._redirect = args
    3.72 +  if args.module and args.view then  -- TODO: support for external redirects
    3.73 +    trace.redirect{ module = args.module, view = args.view }
    3.74 +  end
    3.75  end

Impressum / About Us