* Use CRLF instead of LF in HTTP headers
[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("ltn12")
17
18
19
20 -- Server handler implementation
21 Handler = luci.util.class()
22
23 -- Constructor
24 function Handler.__init__(self)
25 self.filters = {}
26 end
27
28
29 -- Adds a filter to the filter chain
30 function Handler.addfilter(self, filter)
31 table.insert(self.filters, filter)
32 end
33
34
35 -- Creates a failure reply
36 function Handler.failure(self, message)
37 response = {
38 status = 500,
39 headers = {
40 ["Content-Type"] = "text/plain"
41 }
42 }
43
44 sourceout = ltn12.source.string(message)
45
46 return response, sourceout
47 end
48
49
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")
55
56 if i then
57 sourcein = ltn12.source.chain(sourcein, i)
58 end
59
60 if f.request then
61 f:request(request)
62 end
63 end
64
65 -- Run the handler
66 local stat, response, sourceout = luci.util.copcall(
67 self.handle, self, request, sourcein, sinkerr
68 )
69
70 -- Check for any errors
71 if not stat then
72 response, sourceout = self:failure(response)
73 end
74
75 -- Check data
76 if not luci.util.instanceof(response, Response) then
77 response, sourceout = self:failure("Core error: Invalid module response!")
78 end
79
80 -- Process outgoing filters
81 for i, f in ipairs(self.filters) do
82 local o = f:get("output")
83
84 if o then
85 sourceout = ltn12.source.chain(sourceout, o)
86 end
87
88 if f.response then
89 f:response(response)
90 end
91 end
92
93
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")
98 end
99
100 -- End of Headers
101 sinkout("\r\n")
102
103 -- Pump content
104 if sourceout then
105 ltn12.pump.all(sourceout, sinkout)
106 end
107 end
108
109
110
111 -- Server Filter implementation
112 Filter = luci.util.class()
113
114 function Filter.get(self, name)
115 return self[name] and function(...) return self[name](self, ...) end
116 end
117
118 -- Filters the incoming body stream
119 -- abstract function Filter.input(chunk)
120
121 -- Filters the outgoing body stream
122 -- abstract function Filter.output(chunk)
123
124 -- Filters the request object
125 -- abstract function Filter.request(request)
126
127 -- Filters the response object
128 -- abstract function Filter.response(response)
129
130
131
132 -- Handler Response
133 Response = luci.util.class()
134
135 function Response.__init__(self, status, headers)
136 self.status = tonumber(status) or 200
137 self.headers = (type(headers) == "table") and headers or {}
138 end
139
140 function Response.addheader(self, key, value)
141 self.headers[key] = value
142 end
143
144 function Response.setstatus(self, status)
145 self.status = status
146 end
147
148
149 -- Status codes
150 statusmsg = {
151 [200] = "OK",
152 [404] = "Not Found",
153 [500] = "Internal Server Error",
154 }