2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
14 module("luci.httpd.module", package.seeall)
20 -- Server handler implementation
21 Handler = luci.util.class()
24 function Handler.__init__(self)
29 -- Adds a filter to the filter chain
30 function Handler.addfilter(self, filter)
31 table.insert(self.filters, filter)
35 -- Creates a failure reply
36 function Handler.failure(self, message)
40 ["Content-Type"] = "text/plain"
44 sourceout = ltn12.source.string(message)
46 return response, sourceout
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")
57 sourcein = ltn12.source.chain(sourcein, i)
66 local stat, response, sourceout = luci.util.copcall(
67 self.handle, self, request, sourcein, sinkerr
70 -- Check for any errors
72 response, sourceout = self:failure(response)
76 if not luci.util.instanceof(response, Response) then
77 response, sourceout = self:failure("Core error: Invalid module response!")
80 -- Process outgoing filters
81 for i, f in ipairs(self.filters) do
82 local o = f:get("output")
85 sourceout = ltn12.source.chain(sourceout, o)
93 luci.http.protocol.push_response(request,response, sourceout, sinkout, sinkerr)
98 -- Server Filter implementation
99 Filter = luci.util.class()
101 function Filter.get(self, name)
102 return self[name] and function(...) return self[name](self, ...) end
105 -- Filters the incoming body stream
106 -- abstract function Filter.input(chunk)
108 -- Filters the outgoing body stream
109 -- abstract function Filter.output(chunk)
111 -- Filters the request object
112 -- abstract function Filter.request(request)
114 -- Filters the response object
115 -- abstract function Filter.response(response)
120 Response = luci.util.class()
122 function Response.__init__(self, status, headers)
123 self.status = tonumber(status) or 200
124 self.headers = (type(headers) == "table") and headers or {}
127 function Response.addheader(self, key, value)
128 self.headers[key] = value
131 function Response.setstatus(self, status)
140 [500] = "Internal Server Error",