cgi-bin/tek/class/fastcgi.lua
changeset 201 d52b05a9fe9c
child 212 a1aad6415384
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cgi-bin/tek/class/fastcgi.lua	Fri Nov 23 01:46:43 2007 +0100
     1.3 @@ -0,0 +1,373 @@
     1.4 +
     1.5 +--
     1.6 +--	tek.class.fastcgi
     1.7 +--	Written by Timm S. Mueller <tmueller@neoscientists.org>
     1.8 +--
     1.9 +--	Lua FastCGI protocol implementation - specifically written for
    1.10 +--	the "external" server configurations (via IP socket)
    1.11 +--
    1.12 +--	Methods that must be overwritten by the user:
    1.13 +--		fcgi:have_params(req, params) -- all parameters transferred
    1.14 +--
    1.15 +--	Methods that can be overwritten by the user:
    1.16 +--		fcgi:abort(req) -- request aborted by webserver
    1.17 +--		fcgi:update_stream(req, type, stream) -- stream created/updated
    1.18 +--		fcgi:have_stream(req, type, stream) -- stream completed
    1.19 +--
    1.20 +
    1.21 +local Class = require "tek.class"
    1.22 +local socket = require "socket"
    1.23 +local bit = require "bit"
    1.24 +
    1.25 +local insert, remove, concat, pairs, ipairs, error, assert =
    1.26 +	table.insert, table.remove, table.concat, pairs, ipairs, error, assert
    1.27 +local tonumber, setmetatable, unpack, type =
    1.28 +	tonumber, setmetatable, unpack, type
    1.29 +local char = string.char
    1.30 +local math = math
    1.31 +
    1.32 +module("tek.class.fastcgi", Class)
    1.33 +_VERSION = "FastCGI 0.2"
    1.34 +
    1.35 +-------------------------------------------------------------------------------
    1.36 +-- local FIFO class:
    1.37 +-------------------------------------------------------------------------------
    1.38 +
    1.39 +local FIFO = Class:newClass()
    1.40 +
    1.41 +function FIFO.new(class, self)
    1.42 +	self = Class.new(class, self or { })
    1.43 +	self.buf = self.buf or { }
    1.44 +	return self
    1.45 +end
    1.46 +
    1.47 +function FIFO:write(s)
    1.48 +	if s and s ~= -1 then
    1.49 +		insert(self.buf, s)
    1.50 +	elseif self.buf[#self.buf] ~= -1 then
    1.51 +		insert(self.buf, -1) -- EOF
    1.52 +	end
    1.53 +end
    1.54 +
    1.55 +function FIFO:readn(len)
    1.56 +	local t, p, l = { }
    1.57 +	while len > 0 do
    1.58 +		p = remove(self.buf, 1)
    1.59 +		if not p then
    1.60 +			break -- no more data
    1.61 +		end
    1.62 +		if p == -1 then
    1.63 +			insert(self.buf, -1) -- push back EOF
    1.64 +			break -- end of stream, EOF in next turn
    1.65 +		end
    1.66 +		l = p:len()
    1.67 +		if l > len then -- break buffer fragment in two
    1.68 +			insert(t, p:sub(1, len))
    1.69 +			insert(self.buf, 1, p:sub(len + 1))
    1.70 +			break
    1.71 +		end
    1.72 +		insert(t, p)
    1.73 +		len = len - l
    1.74 +	end
    1.75 +	return concat(t)
    1.76 +end
    1.77 +
    1.78 +function FIFO:reada()
    1.79 +	local last = remove(self.buf)
    1.80 +	if last ~= -1 then -- not EOF
    1.81 +		insert(self.buf, last) -- push back
    1.82 +		last = nil
    1.83 +	end
    1.84 +	local s = concat(self.buf)
    1.85 +	self.buf = { last }
    1.86 +	return s
    1.87 +end
    1.88 +
    1.89 +function FIFO:read(...)
    1.90 +	if self.buf[1] == -1 then
    1.91 +		return -- EOF
    1.92 +	end
    1.93 +	local t, s = { }
    1.94 +	for _, what in ipairs(arg) do
    1.95 +		if what == "*a" then
    1.96 +			s = self:reada()
    1.97 +		elseif type(what) == "number" then
    1.98 +			s = self:readn(tonumber(what))
    1.99 +		else
   1.100 +			error("unknwon format")
   1.101 +		end
   1.102 +		insert(t, s)
   1.103 +	end
   1.104 +	return unpack(t)
   1.105 +end
   1.106 +
   1.107 +--	FCGI encoded lengths and key/value pairs:
   1.108 +
   1.109 +function FIFO:readlen()
   1.110 +	local l = self:read(1)
   1.111 +	if not l then return end
   1.112 +	l = l:byte()
   1.113 +	if l < 128 then
   1.114 +		return l
   1.115 +	end
   1.116 +	local t = self:read(3)
   1.117 +	if not t then return end
   1.118 +	return (l % 128) * 16777216 + t:byte(1) * 65536 +
   1.119 +		t:byte(2) * 256 + t:byte(3)
   1.120 +end
   1.121 +
   1.122 +function FIFO:readkeyvals()
   1.123 +	local t = { }
   1.124 +	local l1, l2, key, val
   1.125 +	while true do
   1.126 +		l1 = self:readlen()
   1.127 +		if not l1 then break end
   1.128 +		l2 = self:readlen()
   1.129 +		if not l2 then break end
   1.130 + 		key = self:read(l1)
   1.131 + 		val = self:read(l2)
   1.132 +		t[key] = val
   1.133 +	end
   1.134 +	return t
   1.135 +end
   1.136 +
   1.137 +-------------------------------------------------------------------------------
   1.138 +-- FCGI class:
   1.139 +-------------------------------------------------------------------------------
   1.140 +
   1.141 +FCGI_BEGIN_REQUEST = 1
   1.142 +FCGI_ABORT_REQUEST = 2
   1.143 +FCGI_END_REQUEST = 3
   1.144 +FCGI_PARAMS = 4
   1.145 +FCGI_STDIN = 5
   1.146 +FCGI_STDOUT = 6
   1.147 +FCGI_STDERR = 7
   1.148 +FCGI_DATA = 8
   1.149 +FCGI_GET_VALUES = 9
   1.150 +FCGI_GET_VALUES_RESULT = 10
   1.151 +FCGI_UNKNOWN_TYPE = 11
   1.152 +
   1.153 +local FCGI_RESPONDER = 1
   1.154 +local FCGI_REQUEST_COMPLETE = 0
   1.155 +local FCGI_UNKNOWN_ROLE = 3
   1.156 +
   1.157 +local function encodelen(buf, l)
   1.158 +	if l > 127 then
   1.159 +		insert(buf, char(bit.rshift(bit.band(l, 0x7f000000), 24) + 128))
   1.160 +		insert(buf, char(bit.rshift(bit.band(l, 0x00ff0000), 16)))
   1.161 +		insert(buf, char(bit.rshift(bit.band(l, 0x0000ff00), 8)))
   1.162 +	end
   1.163 +	insert(buf, char(bit.band(l, 0xff)))
   1.164 +end
   1.165 +
   1.166 +local function encodekeyvals(t)
   1.167 +	local buf = { }
   1.168 +	for key, val in pairs(t) do
   1.169 +		encodelen(buf, key:len())
   1.170 +		encodelen(buf, val:len())
   1.171 +		insert(buf, key)
   1.172 +		insert(buf, val)
   1.173 +	end
   1.174 +	return concat(buf)
   1.175 +end
   1.176 +
   1.177 +-- local FCGI = Class:newClass(_M)
   1.178 +local FCGI = _M
   1.179 +
   1.180 +function FCGI.new(class, self)
   1.181 +	self = Class.new(class, self or { })
   1.182 +	self.requests = { }
   1.183 +	return self
   1.184 +end
   1.185 +
   1.186 +function FCGI:readrecord()
   1.187 +	local t, err = self.socket:receive(8)
   1.188 +	if not t then return end
   1.189 +	if err then error(err) end
   1.190 +	local r = {
   1.191 +		ver = t:byte(1),
   1.192 +		type = t:byte(2),
   1.193 +		id = t:byte(3) * 256 + t:byte(4),
   1.194 +		len = t:byte(5) * 256 + t:byte(6)
   1.195 +	}
   1.196 +	if r.len == 0 then
   1.197 +		r.content = ""
   1.198 +	else
   1.199 +		r.content, err = self.socket:receive(r.len)
   1.200 +		if not r.content then return end
   1.201 +		if err then error(err) end
   1.202 +	end
   1.203 +	local pad = t:byte(7)
   1.204 +	if pad > 0 then
   1.205 +		t, err = self.socket:receive(pad)
   1.206 +		if not t then return end
   1.207 +		if err then error(err) end
   1.208 +	end
   1.209 +	return r
   1.210 +end
   1.211 +
   1.212 +function FCGI:write(type, id, s)
   1.213 +	local totlen = s:len()
   1.214 +	local totpos = 1
   1.215 +	while totlen > 0 do
   1.216 +		local len = math.min(totlen, 65535)
   1.217 +		local buf = concat {
   1.218 +			char(1), -- version
   1.219 +			char(type), -- type
   1.220 +			char(bit.rshift(id, 8)), -- id1
   1.221 +			char(id % 256), -- id0
   1.222 +			char(bit.rshift(len, 8)), -- len1
   1.223 +			char(len % 256), -- len0
   1.224 +			char(0), -- pad = 0
   1.225 +			char(0), -- reserved
   1.226 +			s:sub(totpos, totpos + len - 1) -- content
   1.227 +		}
   1.228 +		totpos = totpos + len
   1.229 +		totlen = totlen - len
   1.230 +
   1.231 +		len = buf:len()
   1.232 +		local pos, res, err = 1
   1.233 +		while pos <= len do
   1.234 +			res, err = self.socket:send(buf, pos)
   1.235 +			if not res then
   1.236 +				return nil, err
   1.237 +			end
   1.238 +			pos = res + 1
   1.239 +		end
   1.240 +	end
   1.241 +	return true
   1.242 +end
   1.243 +
   1.244 +function FCGI:write_stdout(id, s)
   1.245 +	return self:write(FCGI_STDOUT, id, s)
   1.246 +end
   1.247 +
   1.248 +function FCGI:collectstream(r)
   1.249 +	local req = self.requests[r.id]
   1.250 +	local s = req.streams[r.type]
   1.251 +	if not s then
   1.252 +		s = FIFO:new()
   1.253 +		req.streams[r.type] = s
   1.254 +	end
   1.255 +	if r.len == 0 then
   1.256 +		s:write() -- append EOF
   1.257 +		return s, true -- finished
   1.258 +	end
   1.259 +	s:write(r.content)
   1.260 +	return s
   1.261 +end
   1.262 +
   1.263 +function FCGI:endrequest(req, protstatus, appstatus)
   1.264 +	protstatus = protstatus or req.protstatus or FCGI_REQUEST_COMPLETE
   1.265 +	appstatus = appstatus or req.appstatus or 0
   1.266 +	self.requests[req.id] = nil -- delete request
   1.267 +	return self:write(FCGI_END_REQUEST, req.id, concat {
   1.268 +		char(bit.rshift(bit.band(appstatus, 0x7f000000), 24)),
   1.269 +		char(bit.rshift(bit.band(appstatus, 0x00ff0000), 16)),
   1.270 +		char(bit.rshift(bit.band(appstatus, 0x0000ff00), 8)),
   1.271 +		char(bit.band(appstatus, 0xff)),
   1.272 +		char(protstatus),
   1.273 +		char(0), char(0), char(0)
   1.274 +	})
   1.275 +end
   1.276 +
   1.277 +function FCGI:newrequest(id, role, flags)
   1.278 +	assert(not self.requests[id])
   1.279 +	local req = { id = id, role = role, flags = flags, streams = { } }
   1.280 +	self.requests[id] = req
   1.281 +	return req
   1.282 +end
   1.283 +
   1.284 +function FCGI:processrecord(r)
   1.285 +
   1.286 +	local c = r.content
   1.287 +	local req = self.requests[r.id]
   1.288 +
   1.289 +	if r.type == FCGI_BEGIN_REQUEST then
   1.290 +		assert(not req)
   1.291 +		local role = c:byte(1) * 256 + c:byte(2)
   1.292 +		if role == FCGI_RESPONDER then
   1.293 +			-- new request
   1.294 +			local flags = c:byte(3)
   1.295 +			req = self:newrequest(r.id, role, flags)
   1.296 +			return true -- continue
   1.297 +		end
   1.298 +		-- unknown role
   1.299 +		return self:endrequest(req, FCGI_UNKNOWN_ROLE)
   1.300 +	end
   1.301 +
   1.302 +	if not req then -- request already closed
   1.303 +		return true -- continue
   1.304 +	end
   1.305 +
   1.306 +	if r.type == FCGI_ABORT_REQUEST then
   1.307 +		return self:abortrequest(req)
   1.308 +
   1.309 +	elseif r.type == FCGI_GET_VALUES then
   1.310 +		local s, fin = self:collectstream(r)
   1.311 +		if s and fin then
   1.312 +			local res = { }
   1.313 +			for k in pairs(s:readkeyvals()) do
   1.314 +				if k == "FCGI_MAX_CONNS" then
   1.315 +					res.FCGI_MAX_CONNS = "16"
   1.316 +				elseif k == "FCGI_MAX_REQS" then
   1.317 +					res.FCGI_MAX_CONNS = "32"
   1.318 +				elseif k == "FCGI_MAX_REQS" then
   1.319 +					res.FCGI_MAX_CONNS = "1"
   1.320 +				end
   1.321 +			end
   1.322 +			res = encodekeyvals(res)
   1.323 +			return self:write(FCGI_GET_VALUES_RESULT, 0, res)
   1.324 +		end
   1.325 +
   1.326 +	elseif r.type == FCGI_PARAMS then
   1.327 +		local s, fin = self:collectstream(r)
   1.328 +		if s and fin then
   1.329 +			req.params = s:readkeyvals()
   1.330 +			return self:have_params(req, req.params)
   1.331 +		end
   1.332 +
   1.333 +	elseif r.type == self.FCGI_STDIN or r.type == self.FCGI_DATA then
   1.334 +		local s, fin = self:collectstream(r)
   1.335 +		if fin then
   1.336 +			if self.have_stream then
   1.337 +				return self:have_stream(req, r.type, s)
   1.338 +			end
   1.339 +		else
   1.340 +			if self.update_stream then
   1.341 +				return self:update_stream(req, r.type, s)
   1.342 +			end
   1.343 +		end
   1.344 +
   1.345 +	else
   1.346 +		-- unknown record
   1.347 +		local buf = char(r.type) .. char(0):rep(7)
   1.348 +		return self:write(FCGI_UNKNOWN_TYPE, 0, buf)
   1.349 +	end
   1.350 +
   1.351 +	return true -- continue
   1.352 +end
   1.353 +
   1.354 +function FCGI:serve(socket)
   1.355 +	assert(socket)
   1.356 +	self.socket = socket
   1.357 +	self.serve = true
   1.358 +	while self.serve do
   1.359 +		local r = self:readrecord()
   1.360 +		if not r then
   1.361 +			break
   1.362 +		end
   1.363 +		if not self:processrecord(r) then
   1.364 +			break
   1.365 +		end
   1.366 +	end
   1.367 +end
   1.368 +
   1.369 +function FCGI:stop()
   1.370 +	self.serve = false
   1.371 +end
   1.372 +
   1.373 +function FCGI:abortrequest(req)
   1.374 +	-- request aborted by webserver, confirm:
   1.375 +	return self:endrequest(req)
   1.376 +end