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)
31 -- Adds a filter to the filter chain
32 function Handler.addfilter(self, filter)
33 table.insert(self.filters, filter)
37 -- Creates a failure reply
38 function Handler.failure(self, code, message)
39 local response = Response(code, { ["Content-Type"] = "text/plain" })
40 local sourceout = ltn12.source.string(message)
42 return response, sourceout
45 -- Processes a request
46 function Handler.process(self, request, sourcein, sinkerr, ...)
47 local stat, response, sourceout
49 -- Process incoming filters
50 for i, f in ipairs(self.filters) do
51 local i = f:get("input")
54 sourcein = ltn12.source.chain(sourcein, i)
62 -- Detect request Method
63 local hname = "handle_" .. request.request_method
66 stat, response, sourceout = luci.util.copcall(
67 self[hname], self, request, sourcein, sinkerr, ...
70 -- Check for any errors
72 response, sourceout = self:failure(500, response)
75 response, sourceout = self:failure(405, luci.http.protocol.statusmsg[405])
79 if not luci.util.instanceof(response, Response) then
80 response, sourceout = self:failure(500, "Core error: Invalid module response!")
83 -- Process outgoing filters
84 for i, f in ipairs(self.filters) do
85 local o = f:get("output")
88 sourceout = ltn12.source.chain(sourceout, o)
96 return response, sourceout
101 -- Server Filter implementation
102 Filter = luci.util.class()
104 function Filter.get(self, name)
105 return self[name] and function(...) return self[name](self, ...) end
108 -- Filters the incoming body stream
109 -- abstract function Filter.input(chunk)
111 -- Filters the outgoing body stream
112 -- abstract function Filter.output(chunk)
114 -- Filters the request object
115 -- abstract function Filter.request(request)
117 -- Filters the response object
118 -- abstract function Filter.response(response)
123 Response = luci.util.class()
125 function Response.__init__(self, status, headers)
126 self.status = tonumber(status) or 200
127 self.headers = (type(headers) == "table") and headers or {}
130 function Response.addheader(self, key, value)
131 self.headers[key] = value
134 function Response.setstatus(self, status)