summaryrefslogtreecommitdiffhomepage
path: root/libs/lucid-http/luasrc/lucid/http/handler/catchall.lua
blob: 30af84ba2aa291e6f02f79b421e13c29db3b2995 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
--[[
LuCId HTTP-Slave
(c) 2009 Steven Barth <steven@midlink.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

$Id$
]]--

local srv = require "luci.lucid.http.server"
local proto = require "luci.http.protocol"
local util = require "luci.util"

--- Catchall Handler
-- @cstyle instance
module "luci.lucid.http.handler.catchall"

--- Create a Redirect handler.
-- @param name Name
-- @param target Redirect Target
-- @class function
-- @return Redirect handler object
Redirect = util.class(srv.Handler)

function Redirect.__init__(self, name, target)
	srv.Handler.__init__(self, name)
	self.target = target
end

--- Handle a GET request.
-- @param request Request object
-- @return status code, header table, response source
function Redirect.handle_GET(self, request)
	local target = self.target
	local protocol = request.env.HTTPS and "https://" or "http://"
	local server = request.env.SERVER_ADDR
	if server:find(":") then
		server = "[" .. server .. "]"
	end

	if self.target:sub(1,1) == ":" then
		target = protocol .. server .. target
	end

	local s, e = target:find("%TARGET%", 1, true)
	if s then
		local req = protocol .. (request.env.HTTP_HOST or server)
			.. request.env.REQUEST_URI 
		target = target:sub(1, s-1) .. req .. target:sub(e+1)
	end

	return 302, { Location = target }
end

--- Handle a POST request.
-- @class function
-- @param request Request object
-- @return status code, header table, response source
Redirect.handle_POST = Redirect.handle_GET

--- Handle a HEAD request.
-- @class function
-- @param request Request object
-- @return status code, header table, response source
function Redirect.handle_HEAD(self, request)
	local stat, head = self:handle_GET(request)
	return stat, head
end