a5cdce53417a239709d9a5f27a7bb77d073e130b
[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
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         else
55                 server = request.env.SERVER_ADDR
56         end
57         if server:find(":") then
58                 server = "[" .. server .. "]"
59         end
60
61         if self.target:sub(1,1) == ":" then
62                 target = protocol .. server .. target
63         end
64
65         local s, e = target:find("%TARGET%", 1, true)
66         if s then
67                 local req = protocol .. (request.env.HTTP_HOST or server)
68                         .. request.env.REQUEST_URI 
69                 target = target:sub(1, s-1) .. req .. target:sub(e+1)
70         end
71
72         return 302, { Location = target }
73 end
74
75 --- Handle a POST request.
76 -- @class function
77 -- @param request Request object
78 -- @return status code, header table, response source
79 Redirect.handle_POST = Redirect.handle_GET
80
81 --- Handle a HEAD request.
82 -- @class function
83 -- @param request Request object
84 -- @return status code, header table, response source
85 function Redirect.handle_HEAD(self, request)
86         local stat, head = self:handle_GET(request)
87         return stat, head
88 end