4 -- Written by Timm S. Mueller <tmueller@neoscientists.org>
6 -- Lua FastCGI protocol implementation - specifically written for
7 -- the "external" server configurations (via IP socket)
9 -- Methods that must be overwritten by the user:
10 -- fcgi:have_params(req, params) -- all parameters transferred
12 -- Methods that can be overwritten by the user:
13 -- fcgi:abort(req) -- request aborted by webserver
14 -- fcgi:update_stream(req, type, stream) -- stream created/updated
15 -- fcgi:have_stream(req, type, stream) -- stream completed
18 local Class = require "tek.class"
19 local socket = require "socket"
20 local bit = require "bit"
22 local insert, remove, concat, pairs, ipairs, error, assert =
23 table.insert, table.remove, table.concat, pairs, ipairs, error, assert
24 local tonumber, setmetatable, unpack, type =
25 tonumber, setmetatable, unpack, type
26 local char = string.char
29 module("tek.class.fastcgi", Class)
30 _VERSION = "FastCGI 0.3"
32 -------------------------------------------------------------------------------
34 -------------------------------------------------------------------------------
36 local FIFO = Class:newClass()
38 function FIFO.new(class, self)
39 self = Class.new(class, self or { })
40 self.buf = self.buf or { }
44 function FIFO:write(s)
47 elseif self.buf[#self.buf] ~= -1 then
48 insert(self.buf, -1) -- EOF
52 function FIFO:readn(len)
55 p = remove(self.buf, 1)
60 insert(self.buf, -1) -- push back EOF
61 break -- end of stream, EOF in next turn
64 if l > len then -- break buffer fragment in two
65 insert(t, p:sub(1, len))
66 insert(self.buf, 1, p:sub(len + 1))
76 local last = remove(self.buf)
77 if last ~= -1 then -- not EOF
78 insert(self.buf, last) -- push back
81 local s = concat(self.buf)
86 function FIFO:read(...)
87 if self.buf[1] == -1 then
91 for _, what in ipairs(arg) do
94 elseif type(what) == "number" then
95 s = self:readn(tonumber(what))
97 error("unknwon format")
104 -- FCGI encoded lengths and key/value pairs:
106 function FIFO:readlen()
107 local l = self:read(1)
108 if not l then return end
113 local t = self:read(3)
114 if not t then return end
115 return (l % 128) * 16777216 + t:byte(1) * 65536 +
116 t:byte(2) * 256 + t:byte(3)
119 function FIFO:readkeyvals()
121 local l1, l2, key, val
124 if not l1 then break end
126 if not l2 then break end
134 -------------------------------------------------------------------------------
136 -------------------------------------------------------------------------------
138 FCGI_BEGIN_REQUEST = 1
139 FCGI_ABORT_REQUEST = 2
147 FCGI_GET_VALUES_RESULT = 10
148 FCGI_UNKNOWN_TYPE = 11
150 local FCGI_RESPONDER = 1
151 local FCGI_REQUEST_COMPLETE = 0
152 local FCGI_UNKNOWN_ROLE = 3
154 local function encodelen(buf, l)
156 insert(buf, char(bit.rshift(bit.band(l, 0x7f000000), 24) + 128))
157 insert(buf, char(bit.rshift(bit.band(l, 0x00ff0000), 16)))
158 insert(buf, char(bit.rshift(bit.band(l, 0x0000ff00), 8)))
160 insert(buf, char(bit.band(l, 0xff)))
163 local function encodekeyvals(t)
165 for key, val in pairs(t) do
166 encodelen(buf, key:len())
167 encodelen(buf, val:len())
174 -- local FCGI = Class:newClass(_M)
177 function FCGI.new(class, self)
178 self = Class.new(class, self or { })
183 function FCGI:readrecord()
184 local t, err = self.socket:receive(8)
185 if not t then return end
186 if err then error(err) end
190 id = t:byte(3) * 256 + t:byte(4),
191 len = t:byte(5) * 256 + t:byte(6)
196 r.content, err = self.socket:receive(r.len)
197 if not r.content then return end
198 if err then error(err) end
200 local pad = t:byte(7)
202 t, err = self.socket:receive(pad)
203 if not t then return end
204 if err then error(err) end
209 function FCGI:write(type, id, s)
210 local totlen = s:len()
213 local len = min(totlen, 65535)
217 char(bit.rshift(id, 8)), -- id1
218 char(id % 256), -- id0
219 char(bit.rshift(len, 8)), -- len1
220 char(len % 256), -- len0
223 s:sub(totpos, totpos + len - 1) -- content
225 totpos = totpos + len
226 totlen = totlen - len
229 local pos, res, err = 1
231 res, err = self.socket:send(buf, pos)
241 function FCGI:write_stdout(id, s)
242 return self:write(FCGI_STDOUT, id, s)
245 function FCGI:collectstream(r)
246 local req = self.requests[r.id]
247 local s = req.streams[r.type]
250 req.streams[r.type] = s
253 s:write() -- append EOF
254 return s, true -- finished
260 function FCGI:endrequest(req, protstatus, appstatus)
261 protstatus = protstatus or req.protstatus or FCGI_REQUEST_COMPLETE
262 appstatus = appstatus or req.appstatus or 0
263 self.requests[req.id] = nil -- delete request
264 return self:write(FCGI_END_REQUEST, req.id, concat {
265 char(bit.rshift(bit.band(appstatus, 0x7f000000), 24)),
266 char(bit.rshift(bit.band(appstatus, 0x00ff0000), 16)),
267 char(bit.rshift(bit.band(appstatus, 0x0000ff00), 8)),
268 char(bit.band(appstatus, 0xff)),
270 char(0), char(0), char(0)
274 function FCGI:newrequest(id, role, flags)
275 assert(not self.requests[id])
276 local req = { id = id, role = role, flags = flags, streams = { } }
277 self.requests[id] = req
281 function FCGI:processrecord(r)
284 local req = self.requests[r.id]
286 if r.type == FCGI_BEGIN_REQUEST then
288 local role = c:byte(1) * 256 + c:byte(2)
289 if role == FCGI_RESPONDER then
291 local flags = c:byte(3)
292 req = self:newrequest(r.id, role, flags)
293 return true -- continue
296 return self:endrequest(req, FCGI_UNKNOWN_ROLE)
299 if not req then -- request already closed
300 return true -- continue
303 if r.type == FCGI_ABORT_REQUEST then
304 return self:abortrequest(req)
306 elseif r.type == FCGI_GET_VALUES then
307 local s, fin = self:collectstream(r)
310 for k in pairs(s:readkeyvals()) do
311 if k == "FCGI_MAX_CONNS" then
312 res.FCGI_MAX_CONNS = "16"
313 elseif k == "FCGI_MAX_REQS" then
314 res.FCGI_MAX_CONNS = "32"
315 elseif k == "FCGI_MAX_REQS" then
316 res.FCGI_MAX_CONNS = "1"
319 res = encodekeyvals(res)
320 return self:write(FCGI_GET_VALUES_RESULT, 0, res)
323 elseif r.type == FCGI_PARAMS then
324 local s, fin = self:collectstream(r)
326 req.params = s:readkeyvals()
327 return self:have_params(req, req.params)
330 elseif r.type == self.FCGI_STDIN or r.type == self.FCGI_DATA then
331 local s, fin = self:collectstream(r)
333 if self.have_stream then
334 return self:have_stream(req, r.type, s)
337 if self.update_stream then
338 return self:update_stream(req, r.type, s)
344 local buf = char(r.type) .. char(0):rep(7)
345 return self:write(FCGI_UNKNOWN_TYPE, 0, buf)
348 return true -- continue
351 function FCGI:serve(socket)
356 local r = self:readrecord()
360 if not self:processrecord(r) then
370 function FCGI:abortrequest(req)
371 -- request aborted by webserver, confirm:
372 return self:endrequest(req)