0523751bc1c5a152ad48f84c360382097489a529
[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
17 module "luci.lucid.http.handler.catchall"
18
19 Redirect = util.class(srv.Handler)
20
21 function Redirect.__init__(self, name, target)
22 srv.Handler.__init__(self, name)
23 self.target = target
24 end
25
26 function Redirect.handle_GET(self, request)
27 local target = self.target
28 local protocol = request.env.HTTPS and "https://" or "http://"
29 local server = request.env.SERVER_ADDR
30 if server:find(":") then
31 server = "[" .. server .. "]"
32 end
33
34 if self.target:sub(1,1) == ":" then
35 target = protocol .. server .. target
36 end
37
38 local s, e = target:find("%TARGET%", 1, true)
39 if s then
40 local req = protocol .. (request.env.HTTP_HOST or server)
41 .. request.env.REQUEST_URI
42 target = target:sub(1, s-1) .. req .. target:sub(e+1)
43 end
44
45 return 302, { Location = target }
46 end
47
48 Redirect.handle_POST = Redirect.handle_GET
49
50 function Redirect.handle_HEAD(self, request)
51 local stat, head = self:handle_GET(request)
52 return stat, head
53 end