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)
16 require("luci.http.protocol")
21 -- Server handler implementation
22 Handler = luci.util.class()
25 function Handler.__init__(self)
30 -- Adds a filter to the filter chain
31 function Handler.addfilter(self, filter)
32 table.insert(self.filters, filter)
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)
41 return response, sourceout
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")
52 sourcein = ltn12.source.chain(sourcein, i)
61 local stat, response, sourceout = luci.util.copcall(
62 self.handle, self, request, sourcein, sinkerr
65 -- Check for any errors
67 response, sourceout = self:failure(500, response)
71 if not luci.util.instanceof(response, Response) then
72 response, sourceout = self:failure(500, "Core error: Invalid module response!")
75 -- Process outgoing filters
76 for i, f in ipairs(self.filters) do
77 local o = f:get("output")
80 sourceout = ltn12.source.chain(sourceout, o)
88 luci.http.protocol.push_response(request, response, sourceout, sinkout, sinkerr)
93 -- Server Filter implementation
94 Filter = luci.util.class()
96 function Filter.get(self, name)
97 return self[name] and function(...) return self[name](self, ...) end
100 -- Filters the incoming body stream
101 -- abstract function Filter.input(chunk)
103 -- Filters the outgoing body stream
104 -- abstract function Filter.output(chunk)
106 -- Filters the request object
107 -- abstract function Filter.request(request)
109 -- Filters the response object
110 -- abstract function Filter.response(response)
115 Response = luci.util.class()
117 function Response.__init__(self, status, headers)
118 self.status = tonumber(status) or 200
119 self.headers = (type(headers) == "table") and headers or {}
122 function Response.addheader(self, key, value)
123 self.headers[key] = value
126 function Response.setstatus(self, status)