summaryrefslogtreecommitdiff
path: root/contrib/core__xsendfile.lua
blob: fc4c62b6201df5c5cec72cffb9e6310c54320190 (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
73
74
75

local filename, args = ...

local docroot = args

-- create new class XFilterDrop
local XFilterDrop = { }
XFilterDrop.__index = XFilterDrop

-- "classmethod" to create new instance
function XFilterDrop:new(vr)
--	vr:debug("New XSendfile instance")
	local o = { }
	setmetatable(o, self)
	return o
end

-- normal method to handle content
function XFilterDrop:handle(vr, outq, inq)
	-- drop input, close it
	if nil ~= inq then
		inq.is_closed = true
		inq:skip_all()
	end
	return lighty.HANDLER_GO_ON
end

-- create a new filter which drops all input and adds it to the vrequest
-- returns the filter object so you can insert your own content in f.out (it is already closed)
local function add_drop_filter(vr)
	local f = vr:add_filter_out(XFilterDrop:new())
	local inq = f['in']
	if nil ~= inq then
		inq.is_closed = true
		inq:skip_all()
	end
	f.out.is_closed = true
	return f
end

local function handle_x_sendfile(vr)
-- 	vr:debug("handle x-sendfile")
	-- wait for response
	if not vr.has_response then
		if vr.is_handled then
-- 			vr:debug("x-sendfile: waiting for response headers")
			return lighty.HANDLER_WAIT_FOR_EVENT
		else
-- 			vr:debug("No response handler yet, cannot handle X-Sendfile")
			return lighty.HANDLER_GO_ON
		end
	end
-- 	vr:debug("handle x-sendfile: headers available")
	-- add filter if x-sendfile header is not empty
	local xs = vr.resp.headers["X-Sendfile"]
	if xs and xs ~= "" then
		xs = lighty.path_simplify(xs)
		if docroot and xs:sub(docroot.len()) ~= docroot then
			vr:error("x-sendfile: File '".. xs .. "'not in required docroot '" .. docroot .. "'")
			return lighty.HANDLER_GO_ON
		end

		-- make sure to drop all other content from the backend
		local f = add_drop_filter(vr)

		vr.resp.headers["X-Sendfile"] = nil -- remove header from response

		-- Add checks for the pathname here

		vr:debug("XSendfile:handle: pushing file '" .. xs .. "' as content")
		f.out:add_file(xs)
	end
end

actions = handle_x_sendfile