summaryrefslogtreecommitdiffhomepage
path: root/libs/httpd/luasrc/httpd.lua
blob: 773d3c873dfd821dae28655a246eba7d8d83c5c1 (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
76
77
78
79
80
81
82
83
84
--[[
LuCI - HTTPD
]]--
module("luci.httpd", package.seeall)
require("luci.copas")
require("luci.http.protocol")
require("luci.sys")



function run(config)
	-- TODO: process config
	local server = socket.bind("0.0.0.0", 8080)
	copas.addserver(server, spawnworker)
	
	while true do
		copas.step()
	end
end


function spawnworker(socket)
	socket = copas.wrap(socket)
	local request = luci.http.protocol.parse_message_header(socket)
	request.input = socket -- TODO: replace with streamreader
	request.error = io.stderr
	
	
	local output = socket -- TODO: replace with streamwriter
	
	-- TODO: detect matching handler
	local h = luci.httpd.FileHandler.SimpleHandler(luci.sys.libpath() .. "/httpd/httest")
	h:process(request, output)
end


Response = luci.util.class()
function Response.__init__(self, sourceout, headers, status)
	self.sourceout = sourceout or function() end
	self.headers   = headers or {}
	self.status    = status or 200
end

function Response.addheader(self, key, value)
	self.headers[key] = value
end

function Response.setstatus(self, status)
	self.status = status
end

function Response.setsource(self, source)
	self.sourceout = source
end


Handler = luci.util.class()
function Handler.__init__(self)
	self.filter = {}
end

function Handler.addfilter(self, filter)
	table.insert(self.filter, filter)
end

function Handler.process(self, request, output)
	-- TODO: Process input filters

	local response = self:handle(request)
	
	-- TODO: Process output filters
	
	output:send("HTTP/1.0 " .. response.status .. " BLA\r\n")
	for k, v in pairs(response.headers) do
		output:send(k .. ": " .. v .. "\r\n")
	end
	
	output:send("\r\n")

	for chunk in response.sourceout do
		output:send(chunk)
	end
end