* Fixed last commit
[project/luci.git] / libs / httpd / luasrc / httpd / module.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14 module("luci.httpd.module", package.seeall)
15 require("luci.util")
16 require("ltn12")
17
18
19
20 -- Server handler implementation
21 Handler = luci.util.class()
22
23 -- Constructor
24 function Handler.__init__(self)
25 self.filters = {}
26 end
27
28
29 -- Adds a filter to the filter chain
30 function Handler.addfilter(self, filter)
31 table.insert(self.filters, filter)
32 end
33
34
35 -- Creates a failure reply
36 function Handler.failure(self, message)
37 response = {
38 status = 500,
39 headers = {
40 ["Content-Type"] = "text/plain"
41 }
42 }
43
44 sourceout = ltn12.source.string(message)
45
46 return response, sourceout
47 end
48
49
50 -- Processes a request
51 function Handler.process(self, request, sourcein, sinkout, sinkerr)
52 -- Process incoming filters
53 for i, f in ipairs(self.filters) do
54 local i = f:get("input")
55
56 if i then
57 sourcein = ltn12.source.chain(sourcein, i)
58 end
59
60 if f.request then
61 f:request(request)
62 end
63 end
64
65 -- Run the handler
66 local stat, response, sourceout = luci.util.copcall(
67 self.handle, self, request, sourcein, sinkerr
68 )
69
70 -- Check for any errors
71 if not stat then
72 response, sourceout = self:failure(response)
73 end
74
75 -- Check data
76 if not luci.util.instanceof(response, Response) then
77 response, sourceout = self:failure("Core error: Invalid module response!")
78 end
79
80 -- Process outgoing filters
81 for i, f in ipairs(self.filters) do
82 local o = f:get("output")
83
84 if o then
85 sourceout = ltn12.source.chain(sourceout, o)
86 end
87
88 if f.response then
89 f:response(response)
90 end
91 end
92
93 luci.http.push_response(request, status, response, sourceout, sinkout, sinkerr)
94 end
95
96
97
98 -- Server Filter implementation
99 Filter = luci.util.class()
100
101 function Filter.get(self, name)
102 return self[name] and function(...) return self[name](self, ...) end
103 end
104
105 -- Filters the incoming body stream
106 -- abstract function Filter.input(chunk)
107
108 -- Filters the outgoing body stream
109 -- abstract function Filter.output(chunk)
110
111 -- Filters the request object
112 -- abstract function Filter.request(request)
113
114 -- Filters the response object
115 -- abstract function Filter.response(response)
116
117
118
119 -- Handler Response
120 Response = luci.util.class()
121
122 function Response.__init__(self, status, headers)
123 self.status = tonumber(status) or 200
124 self.headers = (type(headers) == "table") and headers or {}
125 end
126
127 function Response.addheader(self, key, value)
128 self.headers[key] = value
129 end
130
131 function Response.setstatus(self, status)
132 self.status = status
133 end
134
135
136 -- Status codes
137 statusmsg = {
138 [200] = "OK",
139 [404] = "Not Found",
140 [500] = "Internal Server Error",
141 }