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, message)
41 ["Content-Type"] = "text/plain"
45 sourceout = ltn12.source.string(message)
47 return response, sourceout
51 -- Processes a request
52 function Handler.process(self, request, sourcein, sinkout, sinkerr)
53 -- Process incoming filters
54 for i, f in ipairs(self.filters) do
55 local i = f:get("input")
58 sourcein = ltn12.source.chain(sourcein, i)
67 local stat, response, sourceout = luci.util.copcall(
68 self.handle, self, request, sourcein, sinkerr
71 -- Check for any errors
73 response, sourceout = self:failure(response)
77 if not luci.util.instanceof(response, Response) then
78 response, sourceout = self:failure("Core error: Invalid module response!")
81 -- Process outgoing filters
82 for i, f in ipairs(self.filters) do
83 local o = f:get("output")
86 sourceout = ltn12.source.chain(sourceout, o)
94 luci.http.protocol.push_response(request, response, sourceout, sinkout, sinkerr)
99 -- Server Filter implementation
100 Filter = luci.util.class()
102 function Filter.get(self, name)
103 return self[name] and function(...) return self[name](self, ...) end
106 -- Filters the incoming body stream
107 -- abstract function Filter.input(chunk)
109 -- Filters the outgoing body stream
110 -- abstract function Filter.output(chunk)
112 -- Filters the request object
113 -- abstract function Filter.request(request)
115 -- Filters the response object
116 -- abstract function Filter.response(response)
121 Response = luci.util.class()
123 function Response.__init__(self, status, headers)
124 self.status = tonumber(status) or 200
125 self.headers = (type(headers) == "table") and headers or {}
128 function Response.addheader(self, key, value)
129 self.headers[key] = value
132 function Response.setstatus(self, status)
141 [500] = "Internal Server Error",