"
return msg
end
function handleVFSsource(source)
local msg = "
".. source .."
\n"
local frame = vfs.data[source]
if frame then
msg = msg .. "
".. tostring(frame).."
"
--msg = msg .. "
".. pretty.write(frame).."
"
else
msg = msg .. "Not in the cache"
end
msg = msg .. ""
return msg
end
function service(request)
local source,section,key
_, _, method, path, major, minor = request:find("([A-Z]+) (.+) HTTP/(%d).(%d)");
if method ~= "GET" then
error = "Method not implemented"
if method == nil then
error = error .. "."
else
error = error .. ": ".. url.escape(method)
end
sendError(client, 501, error)
return
end
path = url.unescape( path )
local message = "HTTP/1.0 200 OK\r\n"
message = message .. "Server: xapCacheApplet" .. info.version .. "\r\n";
message = message .. "Content-Type: text/html\r\n"
message = message .. "Connection: close\r\n\r\n"
_,source,section,key = path:splitv('/')
if source and section and key then
local frame = vfs.data[source]
local data = ""
if frame then
data = frame:getValue(section, key) or ""
end
message = message .. data
elseif source and section then
message = message .. "Unhandled URL: " .. path
elseif source then
message = message .. handleVFSsource(source)
else
message = message .. handleVFSroot()
end
client:send(message)
client:close()
end
function updateCache(frame)
vfs.data[frame["xap-header"].source] = frame
end
function handleConnection(server)
client = server:accept()
client:settimeout(60)
local request, err = client:receive()
if not err then
service(request)
end
end
function init()
local server = socket.bind("*", 8080)
xap.Select(handleConnection, server)
for _,k in pairs(vfs.filter) do
k:callback(updateCache)
end
end