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)
94 -- Print status and headers
95 sinkout("HTTP/1.1 " .. response.status .. " " .. statusmsg[response.status] .. "\r\n")
96 for k, v in pairs(response.headers) do
97 sinkout(k .. ": " .. v .. "\r\n")
105 ltn12.pump.all(sourceout, sinkout)
111 -- Server Filter implementation
112 Filter = luci.util.class()
114 function Filter.get(self, name)
115 return self[name] and function(...) return self[name](self, ...) end
118 -- Filters the incoming body stream
119 -- abstract function Filter.input(chunk)
121 -- Filters the outgoing body stream
122 -- abstract function Filter.output(chunk)
124 -- Filters the request object
125 -- abstract function Filter.request(request)
127 -- Filters the response object
128 -- abstract function Filter.response(response)
133 Response = luci.util.class()
135 function Response.__init__(self, status, headers)
136 self.status = tonumber(status) or 200
137 self.headers = (type(headers) == "table") and headers or {}
140 function Response.addheader(self, key, value)
141 self.headers[key] = value
144 function Response.setstatus(self, status)
153 [500] = "Internal Server Error",