3 (c) 2009 Steven Barth <steven@midlink.org>
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
14 local srv = require "luci.lucid.http.server"
15 local proto = require "luci.http.protocol"
16 local util = require "luci.util"
20 module "luci.lucid.http.handler.catchall"
22 --- Create a Redirect handler.
24 -- @param target Redirect Target
26 -- @return Redirect handler object
27 Redirect = util.class(srv.Handler)
29 function Redirect.__init__(self, name, target)
30 srv.Handler.__init__(self, name)
34 --- Handle a GET request.
35 -- @param request Request object
36 -- @return status code, header table, response source
37 function Redirect.handle_GET(self, request)
38 local target = self.target
39 local protocol = request.env.HTTPS and "https://" or "http://"
40 local server = request.env.SERVER_ADDR
41 if server:find(":") then
42 server = "[" .. server .. "]"
45 if self.target:sub(1,1) == ":" then
46 target = protocol .. server .. target
49 local s, e = target:find("%TARGET%", 1, true)
51 local req = protocol .. (request.env.HTTP_HOST or server)
52 .. request.env.REQUEST_URI
53 target = target:sub(1, s-1) .. req .. target:sub(e+1)
56 return 302, { Location = target }
59 --- Handle a POST request.
61 -- @param request Request object
62 -- @return status code, header table, response source
63 Redirect.handle_POST = Redirect.handle_GET
65 --- Handle a HEAD request.
67 -- @param request Request object
68 -- @return status code, header table, response source
69 function Redirect.handle_HEAD(self, request)
70 local stat, head = self:handle_GET(request)