]]--
module("luci.httpd.module", package.seeall)
require("luci.util")
+require("luci.http.protocol")
require("ltn12")
-- Constructor
function Handler.__init__(self)
self.filters = {}
+ self.handler = {}
end
-- Creates a failure reply
-function Handler.failure(self, message)
- response = {
- status = 500,
- headers = {
- ["Content-Type"] = "text/plain"
- }
- }
-
- sourceout = ltn12.source.string(message)
+function Handler.failure(self, code, message)
+ local response = Response(code, { ["Content-Type"] = "text/plain" })
+ local sourceout = ltn12.source.string(message)
return response, sourceout
end
-
-- Processes a request
-function Handler.process(self, request, sourcein, sinkout, sinkerr)
+function Handler.process(self, request, sourcein, sinkerr, ...)
+ local stat, response, sourceout
+
-- Process incoming filters
for i, f in ipairs(self.filters) do
local i = f:get("input")
end
end
- -- Run the handler
- local stat, response, sourceout = luci.util.copcall(
- self.handle, self, request, sourcein, sinkerr
- )
+ -- Detect request Method
+ local hname = "handle_" .. request.request_method
+ if self[hname] then
+ -- Run the handler
+ stat, response, sourceout = luci.util.copcall(
+ self[hname], self, request, sourcein, sinkerr, ...
+ )
- -- Check for any errors
- if not stat then
- response, sourceout = self:failure(response)
+ -- Check for any errors
+ if not stat then
+ response, sourceout = self:failure(500, response)
+ end
+ else
+ response, sourceout = self:failure(405, luci.http.protocol.statusmsg[405])
end
-- Check data
if not luci.util.instanceof(response, Response) then
- response, sourceout = self:failure("Core error: Invalid module response!")
+ response, sourceout = self:failure(500, "Core error: Invalid module response!")
end
-- Process outgoing filters
end
end
-
- -- Print status and headers
- sinkout("HTTP/1.1 " .. response.status .. " " .. statusmsg[response.status] .. "\r\n")
- for k, v in pairs(response.headers) do
- sinkout(k .. ": " .. v .. "\r\n")
- end
-
- -- End of Headers
- sinkout("\r\n")
-
- -- Pump content
- if sourceout then
- ltn12.pump.all(sourceout, sinkout)
- end
+ return response, sourceout
end
function Response.setstatus(self, status)
self.status = status
-end
-
-
--- Status codes
-statusmsg = {
- [200] = "OK",
- [404] = "Not Found",
- [500] = "Internal Server Error",
-}
\ No newline at end of file
+end
\ No newline at end of file