c321856a8a559bf59c8ed5ed0dd633f1da6f322d
[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("luci.http.protocol")
17 require("ltn12")
18
19
20
21 -- Server handler implementation
22 Handler = luci.util.class()
23
24 -- Constructor
25 function Handler.__init__(self)
26         self.filters = {}
27 end
28
29
30 -- Adds a filter to the filter chain
31 function Handler.addfilter(self, filter)
32         table.insert(self.filters, filter)
33 end
34
35
36 -- Creates a failure reply
37 function Handler.failure(self, code, message)
38         local response = Response(code, { ["Content-Type"] = "text/plain" })
39         local sourceout = ltn12.source.string(message)
40         
41         return response, sourceout 
42 end
43
44
45 -- Processes a request
46 function Handler.process(self, request, sourcein, sinkout, sinkerr)
47         -- Process incoming filters
48         for i, f in ipairs(self.filters) do
49                 local i = f:get("input")
50                 
51                 if i then
52                         sourcein = ltn12.source.chain(sourcein, i) 
53                 end
54                 
55                 if f.request then
56                         f:request(request)
57                 end
58         end
59         
60         -- Run the handler
61         local stat, response, sourceout = luci.util.copcall(
62                 self.handle, self, request, sourcein, sinkerr
63         )
64         
65         -- Check for any errors
66         if not stat then
67                 response, sourceout = self:failure(500, response)
68         end
69         
70         -- Check data
71         if not luci.util.instanceof(response, Response) then
72                 response, sourceout = self:failure(500, "Core error: Invalid module response!")
73         end
74         
75         -- Process outgoing filters
76         for i, f in ipairs(self.filters) do
77                 local o = f:get("output")
78                 
79                 if o then
80                         sourceout = ltn12.source.chain(sourceout, o) 
81                 end
82                 
83                 if f.response then
84                         f:response(response)
85                 end
86         end
87         
88         luci.http.protocol.push_response(request, response, sourceout, sinkout, sinkerr) 
89 end
90
91
92
93 -- Server Filter implementation
94 Filter = luci.util.class()
95
96 function Filter.get(self, name)
97         return self[name] and function(...) return self[name](self, ...) end
98 end
99
100 -- Filters the incoming body stream
101 -- abstract function Filter.input(chunk)
102
103 -- Filters the outgoing body stream
104 -- abstract function Filter.output(chunk)
105
106 -- Filters the request object
107 -- abstract function Filter.request(request)
108
109 -- Filters the response object
110 -- abstract function Filter.response(response)
111
112
113
114 -- Handler Response 
115 Response = luci.util.class()
116
117 function Response.__init__(self, status, headers)
118         self.status = tonumber(status) or 200
119         self.headers = (type(headers) == "table") and headers or {}
120 end
121
122 function Response.addheader(self, key, value)
123         self.headers[key] = value
124 end
125
126 function Response.setstatus(self, status)
127         self.status = status
128 end