* luci/libs: add support for chunked transfer decoding in http.protocol
[project/luci.git] / libs / httpd / luasrc / httpd / module.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14 module("luci.httpd.module", package.seeall)
15 require("luci.util")
16 require("luci.http.protocol")
17 require("ltn12")
18
19
20
21 -- Server handler implementation
22 Handler = luci.util.class()
23
24 -- Constructor
25 function Handler.__init__(self)
26 self.filters = {}
27 end
28
29
30 -- Adds a filter to the filter chain
31 function Handler.addfilter(self, filter)
32 table.insert(self.filters, filter)
33 end
34
35
36 -- Creates a failure reply
37 function Handler.failure(self, message)
38 response = {
39 status = 500,
40 headers = {
41 ["Content-Type"] = "text/plain"
42 }
43 }
44
45 sourceout = ltn12.source.string(message)
46
47 return response, sourceout
48 end
49
50
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")
56
57 if i then
58 sourcein = ltn12.source.chain(sourcein, i)
59 end
60
61 if f.request then
62 f:request(request)
63 end
64 end
65
66 -- Run the handler
67 local stat, response, sourceout = luci.util.copcall(
68 self.handle, self, request, sourcein, sinkerr
69 )
70
71 -- Check for any errors
72 if not stat then
73 response, sourceout = self:failure(response)
74 end
75
76 -- Check data
77 if not luci.util.instanceof(response, Response) then
78 response, sourceout = self:failure("Core error: Invalid module response!")
79 end
80
81 -- Process outgoing filters
82 for i, f in ipairs(self.filters) do
83 local o = f:get("output")
84
85 if o then
86 sourceout = ltn12.source.chain(sourceout, o)
87 end
88
89 if f.response then
90 f:response(response)
91 end
92 end
93
94 luci.http.protocol.push_response(request, response, sourceout, sinkout, sinkerr)
95 end
96
97
98
99 -- Server Filter implementation
100 Filter = luci.util.class()
101
102 function Filter.get(self, name)
103 return self[name] and function(...) return self[name](self, ...) end
104 end
105
106 -- Filters the incoming body stream
107 -- abstract function Filter.input(chunk)
108
109 -- Filters the outgoing body stream
110 -- abstract function Filter.output(chunk)
111
112 -- Filters the request object
113 -- abstract function Filter.request(request)
114
115 -- Filters the response object
116 -- abstract function Filter.response(response)
117
118
119
120 -- Handler Response
121 Response = luci.util.class()
122
123 function Response.__init__(self, status, headers)
124 self.status = tonumber(status) or 200
125 self.headers = (type(headers) == "table") and headers or {}
126 end
127
128 function Response.addheader(self, key, value)
129 self.headers[key] = value
130 end
131
132 function Response.setstatus(self, status)
133 self.status = status
134 end
135
136
137 -- Status codes
138 statusmsg = {
139 [200] = "OK",
140 [404] = "Not Found",
141 [500] = "Internal Server Error",
142 }