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"
17 local ip = require "luci.ip"
22 module "luci.lucid.http.handler.catchall"
24 --- Create a Redirect handler.
26 -- @param target Redirect Target
28 -- @return Redirect handler object
29 Redirect = util.class(srv.Handler)
31 function Redirect.__init__(self, name, target)
32 srv.Handler.__init__(self, name)
36 --- Handle a GET request.
37 -- @param request Request object
38 -- @return status code, header table, response source
39 function Redirect.handle_GET(self, request)
40 local target = self.target
41 local protocol = request.env.HTTPS and "https://" or "http://"
42 local server = request.env.SERVER_ADDR
44 if request.env.REMOTE_ADDR and not request.env.REMOTE_ADDR:find(":") then
45 local compare = ip.IPv4(request.env.REMOTE_ADDR)
46 for _, iface in ipairs(request.server.interfaces) do
47 if iface.family == "inet" and iface.addr and iface.netmask then
48 if ip.IPv4(iface.addr, iface.netmask):contains(compare) then
56 if server:find(":") then
57 server = "[" .. server .. "]"
60 if self.target:sub(1,1) == ":" then
61 target = protocol .. server .. target
64 local s, e = target:find("%TARGET%", 1, true)
66 local req = protocol .. (request.env.HTTP_HOST or server)
67 .. request.env.REQUEST_URI
68 target = target:sub(1, s-1) .. req .. target:sub(e+1)
71 return 302, { Location = target }
74 --- Handle a POST request.
76 -- @param request Request object
77 -- @return status code, header table, response source
78 Redirect.handle_POST = Redirect.handle_GET
80 --- Handle a HEAD request.
82 -- @param request Request object
83 -- @return status code, header table, response source
84 function Redirect.handle_HEAD(self, request)
85 local stat, head = self:handle_GET(request)