libs/lucid-http: use SERVER_ADDR as fallback
[project/luci.git] / libs / lucid-http / luasrc / lucid / http / handler / catchall.lua
1 --[[
2 LuCId HTTP-Slave
3 (c) 2009 Steven Barth <steven@midlink.org>
4
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
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12 ]]--
13
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"
18 local ipairs = ipairs
19
20 --- Catchall Handler
21 -- @cstyle instance
22 module "luci.lucid.http.handler.catchall"
23
24 --- Create a Redirect handler.
25 -- @param name Name
26 -- @param target Redirect Target
27 -- @class function
28 -- @return Redirect handler object
29 Redirect = util.class(srv.Handler)
30
31 function Redirect.__init__(self, name, target)
32         srv.Handler.__init__(self, name)
33         self.target = target
34 end
35
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
43
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
49                                         server = iface.addr
50                                         break
51                                 end
52                         end
53                 end
54         end
55
56         if server:find(":") then
57                 server = "[" .. server .. "]"
58         end
59
60         if self.target:sub(1,1) == ":" then
61                 target = protocol .. server .. target
62         end
63
64         local s, e = target:find("%TARGET%", 1, true)
65         if s then
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)
69         end
70
71         return 302, { Location = target }
72 end
73
74 --- Handle a POST request.
75 -- @class function
76 -- @param request Request object
77 -- @return status code, header table, response source
78 Redirect.handle_POST = Redirect.handle_GET
79
80 --- Handle a HEAD request.
81 -- @class function
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)
86         return stat, head
87 end